~ubuntu-branches/ubuntu/vivid/ibutils/vivid

« back to all changes in this revision

Viewing changes to ibdm/ibdm/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
# Copyright (c) 2004 Mellanox Technologies LTD. All rights reserved.
 
6
#
 
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:
 
12
#
 
13
#     Redistribution and use in source and binary forms, with or
 
14
#     without modification, are permitted provided that the following
 
15
#     conditions are met:
 
16
#
 
17
#      - Redistributions of source code must retain the above
 
18
#        copyright notice, this list of conditions and the following
 
19
#        disclaimer.
 
20
#
 
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.
 
25
#
 
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
 
33
# SOFTWARE.
 
34
#
 
35
# $Id$
 
36
#--
 
37
 
 
38
proc Usage {} {
 
39
        global argv0
 
40
        puts "Usage: $argv0 \[-r <prompt>\] \[-g\] \[-s\] \[-p\] -o <out-file>"
 
41
}
 
42
 
 
43
proc Help {} {
 
44
        puts "\nfixSwigWrapper : Applies extended behaviour to a swig_wrap.c\n"
 
45
        Usage
 
46
        puts "\nArguments:"
 
47
        puts "-o out-file : The name of the out file\n"
 
48
        puts "\nOptions:"
 
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"
 
53
        exit 1
 
54
}
 
55
 
 
56
# basically this code filters out the two swig functions:
 
57
# SWIG_GetPointerObj
 
58
# SWIG_SetPointerObj
 
59
# from the given file and also adds one line just before the last
 
60
# return statement
 
61
 
 
62
proc LoadFile {fn} {
 
63
        # open the file
 
64
        if {[catch {set f [open $fn "r"]} e]} {
 
65
                error "-E- LoadFile: $e"
 
66
        }
 
67
 
 
68
        while {[gets $f sLine] >= 0} {
 
69
                lappend linesList "$sLine"
 
70
        }
 
71
        close $f
 
72
        set res "[join $linesList \n]"
 
73
        puts stderr "-I- Loaded file: $fn with:[string length $res] chars"
 
74
 
 
75
        return $res
 
76
}
 
77
 
 
78
proc remTrailingBlanks {code} {
 
79
        regsub -line -all {[    ]+$} $code {} code
 
80
        return $code
 
81
}
 
82
 
 
83
 
 
84
# remove the given proc from the file
 
85
proc remProc {code procName procRetType} {
 
86
 
 
87
        # find the idx of the start of the procedure
 
88
        if {! [regexp -indices -line "SWIGSTATIC\[\\s\n\]*$procRetType\[\\s\n\]*$procName" \
 
89
                                 $code sidx]} {
 
90
                error "-E- Fail to find proc:$procName"
 
91
        }
 
92
 
 
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"
 
96
        }
 
97
 
 
98
        return [string replace $code [lindex $sidx 0] [lindex $eidx 1]]
 
99
}
 
100
 
 
101
proc addAtLastReturn {code text} {
 
102
        # find the last "return TCL_OK"
 
103
        set idx [string last {return TCL_OK} $code]
 
104
        if {$idx <= 0} {
 
105
                error "-E- Fail to find last return"
 
106
        }
 
107
 
 
108
        return [string replace $code $idx $idx $text]
 
109
}
 
110
 
 
111
 
 
112
# MAIN
 
113
 
 
114
set removeGetObj 0
 
115
set removeSetObj 0
 
116
set removeMakePtr 0
 
117
set userPrompt 0
 
118
set removeTrailingBlanks 1
 
119
set outFileName ""
 
120
 
 
121
# parse command line args
 
122
while {[llength $argv]} {
 
123
        set sw [lindex $argv 0]
 
124
        set argv [lrange $argv 1 end]
 
125
 
 
126
        switch -- $sw {
 
127
                -r {
 
128
                        if {![llength $argv]} {
 
129
                                puts "-E- Expected prompt value after -r"
 
130
                                Usage
 
131
                                exit 1
 
132
                        }
 
133
                        set userPrompt [lindex $argv 0]
 
134
                        set argv [lrange $argv 1 end]
 
135
                }
 
136
                -o {
 
137
                        if {![llength $argv]} {
 
138
                                puts "-E- Expected file name value after -o"
 
139
                                Usage
 
140
                                exit 1
 
141
                        }
 
142
                        set outFileName [lindex $argv 0]
 
143
                        set argv [lrange $argv 1 end]
 
144
                }
 
145
                -g { set removeGetObj 1}
 
146
                -s { set removeSetObj 1}
 
147
                -p { set removeMakePtr 1}
 
148
                -h {
 
149
                        Help
 
150
                }
 
151
                default {
 
152
                        puts "-E- Unexpected argument: $sw"
 
153
                        Usage
 
154
                        exit 1
 
155
                }
 
156
        }
 
157
}
 
158
 
 
159
set readlineCode "
 
160
   if (Tcl_PkgRequire(interp,\"tclreadline\",0,0) != NULL) \{
 
161
     Tcl_Eval(interp,
 
162
                                  \"if \{\$tcl_interactive\} \{namespace eval tclreadline \{proc prompt1 \{\} \{return \\\"$userPrompt >\\\"\} \}; ::tclreadline::Loop $userPrompt.log \}\"
 
163
     );
 
164
   \}
 
165
"
 
166
 
 
167
if {[catch {set swigCode [LoadFile swig_wrap.c]} e]} {
 
168
        puts "$e"
 
169
        exit 3
 
170
}
 
171
 
 
172
if {$removeGetObj} {
 
173
        if {[catch {set swigCode [remProc $swigCode SWIG_GetPointerObj "char \\*"]} e]} {
 
174
                puts "-E- $e"
 
175
                exit 1
 
176
        }
 
177
}
 
178
 
 
179
if {$removeTrailingBlanks} {
 
180
        if {[catch {set swigCode [remTrailingBlanks $swigCode]} e]} {
 
181
                puts "-E- $e"
 
182
                exit 1
 
183
        }
 
184
}
 
185
 
 
186
if {$removeSetObj} {
 
187
        if {[catch {set swigCode [remProc $swigCode SWIG_SetPointerObj "void"]} e]} {
 
188
                puts "-E- $e"
 
189
                exit 1
 
190
        }
 
191
}
 
192
 
 
193
if {$removeMakePtr} {
 
194
        if {[catch {set swigCode [remProc $swigCode SWIG_MakePtr "int"]} e]} {
 
195
                puts "-E- $e"
 
196
                exit 1
 
197
        }
 
198
}
 
199
 
 
200
if {$userPrompt != 0} {
 
201
        if {[catch {set swigCode [addAtLastReturn $swigCode "${readlineCode}r"]} e]} {
 
202
                puts "-E- $e"
 
203
                exit 1
 
204
        }
 
205
}
 
206
 
 
207
if {$outFileName != 0} {
 
208
        if {[catch {set f [open $outFileName w]} e]} {
 
209
                puts "-E- $e"
 
210
                exit 1
 
211
        }
 
212
        puts $f $swigCode
 
213
        close $f
 
214
} else {
 
215
        puts $swigCode
 
216
}