ffi 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. data/README.rdoc +51 -1
  2. data/Rakefile +38 -28
  3. data/ext/ffi_c/AbstractMemory.c +74 -70
  4. data/ext/ffi_c/AbstractMemory.h +8 -4
  5. data/ext/ffi_c/AutoPointer.c +8 -9
  6. data/ext/ffi_c/AutoPointer.h +2 -2
  7. data/ext/ffi_c/Buffer.c +42 -24
  8. data/ext/ffi_c/Callback.c +85 -33
  9. data/ext/ffi_c/Callback.h +11 -5
  10. data/ext/ffi_c/{NativeLibrary.c → DynamicLibrary.c} +83 -16
  11. data/ext/ffi_c/{NativeLibrary.h → DynamicLibrary.h} +1 -1
  12. data/ext/ffi_c/Invoker.c +152 -192
  13. data/ext/ffi_c/LastError.c +135 -0
  14. data/ext/ffi_c/LastError.h +18 -0
  15. data/ext/ffi_c/MemoryPointer.c +27 -20
  16. data/ext/ffi_c/MemoryPointer.h +3 -3
  17. data/ext/ffi_c/NullPointer.c +51 -47
  18. data/ext/ffi_c/Platform.c +9 -10
  19. data/ext/ffi_c/Platform.h +1 -1
  20. data/ext/ffi_c/Pointer.c +52 -21
  21. data/ext/ffi_c/Pointer.h +8 -6
  22. data/ext/ffi_c/Struct.c +70 -61
  23. data/ext/ffi_c/Struct.h +2 -2
  24. data/ext/ffi_c/Type.c +230 -0
  25. data/ext/ffi_c/Type.h +28 -0
  26. data/ext/ffi_c/Types.c +48 -6
  27. data/ext/ffi_c/Types.h +8 -2
  28. data/ext/ffi_c/endian.h +40 -0
  29. data/ext/ffi_c/extconf.rb +6 -5
  30. data/ext/ffi_c/ffi.c +21 -44
  31. data/ext/ffi_c/libffi.bsd.mk +34 -0
  32. data/ext/ffi_c/libffi.darwin.mk +30 -10
  33. data/ext/ffi_c/libffi.gnu.mk +29 -0
  34. data/ext/ffi_c/libffi.mk +4 -2
  35. data/ext/ffi_c/rbffi.h +6 -8
  36. data/gen/Rakefile +12 -0
  37. data/lib/ffi/autopointer.rb +1 -1
  38. data/lib/ffi/enum.rb +78 -0
  39. data/lib/ffi/ffi.rb +5 -6
  40. data/lib/ffi/io.rb +15 -1
  41. data/lib/ffi/library.rb +79 -18
  42. data/lib/ffi/pointer.rb +2 -2
  43. data/lib/ffi/struct.rb +68 -14
  44. data/lib/ffi/types.rb +6 -3
  45. data/lib/ffi/variadic.rb +2 -2
  46. data/lib/ffi.rb +10 -1
  47. data/spec/ffi/bool_spec.rb +24 -0
  48. data/spec/ffi/callback_spec.rb +217 -17
  49. data/spec/ffi/enum_spec.rb +164 -0
  50. data/spec/ffi/managed_struct_spec.rb +6 -1
  51. data/spec/ffi/number_spec.rb +30 -0
  52. data/spec/ffi/pointer_spec.rb +33 -8
  53. data/spec/ffi/rbx/memory_pointer_spec.rb +0 -6
  54. data/spec/ffi/spec_helper.rb +5 -1
  55. data/spec/ffi/string_spec.rb +65 -4
  56. data/spec/ffi/struct_callback_spec.rb +41 -0
  57. data/spec/ffi/struct_initialize_spec.rb +30 -0
  58. data/spec/ffi/struct_spec.rb +44 -21
  59. metadata +31 -69
  60. data/ext/ffi_c/ffi.mk +0 -23
  61. data/nbproject/Makefile-Default.mk +0 -57
  62. data/nbproject/Makefile-impl.mk +0 -123
  63. data/nbproject/Package-Default.bash +0 -72
  64. data/nbproject/configurations.xml +0 -293
  65. data/nbproject/private/configurations.xml +0 -22
  66. data/nbproject/private/private.xml +0 -7
  67. data/nbproject/project.properties +0 -0
  68. data/nbproject/project.xml +0 -15
  69. data/samples/getlogin.rb +0 -7
  70. data/samples/getpid.rb +0 -7
  71. data/samples/gettimeofday.rb +0 -17
  72. data/samples/hello.rb +0 -6
  73. data/samples/inotify.rb +0 -59
  74. data/samples/pty.rb +0 -75
  75. data/samples/qsort.rb +0 -20
  76. data/samples/sample_helper.rb +0 -6
