#!/bin/sh # # Copyright (c) 2014 Juniper Networks, Inc. # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # $FreeBSD: stable/11/tools/ifnet/convert_drvapi.sh 267870 2014-06-25 15:22:14Z marcel $ # # # Convert a NIC driver to use the procdural API. # It doesn't take care of all the # cases yet, # but still does about 95% of work. # # Author: Sreekanth Rupavatharam # if test $Argc -lt 1 { echo " $0 "; exit 1; } # XXX - This needs to change if the data structure uses different name setglobal __ifp__ = '"ifp'"; setglobal file = $1 proc rotateCursor { match $toggle { with 1 printf " \\ " printf "\b\b" setglobal toggle = '"2'" with 2 printf " | " printf "\b\b\b" setglobal toggle = '"3'" with 3 printf " / " printf "\b\b\b" setglobal toggle = '"4'" with * printf " - " printf "\b\b\b" setglobal toggle = '"1'" } } proc handle_set { # Handle the case where $__ifp__->if_blah = XX; setglobal line = $1 setglobal set = $[echo $line| grep "$__ifp__->.* = ] if test ! -z $set { setglobal word = $[echo $line | awk -F "if_" ' { print $2 }' | awk -F" =" '{ print $1 }] setglobal value = $[echo $line | awk -F "=" '{ print $2 }' | sed -e 's/;//g] setglobal new = $[echo if_set$word"\($__ifp__,"$value");] setglobal new = $[echo $new | sed -e 's/&/\\\&/] setglobal old = $[echo $line|sed -e 's/^[ ]*//] setglobal line = $[echo $line| sed -e's/'$old'/'$new'/g] return 0 } return 1 } proc handle_inc { setglobal line = $1 setglobal inc = $[echo $line | grep "$__ifp__->.*++\|++$__ifp__->.*] if test ! -z $inc { setglobal word = $[echo $line | awk -F"if_" '{ print $2 }'|awk -F"\+" '{ print $1}] setglobal value = '' 1''; setglobal old = $[echo $line|sed -e 's/^[ ]*//] setglobal new = $[echo if_inc$word"\($__ifp__,"$value");] setglobal new = $[echo $new | sed -e 's/&/\\\&/] setglobal line = $[echo $line| sed -e's/'$old'/'$new'/g] return 0; } return 1; } proc handle_add { setglobal line = $1 setglobal add = $[echo $line|grep "$__ifp__->.*+= ] if test ! -z $add { setglobal word = $[echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}] setglobal value = $[echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g] setglobal new = $[echo if_inc$word"\($__ifp__,$value);] setglobal new = $[echo $new | sed -e 's/&/\\\&/] setglobal old = $[echo $line|sed -e 's/^[ ]*//] setglobal line = $[echo $line| sed -e's/'$old'/'$new'/g] return 0 } return 1; } proc handle_or { setglobal line = $1 setglobal or = $[echo $line|grep "$__ifp__->.*|= ] if test ! -z $or { setglobal word = $[echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}] setglobal value = $[echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g] setglobal new = $[echo if_set$(word)bit"($__ifp__,$value, 0);] setglobal new = $[echo $new | sed -e 's/&/\\\&/] #line=`echo $line|sed -e 's/&/\\&/'` setglobal old = $[echo $line|sed -e 's/^[ ]*//] setglobal line = $[echo $line| sed -e's/'$old'/'$new'/g] return 0; } return 1; } proc handle_and { setglobal line = $1 setglobal or = $[echo $line|grep "$__ifp__->.*&= ] if test ! -z $or { setglobal word = $[echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}] setglobal value = $[echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g] setglobal value = $[echo $value | sed -e's/~//g] setglobal new = $[echo if_set$(word)bit"\($__ifp__, 0,$value);] setglobal new = $[echo $new | sed -e 's/&/\\\&/] setglobal old = $[echo $line|sed -e 's/^[ ]*//] setglobal line = $[echo $line| sed -e's/'$old'/'$new'/g] return 0; } return 1; } proc handle_toggle { setglobal line = $1 if test ! -z $[echo $line | grep "\^=] { setglobal line = $[echo $line | sed -e 's/'"$__ifp__"'->if_\(.*\) ^=\(.*\);/if_toggle\1('"$__ifp__"',\2);/g] return 0; } return 1 } # XXX - this needs updating proc handle_misc { setglobal line = $1 setglobal get = $[echo $line | grep "if_capabilities\|if_flags\|if_softc\|if_capenable\|if_mtu\|if_drv_flags] if test ! -z $get { setglobal word = $[echo $line |awk -F"$__ifp__->if_" '{ print $2 }' | \ sed -e's/[^a-zA-Z0-9_]/\@/'|awk -F"\@" '{ print $1}] setglobal old = $[echo "$__ifp__->if_"$(word)] setglobal new = $[echo "if_get"$(word)"($__ifp__)] setglobal new = $[echo $new | sed -e 's/&/\\\&/] setglobal line = $[echo $line| sed -e's/'$old'/'$new'/g] return 0; } return 1; } proc replace_str { setglobal line = $1 setglobal orig = $2 setglobal new = $3 setglobal line = $[echo $line | sed -e 's/'"$orig"'\(.*\)/'"$new"'\1/g] return 0; } # Handle special cases which do not fall under regular patterns proc handle_special { setglobal line = $1 replace_str $line "(\*$__ifp__->if_input)" "if_input" replace_str $line "if_setinit" "if_setinitfn" replace_str $line "if_setioctl" "if_setioctlfn" replace_str $line "if_getdrv_flags" "if_getdrvflags" replace_str $line "if_setdrv_flagsbit" "if_setdrvflagbits" replace_str $line "if_setstart" "if_setstartfn" replace_str $line "if_sethwassistbit" "if_sethwassistbits" replace_str $line "ifmedia_init" "ifmedia_init_drv" replace_str $line "IFQ_DRV_IS_EMPTY(&$__ifp__->if_snd)" "if_sendq_empty($__ifp__)" replace_str $line "IFQ_DRV_PREPEND(&$__ifp__->if_snd" "if_sendq_prepend($__ifp__" replace_str $line "IFQ_SET_READY(&ifp->if_snd)" "if_setsendqready($__ifp__)" setglobal line = $[echo $line | sed -e 's/IFQ_SET_MAXLEN(&'$__ifp__'->if_snd, \(.*\))/if_setsendqlen('$__ifp__', \1)/g] setglobal line = $[echo $line | sed -e 's/IFQ_DRV_DEQUEUE(&'$__ifp__'->if_snd, \(.*\))/\1 = if_dequeue('$__ifp__')/g] return 0 } if test -e $file.tmp { rm $file.tmp } setglobal IFS = '' echo -n "Conversion for $file started, please wait: " setglobal FAIL_PAT = '"XXX - DRVAPI'" setglobal count = '0' cat $1 | while read -r line { setglobal count = $[expr $count + 1] rotateCursor setglobal pat = $[echo $line | grep "$__ifp__->] while [ "$pat" != "" ] { setglobal pat = $[echo $line | grep "$__ifp__->] if test ! -z $[echo $pat | grep $FAIL_PAT] { break; } handle_set $line if test $Status != 0 { handle_inc $line } if test $Status != 0 { handle_add $line } if test $Status != 0 { handle_or $line } if test $Status != 0 { handle_and $line } if test $Status != 0 { handle_toggle $line } if test $Status != 0 { handle_misc $line } if test $Status != 0 { handle_special $line } if test ! -z $[echo $line | grep "$__ifp__->] { setglobal line = $[echo $line | sed -e 's:$: \/* '$(FAIL_PAT)' *\/:g] } } setglobal line = $[echo $line | sed -e 's:VLAN_CAPABILITIES('$__ifp__'):if_vlancap('$__ifp__'):g] # Replace the ifnet * with if_t if test ! -z $[echo $line | grep "struct ifnet] { setglobal line = $[echo $line | sed -e 's/struct ifnet[ \t]*\*/if_t /g] } echo $line >> $file.tmp } echo "" setglobal count = $[grep $FAIL_PAT $file.tmp | wc -l] if test $count -gt 0 { echo "$count lines could not be converted to DRVAPI" echo "Look for /* $FAIL_PAT */ in the converted file" } echo "original $file has been moved to $file.orig" mv $file $file.orig mv $file.tmp $file