diff options
author | Jeremie Dimino <jdimino@janestreet.com> | 2016-02-24 17:23:21 +0000 |
---|---|---|
committer | Jeremie Dimino <jdimino@janestreet.com> | 2016-03-01 10:36:24 +0000 |
commit | 86daba0e9093cdff18e75555e05f5fbe44ea7722 (patch) | |
tree | a9322f580dd81a589a406634d305ee557a0c48f6 | |
parent | b98461c3f35a3d046940d06054eef874769ee8d7 (diff) | |
download | ocaml-86daba0e9093cdff18e75555e05f5fbe44ea7722.tar.gz |
Add Toploop.override_sys_argv
-rw-r--r-- | toplevel/opttoploop.ml | 7 | ||||
-rw-r--r-- | toplevel/opttoploop.mli | 10 | ||||
-rw-r--r-- | toplevel/toploop.ml | 9 | ||||
-rw-r--r-- | toplevel/toploop.mli | 10 |
4 files changed, 33 insertions, 3 deletions
diff --git a/toplevel/opttoploop.ml b/toplevel/opttoploop.ml index 86449bf681..6d6b5beb0d 100644 --- a/toplevel/opttoploop.ml +++ b/toplevel/opttoploop.ml @@ -565,6 +565,13 @@ let loop ppf = (* Execute a script. If [name] is "", read the script from stdin. *) +let override_sys_argv args = + let len = Array.length args in + if Array.length Sys.argv < len then invalid_arg "Toploop.override_sys_argv"; + Array.blit args 0 Sys.argv 0 len; + Obj.truncate (Obj.repr Sys.argv) len; + Arg.current := 0 + let run_script ppf name args = let len = Array.length args in if Array.length Sys.argv < len then invalid_arg "Toploop.run_script"; diff --git a/toplevel/opttoploop.mli b/toplevel/opttoploop.mli index 3ce4d19108..f234b4f4cf 100644 --- a/toplevel/opttoploop.mli +++ b/toplevel/opttoploop.mli @@ -120,3 +120,13 @@ val read_interactive_input : (string -> bytes -> int -> int * bool) ref (* Hooks for initialization *) val toplevel_startup_hook : (unit -> unit) ref + +(* Misc *) + +val override_sys_argv : string array -> unit +(* [override_sys_argv args] replaces the contents of [Sys.argv] by [args] + and reset [Arg.current] to [0]. + + This is called by [run_script] so that [Sys.argv] represents + "script.ml args..." instead of the full command line: + "ocamlrun unix.cma ... script.ml args...". *) diff --git a/toplevel/toploop.ml b/toplevel/toploop.ml index a72c9ce63b..445eddd47e 100644 --- a/toplevel/toploop.ml +++ b/toplevel/toploop.ml @@ -539,12 +539,15 @@ let loop ppf = (* Execute a script. If [name] is "", read the script from stdin. *) -let run_script ppf name args = +let override_sys_argv args = let len = Array.length args in - if Array.length Sys.argv < len then invalid_arg "Toploop.run_script"; + if Array.length Sys.argv < len then invalid_arg "Toploop.override_sys_argv"; Array.blit args 0 Sys.argv 0 len; Obj.truncate (Obj.repr Sys.argv) len; - Arg.current := 0; + Arg.current := 0 + +let run_script ppf name args = + override_sys_argv args; Compmisc.init_path ~dir:(Filename.dirname name) true; (* Note: would use [Filename.abspath] here, if we had it. *) begin diff --git a/toplevel/toploop.mli b/toplevel/toploop.mli index ba2f0c6d9b..7a478b3c34 100644 --- a/toplevel/toploop.mli +++ b/toplevel/toploop.mli @@ -145,3 +145,13 @@ val toplevel_startup_hook : (unit -> unit) ref (* Used by Trace module *) val may_trace : bool ref + +(* Misc *) + +val override_sys_argv : string array -> unit +(* [override_sys_argv args] replaces the contents of [Sys.argv] by [args] + and reset [Arg.current] to [0]. + + This is called by [run_script] so that [Sys.argv] represents + "script.ml args..." instead of the full command line: + "ocamlrun unix.cma ... script.ml args...". *) |