2
# the next line restarts using tclsh \
7
puts "Usage: $argv0 \[-r <prompt>\] \[-g\] \[-s\] \[-p\] -o <out-file>"
11
puts "\nfixSwigWrapper : Applies extended behaviour to a swig_wrap.c\n"
14
puts "-o out-file : The name of the out file\n"
16
puts "-r prompt : add readline support using the given prompt"
17
puts "-g : Cleanup SWIG_GetPointerObj"
18
puts "-s : Cleanup SWIG_SetPointerObj"
19
puts "-p : Cleanup SWIG_MakePtr\n\n"
23
# basically this code filters out the two swig functions:
26
# from the given file and also adds one line just before the last
31
if {[catch {set f [open $fn "r"]} e]} {
32
error "-E- LoadFile: $e"
35
while {[gets $f sLine] >= 0} {
36
lappend linesList "$sLine"
39
set res "[join $linesList \n]"
40
puts stderr "-I- Loaded file: $fn with:[string length $res] chars"
45
proc remTrailingBlanks {code} {
46
regsub -line -all {[ ]+$} $code {} code
51
# remove the given proc from the file
52
proc remProc {code procName procRetType} {
54
# find the idx of the start of the procedure
55
if {! [regexp -indices -line "SWIGSTATIC\[\\s\n\]*$procRetType\[\\s\n\]*$procName" \
57
error "-E- Fail to find proc:$procName"
60
# assume the end of the procedure has a nice indented \}
61
if {! [regexp -start [lindex $sidx 0] -indices "\n\}" $code eidx]} {
62
error "-E- Fail to find proc:$procName end"
65
return [string replace $code [lindex $sidx 0] [lindex $eidx 1]]
68
proc addAtLastReturn {code text} {
69
# find the last "return TCL_OK"
70
set idx [string last {return TCL_OK} $code]
72
error "-E- Fail to find last return"
75
return [string replace $code $idx $idx $text]
78
proc addVoidParam {code procName procRetType} {
79
# find the idx of the start of the procedure
80
if {! [regexp -indices "SWIGSTATIC\[\\s\n\]*$procRetType\[\\s\n\]*${procName}\\(\\)" \
82
error "-E- Fail to find proc:$procName"
84
return [string replace $code [lindex $sidx 1] [lindex $sidx 1] " void )"]
93
set removeTrailingBlanks 1
96
# parse command line args
97
while {[llength $argv]} {
98
set sw [lindex $argv 0]
99
set argv [lrange $argv 1 end]
103
if {![llength $argv]} {
104
puts "-E- Expected prompt value after -r"
108
set userPrompt [lindex $argv 0]
109
set argv [lrange $argv 1 end]
112
if {![llength $argv]} {
113
puts "-E- Expected file name value after -o"
117
set outFileName [lindex $argv 0]
118
set argv [lrange $argv 1 end]
120
-g { set removeGetObj 1}
121
-s { set removeSetObj 1}
122
-p { set removeMakePtr 1}
127
puts "-E- Unexpected argument: $sw"
135
if (Tcl_PkgRequire(interp,\"tclreadline\",0,0) != NULL) \{
137
\"if \{\$tcl_interactive\} \{namespace eval tclreadline \{proc prompt1 \{\} \{return \\\"$userPrompt >\\\"\} \}; ::tclreadline::Loop $userPrompt.log \}\"
142
if {[catch {set swigCode [LoadFile swig_wrap.c]} e]} {
148
if {[catch {set swigCode [remProc $swigCode SWIG_GetPointerObj "char \\*"]} e]} {
154
if {$removeTrailingBlanks} {
155
if {[catch {set swigCode [remTrailingBlanks $swigCode]} e]} {
162
if {[catch {set swigCode [remProc $swigCode SWIG_SetPointerObj "void"]} e]} {
168
if {$removeMakePtr} {
169
if {[catch {set swigCode [remProc $swigCode SWIG_MakePtr "int"]} e]} {
175
if {$userPrompt != 0} {
176
if {[catch {set swigCode [addAtLastReturn $swigCode "${readlineCode}r"]} e]} {
182
if {[catch {set swigCode [addVoidParam $swigCode SWIG_RegisterType "void"]} e]} {
187
if {$outFileName != 0} {
188
if {[catch {set f [open $outFileName w]} e]} {