2
# the next line restarts using tclsh \
5
# Copyright (c) 2004 Mellanox Technologies LTD. All rights reserved.
7
# This software is available to you under a choice of one of two
8
# licenses. You may choose to be licensed under the terms of the GNU
9
# General Public License (GPL) Version 2, available from the file
10
# COPYING in the main directory of this source tree, or the
11
# OpenIB.org BSD license below:
13
# Redistribution and use in source and binary forms, with or
14
# without modification, are permitted provided that the following
17
# - Redistributions of source code must retain the above
18
# copyright notice, this list of conditions and the following
21
# - Redistributions in binary form must reproduce the above
22
# copyright notice, this list of conditions and the following
23
# disclaimer in the documentation and/or other materials
24
# provided with the distribution.
26
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40
puts "Usage: $argv0 \[-r <prompt>\] \[-g\] \[-s\] \[-p\] -o <out-file>"
44
puts "\nfixSwigWrapper : Applies extended behaviour to a swig_wrap.c\n"
47
puts "-o out-file : The name of the out file\n"
49
puts "-r prompt : add readline support using the given prompt"
50
puts "-g : Cleanup SWIG_GetPointerObj"
51
puts "-s : Cleanup SWIG_SetPointerObj"
52
puts "-p : Cleanup SWIG_MakePtr\n\n"
56
# basically this code filters out the two swig functions:
59
# from the given file and also adds one line just before the last
64
if {[catch {set f [open $fn "r"]} e]} {
65
error "-E- LoadFile: $e"
68
while {[gets $f sLine] >= 0} {
69
lappend linesList "$sLine"
72
set res "[join $linesList \n]"
73
puts stderr "-I- Loaded file: $fn with:[string length $res] chars"
78
proc remTrailingBlanks {code} {
79
regsub -line -all {[ ]+$} $code {} code
84
# remove the given proc from the file
85
proc remProc {code procName procRetType} {
87
# find the idx of the start of the procedure
88
if {! [regexp -indices -line "SWIGSTATIC\[\\s\n\]*$procRetType\[\\s\n\]*$procName" \
90
error "-E- Fail to find proc:$procName"
93
# assume the end of the procedure has a nice indented \}
94
if {! [regexp -start [lindex $sidx 0] -indices "\n\}" $code eidx]} {
95
error "-E- Fail to find proc:$procName end"
98
return [string replace $code [lindex $sidx 0] [lindex $eidx 1]]
101
proc addAtLastReturn {code text} {
102
# find the last "return TCL_OK"
103
set idx [string last {return TCL_OK} $code]
105
error "-E- Fail to find last return"
108
return [string replace $code $idx $idx $text]
118
set removeTrailingBlanks 1
121
# parse command line args
122
while {[llength $argv]} {
123
set sw [lindex $argv 0]
124
set argv [lrange $argv 1 end]
128
if {![llength $argv]} {
129
puts "-E- Expected prompt value after -r"
133
set userPrompt [lindex $argv 0]
134
set argv [lrange $argv 1 end]
137
if {![llength $argv]} {
138
puts "-E- Expected file name value after -o"
142
set outFileName [lindex $argv 0]
143
set argv [lrange $argv 1 end]
145
-g { set removeGetObj 1}
146
-s { set removeSetObj 1}
147
-p { set removeMakePtr 1}
152
puts "-E- Unexpected argument: $sw"
160
if (Tcl_PkgRequire(interp,\"tclreadline\",0,0) != NULL) \{
162
\"if \{\$tcl_interactive\} \{namespace eval tclreadline \{proc prompt1 \{\} \{return \\\"$userPrompt >\\\"\} \}; ::tclreadline::Loop $userPrompt.log \}\"
167
if {[catch {set swigCode [LoadFile swig_wrap.c]} e]} {
173
if {[catch {set swigCode [remProc $swigCode SWIG_GetPointerObj "char \\*"]} e]} {
179
if {$removeTrailingBlanks} {
180
if {[catch {set swigCode [remTrailingBlanks $swigCode]} e]} {
187
if {[catch {set swigCode [remProc $swigCode SWIG_SetPointerObj "void"]} e]} {
193
if {$removeMakePtr} {
194
if {[catch {set swigCode [remProc $swigCode SWIG_MakePtr "int"]} e]} {
200
if {$userPrompt != 0} {
201
if {[catch {set swigCode [addAtLastReturn $swigCode "${readlineCode}r"]} e]} {
207
if {$outFileName != 0} {
208
if {[catch {set f [open $outFileName w]} e]} {