~ubuntu-branches/ubuntu/raring/ibutils/raring-proposed

« back to all changes in this revision

Viewing changes to ibis/src/fixSwigWrapper

  • Committer: Bazaar Package Importer
  • Author(s): Benoit Mortier
  • Date: 2010-01-11 22:22:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100111222200-53kum2et5nh13rv3
Tags: upstream-1.2-OFED-1.4.2
ImportĀ upstreamĀ versionĀ 1.2-OFED-1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# the next line restarts using tclsh \
 
3
  exec tclsh "$0" "$@"
 
4
 
 
5
proc Usage {} {
 
6
        global argv0
 
7
        puts "Usage: $argv0 \[-r <prompt>\] \[-g\] \[-s\] \[-p\] -o <out-file>"
 
8
}
 
9
 
 
10
proc Help {} {
 
11
        puts "\nfixSwigWrapper : Applies extended behaviour to a swig_wrap.c\n"
 
12
        Usage
 
13
        puts "\nArguments:"
 
14
        puts "-o out-file : The name of the out file\n"
 
15
        puts "\nOptions:"
 
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"
 
20
        exit 1
 
21
}
 
22
 
 
23
# basically this code filters out the two swig functions:
 
24
# SWIG_GetPointerObj
 
25
# SWIG_SetPointerObj
 
26
# from the given file and also adds one line just before the last
 
27
# return statement
 
28
 
 
29
proc LoadFile {fn} {
 
30
        # open the file
 
31
        if {[catch {set f [open $fn "r"]} e]} {
 
32
                error "-E- LoadFile: $e"
 
33
        }
 
34
 
 
35
        while {[gets $f sLine] >= 0} {
 
36
                lappend linesList "$sLine"
 
37
        }
 
38
        close $f
 
39
        set res "[join $linesList \n]"
 
40
        puts stderr "-I- Loaded file: $fn with:[string length $res] chars"
 
41
 
 
42
        return $res
 
43
}
 
44
 
 
45
proc remTrailingBlanks {code} {
 
46
        regsub -line -all {[    ]+$} $code {} code
 
47
        return $code
 
48
}
 
49
 
 
50
 
 
51
# remove the given proc from the file
 
52
proc remProc {code procName procRetType} {
 
53
 
 
54
        # find the idx of the start of the procedure
 
55
        if {! [regexp -indices -line "SWIGSTATIC\[\\s\n\]*$procRetType\[\\s\n\]*$procName" \
 
56
                                 $code sidx]} {
 
57
                error "-E- Fail to find proc:$procName"
 
58
        }
 
59
 
 
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"
 
63
        }
 
64
 
 
65
        return [string replace $code [lindex $sidx 0] [lindex $eidx 1]]
 
66
}
 
67
 
 
68
proc addAtLastReturn {code text} {
 
69
        # find the last "return TCL_OK"
 
70
        set idx [string last {return TCL_OK} $code]
 
71
        if {$idx <= 0} {
 
72
                error "-E- Fail to find last return"
 
73
        }
 
74
 
 
75
        return [string replace $code $idx $idx $text]
 
76
}
 
77
 
 
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}\\(\\)" \
 
81
                                 $code sidx]} {
 
82
                error "-E- Fail to find proc:$procName"
 
83
        }
 
84
        return [string replace $code [lindex $sidx 1] [lindex $sidx 1] " void )"]
 
85
}
 
86
 
 
87
# MAIN
 
88
 
 
89
set removeGetObj 0
 
90
set removeSetObj 0
 
91
set removeMakePtr 0
 
92
set userPrompt 0
 
93
set removeTrailingBlanks 1
 
94
set outFileName ""
 
95
 
 
96
# parse command line args
 
97
while {[llength $argv]} {
 
98
        set sw [lindex $argv 0]
 
99
        set argv [lrange $argv 1 end]
 
100
 
 
101
        switch -- $sw {
 
102
                -r {
 
103
                        if {![llength $argv]} {
 
104
                                puts "-E- Expected prompt value after -r"
 
105
                                Usage
 
106
                                exit 1
 
107
                        }
 
108
                        set userPrompt [lindex $argv 0]
 
109
                        set argv [lrange $argv 1 end]
 
110
                }
 
111
                -o {
 
112
                        if {![llength $argv]} {
 
113
                                puts "-E- Expected file name value after -o"
 
114
                                Usage
 
115
                                exit 1
 
116
                        }
 
117
                        set outFileName [lindex $argv 0]
 
118
                        set argv [lrange $argv 1 end]
 
119
                }
 
120
                -g { set removeGetObj 1}
 
121
                -s { set removeSetObj 1}
 
122
                -p { set removeMakePtr 1}
 
123
                -h {
 
124
                        Help
 
125
                }
 
126
                default {
 
127
                        puts "-E- Unexpected argument: $sw"
 
128
                        Usage
 
129
                        exit 1
 
130
                }
 
131
        }
 
132
}
 
133
 
 
134
set readlineCode "
 
135
   if (Tcl_PkgRequire(interp,\"tclreadline\",0,0) != NULL) \{
 
136
     Tcl_Eval(interp,
 
137
                                  \"if \{\$tcl_interactive\} \{namespace eval tclreadline \{proc prompt1 \{\} \{return \\\"$userPrompt >\\\"\} \}; ::tclreadline::Loop $userPrompt.log \}\"
 
138
     );
 
139
   \}
 
140
"
 
141
 
 
142
if {[catch {set swigCode [LoadFile swig_wrap.c]} e]} {
 
143
        puts "$e"
 
144
        exit 3
 
145
}
 
146
 
 
147
if {$removeGetObj} {
 
148
        if {[catch {set swigCode [remProc $swigCode SWIG_GetPointerObj "char \\*"]} e]} {
 
149
                puts "-E- $e"
 
150
                exit 1
 
151
        }
 
152
}
 
153
 
 
154
if {$removeTrailingBlanks} {
 
155
        if {[catch {set swigCode [remTrailingBlanks $swigCode]} e]} {
 
156
                puts "-E- $e"
 
157
                exit 1
 
158
        }
 
159
}
 
160
 
 
161
if {$removeSetObj} {
 
162
        if {[catch {set swigCode [remProc $swigCode SWIG_SetPointerObj "void"]} e]} {
 
163
                puts "-E- $e"
 
164
                exit 1
 
165
        }
 
166
}
 
167
 
 
168
if {$removeMakePtr} {
 
169
        if {[catch {set swigCode [remProc $swigCode SWIG_MakePtr "int"]} e]} {
 
170
                puts "-E- $e"
 
171
                exit 1
 
172
        }
 
173
}
 
174
 
 
175
if {$userPrompt != 0} {
 
176
        if {[catch {set swigCode [addAtLastReturn $swigCode "${readlineCode}r"]} e]} {
 
177
                puts "-E- $e"
 
178
                exit 1
 
179
        }
 
180
}
 
181
 
 
182
if {[catch {set swigCode [addVoidParam $swigCode SWIG_RegisterType "void"]} e]} {
 
183
   puts "-E- $e"
 
184
   exit 1
 
185
}
 
186
 
 
187
if {$outFileName != 0} {
 
188
        if {[catch {set f [open $outFileName w]} e]} {
 
189
                puts "-E- $e"
 
190
                exit 1
 
191
        }
 
192
        puts $f $swigCode
 
193
        close $f
 
194
} else {
 
195
        puts $swigCode
 
196
}