@@ -1,293 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <configurationDescriptor version="51">
3
- <logicalFolder name="root" displayName="root" projectFiles="true">
4
- <logicalFolder name="ruby-ffi" displayName="ruby-ffi" projectFiles="true">
5
- <logicalFolder name="ext" displayName="ext" projectFiles="true">
6
- <itemPath>ext/ffi_c/AbstractMemory.c</itemPath>
7
- <itemPath>ext/ffi_c/AbstractMemory.h</itemPath>
8
- <itemPath>ext/ffi_c/AutoPointer.c</itemPath>
9
- <itemPath>ext/ffi_c/AutoPointer.h</itemPath>
10
- <itemPath>ext/ffi_c/Buffer.c</itemPath>
11
- <itemPath>ext/ffi_c/Callback.c</itemPath>
12
- <itemPath>ext/ffi_c/Callback.h</itemPath>
13
- <itemPath>ext/ffi_c/compat.h</itemPath>
14
- <itemPath>ext/ffi_c/extconf.rb</itemPath>
15
- <itemPath>ext/ffi_c/ffi.c</itemPath>
16
- <itemPath>ext/ffi_c/Invoker.c</itemPath>
17
- <itemPath>ext/ffi_c/MemoryPointer.c</itemPath>
18
- <itemPath>ext/ffi_c/MemoryPointer.h</itemPath>
19
- <itemPath>ext/ffi_c/NativeLibrary.c</itemPath>
20
- <itemPath>ext/ffi_c/NativeLibrary.h</itemPath>
21
- <itemPath>ext/ffi_c/NullPointer.c</itemPath>
22
- <itemPath>ext/ffi_c/Platform.c</itemPath>
23
- <itemPath>ext/ffi_c/Platform.h</itemPath>
24
- <itemPath>ext/ffi_c/Pointer.c</itemPath>
25
- <itemPath>ext/ffi_c/Pointer.h</itemPath>
26
- <itemPath>ext/ffi_c/rbffi.h</itemPath>
27
- <itemPath>ext/ffi_c/Struct.c</itemPath>
28
- <itemPath>ext/ffi_c/Struct.h</itemPath>
29
- <itemPath>ext/ffi_c/Types.c</itemPath>
30
- <itemPath>ext/ffi_c/Types.h</itemPath>
31
- </logicalFolder>
32
- <logicalFolder name="f1" displayName="lib" projectFiles="true">
33
- <logicalFolder name="ffi" displayName="ffi" projectFiles="true">
34
- <itemPath>lib/ffi/autopointer.rb</itemPath>
35
- <itemPath>lib/ffi/callback.rb</itemPath>
36
- <itemPath>lib/ffi/ffi.rb</itemPath>
37
- <itemPath>lib/ffi/io.rb</itemPath>
38
- <itemPath>lib/ffi/library.rb</itemPath>
39
- <itemPath>lib/ffi/memorypointer.rb</itemPath>
40
- <itemPath>lib/ffi/platform.rb</itemPath>
41
- <itemPath>lib/ffi/pointer.rb</itemPath>
42
- <itemPath>lib/ffi/struct.rb</itemPath>
43
- <itemPath>lib/ffi/types.rb</itemPath>
44
- <itemPath>lib/ffi/variadic.rb</itemPath>
45
- </logicalFolder>
46
- <itemPath>lib/ffi.rb</itemPath>
47
- </logicalFolder>
48
- <logicalFolder name="libtest" displayName="libtest" projectFiles="true">
49
- <itemPath>libtest/Benchmark.c</itemPath>
50
- <itemPath>libtest/BufferTest.c</itemPath>
51
- <itemPath>libtest/ClosureTest.c</itemPath>
52
- <itemPath>libtest/GlobalVariable.c</itemPath>
53
- <itemPath>libtest/LastErrorTest.c</itemPath>
54
- <itemPath>libtest/NumberTest.c</itemPath>
55
- <itemPath>libtest/PointerTest.c</itemPath>
56
- <itemPath>libtest/ReferenceTest.c</itemPath>
57
- <itemPath>libtest/StringTest.c</itemPath>
58
- <itemPath>libtest/StructTest.c</itemPath>
59
- <itemPath>libtest/VariadicTest.c</itemPath>
60
- </logicalFolder>
61
- <logicalFolder name="f2" displayName="spec" projectFiles="true">
62
- <itemPath>spec/ffi/buffer_spec.rb</itemPath>
63
- <itemPath>spec/ffi/callback_spec.rb</itemPath>
64
- <itemPath>spec/ffi/errno_spec.rb</itemPath>
65
- <itemPath>spec/ffi/library_spec.rb</itemPath>
66
- <itemPath>spec/ffi/managed_struct_spec.rb</itemPath>
67
- <itemPath>spec/ffi/number_spec.rb</itemPath>
68
- <itemPath>spec/ffi/pointer_spec.rb</itemPath>
69
- <itemPath>spec/ffi/spec_helper.rb</itemPath>
70
- <itemPath>spec/ffi/string_spec.rb</itemPath>
71
- <itemPath>spec/ffi/struct_spec.rb</itemPath>
72
- <itemPath>spec/ffi/typedef_spec.rb</itemPath>
73
- <itemPath>spec/ffi/union_spec.rb</itemPath>
74
- <itemPath>spec/ffi/variadic_spec.rb</itemPath>
75
- </logicalFolder>
76
- </logicalFolder>
77
- <logicalFolder name="ExternalFiles"
78
- displayName="Important Files"
79
- projectFiles="false">
80
- <itemPath>Makefile</itemPath>
81
- <itemPath>Rakefile</itemPath>
82
- <itemPath>ruby-ffi-Makefile.mk</itemPath>
83
- </logicalFolder>
84
- </logicalFolder>
85
- <projectmakefile>ruby-ffi-Makefile.mk</projectmakefile>
86
- <confs>
87
- <conf name="Default" type="0">
88
- <toolsSet>
89
- <developmentServer>localhost</developmentServer>
90
- <compilerSet>GNU|GNU</compilerSet>
91
- <platform>4</platform>
92
- </toolsSet>
93
- <makefileType>
94
- <makeTool>
95
- <buildCommandWorkingDir>.</buildCommandWorkingDir>
96
- <buildCommand>rake compile</buildCommand>
97
- <cleanCommand>rake clean</cleanCommand>
98
- <executablePath></executablePath>
99
- <cCompilerTool>
100
- <includeDirectories>
101
- <directoryPath>/opt/local/include</directoryPath>
102
- </includeDirectories>
103
- </cCompilerTool>
104
- </makeTool>
105
- <requiredProjects>
106
- </requiredProjects>
107
- </makefileType>
108
- <item path="ext/ffi_c/AbstractMemory.c">
109
- <itemTool>0</itemTool>
110
- </item>
111
- <item path="ext/ffi_c/AbstractMemory.h">
112
- <itemTool>3</itemTool>
113
- </item>
114
- <item path="ext/ffi_c/AutoPointer.c">
115
- <itemTool>0</itemTool>
116
- </item>
117
- <item path="ext/ffi_c/AutoPointer.h">
118
- <itemTool>3</itemTool>
119
- </item>
120
- <item path="ext/ffi_c/Buffer.c">
121
- <itemTool>0</itemTool>
122
- </item>
123
- <item path="ext/ffi_c/Callback.c">
124
- <itemTool>0</itemTool>
125
- </item>
126
- <item path="ext/ffi_c/Callback.h">
127
- <itemTool>3</itemTool>
128
- </item>
129
- <item path="ext/ffi_c/Invoker.c">
130
- <itemTool>0</itemTool>
131
- </item>
132
- <item path="ext/ffi_c/MemoryPointer.c">
133
- <itemTool>0</itemTool>
134
- </item>
135
- <item path="ext/ffi_c/MemoryPointer.h">
136
- <itemTool>3</itemTool>
137
- </item>
138
- <item path="ext/ffi_c/NativeLibrary.c">
139
- <itemTool>0</itemTool>
140
- </item>
141
- <item path="ext/ffi_c/NativeLibrary.h">
142
- <itemTool>3</itemTool>
143
- </item>
144
- <item path="ext/ffi_c/NullPointer.c">
145
- <itemTool>0</itemTool>
146
- </item>
147
- <item path="ext/ffi_c/Platform.c">
148
- <itemTool>0</itemTool>
149
- </item>
150
- <item path="ext/ffi_c/Platform.h">
151
- <itemTool>3</itemTool>
152
- </item>
153
- <item path="ext/ffi_c/Pointer.c">
154
- <itemTool>0</itemTool>
155
- </item>
156
- <item path="ext/ffi_c/Pointer.h">
157
- <itemTool>3</itemTool>
158
- </item>
159
- <item path="ext/ffi_c/Struct.c">
160
- <itemTool>0</itemTool>
161
- </item>
162
- <item path="ext/ffi_c/Struct.h">
163
- <itemTool>3</itemTool>
164
- </item>
165
- <item path="ext/ffi_c/Types.c">
166
- <itemTool>0</itemTool>
167
- </item>
168
- <item path="ext/ffi_c/Types.h">
169
- <itemTool>3</itemTool>
170
- </item>
171
- <item path="ext/ffi_c/compat.h">
172
- <itemTool>3</itemTool>
173
- </item>
174
- <item path="ext/ffi_c/extconf.rb">
175
- <itemTool>3</itemTool>
176
- </item>
177
- <item path="ext/ffi_c/ffi.c">
178
- <itemTool>0</itemTool>
179
- </item>
180
- <item path="ext/ffi_c/rbffi.h">
181
- <itemTool>3</itemTool>
182
- </item>
183
- <item path="lib/ffi.rb">
184
- <itemTool>3</itemTool>
185
- </item>
186
- <item path="lib/ffi/autopointer.rb">
187
- <itemTool>3</itemTool>
188
- </item>
189
- <item path="lib/ffi/callback.rb">
190
- <itemTool>3</itemTool>
191
- </item>
192
- <item path="lib/ffi/ffi.rb">
193
- <itemTool>3</itemTool>
194
- </item>
195
- <item path="lib/ffi/io.rb">
196
- <itemTool>3</itemTool>
197
- </item>
198
- <item path="lib/ffi/library.rb">
199
- <itemTool>3</itemTool>
200
- </item>
201
- <item path="lib/ffi/memorypointer.rb">
202
- <itemTool>3</itemTool>
203
- </item>
204
- <item path="lib/ffi/platform.rb">
205
- <itemTool>3</itemTool>
206
- </item>
207
- <item path="lib/ffi/pointer.rb">
208
- <itemTool>3</itemTool>
209
- </item>
210
- <item path="lib/ffi/struct.rb">
211
- <itemTool>3</itemTool>
212
- </item>
213
- <item path="lib/ffi/types.rb">
214
- <itemTool>3</itemTool>
215
- </item>
216
- <item path="lib/ffi/variadic.rb">
217
- <itemTool>3</itemTool>
218
- </item>
219
- <item path="libtest/Benchmark.c">
220
- <itemTool>0</itemTool>
221
- </item>
222
- <item path="libtest/BufferTest.c">
223
- <itemTool>0</itemTool>
224
- </item>
225
- <item path="libtest/ClosureTest.c">
226
- <itemTool>0</itemTool>
227
- </item>
228
- <item path="libtest/GlobalVariable.c">
229
- <itemTool>0</itemTool>
230
- </item>
231
- <item path="libtest/LastErrorTest.c">
232
- <itemTool>0</itemTool>
233
- </item>
234
- <item path="libtest/NumberTest.c">
235
- <itemTool>0</itemTool>
236
- </item>
237
- <item path="libtest/PointerTest.c">
238
- <itemTool>0</itemTool>
239
- </item>
240
- <item path="libtest/ReferenceTest.c">
241
- <itemTool>0</itemTool>
242
- </item>
243
- <item path="libtest/StringTest.c">
244
- <itemTool>0</itemTool>
245
- </item>
246
- <item path="libtest/StructTest.c">
247
- <itemTool>0</itemTool>
248
- </item>
249
- <item path="libtest/VariadicTest.c">
250
- <itemTool>0</itemTool>
251
- </item>
252
- <item path="spec/ffi/buffer_spec.rb">
253
- <itemTool>3</itemTool>
254
- </item>
255
- <item path="spec/ffi/callback_spec.rb">
256
- <itemTool>3</itemTool>
257
- </item>
258
- <item path="spec/ffi/errno_spec.rb">
259
- <itemTool>3</itemTool>
260
- </item>
261
- <item path="spec/ffi/library_spec.rb">
262
- <itemTool>3</itemTool>
263
- </item>
264
- <item path="spec/ffi/managed_struct_spec.rb">
265
- <itemTool>3</itemTool>
266
- </item>
267
- <item path="spec/ffi/number_spec.rb">
268
- <itemTool>3</itemTool>
269
- </item>
270
- <item path="spec/ffi/pointer_spec.rb">
271
- <itemTool>3</itemTool>
272
- </item>
273
- <item path="spec/ffi/spec_helper.rb">
274
- <itemTool>3</itemTool>
275
- </item>
276
- <item path="spec/ffi/string_spec.rb">
277
- <itemTool>3</itemTool>
278
- </item>
279
- <item path="spec/ffi/struct_spec.rb">
280
- <itemTool>3</itemTool>
281
- </item>
282
- <item path="spec/ffi/typedef_spec.rb">
283
- <itemTool>3</itemTool>
284
- </item>
285
- <item path="spec/ffi/union_spec.rb">
286
- <itemTool>3</itemTool>
287
- </item>
288
- <item path="spec/ffi/variadic_spec.rb">
289
- <itemTool>3</itemTool>
290
- </item>
291
- </conf>
292
- </confs>
293
- </configurationDescriptor>
@@ -1,22 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <configurationDescriptor version="51">
3
- <projectmakefile>ruby-ffi-Makefile.mk</projectmakefile>
4
- <defaultConf>0</defaultConf>
5
- <confs>
6
- <conf name="Default" type="0">
7
- <gdbdebugger version="2">
8
- <gdb_command>gdb</gdb_command>
9
- <array_repeat_threshold>10</array_repeat_threshold>
10
- </gdbdebugger>
11
- <runprofile version="5">
12
- <args></args>
13
- <rundir></rundir>
14
- <buildfirst>true</buildfirst>
15
- <console-type>0</console-type>
16
- <terminal-type>0</terminal-type>
17
- <environment>
18
- </environment>
19
- </runprofile>
20
- </conf>
21
- </confs>
22
- </configurationDescriptor>
@@ -1,7 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3
- <code-assistance-data xmlns="http://www.netbeans.org/ns/make-project-private/1">
4
- <code-model-enabled>false</code-model-enabled>
5
- </code-assistance-data>
6
- <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
7
- </project-private>
File without changes
@@ -1,15 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project xmlns="http://www.netbeans.org/ns/project/1">
3
- <type>org.netbeans.modules.cnd.makeproject</type>
4
- <configuration>
5
- <data xmlns="http://www.netbeans.org/ns/make-project/1">
6
- <name>ruby-ffi</name>
7
- <make-project-type>0</make-project-type>
8
- <c-extensions>c</c-extensions>
9
- <cpp-extensions/>
10
- <header-extensions>h</header-extensions>
11
- <sourceEncoding>UTF-8</sourceEncoding>
12
- <make-dep-projects/>
13
- </data>
14
- </configuration>
15
- </project>
data/samples/getlogin.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'rubygems'
2
- require 'ffi'
3
- module Foo
4
- extend FFI::Library
5
- attach_function :getlogin, [ ], :string
6
- end
7
- puts "getlogin=#{Foo.getlogin}"
data/samples/getpid.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'rubygems'
2
- require 'ffi'
3
- module Foo
4
- extend FFI::Library
5
- attach_function :getpid, [ ], :int
6
- end
7
- puts "My pid=#{Foo.getpid}"
@@ -1,17 +0,0 @@
1
- require 'rubygems'
2
- require 'ffi'
3
- class Timeval < FFI::Struct
4
- rb_maj, rb_min, rb_micro = RUBY_VERSION.split('.')
5
- if rb_maj.to_i >= 1 && rb_min.to_i >= 9 || RUBY_PLATFORM =~ /java/
6
- layout :tv_sec => :ulong, :tv_usec => :ulong
7
- else
8
- layout :tv_sec, :ulong, 0, :tv_usec, :ulong, 4
9
- end
10
- end
11
- module LibC
12
- extend FFI::Library
13
- attach_function :gettimeofday, [ :pointer, :pointer ], :int
14
- end
15
- t = Timeval.new
16
- LibC.gettimeofday(t.pointer, nil)
17
- puts "t.tv_sec=#{t[:tv_sec]} t.tv_usec=#{t[:tv_usec]}"
data/samples/hello.rb DELETED
@@ -1,6 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "sample_helper"))
2
- module Foo
3
- extend FFI::Library
4
- attach_function("cputs", "puts", [ :string ], :int)
5
- end
6
- Foo.cputs("Hello, World via libc puts using FFI on MRI ruby")
data/samples/inotify.rb DELETED
@@ -1,59 +0,0 @@
1
- require 'rubygems'
2
- require 'ffi'
3
- module Inotify
4
- extend FFI::Library
5
- class Event < FFI::Struct
6
- layout \
7
- :wd, :int,
8
- :mask, :uint,
9
- :cookie, :uint,
10
- :len, :uint
11
- end
12
- attach_function :init, :inotify_init, [ ], :int
13
- attach_function :add_watch, :inotify_add_watch, [ :int, :string, :uint ], :int
14
- attach_function :rm_watch, :inotify_rm_watch, [ :int, :uint ], :int
15
- attach_function :read, [ :int, :buffer_out, :uint ], :int
16
- IN_ACCESS=0x00000001
17
- IN_MODIFY=0x00000002
18
- IN_ATTRIB=0x00000004
19
- IN_CLOSE_WRITE=0x00000008
20
- IN_CLOSE_NOWRITE=0x00000010
21
- IN_CLOSE=(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
22
- IN_OPEN=0x00000020
23
- IN_MOVED_FROM=0x00000040
24
- IN_MOVED_TO=0x00000080
25
- IN_MOVE= (IN_MOVED_FROM | IN_MOVED_TO)
26
- IN_CREATE=0x00000100
27
- IN_DELETE=0x00000200
28
- IN_DELETE_SELF=0x00000400
29
- IN_MOVE_SELF=0x00000800
30
- # Events sent by the kernel.
31
- IN_UNMOUNT=0x00002000
32
- IN_Q_OVERFLOW=0x00004000
33
- IN_IGNORED=0x00008000
34
- IN_ONLYDIR=0x01000000
35
- IN_DONT_FOLLOW=0x02000000
36
- IN_MASK_ADD=0x20000000
37
- IN_ISDIR=0x40000000
38
- IN_ONESHOT=0x80000000
39
- IN_ALL_EVENTS=(IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE \
40
- | IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM \
41
- | IN_MOVED_TO | IN_CREATE | IN_DELETE \
42
- | IN_DELETE_SELF | IN_MOVE_SELF)
43
-
44
- end
45
- if $0 == __FILE__
46
- fd = Inotify.init
47
- puts "fd=#{fd}"
48
- wd = Inotify.add_watch(fd, "/tmp/", Inotify::IN_ALL_EVENTS)
49
- fp = FFI::IO.for_fd(fd)
50
- puts "wfp=#{fp}"
51
- while true
52
- buf = FFI::Buffer.alloc_out(Inotify::Event.size + 4096, 1, false)
53
- ev = Inotify::Event.new buf
54
- ready = IO.select([ fp ], nil, nil, nil)
55
- n = Inotify.read(fd, buf, buf.total)
56
- puts "Read #{n} bytes from inotify fd"
57
- puts "event.wd=#{ev[:wd]} mask=#{ev[:mask]} len=#{ev[:len]} name=#{ev[:len] > 0 ? buf.get_string(16) : 'unknown'}"
58
- end
59
- end
data/samples/pty.rb DELETED
@@ -1,75 +0,0 @@
1
- require 'ffi'
2
-
3
-
4
- module PTY
5
- private
6
- module LibC
7
- extend FFI::Library
8
- attach_function :forkpty, [ :buffer_out, :buffer_out, :buffer_in, :buffer_in ], :int
9
- attach_function :openpty, [ :buffer_out, :buffer_out, :buffer_out, :buffer_in, :buffer_in ], :int
10
- attach_function :login_tty, [ :int ], :int
11
- attach_function :close, [ :int ], :int
12
- attach_function :strerror, [ :int ], :string
13
- attach_function :fork, [], :int
14
- attach_function :execv, [ :string, :buffer_in ], :int
15
- attach_function :execvp, [ :string, :buffer_in ], :int
16
- attach_function :dup2, [ :int, :int ], :int
17
- attach_function :dup, [ :int ], :int
18
- end
19
- Buffer = FFI::Buffer
20
- def self.build_args(args)
21
- cmd = args.shift
22
- cmd_args = args.map do |arg|
23
- MemoryPointer.from_string(arg)
24
- end
25
- exec_args = MemoryPointer.new(:pointer, 1 + cmd_args.length + 1)
26
- exec_cmd = MemoryPointer.from_string(cmd)
27
- exec_args[0].put_pointer(0, exec_cmd)
28
- cmd_args.each_with_index do |arg, i|
29
- exec_args[i + 1].put_pointer(0, arg)
30
- end
31
- [ cmd, exec_args ]
32
- end
33
- public
34
- def self.getpty(*args)
35
- mfdp = Buffer.new :int
36
- name = Buffer.new 1024
37
- #
38
- # All the execv setup is done in the parent, since doing anything other than
39
- # execv in the child after fork is really flakey
40
- #
41
- exec_cmd, exec_args = build_args(args)
42
- pid = LibC.forkpty(mfdp, name, nil, nil)
43
- raise "forkpty failed: #{LibC.strerror(FFI.errno)}" if pid < 0
44
- if pid == 0
45
- LibC.execvp(exec_cmd, exec_args)
46
- exit 1
47
- end
48
- masterfd = mfdp.get_int(0)
49
- rfp = FFI::IO.for_fd(masterfd, "r")
50
- wfp = FFI::IO.for_fd(LibC.dup(masterfd), "w")
51
- if block_given?
52
- yield rfp, wfp, pid
53
- rfp.close unless rfp.closed?
54
- wfp.close unless wfp.closed?
55
- else
56
- [ rfp, wfp, pid ]
57
- end
58
- end
59
- def self.spawn(*args, &block)
60
- self.getpty("/bin/sh", "-c", args[0], &block)
61
- end
62
- end
63
- module LibC
64
- extend FFI::Library
65
- attach_function :close, [ :int ], :int
66
- attach_function :write, [ :int, :buffer_in, :ulong ], :long
67
- attach_function :read, [ :int, :buffer_out, :ulong ], :long
68
- end
69
- PTY.getpty("/bin/ls", "-alR", "/") { |rfd, wfd, pid|
70
- #PTY.spawn("ls -laR /") { |rfd, wfd, pid|
71
- puts "child pid=#{pid}"
72
- while !rfd.eof? && (buf = rfd.gets)
73
- puts "child: '#{buf.strip}'"
74
- end
75
- }
data/samples/qsort.rb DELETED
@@ -1,20 +0,0 @@
1
- require 'rubygems'
2
- require 'ffi'
3
-
4
- module LibC
5
- extend FFI::Library
6
- callback :qsort_cmp, [ :pointer, :pointer ], :int
7
- attach_function :qsort, [ :pointer, :int, :int, :qsort_cmp ], :int
8
- end
9
-
10
- p = MemoryPointer.new(:int, 2)
11
- p.put_array_of_int32(0, [ 2, 1 ])
12
- puts "ptr=#{p.inspect}"
13
- puts "Before qsort #{p.get_array_of_int32(0, 2).join(', ')}"
14
- LibC.qsort(p, 2, 4) do |p1, p2|
15
- i1 = p1.get_int32(0)
16
- i2 = p2.get_int32(0)
17
- puts "In block: comparing #{i1} and #{i2}"
18
- i1 < i2 ? -1 : i1 > i2 ? 1 : 0
19
- end
20
- puts "After qsort #{p.get_array_of_int32(0, 2).join(', ')}"
@@ -1,6 +0,0 @@
1
- require 'rubygems'
2
- require 'spec'
3
-
4
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib"), File.join(File.dirname(__FILE__), "..", "build", RUBY_VERSION) unless RUBY_PLATFORM =~ /java/
5
- require "ffi"
6
-