-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bindings for the readline callback interface #8
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
extern crate readline; | ||
|
||
use std::process::exit; | ||
|
||
fn eval(line: Option<String>) { | ||
let line = match line { | ||
Some(line) => line, | ||
None => { | ||
println!(""); | ||
exit(0); | ||
} | ||
}; | ||
|
||
if line == "quit" { | ||
exit(0); | ||
} | ||
|
||
// add words that start with 'a' to the history to demonstrate | ||
else if line[0 .. 1] == "a".to_string() { | ||
readline::add_history(line.as_ref()); | ||
} | ||
|
||
println!("Input: '{}'", line); | ||
} | ||
|
||
fn main() { | ||
readline::rl_callback_handler_install("Next: ", eval); | ||
|
||
// simple r"e"pl | ||
loop { | ||
// a real program would interleave this with other async i/o, using | ||
// something like mio | ||
readline::rl_callback_read_char(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,12 @@ mod ext_readline { | |
|
||
/* readline */ | ||
pub fn readline(p: *const c_char) -> *const c_char; | ||
|
||
/* Asynchronous Interface */ | ||
pub fn rl_callback_handler_install(prompt: *const c_char, | ||
lhandler: extern fn(*const c_char)); | ||
pub fn rl_callback_read_char(); | ||
pub fn rl_callback_handler_remove(); | ||
} | ||
} | ||
|
||
|
@@ -96,6 +102,27 @@ pub fn history_expand(input: &str) -> Result<Option<String>, String> { | |
} | ||
} | ||
|
||
// converts a malloc'd char* returned from readline into a String, and frees the char* | ||
fn c_str_to_string(cstr: *const c_char) -> Option<String> { | ||
if cstr.is_null() { // user pressed Ctrl-D | ||
None | ||
} | ||
else { | ||
unsafe { | ||
let slice = CStr::from_ptr(cstr); | ||
let bytes = slice.to_bytes(); | ||
|
||
// the cstrurn from readline needs to be explicitly freed | ||
// so clone the input first | ||
let line = String::from_utf8_lossy(bytes).into_owned().clone(); | ||
|
||
libc::free(cstr as *mut libc::c_void); | ||
|
||
Some(line) | ||
} | ||
} | ||
} | ||
|
||
/// Invoke the external `readline()`. | ||
/// | ||
/// Returns an `Option<String>` representing whether a `String` was returned | ||
|
@@ -104,21 +131,46 @@ pub fn readline(prompt: &str) -> Option<String> { | |
let cprmt = CString::new(prompt).unwrap().as_ptr(); | ||
unsafe { | ||
let ret = ext_readline::readline(cprmt); | ||
if ret.is_null() { // user pressed Ctrl-D | ||
None | ||
} | ||
else { | ||
let slice = CStr::from_ptr(ret); | ||
let bytes = slice.to_bytes(); | ||
c_str_to_string(ret) | ||
} | ||
} | ||
|
||
// the return from readline needs to be explicitly freed | ||
// so clone the input first | ||
let line = String::from_utf8_lossy(bytes).into_owned().clone(); | ||
static mut _lhandler: Option<fn(Option<String>)> = None; | ||
|
||
libc::free(ret as *mut libc::c_void); | ||
extern fn coerced_callback(ret: *const c_char) { | ||
let line = c_str_to_string(ret); | ||
|
||
Some(line) | ||
} | ||
unsafe { | ||
_lhandler.unwrap()(line); | ||
} | ||
} | ||
|
||
/// Install a new input handler. | ||
/// | ||
/// This function must be called before `rl_callback_read_char()` or risk | ||
/// panicking. | ||
pub fn rl_callback_handler_install(prompt: &str, lhandler: fn(Option<String>)) { | ||
let cprmt = CString::new(prompt).unwrap().as_ptr(); | ||
unsafe { _lhandler = Some(lhandler); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmmm. Not sure how I feel about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I would like to do in the ideal case, however I don't know how to correctly pass a curried function (=closure) as a pointer to a c function. |
||
unsafe { | ||
ext_readline::rl_callback_handler_install(cprmt, coerced_callback); | ||
} | ||
} | ||
|
||
/// Invokes `rl_callback_read_char()` | ||
/// | ||
/// This function will panic if `rl_callback_handler_install()` has not yet | ||
/// been called. (As there is no handler for it to invoke). | ||
pub fn rl_callback_read_char() { | ||
unsafe { | ||
ext_readline::rl_callback_read_char(); | ||
} | ||
} | ||
|
||
pub fn rl_callback_handler_remove() { | ||
unsafe { _lhandler = None }; | ||
unsafe { | ||
ext_readline::rl_callback_handler_remove(); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this bit. Nice refactor.