1 (***********************************************************************)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. All rights reserved. This file is distributed *)
9 (* under the terms of the Q Public License version 1.0. *)
11 (***********************************************************************)
13 (* $Id: opttopdirs.ml 9134 2008-11-19 02:35:40Z garrigue $ *)
15 (* Toplevel directives *)
24 (* The standard output formatter *)
25 let std_out = std_formatter
29 let dir_quit () = exit 0
31 let _ = Hashtbl.add directive_table "quit" (Directive_none dir_quit)
33 (* To add a directory to the load path *)
36 let d = expand_directory Config.standard_library s in
37 Config.load_path := d :: !Config.load_path
39 let _ = Hashtbl.add directive_table "directory" (Directive_string dir_directory)
40 let _ = Hashtbl.add directive_table "show_dirs"
43 List.iter print_endline !Config.load_path
46 (* To change the current directory *)
48 let dir_cd s = Sys.chdir s
50 let _ = Hashtbl.add directive_table "cd" (Directive_string dir_cd)
52 (* Load in-core a .cmxs file *)
54 let load_file ppf name0 =
56 try Some (find_in_path !Config.load_path name0)
57 with Not_found -> None in
59 | None -> fprintf ppf "File not found: %s@." name0; false
62 if Filename.check_suffix name ".cmx" || Filename.check_suffix name ".cmxa"
64 let cmxs = Filename.temp_file "caml" ".cmxs" in
65 Asmlink.link_shared ppf [name] cmxs;
71 (* The Dynlink interface does not allow us to distinguish between
72 a Dynlink.Error exceptions raised in the loaded modules
73 or a genuine error during dynlink... *)
74 try Dynlink.loadfile fn; true
76 | Dynlink.Error err ->
77 fprintf ppf "Error while loading %s: %s.@."
78 name (Dynlink.error_message err);
81 print_exception_outcome ppf exn;
84 if tmp then (try Sys.remove fn with Sys_error _ -> ());
88 let dir_load ppf name = ignore (load_file ppf name)
90 let _ = Hashtbl.add directive_table "load" (Directive_string (dir_load std_out))
92 (* Load commands from a file *)
94 let dir_use ppf name = ignore(Opttoploop.use_file ppf name)
96 let _ = Hashtbl.add directive_table "use" (Directive_string (dir_use std_out))
98 (* Install, remove a printer *)
100 type 'a printer_type_new = Format.formatter -> 'a -> unit
101 type 'a printer_type_old = 'a -> unit
103 let match_printer_type ppf desc typename =
104 let (printer_type, _) =
106 Env.lookup_type (Ldot(Lident "Topdirs", typename)) !toplevel_env
108 fprintf ppf "Cannot find type Topdirs.%s.@." typename;
110 Ctype.init_def(Ident.current_time());
112 let ty_arg = Ctype.newvar() in
113 Ctype.unify !toplevel_env
114 (Ctype.newconstr printer_type [ty_arg])
115 (Ctype.instance desc.val_type);
117 Ctype.generalize ty_arg;
120 let find_printer_type ppf lid =
122 let (path, desc) = Env.lookup_value lid !toplevel_env in
123 let (ty_arg, is_old_style) =
125 (match_printer_type ppf desc "printer_type_new", false)
126 with Ctype.Unify _ ->
127 (match_printer_type ppf desc "printer_type_old", true) in
128 (ty_arg, path, is_old_style)
131 fprintf ppf "Unbound value %a.@." Printtyp.longident lid;
134 fprintf ppf "%a has a wrong type for a printing function.@."
135 Printtyp.longident lid;
138 let dir_install_printer ppf lid =
140 let (ty_arg, path, is_old_style) = find_printer_type ppf lid in
141 let v = eval_path path in
144 (fun formatter repr -> Obj.obj v (Obj.obj repr))
146 (fun formatter repr -> Obj.obj v formatter (Obj.obj repr)) in
147 install_printer path ty_arg print_function
150 let dir_remove_printer ppf lid =
152 let (ty_arg, path, is_old_style) = find_printer_type ppf lid in
156 fprintf ppf "No printer named %a.@." Printtyp.longident lid
160 let _ = Hashtbl.add directive_table "install_printer"
161 (Directive_ident (dir_install_printer std_out))
162 let _ = Hashtbl.add directive_table "remove_printer"
163 (Directive_ident (dir_remove_printer std_out))
165 let parse_warnings ppf iserr s =
166 try Warnings.parse_options iserr s
167 with Arg.Bad err -> fprintf ppf "%s.@." err
170 (* Control the printing of values *)
172 Hashtbl.add directive_table "print_depth"
173 (Directive_int(fun n -> max_printer_depth := n));
174 Hashtbl.add directive_table "print_length"
175 (Directive_int(fun n -> max_printer_steps := n));
177 (* Set various compiler flags *)
179 Hashtbl.add directive_table "labels"
180 (Directive_bool(fun b -> Clflags.classic := not b));
182 Hashtbl.add directive_table "principal"
183 (Directive_bool(fun b -> Clflags.principal := b));
185 Hashtbl.add directive_table "rectypes"
186 (Directive_none(fun () -> Clflags.recursive_types := true));
188 Hashtbl.add directive_table "warnings"
189 (Directive_string (parse_warnings std_out false));
191 Hashtbl.add directive_table "warn_error"
192 (Directive_string (parse_warnings std_out true))