Skip to content

Commit

Permalink
Make std::rl unsafe. #3921
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Nov 5, 2012
1 parent c8b4dea commit 9aadfc3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/librusti/rusti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,18 @@ pub fn main() {
stmts: ~""
};

do rl::complete |line, suggest| {
if line.starts_with(":") {
suggest(~":clear");
suggest(~":exit");
suggest(~":help");
unsafe {
do rl::complete |line, suggest| {
if line.starts_with(":") {
suggest(~":clear");
suggest(~":exit");
suggest(~":help");
}
}
}

while repl.running {
let result = rl::read(repl.prompt);
let result = unsafe { rl::read(repl.prompt) };

if result.is_none() {
break;
Expand All @@ -290,7 +292,7 @@ pub fn main() {
loop;
}

rl::add_history(line);
unsafe { rl::add_history(line) };

if line.starts_with(~":") {
let full = line.substr(1, line.len() - 1);
Expand Down
15 changes: 9 additions & 6 deletions src/libstd/rl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// FIXME #3921. This is unsafe because linenoise uses global mutable
// state without mutexes.

use libc::{c_char, c_int};

extern mod rustrt {
Expand All @@ -12,33 +15,33 @@ extern mod rustrt {
}

/// Add a line to history
pub fn add_history(line: ~str) -> bool {
pub unsafe fn add_history(line: ~str) -> bool {
do str::as_c_str(line) |buf| {
rustrt::linenoiseHistoryAdd(buf) == 1 as c_int
}
}

/// Set the maximum amount of lines stored
pub fn set_history_max_len(len: int) -> bool {
pub unsafe fn set_history_max_len(len: int) -> bool {
rustrt::linenoiseHistorySetMaxLen(len as c_int) == 1 as c_int
}

/// Save line history to a file
pub fn save_history(file: ~str) -> bool {
pub unsafe fn save_history(file: ~str) -> bool {
do str::as_c_str(file) |buf| {
rustrt::linenoiseHistorySave(buf) == 1 as c_int
}
}

/// Load line history from a file
pub fn load_history(file: ~str) -> bool {
pub unsafe fn load_history(file: ~str) -> bool {
do str::as_c_str(file) |buf| {
rustrt::linenoiseHistoryLoad(buf) == 1 as c_int
}
}

/// Print out a prompt and then wait for input and return it
pub fn read(prompt: ~str) -> Option<~str> {
pub unsafe fn read(prompt: ~str) -> Option<~str> {
do str::as_c_str(prompt) |buf| unsafe {
let line = rustrt::linenoise(buf);

Expand All @@ -52,7 +55,7 @@ pub type CompletionCb = fn~(~str, fn(~str));
fn complete_key(_v: @CompletionCb) {}

/// Bind to the main completion callback
pub fn complete(cb: CompletionCb) unsafe {
pub unsafe fn complete(cb: CompletionCb) unsafe {
task::local_data::local_data_set(complete_key, @(move cb));

extern fn callback(line: *c_char, completions: *()) unsafe {
Expand Down

0 comments on commit 9aadfc3

Please sign in to comment.