#![allow(non_camel_case_types)]
use std::ffi::CStr;
use std::os::raw::{c_char, c_int, c_void};
pub enum irgx_engine {}
pub enum gist_cursor {}
pub enum irgx_cancel {}
pub enum irgx_rows {}
#[repr(C)]
pub struct Submatch {
pub text: *const u8,
pub len: usize,
pub start: usize,
pub end: usize,
}
#[repr(C)]
pub struct MatchView {
pub path: *const u8,
pub path_len: usize,
pub line_number: u64,
pub line: *const u8,
pub line_len: usize,
pub submatches: *const Submatch,
pub nsubmatches: usize,
pub kind: u32,
}
#[repr(C)]
pub struct SearchRequest {
pub struct_size: u32,
pub flags: u32,
pub max_count: u64,
pub before_context: u64,
pub after_context: u64,
pub pattern: *const u8,
pub pattern_len: usize,
pub timeout_ns: u64,
pub max_results: usize,
pub cancel: *mut irgx_cancel,
}
pub const FLAG_FIXED: u32 = 1 << 0;
pub const FLAG_IGNORE_CASE: u32 = 1 << 1;
pub const FLAG_WORD: u32 = 1 << 2;
pub const FLAG_QUIET: u32 = 1 << 3;
pub const FLAG_MAX_COUNT: u32 = 1 << 4;
pub const FLAG_SMART_CASE: u32 = 1 << 5;
pub const FLAG_NO_UNICODE: u32 = 1 << 6;
pub const FLAG_INVERT: u32 = 1 << 7;
pub const OK: c_int = crate::contract::STATUS_OK;
pub const MATCH: c_int = crate::contract::STATUS_MATCH;
pub const STALE: c_int = crate::contract::STATUS_STALE;
pub const OOM: c_int = crate::contract::STATUS_OOM;
pub const OPEN_FAILED: c_int = crate::contract::STATUS_OPEN_FAILED;
pub const INVALID: c_int = crate::contract::STATUS_INVALID;
pub const VAL_TEXT: u32 = 0;
pub const VAL_I64: u32 = 1;
pub const VAL_F64: u32 = 2;
pub const VAL_BOOL: u32 = 3;
pub const VAL_ENUM: u32 = 4;
pub const VAL_TEXTS: u32 = 5;
pub const VAL_ROWS: u32 = 6;
pub const AN_MAX_DISTANCE: u32 = 1 << 0;
pub const AN_MIN_ECHO: u32 = 1 << 1;
pub const AN_NO_INDEX: u32 = 1 << 2;
pub const AN_FIXED: u32 = 1 << 3;
pub const AN_IGNORE_CASE: u32 = 1 << 4;
pub const AN_MATCH_ALL: u32 = 1 << 5;
pub const AN_BY_PATTERN: u32 = 1 << 6;
pub const AN_BY_FILE: u32 = 1 << 7;
pub const AN_DISTINCT: u32 = 1 << 8;
pub const SOURCE_LIVE: u32 = 0;
pub const SOURCE_ATLAS: u32 = 1;
pub const SOURCE_SHELF: u32 = 2;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Text {
pub ptr: *const u8,
pub len: usize,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Value {
pub tag: u32,
pub reserved: u32,
pub integer: i64,
pub real: f64,
pub ptr: *const c_void,
pub len: usize,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Row {
pub schema_id: u32,
pub nvalues: u32,
pub present: u64,
pub values: *const Value,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Field {
pub name: *const c_char,
pub tag: u32,
pub nested: u32,
pub optional: c_int,
pub reserved: c_int,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Schema {
pub struct_size: u32,
pub id: u32,
pub name: *const c_char,
pub nfields: u32,
pub reserved: u32,
pub fields: *const Field,
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
pub struct Stats {
pub struct_size: u32,
pub source: u32,
pub elapsed_ns: u64,
pub files_considered: u64,
pub refreshed: u64,
pub foreign: u64,
pub omitted: u64,
pub rows: u64,
}
#[repr(C)]
pub struct KinshipParams {
pub struct_size: u32,
pub flags: u32,
pub target: *const u8,
pub target_len: usize,
pub channel: u32,
pub unit: u32,
pub max_distance: f64,
pub min_echo: f64,
pub min_grade: u32,
pub min_size: u32,
pub min_lines: u32,
pub top: u32,
}
#[repr(C)]
pub struct RetrievalParams {
pub struct_size: u32,
pub flags: u32,
pub query: *const u8,
pub query_len: usize,
pub top: u32,
pub reserved: u32,
}
#[repr(C)]
pub struct SweepParams {
pub struct_size: u32,
pub flags: u32,
pub patterns: *const Text,
pub npatterns: usize,
pub under: *const u8,
pub under_len: usize,
pub top: u32,
pub reserved: u32,
}
#[repr(C)]
pub struct ComposeParams {
pub struct_size: u32,
pub flags: u32,
pub text: *const u8,
pub text_len: usize,
pub patterns: *const Text,
pub npatterns: usize,
pub max_distance: f64,
pub min_echo: f64,
pub budget: u32,
pub top: u32,
}
#[repr(C)]
pub struct RankParams {
pub struct_size: u32,
pub flags: u32,
pub pattern: *const u8,
pub pattern_len: usize,
pub top: u32,
pub reserved: u32,
}
#[repr(C)]
pub struct Fault {
pub struct_size: u32,
pub status: c_int,
pub at_space: c_int,
pub name: *const c_char,
pub path: *const u8,
pub path_len: usize,
pub at: u64,
}
pub type AnalyticRunFn = unsafe extern "C" fn(
engine: *mut irgx_engine,
op: u32,
params: *const c_void,
cancel: *mut irgx_cancel,
out: *mut *mut irgx_rows,
) -> c_int;
pub type RowsNextFn = unsafe extern "C" fn(rows: *mut irgx_rows, out: *mut Row) -> c_int;
pub type RowsNextBatchFn = unsafe extern "C" fn(
rows: *mut irgx_rows,
out: *mut Row,
cap: usize,
written: *mut usize,
) -> c_int;
pub type RowsStatsFn = unsafe extern "C" fn(rows: *mut irgx_rows, out: *mut Stats) -> c_int;
pub type RowsCloseFn = unsafe extern "C" fn(rows: *mut irgx_rows);
pub type SchemaDigestFn = unsafe extern "C" fn() -> *const c_char;
pub type SchemaCountFn = unsafe extern "C" fn() -> u32;
pub type SchemaGetFn = unsafe extern "C" fn(id: u32, out: *mut Schema) -> c_int;
pub type LastFaultFn = unsafe extern "C" fn(out: *mut Fault) -> c_int;
pub type EngineOpenFn = unsafe extern "C" fn(
roots: *const *const c_char,
nroots: usize,
out: *mut *mut irgx_engine,
) -> c_int;
pub type EngineCloseFn = unsafe extern "C" fn(engine: *mut irgx_engine);
pub type CancelNewFn = unsafe extern "C" fn(out: *mut *mut irgx_cancel) -> c_int;
pub type CancelRequestFn = unsafe extern "C" fn(cancel: *mut irgx_cancel);
pub type CancelFreeFn = unsafe extern "C" fn(cancel: *mut irgx_cancel);
#[cfg(unix)]
unsafe extern "C" {
fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
fn dladdr(addr: *const c_void, info: *mut DlInfo) -> c_int;
fn dlopen(path: *const c_char, flags: c_int) -> *mut c_void;
fn dlclose(handle: *mut c_void) -> c_int;
}
#[cfg(unix)]
#[repr(C)]
struct DlInfo {
dli_fname: *const c_char,
dli_fbase: *mut c_void,
dli_sname: *const c_char,
dli_saddr: *mut c_void,
}
#[cfg(all(unix, target_vendor = "apple"))]
const RTLD_NOLOAD_LAZY: c_int = 0x10 | 0x1;
#[cfg(all(unix, not(target_vendor = "apple")))]
const RTLD_NOLOAD_LAZY: c_int = 0x4 | 0x1;
#[cfg(all(unix, target_vendor = "apple"))]
const RTLD_DEFAULT: *mut c_void = -2isize as *mut c_void;
#[cfg(all(unix, not(target_vendor = "apple")))]
const RTLD_DEFAULT: *mut c_void = std::ptr::null_mut();
#[cfg(unix)]
pub fn symbol(name: &CStr) -> Option<*mut c_void> {
let found = unsafe { dlsym(RTLD_DEFAULT, name.as_ptr()) };
(!found.is_null()).then_some(found)
}
#[cfg(not(unix))]
pub fn symbol(_name: &CStr) -> Option<*mut c_void> {
None
}
#[cfg(unix)]
pub fn shares_engine(name: &CStr, opener: &CStr) -> bool {
let Some(found) = symbol(name) else {
return false;
};
let mut info = DlInfo {
dli_fname: std::ptr::null(),
dli_fbase: std::ptr::null_mut(),
dli_sname: std::ptr::null(),
dli_saddr: std::ptr::null_mut(),
};
if unsafe { dladdr(found.cast_const(), &raw mut info) } == 0 || info.dli_fname.is_null() {
return false;
}
let image = unsafe { dlopen(info.dli_fname, RTLD_NOLOAD_LAZY) };
if image.is_null() {
return false;
}
let shares = unsafe { !dlsym(image, opener.as_ptr()).is_null() };
unsafe { dlclose(image) };
shares
}
#[cfg(not(unix))]
pub fn shares_engine(_name: &CStr, _opener: &CStr) -> bool {
false
}