Hi, I’m trying to build use the Ctypes/Foreign FFI to bind GNU Readline to Ocaml in order to add vi line editing to the ocaml toplevels. To do this, I need to bind a variable which is a C function pointer and assign it at runtime to an ocaml function, which RWO doesn’t cover. I’m doing this with the functioni pointer rl_attempted_completion_function (cf. The relevant code from readline.h
:
\\ ..
typedef char **rl_completion_func_t (const char *, int, int);
\\ ...
extern rl_completion_func_t *rl_attempted_completion_function;
In my readline.ml I have:
open Ctypes
open Foreign
let rl_completion_func_t =
funptr ( string @-> int @-> int @-> returning (ptr string))
let rl_attempted_completion_function =
foreign_value "rl_attempted_completion_function"
( rl_completion_func_t )
And to test it, I write in term.ml:
open Ctypes
open Readline
let custom_attempted_completion_function text a b = text
let () =
rl_attempted_completion_function <-@ custom_attempted_completion_function;
This does not work, because
-
rl_attempted function
is aptr
to afunptr
, whilecustom_attempted_completion_function
isn’t afunptr
at all - my function is returning a
string
, when it ought to return the ocaml equivalent of achar **
, which I suppose isstring ptr
. But I don’t know how to get a pointer to the string argument?
How can I assign the bound function pointer variable to my ocaml function? Or is my approach incorrect?