diff --git a/src/comp/front/lexer.rs b/src/comp/front/lexer.rs index 0e15e3d8c35bf..95fd32c7b4992 100644 --- a/src/comp/front/lexer.rs +++ b/src/comp/front/lexer.rs @@ -1,4 +1,4 @@ -import std.io.stdio_reader; +import std.io; import std._str; import std.map; import std.map.hashmap; @@ -18,9 +18,9 @@ state type reader = state obj { fn get_reserved() -> hashmap[str,()]; }; -fn new_reader(stdio_reader rdr, str filename) -> reader +impure fn new_reader(io.reader rdr, str filename) -> reader { - state obj reader(stdio_reader rdr, + state obj reader(io.reader rdr, str filename, mutable char c, mutable char n, @@ -72,7 +72,7 @@ fn new_reader(stdio_reader rdr, str filename) -> reader col += 1u; } - n = rdr.getc() as char; + n = rdr.read_char() as char; } fn mark() { @@ -200,8 +200,8 @@ fn new_reader(stdio_reader rdr, str filename) -> reader reserved.insert("m128", ()); // IEEE 754-2008 'decimal128' reserved.insert("dec", ()); // One of m32, m64, m128 - ret reader(rdr, filename, rdr.getc() as char, rdr.getc() as char, - 1u, 0u, 1u, 0u, keywords, reserved); + ret reader(rdr, filename, rdr.read_char() as char, + rdr.read_char() as char, 1u, 0u, 1u, 0u, keywords, reserved); } diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 71e592ff10686..03bb9008bc400 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -110,7 +110,7 @@ impure fn new_parser(session.session sess, if (_str.ends_with(path, ".rc")) { ftype = CRATE_FILE; } - auto srdr = io.new_stdio_reader(path); + auto srdr = io.file_reader(path); auto rdr = lexer.new_reader(srdr, path); auto npos = rdr.get_curr_pos(); ret stdio_parser(sess, env, ftype, lexer.next_token(rdr), diff --git a/src/lib/_str.rs b/src/lib/_str.rs index 3f45334924d8a..526dac8f21fb7 100644 --- a/src/lib/_str.rs +++ b/src/lib/_str.rs @@ -106,6 +106,11 @@ fn from_bytes(vec[u8] v) : is_utf8(v) -> str { ret rustrt.str_from_vec(v); } +// FIXME temp thing +fn unsafe_from_bytes(vec[u8] v) -> str { + ret rustrt.str_from_vec(v); +} + fn refcount(str s) -> uint { auto r = rustrt.refcount[u8](s); if (r == dbg.const_refcount) { diff --git a/src/lib/fs.rs b/src/lib/fs.rs new file mode 100644 index 0000000000000..37fbcfff98b3f --- /dev/null +++ b/src/lib/fs.rs @@ -0,0 +1,20 @@ +native "rust" mod rustrt { + fn rust_file_is_dir(str path) -> int; +} + +impure fn file_is_dir(str path) -> bool { + ret rustrt.rust_file_is_dir(path) != 0; +} + +impure fn list_dir(str path) -> vec[str] { + if (path.(_str.byte_len(path)-1u) as char != os_fs.path_sep) { + path += _str.unsafe_from_bytes(vec(os_fs.path_sep as u8)); + } + let vec[str] full_paths = vec(); + for (str filename in os_fs.list_dir(path)) { + if (!_str.eq(filename, ".")) {if (!_str.eq(filename, "..")) { + full_paths = _vec.push[str](full_paths, path + filename); + }} + } + ret full_paths; +} diff --git a/src/lib/io.rs b/src/lib/io.rs index 34c4a98d9ac4d..c9632d49b04c2 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -1,93 +1,113 @@ import os.libc; -type stdio_reader = state obj { - fn getc() -> int; - fn ungetc(int i); -}; - -fn new_stdio_reader(str path) -> stdio_reader { - state obj stdio_FILE_reader(os.libc.FILE f) { - fn getc() -> int { - ret os.libc.fgetc(f); - } - fn ungetc(int i) { - os.libc.ungetc(i, f); - } - drop { - os.libc.fclose(f); - } - } - auto FILE = os.libc.fopen(_str.buf(path), _str.buf("r")); - check (FILE as uint != 0u); - ret stdio_FILE_reader(FILE); +native "rust" mod rustrt { + fn rust_get_stdin() -> os.libc.FILE; + fn rust_get_stdout() -> os.libc.FILE; } +// Reading -type buf_reader = state obj { - fn read() -> vec[u8]; -}; - -type buf_writer = state obj { - fn write(vec[u8] v); -}; - -fn default_bufsz() -> uint { - ret 4096u; -} - -fn new_buf() -> vec[u8] { - ret _vec.alloc[u8](default_bufsz()); -} - -fn new_buf_reader(str path) -> buf_reader { +// TODO This is all buffered. We might need an unbuffered variant as well - state obj fd_buf_reader(int fd, mutable vec[u8] buf) { +tag seek_style {seek_set; seek_end; seek_cur;} - fn read() -> vec[u8] { - - // Ensure our buf is singly-referenced. - if (_vec.rustrt.refcount[u8](buf) != 1u) { - buf = new_buf(); - } - - auto len = default_bufsz(); - auto vbuf = _vec.buf[u8](buf); - auto count = os.libc.read(fd, vbuf, len); - - if (count < 0) { - log "error filling buffer"; - log sys.rustrt.last_os_error(); - fail; - } +type reader = + state obj { + impure fn read_byte() -> u8; + impure fn read_bytes(uint len) -> vec[u8]; + impure fn read_char() -> int; + impure fn unread_char(int i); + impure fn read_c_str() -> str; + impure fn read_le_uint(uint size) -> uint; + impure fn read_le_int(uint size) -> int; + + impure fn seek(int offset, seek_style whence); + }; - _vec.len_set[u8](buf, count as uint); - ret buf; +state obj FILE_reader(os.libc.FILE f, bool must_close) { + impure fn read_byte() -> u8 { + ret os.libc.fgetc(f) as u8; + } + impure fn read_bytes(uint len) -> vec[u8] { + auto buf = _vec.alloc[u8](len); + auto read = os.libc.fread(_vec.buf[u8](buf), 1u, len, f); + check(read == len); + ret buf; + } + impure fn read_char() -> int { + ret os.libc.fgetc(f); + } + impure fn unread_char(int ch) { + os.libc.ungetc(ch, f); + } + impure fn read_c_str() -> str { + auto buf = ""; + while (true) { + auto ch = os.libc.fgetc(f); + if (ch < 1) {break;} + buf += _str.unsafe_from_bytes(vec(ch as u8)); } - - drop { - os.libc.close(fd); + ret buf; + } + // TODO deal with eof? + impure fn read_le_uint(uint size) -> uint { + auto val = 0u; + auto pos = 0u; + while (size > 0u) { + val += (os.libc.fgetc(f) as uint) << pos; + pos += 8u; + size -= 1u; + } + ret val; + } + impure fn read_le_int(uint size) -> int { + auto val = 0u; + auto pos = 0u; + while (size > 0u) { + val += (os.libc.fgetc(f) as uint) << pos; + pos += 8u; + size -= 1u; } + ret val as int; // TODO does that work? + } + impure fn seek(int offset, seek_style whence) { + auto wh; + alt (whence) { + case (seek_set) {wh = 0;} + case (seek_cur) {wh = 1;} + case (seek_end) {wh = 2;} + } + check(os.libc.fseek(f, offset, wh) == 0); + } + drop { + if (must_close) {os.libc.fclose(f);} } +} - auto fd = os.libc.open(_str.buf(path), - os.libc_constants.O_RDONLY() | - os.libc_constants.O_BINARY(), - 0u); +fn stdin_reader() -> reader { + ret FILE_reader(rustrt.rust_get_stdin(), false); +} - if (fd < 0) { - log "error opening file for reading"; - log sys.rustrt.last_os_error(); - fail; - } - ret fd_buf_reader(fd, new_buf()); +fn file_reader(str path) -> reader { + auto f = os.libc.fopen(_str.buf(path), _str.buf("r")); + check (f as uint != 0u); + ret FILE_reader(f, true); } +// Writing + +// TODO This is all unbuffered. We might need a buffered variant as well + tag fileflag { append; create; truncate; } +type buf_writer = state obj { + fn write(vec[u8] v); +}; + state obj fd_buf_writer(int fd, bool must_close) { fn write(vec[u8] v) { auto len = _vec.len[u8](v); @@ -141,8 +161,21 @@ type writer = impure fn write_str(str s); impure fn write_int(int n); impure fn write_uint(uint n); + impure fn write_bytes(vec[u8] bytes); + impure fn write_le_uint(uint n, uint size); + impure fn write_le_int(int n, uint size); }; +fn uint_to_bytes(uint n, uint size) -> vec[u8] { + let vec[u8] bytes = vec(); + while (size > 0u) { + bytes += vec((n & 255u) as u8); + n >>= 8u; + size -= 1u; + } + ret bytes; +} + state obj new_writer(buf_writer out) { impure fn write_str(str s) { out.write(_str.bytes(s)); @@ -153,6 +186,15 @@ state obj new_writer(buf_writer out) { impure fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); } + impure fn write_bytes(vec[u8] bytes) { + out.write(bytes); + } + impure fn write_le_uint(uint n, uint size) { + out.write(uint_to_bytes(n, size)); + } + impure fn write_le_int(int n, uint size) { + out.write(uint_to_bytes(n as uint, size)); + } } fn file_writer(str path, vec[fileflag] flags) -> writer { @@ -170,21 +212,21 @@ type str_writer = fn get_str() -> str; }; -type str_buf = @rec(mutable str buf); +type byte_buf = @rec(mutable vec[u8] buf); + +state obj byte_buf_writer(byte_buf buf) { + fn write(vec[u8] v) {buf.buf += v;} +} // TODO awkward! it's not possible to implement a writer with an extra method fn string_writer() -> str_writer { - auto buf = @rec(mutable buf = ""); - state obj str_writer_writer(str_buf buf) { - impure fn write_str(str s) { buf.buf += s; } - impure fn write_int(int n) { buf.buf += _int.to_str(n, 10u); } - impure fn write_uint(uint n) { buf.buf += _uint.to_str(n, 10u); } - } - state obj str_writer_wrap(writer wr, str_buf buf) { + let vec[u8] b = vec(); + let byte_buf buf = @rec(mutable buf = b); + state obj str_writer_wrap(writer wr, byte_buf buf) { fn get_writer() -> writer {ret wr;} - fn get_str() -> str {ret buf.buf;} + fn get_str() -> str {ret _str.unsafe_from_bytes(buf.buf);} } - ret str_writer_wrap(str_writer_writer(buf), buf); + ret str_writer_wrap(new_writer(byte_buf_writer(buf)), buf); } // diff --git a/src/lib/linux_os.rs b/src/lib/linux_os.rs index d5ef2a6b8009e..3852e1dcf2f72 100644 --- a/src/lib/linux_os.rs +++ b/src/lib/linux_os.rs @@ -13,11 +13,14 @@ native mod libc = "libc.so.6" { fn fclose(FILE f); fn fgetc(FILE f) -> int; fn ungetc(int c, FILE f); + fn fread(vbuf buf, uint size, uint n, FILE f) -> uint; + fn fseek(FILE f, int offset, int whence) -> int; type dir; - // readdir is a mess; handle via wrapper function in rustrt. fn opendir(sbuf d) -> dir; fn closedir(dir d) -> int; + type dirent; + fn readdir(dir d) -> dirent; fn getenv(sbuf n) -> sbuf; fn setenv(sbuf n, sbuf v, int overwrite) -> int; diff --git a/src/lib/macos_os.rs b/src/lib/macos_os.rs index 53a66b56becf6..d7a93fa6b3ea5 100644 --- a/src/lib/macos_os.rs +++ b/src/lib/macos_os.rs @@ -13,11 +13,14 @@ native mod libc = "libc.dylib" { fn fclose(FILE f); fn fgetc(FILE f) -> int; fn ungetc(int c, FILE f); + fn fread(vbuf buf, uint size, uint n, FILE f) -> uint; + fn fseek(FILE f, int offset, int whence) -> int; type dir; - // readdir is a mess; handle via wrapper function in rustrt. fn opendir(sbuf d) -> dir; fn closedir(dir d) -> int; + type dirent; + fn readdir(dir d) -> dirent; fn getenv(sbuf n) -> sbuf; fn setenv(sbuf n, sbuf v, int overwrite) -> int; diff --git a/src/lib/posix_fs.rs b/src/lib/posix_fs.rs new file mode 100644 index 0000000000000..05366a15dbfd2 --- /dev/null +++ b/src/lib/posix_fs.rs @@ -0,0 +1,19 @@ +native "rust" mod rustrt { + fn rust_dirent_filename(os.libc.dirent ent) -> str; +} + +impure fn list_dir(str path) -> vec[str] { + // TODO ensure this is always closed + auto dir = os.libc.opendir(_str.buf(path)); + check (dir as uint != 0u); + let vec[str] result = vec(); + while (true) { + auto ent = os.libc.readdir(dir); + if (ent as int == 0) {break;} + result = _vec.push[str](result, rustrt.rust_dirent_filename(ent)); + } + os.libc.closedir(dir); + ret result; +} + +const char path_sep = '/'; diff --git a/src/lib/std.rc b/src/lib/std.rc index 4ad422a336374..70424ed9c42b5 100644 --- a/src/lib/std.rc +++ b/src/lib/std.rc @@ -26,6 +26,8 @@ mod util; // Authorize various rule-bendings. auth io = unsafe; +auth fs = unsafe; +auth os_fs = unsafe; auth _str = unsafe; auth _vec = unsafe; auth _task = unsafe; @@ -41,12 +43,17 @@ auth rand.mk_rng = unsafe; alt (target_os) { case ("win32") { mod os = "win32_os.rs"; + mod os_fs = "win32_fs.rs"; } case ("macos") { mod os = "macos_os.rs"; + mod os_fs = "posix_fs.rs"; } else { mod os = "linux_os.rs"; + mod os_fs = "posix_fs.rs"; } - } +} +mod fs; + // FIXME: parametric mod map; diff --git a/src/lib/win32_fs.rs b/src/lib/win32_fs.rs new file mode 100644 index 0000000000000..18a02642c7f82 --- /dev/null +++ b/src/lib/win32_fs.rs @@ -0,0 +1,10 @@ +native "rust" mod rustrt { + fn rust_list_files[T,U](str path) -> vec[str]; + fn rust_file_is_dir(str path) -> int; +} + +impure fn list_dir(str path) -> vec[str] { + ret rustrt.rust_list_files[vec[str],str](path+"*"); +} + +const char path_sep = '\\'; diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs index 9f9ec2c2b9e85..bce642dd83fa2 100644 --- a/src/lib/win32_os.rs +++ b/src/lib/win32_os.rs @@ -12,6 +12,8 @@ native mod libc = "msvcrt.dll" { fn fclose(FILE f); fn fgetc(FILE f) -> int; fn ungetc(int c, FILE f); + fn fread(vbuf buf, uint size, uint n, FILE f) -> uint; + fn fseek(FILE f, int offset, int whence) -> int; } mod libc_constants { diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index a859c7b9d4911..1d8dd22d41626 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -1,4 +1,7 @@ +#include +#include +#include #include "rust_internal.h" /* Native builtins. */ @@ -371,6 +374,49 @@ debug_trap(rust_task *task, rust_str *s) __asm__("int3"); } +rust_str* c_str_to_rust(rust_task *task, char const *str) { + size_t len = strlen(str) + 1; + return str_alloc_with_data(task, len, len, (uint8_t const *)str); +} + +#if defined(__WIN32__) +extern "C" CDECL rust_vec* +rust_list_files(rust_task *task, type_desc *vec_desc, + type_desc *str_desc, rust_str *path) { + rust_str* strings[200]; + size_t pos = 0; + WIN32_FIND_DATA FindFileData; + HANDLE hFind = FindFirstFile((char*)path->data, &FindFileData); + if (hFind != INVALID_HANDLE_VALUE) { + strings[pos++] = c_str_to_rust(task, FindFileData.cFileName); + while (FindNextFile(hFind, &FindFileData)) { + strings[pos++] = c_str_to_rust(task, FindFileData.cFileName); + } + FindClose(hFind); + } + rust_vec* result = vec_alloc(task, vec_desc, str_desc, pos); + for (size_t i = 0; i < pos; ++i) { + *(rust_str**)vec_buf(task, str_desc, result, i) = strings[i]; + } + vec_len_set(task, str_desc, result, pos); + return result; +} +#else +extern "C" CDECL rust_str * +rust_dirent_filename(rust_task *task, dirent* ent) { + return c_str_to_rust(task, ent->d_name); +} +#endif + +extern "C" CDECL int +rust_file_is_dir(rust_task *task, rust_str *path) { + struct stat buf; + stat((char*)path->data, &buf); + return S_ISDIR(buf.st_mode); +} + +extern "C" CDECL FILE* rust_get_stdin() {return stdin;} +extern "C" CDECL FILE* rust_get_stdout() {return stdout;} // // Local Variables: