File: fixSwigWrapper

package info (click to toggle)
ibutils 1.2-OFED-1.4.2-1.3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 23,616 kB
  • sloc: cpp: 153,349; ansic: 77,237; tcl: 13,447; sh: 11,852; makefile: 494; yacc: 333; lex: 169; awk: 53
file content (216 lines) | stat: -rwxr-xr-x 5,271 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/sh
# the next line restarts using tclsh \
  exec tclsh "$0" "$@"
#--
# Copyright (c) 2004 Mellanox Technologies LTD. All rights reserved.
#
# This software is available to you under a choice of one of two
# licenses.  You may choose to be licensed under the terms of the GNU
# General Public License (GPL) Version 2, available from the file
# COPYING in the main directory of this source tree, or the
# OpenIB.org BSD license below:
#
#     Redistribution and use in source and binary forms, with or
#     without modification, are permitted provided that the following
#     conditions are met:
#
#      - Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      - 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.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# $Id$
#--

proc Usage {} {
	global argv0
	puts "Usage: $argv0 \[-r <prompt>\] \[-g\] \[-s\] \[-p\] -o <out-file>"
}

proc Help {} {
	puts "\nfixSwigWrapper : Applies extended behaviour to a swig_wrap.c\n"
	Usage
	puts "\nArguments:"
	puts "-o out-file : The name of the out file\n"
	puts "\nOptions:"
	puts "-r prompt : add readline support using the given prompt"
	puts "-g : Cleanup SWIG_GetPointerObj"
	puts "-s : Cleanup SWIG_SetPointerObj"
	puts "-p : Cleanup SWIG_MakePtr\n\n"
	exit 1
}

# basically this code filters out the two swig functions:
# SWIG_GetPointerObj
# SWIG_SetPointerObj
# from the given file and also adds one line just before the last
# return statement

proc LoadFile {fn} {
	# open the file
	if {[catch {set f [open $fn "r"]} e]} {
		error "-E- LoadFile: $e"
	}

	while {[gets $f sLine] >= 0} {
	        lappend linesList "$sLine"
	}
	close $f
	set res "[join $linesList \n]"
	puts stderr "-I- Loaded file: $fn with:[string length $res] chars"

	return $res
}

proc remTrailingBlanks {code} {
	regsub -line -all {[ 	]+$} $code {} code
	return $code
}


# remove the given proc from the file
proc remProc {code procName procRetType} {

	# find the idx of the start of the procedure
	if {! [regexp -indices -line "SWIGSTATIC\[\\s\n\]*$procRetType\[\\s\n\]*$procName" \
				 $code sidx]} {
		error "-E- Fail to find proc:$procName"
	}

	# assume the end of the procedure has a nice indented \}
	if {! [regexp -start [lindex $sidx 0] -indices "\n\}" $code eidx]} {
		error "-E- Fail to find proc:$procName end"
	}

	return [string replace $code [lindex $sidx 0] [lindex $eidx 1]]
}

proc addAtLastReturn {code text} {
	# find the last "return TCL_OK"
	set idx [string last {return TCL_OK} $code]
	if {$idx <= 0} {
		error "-E- Fail to find last return"
	}

	return [string replace $code $idx $idx $text]
}


# MAIN

set removeGetObj 0
set removeSetObj 0
set removeMakePtr 0
set userPrompt 0
set removeTrailingBlanks 1
set outFileName ""

# parse command line args
while {[llength $argv]} {
	set sw [lindex $argv 0]
	set argv [lrange $argv 1 end]

	switch -- $sw {
		-r {
			if {![llength $argv]} {
				puts "-E- Expected prompt value after -r"
				Usage
				exit 1
			}
			set userPrompt [lindex $argv 0]
			set argv [lrange $argv 1 end]
		}
		-o {
			if {![llength $argv]} {
				puts "-E- Expected file name value after -o"
				Usage
				exit 1
			}
			set outFileName [lindex $argv 0]
			set argv [lrange $argv 1 end]
		}
		-g { set removeGetObj 1}
		-s { set removeSetObj 1}
		-p { set removeMakePtr 1}
		-h {
			Help
		}
		default {
			puts "-E- Unexpected argument: $sw"
			Usage
			exit 1
		}
	}
}

set readlineCode "
   if (Tcl_PkgRequire(interp,\"tclreadline\",0,0) != NULL) \{
     Tcl_Eval(interp,
				  \"if \{\$tcl_interactive\} \{namespace eval tclreadline \{proc prompt1 \{\} \{return \\\"$userPrompt >\\\"\} \}; ::tclreadline::Loop $userPrompt.log \}\"
     );
   \}
"

if {[catch {set swigCode [LoadFile swig_wrap.c]} e]} {
	puts "$e"
	exit 3
}

if {$removeGetObj} {
	if {[catch {set swigCode [remProc $swigCode SWIG_GetPointerObj "char \\*"]} e]} {
		puts "-E- $e"
		exit 1
	}
}

if {$removeTrailingBlanks} {
	if {[catch {set swigCode [remTrailingBlanks $swigCode]} e]} {
		puts "-E- $e"
		exit 1
	}
}

if {$removeSetObj} {
	if {[catch {set swigCode [remProc $swigCode SWIG_SetPointerObj "void"]} e]} {
		puts "-E- $e"
		exit 1
	}
}

if {$removeMakePtr} {
	if {[catch {set swigCode [remProc $swigCode SWIG_MakePtr "int"]} e]} {
		puts "-E- $e"
		exit 1
	}
}

if {$userPrompt != 0} {
	if {[catch {set swigCode [addAtLastReturn $swigCode "${readlineCode}r"]} e]} {
		puts "-E- $e"
		exit 1
	}
}

if {$outFileName != 0} {
	if {[catch {set f [open $outFileName w]} e]} {
		puts "-E- $e"
		exit 1
	}
	puts $f $swigCode
	close $f
} else {
	puts $swigCode
}