~jojo/Carth

380671c3f8de56a60f71e4c4b482ca6f8cf9d62d — JoJo 1 year, 2 months ago 8cf0fde
std: Add `get-contents` to read stdin until EOF

Like Haskell getContents
2 files changed, 14 insertions(+), 1 deletions(-)

M foreign-core/src/lib.rs
M std/io.carth
M foreign-core/src/lib.rs => foreign-core/src/lib.rs +10 -1
@@ 3,7 3,7 @@
mod ffi;

use libc::*;
use std::io::{self, Write};
use std::io::{self, Read, Write};
use std::{alloc, mem, slice, str};

#[no_mangle]


@@ 122,6 122,15 @@ pub extern "C" fn panic(s: Str) {
    std::process::abort()
}

#[export_name = "-get-contents"]
pub extern "C" fn get_contents() -> Str {
    let mut s = String::new();
    io::stdin()
        .read_to_string(&mut s)
        .expect("read all of stdin");
    Str::new(&s)
}

// NOTE: This is a hack to ensure that Rust links in libm.
//
//       It seems that if no non-dead code makes use of functions from libm, then rustc or

M std/io.carth => std/io.carth +4 -0
@@ 37,3 37,7 @@

(define (display s)
  (display-inline (str-append s "\n")))

(extern -get-contents (Fun RealWorld [Str RealWorld]))

(define get-contents (IO -get-contents))