Facon
Facon

Reputation: 669

Interfacing readline into Rust

I try to do this tutorial in rust, so far I have a lot of problems interfacing the C library into Rust.

C equivalent code:

#include <stdio.h>
#include <stdlib.h>

#include <editline/readline.h>
#include <editline/history.h>

int main(int argc, char** argv) {

  /* Print Version and Exit Information */
  puts("Lispy Version 0.0.0.0.1");
  puts("Press Ctrl+c to Exit\n");

  /* In a never ending loop */
  while (1) {

    /* Output our prompt and get input */
    char* input = readline("lispy> ");

    /* Add input to history */
    add_history(input);

    /* Echo input back to user */    
    printf("No you're a %s\n", input);

    /* Free retrived input */
    free(input);

  }

  return 0;
}

So far i got this:

extern crate libc;

use std::c_str;

#[link(name = "readline")]
extern {
    fn readline (p: *const libc::c_char) -> *const libc::c_char;
}

fn rust_readline (prompt: &str) -> Option<Box<str>> {
    let cprmt = prompt.to_c_str();
    cprmt.with_ref(|c_buf| {
        unsafe {
            let ret = c_str::CString::new (readline (c_buf), true);
            ret.as_str().map(|ret| ret.to_owned())
        }
    })
}

fn main() {
    println!("Lispy Version 0.0.1");
    println!("Press Ctrl+c to Exit.\n");

// I want to have "history" in Linux of this:
//  
//  loop {
//      print!("lispy> ");
//      let input = io::stdin().read_line().unwrap();
//      print!("No you're a {}", input);
//  }

    loop {
        let val = rust_readline ("lispy> ");
        match val {
            None => { break }
            _ => {
                let input = val.unwrap();
                println!("No you're a {}", input);
            }
        }
    }
}

Especifically, I'm having issues with rust_readline function, i don't understand very well what's doing inside.

Upvotes: 5

Views: 1933

Answers (2)

Zelphir Kaltstahl
Zelphir Kaltstahl

Reputation: 6189

Funny thing is, that I found this question reading the very same book, but not including any book specific information in my search query.

There is now a crate for this. Facon's solution worked for me, but the prompt string always printed as garbage using that library, so I looked for another crate and found one working nicely. Here is an updated example:

cargo.toml:

[dependencies]
# https://crates.io/crates/rustyline
rustyline = "3.0.0"

main.rs:

extern crate rustyline;

use rustyline::error::ReadlineError;
use rustyline::Editor;

const HISTORY_FILENAME: &str = "history.txt";

fn main() {
    println!("Lispy Version 0.0.1");
    println!("Press Ctrl+c to Exit.\n");

    // We create an editor for the readline history.
    let mut readline_editor = Editor::<()>::new();
    // And then load the history, if it exists.
    if readline_editor.load_history(HISTORY_FILENAME).is_err() {
        println!("No previous history.");
    }


    loop {
        // We read some input from CLI.
        let readline = readline_editor.readline("LISPY>> ");
        // The reading of the input could fail, if a user uses special
        // key combinations. So we match against the readline Result
        // type. Result can either be some `Ok` or an some `Err`.
        match readline {
            Ok(line) => {
                readline_editor.add_history_entry(line.as_ref());
                println!("No, you are {}", line);
            },
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
                break
            },
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break
            },
            Err(err) => {
                println!("Error: {:?}", err);
                break
            }
        }
        readline_editor.save_history(HISTORY_FILENAME).expect("Could not save to readline history.");
    }
}

Upvotes: 1

Facon
Facon

Reputation: 669

Edit Cargo.toml, put this:

[dependencies.readline]

git = "https://github.com/shaleh/rust-readline"

Code corrected:

extern crate readline;

fn main() {
    println!("Lispy Version 0.0.1");
    println!("Press Ctrl+c to Exit.\n");

    loop {
        let input = readline::readline("lispy> ").unwrap();
        readline::add_history(input.as_str());
        println!("No you're a {}", input);
    }
}

Happy Lisping :-) .

Upvotes: 4

Related Questions