summaryrefslogtreecommitdiff
path: root/toplevel/toploop.ml
diff options
context:
space:
mode:
Diffstat (limited to 'toplevel/toploop.ml')
-rw-r--r--toplevel/toploop.ml17
1 files changed, 12 insertions, 5 deletions
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