diff --git a/src/libcore/os.rs b/src/libcore/os.rs index d37bdabd48a9..68571da3a1e9 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -32,6 +32,8 @@ use task::TaskBuilder; // FIXME: move these to str perhaps? #2620 extern mod rustrt { + fn rust_get_argc() -> c_int; + fn rust_get_argv() -> **c_char; fn rust_getcwd() -> ~str; fn rust_path_is_dir(path: *libc::c_char) -> c_int; fn rust_path_exists(path: *libc::c_char) -> c_int; @@ -732,6 +734,14 @@ pub fn set_exit_status(code: int) { rustrt::rust_set_exit_status(code as libc::intptr_t); } +unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] { + let mut args = ~[]; + for uint::range(0, argc as uint) |i| { + vec::push(&mut args, str::raw::from_c_str(*argv.offset(i))); + } + return args; +} + /** * Returns the command line arguments * @@ -740,23 +750,20 @@ pub fn set_exit_status(code: int) { #[cfg(target_os = "macos")] fn real_args() -> ~[~str] { unsafe { - let (argc, argv) = (*_NSGetArgc() as uint, *_NSGetArgv()); - let mut args = ~[]; - for uint::range(0, argc) |i| { - vec::push(&mut args, str::raw::from_c_str(*argv.offset(i))); - } - return args; + let (argc, argv) = (*_NSGetArgc() as c_int, + *_NSGetArgv() as **c_char); + load_argc_and_argv(argc, argv) } } #[cfg(target_os = "linux")] -fn real_args() -> ~[~str] { - fail // Needs implementing. -} - #[cfg(target_os = "freebsd")] fn real_args() -> ~[~str] { - fail // Needs implementing. + unsafe { + let argc = rustrt::rust_get_argc(); + let argv = rustrt::rust_get_argv(); + load_argc_and_argv(argc, argv) + } } #[cfg(windows)] @@ -775,8 +782,9 @@ fn real_args() -> ~[~str] { while *ptr.offset(len) != 0 { len += 1; } // Push it onto the list. - vec::push(&mut args, vec::raw::form_slice(ptr, len, - str::from_utf16)); + vec::push(&mut args, + vec::raw::buf_as_slice(ptr, len, + str::from_utf16)); } } diff --git a/src/rt/rust.cpp b/src/rt/rust.cpp index dc28f6244155..11e65347f11b 100644 --- a/src/rt/rust.cpp +++ b/src/rt/rust.cpp @@ -86,7 +86,7 @@ rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) { // Load runtime configuration options from the environment. // FIXME #1497: Should provide a way to get these from the command // line as well. - rust_env *env = load_env(); + rust_env *env = load_env(argc, argv); global_crate_map = crate_map; diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index 6f985601f8bd..5baa95c7323f 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -588,6 +588,18 @@ rust_num_threads() { return task->kernel->env->num_sched_threads; } +extern "C" CDECL int +rust_get_argc() { + rust_task *task = rust_get_current_task(); + return task->kernel->env->argc; +} + +extern "C" CDECL char** +rust_get_argv() { + rust_task *task = rust_get_current_task(); + return task->kernel->env->argv; +} + extern "C" CDECL rust_sched_id rust_new_sched(uintptr_t threads) { rust_task *task = rust_get_current_task(); diff --git a/src/rt/rust_env.cpp b/src/rt/rust_env.cpp index a54dc27c71f4..4e653c8f9e63 100644 --- a/src/rt/rust_env.cpp +++ b/src/rt/rust_env.cpp @@ -107,7 +107,7 @@ copyenv(const char* name) { } rust_env* -load_env() { +load_env(int argc, char **argv) { rust_env *env = (rust_env*)malloc(sizeof(rust_env)); env->num_sched_threads = (size_t)get_num_threads(); @@ -118,7 +118,8 @@ load_env() { env->detailed_leaks = getenv(DETAILED_LEAKS) != NULL; env->rust_seed = copyenv(RUST_SEED); env->poison_on_free = getenv(RUST_POISON_ON_FREE) != NULL; - + env->argc = argc; + env->argv = argv; return env; } diff --git a/src/rt/rust_env.h b/src/rt/rust_env.h index 007ac9b1e0b1..0e3af9eae60c 100644 --- a/src/rt/rust_env.h +++ b/src/rt/rust_env.h @@ -13,9 +13,11 @@ struct rust_env { bool detailed_leaks; char* rust_seed; bool poison_on_free; + int argc; + char **argv; }; -rust_env* load_env(); +rust_env* load_env(int argc, char **argv); void free_env(rust_env *rust_env); #endif diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index 551378a3d6cb..7412f06d8cd8 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -31,6 +31,8 @@ rand_new_seeded2 rand_next rand_seed rust_get_sched_id +rust_get_argc +rust_get_argv rust_new_sched rust_new_task_in_sched rust_num_threads