1266975Smarcel#!/bin/sh
2266975Smarcel#
3266975Smarcel# Copyright (c) 2014 Juniper Networks, Inc.
4266975Smarcel# All rights reserved.
5266975Smarcel#
6266975Smarcel# Redistribution and use in source and binary forms, with or without
7266975Smarcel# modification, are permitted provided that the following conditions
8266975Smarcel# are met:
9266975Smarcel#
10266975Smarcel# 1. Redistributions of source code must retain the above copyright
11266975Smarcel#    notice, this list of conditions and the following disclaimer.
12266975Smarcel# 2. Redistributions in binary form must reproduce the above copyright
13266975Smarcel#    notice, this list of conditions and the following disclaimer in the
14266975Smarcel#    documentation and/or other materials provided with the distribution.
15266975Smarcel#
16266975Smarcel# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17266975Smarcel# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18266975Smarcel# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19266975Smarcel# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20266975Smarcel# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21266975Smarcel# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22266975Smarcel# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23266975Smarcel# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24266975Smarcel# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25266975Smarcel# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26266975Smarcel#
27266975Smarcel# $FreeBSD$
28266975Smarcel#
29266975Smarcel
30266975Smarcel#
31266975Smarcel# Convert a NIC driver to use the procdural API.
32266975Smarcel# It doesn't take care of all the # cases yet,
33266975Smarcel# but still does about 95% of work.
34266975Smarcel#
35266975Smarcel# Author: Sreekanth Rupavatharam
36266975Smarcel#
37266975Smarcel
38266975Smarcelif [ $# -lt 1 ]
39266975Smarcelthen
40266975Smarcel	echo " $0 <driver source (e.g., if_em.c)>";
41266975Smarcel	exit 1;
42266975Smarcelfi
43266975Smarcel
44266975Smarcel# XXX - This needs to change if the data structure uses different name
45266975Smarcel__ifp__="ifp";
46266975Smarcel
47266975Smarcelfile=$1
48266975Smarcel
49266975SmarcelrotateCursor() {
50266975Smarcel  case $toggle
51266975Smarcel  in
52266975Smarcel    1)
53266975Smarcel      printf " \\ "
54266975Smarcel      printf "\b\b"
55266975Smarcel      toggle="2"
56266975Smarcel    ;;
57266975Smarcel
58266975Smarcel    2)
59266975Smarcel      printf " | "
60266975Smarcel      printf "\b\b\b"
61266975Smarcel      toggle="3"
62266975Smarcel    ;;
63266975Smarcel
64266975Smarcel    3)
65266975Smarcel      printf " / "
66266975Smarcel      printf "\b\b\b"
67266975Smarcel      toggle="4"
68266975Smarcel    ;;
69266975Smarcel
70266975Smarcel    *)
71266975Smarcel      printf " - "
72266975Smarcel      printf "\b\b\b"
73266975Smarcel      toggle="1"
74266975Smarcel    ;;
75266975Smarcel  esac
76266975Smarcel}
77266975Smarcel
78266975Smarcelhandle_set() {
79266975Smarcel# Handle the case where $__ifp__->if_blah = XX;
80266975Smarcel	line=$1
81266975Smarcel	set=`echo $line| grep "$__ifp__->.* = "`
82266975Smarcel	if [ ! -z "$set" ]
83266975Smarcel	then
84266975Smarcel		word=`echo $line | awk -F "if_" ' { print $2 }' | awk -F" =" '{ print $1 }'`
85266975Smarcel		value=`echo $line | awk -F "=" '{ print $2 }' | sed -e 's/;//g'`
86266975Smarcel		new=`echo if_set$word"\($__ifp__,"$value");"`
87266975Smarcel		new=`echo $new | sed -e 's/&/\\\&/'`
88266975Smarcel		old=`echo $line|sed -e 's/^[ 	]*//'`
89266975Smarcel		line=`echo $line| sed -e's/'$old'/'$new'/g'`
90266975Smarcel		return 0
91266975Smarcel	fi
92266975Smarcel	return 1
93266975Smarcel}
94266975Smarcel
95266975Smarcelhandle_inc() {
96266975Smarcel	line=$1	
97267870Smarcel	inc=`echo $line | grep "$__ifp__->.*++\|++$__ifp__->.*"`
98266975Smarcel	if [ ! -z "$inc" ]
99266975Smarcel	then
100266975Smarcel		word=`echo $line | awk -F"if_" '{ print $2 }'|awk -F"\+" '{ print $1}'`
101266975Smarcel		value=' 1';
102266975Smarcel		old=`echo $line|sed -e 's/^[ 	]*//'`
103266975Smarcel		new=`echo if_inc$word"\($__ifp__,"$value");"`
104266975Smarcel		new=`echo $new | sed -e 's/&/\\\&/'`
105266975Smarcel		line=`echo $line| sed -e's/'$old'/'$new'/g'`
106266975Smarcel		return 0;
107266975Smarcel	fi	
108266975Smarcel	return 1;
109266975Smarcel}
110266975Smarcel
111266975Smarcelhandle_add() {
112266975Smarcel	line=$1
113266975Smarcel	add=`echo $line|grep "$__ifp__->.*+= "`
114266975Smarcel	if [ ! -z "$add" ]
115266975Smarcel	then
116266975Smarcel		word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'`
117266975Smarcel		value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'`
118266975Smarcel		new=`echo if_inc$word"\($__ifp__,$value);"`
119266975Smarcel		new=`echo $new | sed -e 's/&/\\\&/'`
120266975Smarcel		old=`echo $line|sed -e 's/^[ 	]*//'`
121266975Smarcel		line=`echo $line| sed -e's/'$old'/'$new'/g'`
122266975Smarcel		return 0
123266975Smarcel	fi
124266975Smarcel	return 1;
125266975Smarcel
126266975Smarcel}
127266975Smarcel
128266975Smarcelhandle_or() {
129266975Smarcel	line=$1
130266975Smarcel	or=`echo $line|grep "$__ifp__->.*|= "`
131266975Smarcel	if [ ! -z "$or" ]
132266975Smarcel	then
133266975Smarcel		word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'`	
134266975Smarcel		value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'`
135266975Smarcel		new=`echo if_set${word}bit"($__ifp__,$value, 0);"`
136266975Smarcel		new=`echo $new | sed -e 's/&/\\\&/'` 
137266975Smarcel		#line=`echo $line|sed -e 's/&/\\&/'`
138266975Smarcel		old=`echo $line|sed -e 's/^[ 	]*//'`
139266975Smarcel		line=`echo $line| sed -e's/'$old'/'$new'/g'`
140266975Smarcel		return 0;
141266975Smarcel	fi
142266975Smarcel	return 1;
143266975Smarcel
144266975Smarcel}
145266975Smarcel
146266975Smarcelhandle_and() {
147266975Smarcel	line=$1
148266975Smarcel	or=`echo $line|grep "$__ifp__->.*&= "`
149266975Smarcel	if [ ! -z "$or" ]
150266975Smarcel	then
151266975Smarcel		word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'`	
152266975Smarcel		value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'`
153266975Smarcel		value=`echo $value | sed -e's/~//g'`
154266975Smarcel		new=`echo if_set${word}bit"\($__ifp__, 0,$value);"`
155266975Smarcel		new=`echo $new | sed -e 's/&/\\\&/'`
156266975Smarcel		old=`echo $line|sed -e 's/^[ 	]*//'`
157266975Smarcel		line=`echo $line| sed -e's/'$old'/'$new'/g'`
158266975Smarcel		return 0;
159266975Smarcel	fi
160266975Smarcel	return 1;
161266975Smarcel
162266975Smarcel}
163266975Smarcel
164267870Smarcelhandle_toggle() {
165267870Smarcel	line=$1
166267870Smarcel	if [ ! -z `echo $line | grep "\^="` ]
167267870Smarcel	then
168267870Smarcel		line=`echo $line | sed -e 's/'"$__ifp__"'->if_\(.*\) ^=\(.*\);/if_toggle\1('"$__ifp__"',\2);/g'`
169267870Smarcel		return 0;
170267870Smarcel	fi
171267870Smarcel	return 1
172267870Smarcel
173267870Smarcel}
174267870Smarcel
175266975Smarcel# XXX - this needs updating
176266975Smarcelhandle_misc() {
177266975Smarcel	line=$1
178266975Smarcel	get=`echo $line | grep "if_capabilities\|if_flags\|if_softc\|if_capenable\|if_mtu\|if_drv_flags"`
179266975Smarcel	if [ ! -z "$get" ]
180266975Smarcel	then
181266975Smarcel		word=`echo $line |awk -F"$__ifp__->if_" '{ print $2 }' | \
182266975Smarcel			sed -e's/[^a-zA-Z0-9_]/\@/'|awk -F"\@" '{ print $1}'`
183266975Smarcel		old=`echo "$__ifp__->if_"${word}`
184266975Smarcel		new=`echo "if_get"${word}"($__ifp__)"`
185266975Smarcel		new=`echo $new | sed -e 's/&/\\\&/'`
186266975Smarcel		line=`echo $line| sed -e's/'$old'/'$new'/g'`
187266975Smarcel		return 0;
188266975Smarcel	fi
189266975Smarcel	return 1;
190266975Smarcel
191266975Smarcel}
192266975Smarcel
193267870Smarcelreplace_str ()
194267870Smarcel{
195267870Smarcel	line=$1
196267870Smarcel	orig=$2
197267870Smarcel	new=$3
198267870Smarcel	line=`echo $line | sed -e 's/'"$orig"'\(.*\)/'"$new"'\1/g'`
199267870Smarcel	return 0;
200267870Smarcel}
201267870Smarcel
202267870Smarcel# Handle special cases which do not fall under regular patterns
203267870Smarcelhandle_special ()
204267870Smarcel{
205267870Smarcel	line=$1
206267870Smarcel	replace_str $line "(\*$__ifp__->if_input)" "if_input"
207267870Smarcel	replace_str $line "if_setinit" "if_setinitfn"
208267870Smarcel	replace_str $line "if_setioctl" "if_setioctlfn"
209267870Smarcel	replace_str $line "if_getdrv_flags" "if_getdrvflags"
210267870Smarcel	replace_str $line "if_setdrv_flagsbit" "if_setdrvflagbits"
211267870Smarcel	replace_str $line "if_setstart" "if_setstartfn"
212267870Smarcel	replace_str $line "if_sethwassistbit" "if_sethwassistbits"
213267870Smarcel	replace_str $line "ifmedia_init" "ifmedia_init_drv"
214267870Smarcel	replace_str $line "IFQ_DRV_IS_EMPTY(&$__ifp__->if_snd)" "if_sendq_empty($__ifp__)"
215267870Smarcel	replace_str $line "IFQ_DRV_PREPEND(&$__ifp__->if_snd" "if_sendq_prepend($__ifp__"
216267870Smarcel	replace_str $line "IFQ_SET_READY(&ifp->if_snd)" "if_setsendqready($__ifp__)"
217267870Smarcel	line=`echo $line | sed -e 's/IFQ_SET_MAXLEN(&'$__ifp__'->if_snd, \(.*\))/if_setsendqlen('$__ifp__', \1)/g'`
218267870Smarcel	line=`echo $line | sed -e 's/IFQ_DRV_DEQUEUE(&'$__ifp__'->if_snd, \(.*\))/\1 = if_dequeue('$__ifp__')/g'`
219267870Smarcel	return 0
220267870Smarcel}
221267870Smarcel
222266975Smarcelif [ -e $file.tmp ]
223266975Smarcelthen
224266975Smarcel	rm $file.tmp
225266975Smarcelfi
226266975SmarcelIFS=
227266975Smarcelecho -n "Conversion for $file started, please wait: "
228266975SmarcelFAIL_PAT="XXX - DRVAPI"
229266975Smarcelcount=0
230266975Smarcelcat $1 | while read -r line
231266975Smarceldo
232266975Smarcelcount=`expr $count + 1`
233266975SmarcelrotateCursor 
234266975Smarcelpat=`echo $line | grep "$__ifp__->"`
235266975Smarcelwhile  [ "$pat" != "" ]
236266975Smarceldo
237266975Smarcel	pat=`echo $line | grep "$__ifp__->"`
238266975Smarcel	if [ ! -z `echo $pat | grep "$FAIL_PAT"` ]
239266975Smarcel	then
240266975Smarcel		break;
241266975Smarcel	fi
242266975Smarcel
243266975Smarcel	handle_set $line
244267870Smarcel
245266975Smarcel	if [ $? != 0 ]
246267870Smarcel	then 
247266975Smarcel		handle_inc $line
248266975Smarcel	fi
249266975Smarcel
250266975Smarcel	if [ $? != 0 ]
251266975Smarcel	then
252266975Smarcel		handle_add $line
253266975Smarcel	fi
254266975Smarcel
255266975Smarcel	if [ $? != 0 ]
256266975Smarcel	then
257266975Smarcel		handle_or $line
258266975Smarcel	fi
259266975Smarcel
260266975Smarcel	if [ $? != 0 ]
261266975Smarcel	then
262266975Smarcel		handle_and $line
263266975Smarcel	fi
264266975Smarcel
265266975Smarcel	if [ $? != 0 ]
266267870Smarcel	then
267267870Smarcel		handle_toggle $line
268267870Smarcel	fi
269266975Smarcel
270267870Smarcel	if [ $? != 0 ]
271266975Smarcel	then
272266975Smarcel		handle_misc $line
273266975Smarcel	fi
274266975Smarcel	
275266975Smarcel	if [ $? != 0 ]
276266975Smarcel	then
277267870Smarcel		handle_special $line
278267870Smarcel	fi	
279267870Smarcel
280267870Smarcel	if [ ! -z `echo $line | grep "$__ifp__->"` ]
281267870Smarcel	then
282267870Smarcel		line=`echo $line | sed -e 's:$: \/* '${FAIL_PAT}' *\/:g'`
283266975Smarcel	fi
284266975Smarceldone
285267870Smarcel	line=`echo "$line" | sed -e 's:VLAN_CAPABILITIES('$__ifp__'):if_vlancap('$__ifp__'):g'`
286266975Smarcel	# Replace the ifnet * with if_t
287266975Smarcel	if [ ! -z `echo $line | grep "struct ifnet"` ]
288266975Smarcel	then
289266975Smarcel		line=`echo $line | sed -e 's/struct ifnet[ \t]*\*/if_t /g'`
290266975Smarcel	fi
291266975Smarcel	echo "$line" >> $file.tmp
292266975Smarceldone
293266975Smarcelecho ""
294266975Smarcelcount=`grep $FAIL_PAT $file.tmp | wc -l`
295266975Smarcelif [ $count -gt 0 ]
296266975Smarcelthen
297266975Smarcel	echo "$count lines could not be converted to DRVAPI"
298266975Smarcel	echo "Look for /* $FAIL_PAT */ in the converted file"
299266975Smarcelfi
300266975Smarcelecho "original $file  has been moved to $file.orig"
301266975Smarcelmv $file $file.orig
302266975Smarcelmv $file.tmp $file
303