summaryrefslogtreecommitdiff
path: root/toplevel
diff options
context:
space:
mode:
Diffstat (limited to 'toplevel')
-rw-r--r--toplevel/opttoploop.ml13
-rw-r--r--toplevel/opttopmain.ml1
-rw-r--r--toplevel/toploop.ml17
-rw-r--r--toplevel/topmain.ml5
4 files changed, 27 insertions, 9 deletions
diff --git a/toplevel/opttoploop.ml b/toplevel/opttoploop.ml
index a34e231aaf..1fa5a3fd08 100644
--- a/toplevel/opttoploop.ml
+++ b/toplevel/opttoploop.ml
@@ -300,8 +300,15 @@ let use_print_results = ref true
let use_file ppf name =
try
- let filename = find_in_path !Config.load_path name in
- let ic = open_in_bin filename in
+ let (filename, ic, must_close) =
+ if name = "" then
+ ("(stdin)", stdin, false)
+ else begin
+ let filename = find_in_path !Config.load_path name in
+ let ic = open_in_bin filename in
+ (filename, ic, true)
+ end
+ in
let lb = Lexing.from_channel ic in
Location.init lb filename;
(* Skip initial #! line if any *)
@@ -319,7 +326,7 @@ let use_file ppf name =
| Exit -> false
| Sys.Break -> fprintf ppf "Interrupted.@."; false
| x -> Opterrors.report_error ppf x; false) in
- close_in ic;
+ if must_close then close_in ic;
success
with Not_found -> fprintf ppf "Cannot find file %s.@." name; false
diff --git a/toplevel/opttopmain.ml b/toplevel/opttopmain.ml
index dd4a52b473..e13bfca4ed 100644
--- a/toplevel/opttopmain.ml
+++ b/toplevel/opttopmain.ml
@@ -79,6 +79,7 @@ module Options = Main_args.Make_opttop_options (struct
let _rectypes = set recursive_types
let _strict_sequence = set strict_sequence
let _S = set keep_asm_file
+ let _stdin () = file_argument ""
let _unsafe = set fast
let _version () = print_version ()
let _vnum () = print_version_num ()
diff --git a/toplevel/toploop.ml b/toplevel/toploop.ml
index 28894d2557..3d2f72f201 100644
--- a/toplevel/toploop.ml
+++ b/toplevel/toploop.ml
@@ -283,14 +283,21 @@ let protect r newval body =
r := oldval;
raise x
-(* Read and execute commands from a file *)
+(* Read and execute commands from a file, or from stdin if [name] is "". *)
let use_print_results = ref true
let use_file ppf name =
try
- let filename = find_in_path !Config.load_path name in
- let ic = open_in_bin filename in
+ let (filename, ic, must_close) =
+ if name = "" then
+ ("(stdin)", stdin, false)
+ else begin
+ let filename = find_in_path !Config.load_path name in
+ let ic = open_in_bin filename in
+ (filename, ic, true)
+ end
+ in
let lb = Lexing.from_channel ic in
Location.init lb filename;
(* Skip initial #! line if any *)
@@ -308,7 +315,7 @@ let use_file ppf name =
| Exit -> false
| Sys.Break -> fprintf ppf "Interrupted.@."; false
| x -> Errors.report_error ppf x; false) in
- close_in ic;
+ if must_close then close_in ic;
success
with Not_found -> fprintf ppf "Cannot find file %s.@." name; false
@@ -423,7 +430,7 @@ let loop ppf =
| x -> Errors.report_error ppf x; Btype.backtrack snap
done
-(* Execute a script *)
+(* Execute a script. If [name] is "", read the script from stdin. *)
let run_script ppf name args =
let len = Array.length args in
diff --git a/toplevel/topmain.ml b/toplevel/topmain.ml
index a3dc5a458a..d6053e381a 100644
--- a/toplevel/topmain.ml
+++ b/toplevel/topmain.ml
@@ -14,7 +14,8 @@
open Clflags
-let usage = "Usage: ocaml <options> <object-files> [script-file]\noptions are:"
+let usage = "Usage: ocaml <options> <object-files> [script-file [arguments]]\n\
+ options are:"
let preload_objects = ref []
@@ -31,6 +32,7 @@ let prepare ppf =
Format.fprintf ppf "Uncaught exception: %s\n" (Printexc.to_string x);
false
+(* If [name] is "", then the "file" is stdin treated as a script file. *)
let file_argument name =
let ppf = Format.err_formatter in
if Filename.check_suffix name ".cmo" || Filename.check_suffix name ".cma"
@@ -72,6 +74,7 @@ module Options = Main_args.Make_bytetop_options (struct
let _nostdlib = set no_std_include
let _principal = set principal
let _rectypes = set recursive_types
+ let _stdin () = file_argument ""
let _strict_sequence = set strict_sequence
let _unsafe = set fast
let _version () = print_version ()