libcall 0.0.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.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/exe/libcall +6 -0
- data/lib/libcall/caller.rb +63 -0
- data/lib/libcall/cli.rb +179 -0
- data/lib/libcall/library_finder.rb +127 -0
- data/lib/libcall/parser.rb +101 -0
- data/lib/libcall/version.rb +5 -0
- data/lib/libcall.rb +11 -0
- metadata +76 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d33e4ff88ea0b7f5200165a946f7a146cf0a1d4781d6fd5583d4ccdf3ec17dcf
|
|
4
|
+
data.tar.gz: 24d91124d8f8207fca38aabadab15cced21106e649755130180d248491e03eb0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b79e721348b381ca177f8662ced2a942f7a34b65102a9dc3e8e852ad726c3501e48fadc311f63b7a86eab3113b2081e20c78de1dbbe4b58f2b6249d8541329fc
|
|
7
|
+
data.tar.gz: 9924983519f2f36b5930edab9ae607419eba0199ad6c3aa7a0707ea06006be7512a3d2b1087985e711eeefc928f4d287f23159ba80df240b130acd16cffcfa12
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kojix2
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# libcall
|
|
2
|
+
|
|
3
|
+
[](https://github.com/kojix2/libcall/actions/workflows/main.yml)
|
|
4
|
+
[](https://tokei.kojix2.net/github/kojix2/libcall)
|
|
5
|
+
|
|
6
|
+
Call C functions in shared libraries from the command line.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
gem install libcall
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
libcall [OPTIONS] <LIBRARY> <FUNCTION> [ARGS...]
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Quick Examples
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
# Math function with type suffix
|
|
24
|
+
libcall -lm sqrt 16.0f64 -r f64
|
|
25
|
+
# => 4.0
|
|
26
|
+
|
|
27
|
+
# Custom library
|
|
28
|
+
libcall ./mylib.so add 10i32 20i32 -r i32
|
|
29
|
+
# => 30
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Type Syntax
|
|
33
|
+
|
|
34
|
+
Use Rust-style type suffixes:
|
|
35
|
+
|
|
36
|
+
- Integers: `42i32`, `100u64`, `255u8`
|
|
37
|
+
- Floats: `3.14f64`, `2.5f32`
|
|
38
|
+
- Strings: `"hello"`
|
|
39
|
+
|
|
40
|
+
### Options
|
|
41
|
+
|
|
42
|
+
- `-l LIBRARY` - library name (searches standard paths)
|
|
43
|
+
- `-L PATH` - add library search path
|
|
44
|
+
- `-r TYPE` - return type (void, i32, f64, cstr, ptr)
|
|
45
|
+
- `--dry-run` - validate without executing
|
|
46
|
+
- `--json` - JSON output
|
|
47
|
+
- `--verbose` - detailed info
|
|
48
|
+
- `-h, --help` - show help
|
|
49
|
+
- `-v, --version` - show version
|
|
50
|
+
|
|
51
|
+
### More Examples
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
# JSON output
|
|
55
|
+
libcall --json -lm sqrt 9.0f64 -r f64
|
|
56
|
+
|
|
57
|
+
# Dry run
|
|
58
|
+
libcall --dry-run ./mylib.so test 42i32 -r void
|
|
59
|
+
|
|
60
|
+
# Using -L and -l (like gcc)
|
|
61
|
+
libcall -lmylib -L./build add 10i32 20i32 -r i32
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Type Reference
|
|
65
|
+
|
|
66
|
+
| Suffix | Type | Range/Note |
|
|
67
|
+
| ------ | --------------- | ------------------ |
|
|
68
|
+
| `i8` | signed 8-bit | -128 to 127 |
|
|
69
|
+
| `u8` | unsigned 8-bit | 0 to 255 |
|
|
70
|
+
| `i16` | signed 16-bit | -32768 to 32767 |
|
|
71
|
+
| `u16` | unsigned 16-bit | 0 to 65535 |
|
|
72
|
+
| `i32` | signed 32-bit | standard int |
|
|
73
|
+
| `u32` | unsigned 32-bit | unsigned int |
|
|
74
|
+
| `i64` | signed 64-bit | long long |
|
|
75
|
+
| `u64` | unsigned 64-bit | unsigned long long |
|
|
76
|
+
| `f32` | 32-bit float | single precision |
|
|
77
|
+
| `f64` | 64-bit float | double precision |
|
|
78
|
+
|
|
79
|
+
## pkg-config Support
|
|
80
|
+
|
|
81
|
+
Set `PKG_CONFIG_PATH` and use package names with `-l`:
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
PKG_CONFIG_PATH=/path/to/pkgconfig libcall -lmypackage func 42i32 -r i32
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Warning
|
|
88
|
+
|
|
89
|
+
FFI calls are inherently unsafe. You must:
|
|
90
|
+
|
|
91
|
+
- Provide correct function signatures
|
|
92
|
+
- Match argument types exactly
|
|
93
|
+
- Handle memory correctly
|
|
94
|
+
- Understand ABI compatibility
|
|
95
|
+
|
|
96
|
+
Incorrect usage can crash your program.
|
|
97
|
+
|
|
98
|
+
## Development
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
bundle install
|
|
102
|
+
bundle exec rake test
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
MIT
|
data/exe/libcall
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fiddle'
|
|
4
|
+
|
|
5
|
+
module Libcall
|
|
6
|
+
class Caller
|
|
7
|
+
attr_reader :lib_path, :func_name, :return_type, :args
|
|
8
|
+
|
|
9
|
+
def initialize(lib_path, func_name, args: [], return_type: :void)
|
|
10
|
+
@lib_path = lib_path
|
|
11
|
+
@func_name = func_name
|
|
12
|
+
@return_type = return_type
|
|
13
|
+
@args = args
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call
|
|
17
|
+
arg_types = []
|
|
18
|
+
arg_values = []
|
|
19
|
+
|
|
20
|
+
args.each do |arg|
|
|
21
|
+
type_sym, value = Parser.parse_arg(arg)
|
|
22
|
+
arg_types << Parser.fiddle_type(type_sym)
|
|
23
|
+
arg_values << value
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
ret_type = Parser.fiddle_type(return_type)
|
|
27
|
+
|
|
28
|
+
handle = Fiddle.dlopen(lib_path)
|
|
29
|
+
func_ptr = handle[func_name]
|
|
30
|
+
func = Fiddle::Function.new(func_ptr, arg_types, ret_type)
|
|
31
|
+
|
|
32
|
+
result = func.call(*arg_values)
|
|
33
|
+
|
|
34
|
+
format_result(result, return_type)
|
|
35
|
+
rescue Fiddle::DLError => e
|
|
36
|
+
raise Error, "Failed to load library or function: #{e.message}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def format_result(result, type)
|
|
42
|
+
case type
|
|
43
|
+
when :void
|
|
44
|
+
nil
|
|
45
|
+
when :string, :cstr
|
|
46
|
+
addr = result.to_i
|
|
47
|
+
return '(null)' if addr.zero?
|
|
48
|
+
|
|
49
|
+
begin
|
|
50
|
+
Fiddle::Pointer.new(addr).to_s
|
|
51
|
+
rescue StandardError
|
|
52
|
+
format('0x%x', addr)
|
|
53
|
+
end
|
|
54
|
+
when :float, :double
|
|
55
|
+
result.to_f
|
|
56
|
+
when :voidp, :ptr
|
|
57
|
+
format('0x%x', result.to_i)
|
|
58
|
+
else
|
|
59
|
+
result
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/libcall/cli.rb
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'optparse'
|
|
4
|
+
require 'json'
|
|
5
|
+
|
|
6
|
+
module Libcall
|
|
7
|
+
class CLI
|
|
8
|
+
def initialize(argv)
|
|
9
|
+
@argv = argv
|
|
10
|
+
@options = {
|
|
11
|
+
dry_run: false,
|
|
12
|
+
json: false,
|
|
13
|
+
verbose: false,
|
|
14
|
+
return_type: :void,
|
|
15
|
+
lib: nil,
|
|
16
|
+
lib_name: nil,
|
|
17
|
+
lib_paths: []
|
|
18
|
+
}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def run
|
|
22
|
+
parse_options!
|
|
23
|
+
|
|
24
|
+
if @argv.empty?
|
|
25
|
+
puts @parser.help
|
|
26
|
+
exit 1
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Resolve library path
|
|
30
|
+
if @options[:lib_name]
|
|
31
|
+
finder = LibraryFinder.new(lib_paths: @options[:lib_paths])
|
|
32
|
+
lib_path = finder.find(@options[:lib_name])
|
|
33
|
+
else
|
|
34
|
+
lib_path = @options[:lib] || @argv.shift
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
func_name = @argv.shift
|
|
38
|
+
args = @argv
|
|
39
|
+
|
|
40
|
+
if lib_path.nil? || func_name.nil?
|
|
41
|
+
warn 'Error: Missing required arguments'
|
|
42
|
+
warn 'Usage: libcall <LIBRARY> <FUNCTION> [ARGS...]'
|
|
43
|
+
exit 1
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
if @options[:verbose]
|
|
47
|
+
warn "Library: #{lib_path}"
|
|
48
|
+
warn "Function: #{func_name}"
|
|
49
|
+
warn "Arguments: #{args.inspect}"
|
|
50
|
+
warn "Return type: #{@options[:return_type]}"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
if @options[:dry_run]
|
|
54
|
+
dry_run_info(lib_path, func_name, args)
|
|
55
|
+
else
|
|
56
|
+
execute_call(lib_path, func_name, args)
|
|
57
|
+
end
|
|
58
|
+
rescue Error => e
|
|
59
|
+
warn "Error: #{e.message}"
|
|
60
|
+
exit 1
|
|
61
|
+
rescue StandardError => e
|
|
62
|
+
warn "Unexpected error: #{e.message}"
|
|
63
|
+
warn e.backtrace if @options[:verbose]
|
|
64
|
+
exit 1
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def parse_options!
|
|
70
|
+
@parser = OptionParser.new do |opts|
|
|
71
|
+
opts.banner = <<~BANNER
|
|
72
|
+
Usage: libcall [OPTIONS] <LIBRARY> <FUNCTION> [ARGS...]
|
|
73
|
+
|
|
74
|
+
Call C functions in shared libraries from the command line.
|
|
75
|
+
|
|
76
|
+
Examples:
|
|
77
|
+
libcall /lib/libm.so.6 sqrt 16.0f64 -r f64
|
|
78
|
+
libcall -lm sqrt 16.0f64 -r f64
|
|
79
|
+
libcall -lsum -L. add 10i32 20i32 -r i32
|
|
80
|
+
libcall --dry-run ./mylib.so test 42u64 -r void
|
|
81
|
+
|
|
82
|
+
Options:
|
|
83
|
+
BANNER
|
|
84
|
+
|
|
85
|
+
opts.on('--dry-run', 'Validate arguments without executing') do
|
|
86
|
+
@options[:dry_run] = true
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
opts.on('--json', 'Output result as JSON') do
|
|
90
|
+
@options[:json] = true
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
opts.on('--verbose', 'Show detailed information') do
|
|
94
|
+
@options[:verbose] = true
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
opts.on('-l', '--lib LIBRARY', 'Library name (searches in standard paths)') do |lib|
|
|
98
|
+
@options[:lib_name] = lib
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
opts.on('-L', '--lib-path PATH', 'Add library search path') do |path|
|
|
102
|
+
@options[:lib_paths] << path
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
opts.on('-r', '--ret TYPE', 'Return type (void, i32, f64, cstr, etc.)') do |type|
|
|
106
|
+
@options[:return_type] = Parser.parse_return_type(type)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
opts.on('-h', '--help', 'Show help') do
|
|
110
|
+
puts opts
|
|
111
|
+
exit
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
opts.on('-v', '--version', 'Show version') do
|
|
115
|
+
puts "libcall #{Libcall::VERSION}"
|
|
116
|
+
exit
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
@parser.permute!(@argv)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def dry_run_info(lib_path, func_name, args)
|
|
124
|
+
info = {
|
|
125
|
+
library: lib_path,
|
|
126
|
+
function: func_name,
|
|
127
|
+
arguments: [],
|
|
128
|
+
return_type: @options[:return_type].to_s
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
args.each_with_index do |arg, i|
|
|
132
|
+
type_sym, value = Parser.parse_arg(arg)
|
|
133
|
+
info[:arguments] << {
|
|
134
|
+
index: i,
|
|
135
|
+
raw: arg,
|
|
136
|
+
type: type_sym.to_s,
|
|
137
|
+
value: value
|
|
138
|
+
}
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
if @options[:json]
|
|
142
|
+
puts JSON.pretty_generate(info)
|
|
143
|
+
else
|
|
144
|
+
puts "Library: #{info[:library]}"
|
|
145
|
+
puts "Function: #{info[:function]}"
|
|
146
|
+
puts "Return: #{info[:return_type]}"
|
|
147
|
+
unless info[:arguments].empty?
|
|
148
|
+
puts 'Arguments:'
|
|
149
|
+
info[:arguments].each do |arg|
|
|
150
|
+
puts " [#{arg[:index]}] #{arg[:raw]} => #{arg[:type]} (#{arg[:value].inspect})"
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def execute_call(lib_path, func_name, args)
|
|
157
|
+
caller = Caller.new(
|
|
158
|
+
lib_path,
|
|
159
|
+
func_name,
|
|
160
|
+
args: args,
|
|
161
|
+
return_type: @options[:return_type]
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
result = caller.call
|
|
165
|
+
|
|
166
|
+
if @options[:json]
|
|
167
|
+
output = {
|
|
168
|
+
library: lib_path,
|
|
169
|
+
function: func_name,
|
|
170
|
+
return_type: @options[:return_type].to_s,
|
|
171
|
+
result: result
|
|
172
|
+
}
|
|
173
|
+
puts JSON.pretty_generate(output, allow_nan: true)
|
|
174
|
+
else
|
|
175
|
+
puts result unless result.nil?
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fiddle'
|
|
4
|
+
|
|
5
|
+
begin
|
|
6
|
+
require 'pkg-config'
|
|
7
|
+
rescue LoadError
|
|
8
|
+
# pkg-config is optional
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module Libcall
|
|
12
|
+
class LibraryFinder
|
|
13
|
+
def initialize(lib_paths: [])
|
|
14
|
+
@lib_paths = lib_paths
|
|
15
|
+
@default_paths = default_library_paths
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Find library by name (e.g., "m" -> "/lib/x86_64-linux-gnu/libm.so.6")
|
|
19
|
+
def find(lib_name)
|
|
20
|
+
# If it's a path, return as-is
|
|
21
|
+
return File.expand_path(lib_name) if lib_name.include?('/') || lib_name.include?('\\')
|
|
22
|
+
return File.expand_path(lib_name) if File.file?(lib_name)
|
|
23
|
+
|
|
24
|
+
search_paths = @lib_paths + @default_paths
|
|
25
|
+
|
|
26
|
+
if defined?(PKGConfig)
|
|
27
|
+
pkg_exists = if PKGConfig.respond_to?(:exist?)
|
|
28
|
+
PKGConfig.exist?(lib_name)
|
|
29
|
+
else
|
|
30
|
+
PKGConfig.respond_to?(:have_package) ? PKGConfig.have_package(lib_name) : false
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
if pkg_exists
|
|
34
|
+
lib_dirs = if PKGConfig.respond_to?(:libs_only_L)
|
|
35
|
+
PKGConfig.libs_only_L(lib_name).to_s.split.map { |p| p.start_with?('-L') ? p[2..] : p }
|
|
36
|
+
else
|
|
37
|
+
PKGConfig.libs(lib_name).to_s.split.select { |t| t.start_with?('-L') }.map { |t| t[2..] }
|
|
38
|
+
end
|
|
39
|
+
lib_names = if PKGConfig.respond_to?(:libs_only_l)
|
|
40
|
+
PKGConfig.libs_only_l(lib_name).to_s.split.map { |l| l.start_with?('-l') ? l[2..] : l }
|
|
41
|
+
else
|
|
42
|
+
PKGConfig.libs(lib_name).to_s.split.select { |t| t.start_with?('-l') }.map { |t| t[2..] }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
search_paths = lib_dirs + search_paths
|
|
46
|
+
|
|
47
|
+
# Attempt to resolve any of the advertised libs from the package
|
|
48
|
+
lib_names.each do |lname|
|
|
49
|
+
resolved = resolve_by_name_in_paths(lname, search_paths)
|
|
50
|
+
return resolved if resolved
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Try resolving the provided name using standard conventions
|
|
56
|
+
resolved = resolve_by_name_in_paths(lib_name, search_paths)
|
|
57
|
+
return resolved if resolved
|
|
58
|
+
|
|
59
|
+
raise Error, "Library not found: #{lib_name} (searched in: #{search_paths.join(', ')})"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def default_library_paths
|
|
65
|
+
paths = []
|
|
66
|
+
|
|
67
|
+
# Standard library paths
|
|
68
|
+
paths << '/lib'
|
|
69
|
+
paths << '/usr/lib'
|
|
70
|
+
paths << '/usr/local/lib'
|
|
71
|
+
|
|
72
|
+
# Architecture-specific paths
|
|
73
|
+
if RUBY_PLATFORM =~ /x86_64/
|
|
74
|
+
paths << '/lib/x86_64-linux-gnu'
|
|
75
|
+
paths << '/usr/lib/x86_64-linux-gnu'
|
|
76
|
+
elsif RUBY_PLATFORM =~ /aarch64|arm64/
|
|
77
|
+
paths << '/lib/aarch64-linux-gnu'
|
|
78
|
+
paths << '/usr/lib/aarch64-linux-gnu'
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# macOS paths
|
|
82
|
+
if RUBY_PLATFORM =~ /darwin/
|
|
83
|
+
paths << '/usr/local/lib'
|
|
84
|
+
paths << '/opt/homebrew/lib'
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# LD_LIBRARY_PATH
|
|
88
|
+
paths.concat(ENV['LD_LIBRARY_PATH'].split(':')) if ENV['LD_LIBRARY_PATH']
|
|
89
|
+
|
|
90
|
+
# DYLD_LIBRARY_PATH (macOS)
|
|
91
|
+
paths.concat(ENV['DYLD_LIBRARY_PATH'].split(':')) if ENV['DYLD_LIBRARY_PATH']
|
|
92
|
+
|
|
93
|
+
paths.select { |p| Dir.exist?(p) }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def resolve_by_name_in_paths(lib_name, search_paths)
|
|
97
|
+
# Try direct name first (e.g., "libm.so")
|
|
98
|
+
search_paths.each do |path|
|
|
99
|
+
full_path = File.join(path, lib_name)
|
|
100
|
+
return File.expand_path(full_path) if File.file?(full_path)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Try with lib prefix and common extensions
|
|
104
|
+
extensions = ['', '.so', '.dylib', '.dll']
|
|
105
|
+
prefixes = lib_name.start_with?('lib') ? [''] : ['lib']
|
|
106
|
+
|
|
107
|
+
prefixes.each do |prefix|
|
|
108
|
+
extensions.each do |ext|
|
|
109
|
+
name = "#{prefix}#{lib_name}#{ext}"
|
|
110
|
+
search_paths.each do |path|
|
|
111
|
+
full_path = File.join(path, name)
|
|
112
|
+
return File.expand_path(full_path) if File.file?(full_path)
|
|
113
|
+
|
|
114
|
+
# Check for versioned libraries (libm.so.6, etc.)
|
|
115
|
+
next if ext.empty?
|
|
116
|
+
|
|
117
|
+
pattern = File.join(path, "#{name}.*")
|
|
118
|
+
matches = Dir.glob(pattern).select { |f| File.file?(f) }
|
|
119
|
+
return File.expand_path(matches.first) unless matches.empty?
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
nil
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Libcall
|
|
4
|
+
class Parser
|
|
5
|
+
TYPE_MAP = {
|
|
6
|
+
'i8' => :char,
|
|
7
|
+
'u8' => :uchar,
|
|
8
|
+
'i16' => :short,
|
|
9
|
+
'u16' => :ushort,
|
|
10
|
+
'i32' => :int,
|
|
11
|
+
'u32' => :uint,
|
|
12
|
+
'i64' => :long_long,
|
|
13
|
+
'u64' => :ulong_long,
|
|
14
|
+
'isize' => :long,
|
|
15
|
+
'usize' => :ulong,
|
|
16
|
+
'f32' => :float,
|
|
17
|
+
'f64' => :double,
|
|
18
|
+
'cstr' => :string,
|
|
19
|
+
'ptr' => :voidp,
|
|
20
|
+
'void' => :void,
|
|
21
|
+
'int' => :int,
|
|
22
|
+
'uint' => :uint,
|
|
23
|
+
'long' => :long,
|
|
24
|
+
'ulong' => :ulong,
|
|
25
|
+
'float' => :float,
|
|
26
|
+
'double' => :double,
|
|
27
|
+
'char' => :char,
|
|
28
|
+
'str' => :string
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
def self.parse_arg(arg)
|
|
32
|
+
return [:string, ''] if arg.empty?
|
|
33
|
+
|
|
34
|
+
if (arg.start_with?('"') && arg.end_with?('"')) ||
|
|
35
|
+
(arg.start_with?("'") && arg.end_with?("'"))
|
|
36
|
+
return [:string, arg[1...-1]]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
if arg =~ /^([-+]?(?:\d+\.?\d*|\d*\.\d+))([a-z]\d+|[a-z]+)$/i
|
|
40
|
+
value_str = ::Regexp.last_match(1)
|
|
41
|
+
type_str = ::Regexp.last_match(2)
|
|
42
|
+
|
|
43
|
+
type_sym = TYPE_MAP[type_str]
|
|
44
|
+
raise Error, "Unknown type suffix: #{type_str}" unless type_sym
|
|
45
|
+
|
|
46
|
+
value = if %i[float double].include?(type_sym)
|
|
47
|
+
value_str.to_f
|
|
48
|
+
else
|
|
49
|
+
value_str.to_i
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
return [type_sym, value]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
if arg =~ /^[-+]?\d+$/
|
|
56
|
+
[:int, arg.to_i]
|
|
57
|
+
elsif arg =~ /^[-+]?(?:\d+\.\d*|\d*\.\d+)$/
|
|
58
|
+
[:double, arg.to_f]
|
|
59
|
+
else
|
|
60
|
+
raise Error, "Cannot parse argument: #{arg}"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.parse_return_type(type_str)
|
|
65
|
+
return :void if type_str.nil? || type_str.empty? || type_str == 'void'
|
|
66
|
+
|
|
67
|
+
type_sym = TYPE_MAP[type_str]
|
|
68
|
+
raise Error, "Unknown return type: #{type_str}" unless type_sym
|
|
69
|
+
|
|
70
|
+
type_sym
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.parse_signature(sig)
|
|
74
|
+
raise Error, "Invalid signature format: #{sig}" unless sig =~ /^([a-z]\w*)\((.*)\)$/i
|
|
75
|
+
|
|
76
|
+
ret_type = parse_return_type(::Regexp.last_match(1))
|
|
77
|
+
arg_types = ::Regexp.last_match(2).split(',').map(&:strip).reject(&:empty?).map do |t|
|
|
78
|
+
TYPE_MAP[t] or raise Error, "Unknown type in signature: #{t}"
|
|
79
|
+
end
|
|
80
|
+
[ret_type, arg_types]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def self.fiddle_type(type_sym)
|
|
84
|
+
case type_sym
|
|
85
|
+
when :void then Fiddle::TYPE_VOID
|
|
86
|
+
when :char then Fiddle::TYPE_CHAR
|
|
87
|
+
when :uchar then Fiddle::TYPE_UCHAR
|
|
88
|
+
when :short then Fiddle::TYPE_SHORT
|
|
89
|
+
when :ushort then Fiddle::TYPE_USHORT
|
|
90
|
+
when :int, :uint then Fiddle::TYPE_INT
|
|
91
|
+
when :long, :ulong then Fiddle::TYPE_LONG
|
|
92
|
+
when :long_long, :ulong_long then Fiddle::TYPE_LONG_LONG
|
|
93
|
+
when :float then Fiddle::TYPE_FLOAT
|
|
94
|
+
when :double then Fiddle::TYPE_DOUBLE
|
|
95
|
+
when :voidp, :string then Fiddle::TYPE_VOIDP
|
|
96
|
+
else
|
|
97
|
+
raise Error, "Unknown Fiddle type: #{type_sym}"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
data/lib/libcall.rb
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'libcall/version'
|
|
4
|
+
require_relative 'libcall/parser'
|
|
5
|
+
require_relative 'libcall/library_finder'
|
|
6
|
+
require_relative 'libcall/caller'
|
|
7
|
+
require_relative 'libcall/cli'
|
|
8
|
+
|
|
9
|
+
module Libcall
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: libcall
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- kojix2
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: fiddle
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: pkg-config
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
email: 2xijok@gmail.com
|
|
41
|
+
executables:
|
|
42
|
+
- libcall
|
|
43
|
+
extensions: []
|
|
44
|
+
extra_rdoc_files: []
|
|
45
|
+
files:
|
|
46
|
+
- LICENSE.txt
|
|
47
|
+
- README.md
|
|
48
|
+
- exe/libcall
|
|
49
|
+
- lib/libcall.rb
|
|
50
|
+
- lib/libcall/caller.rb
|
|
51
|
+
- lib/libcall/cli.rb
|
|
52
|
+
- lib/libcall/library_finder.rb
|
|
53
|
+
- lib/libcall/parser.rb
|
|
54
|
+
- lib/libcall/version.rb
|
|
55
|
+
homepage: https://github.com/kojix2/libcall
|
|
56
|
+
licenses:
|
|
57
|
+
- MIT
|
|
58
|
+
metadata: {}
|
|
59
|
+
rdoc_options: []
|
|
60
|
+
require_paths:
|
|
61
|
+
- lib
|
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '3.2'
|
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
- - ">="
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '0'
|
|
72
|
+
requirements: []
|
|
73
|
+
rubygems_version: 3.6.9
|
|
74
|
+
specification_version: 4
|
|
75
|
+
summary: Call functions in shared libraries directly from the CLI
|
|
76
|
+
test_files: []
|