/[svn]/runtime/system.ml
ViewVC logotype

Contents of /runtime/system.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1609 - (show annotations)
Tue Jul 10 19:07:06 2007 UTC (5 years, 10 months ago) by abate
File size: 2429 byte(s)
[r2005-03-29 15:15:50 by afrisch] Pb with ocamlopt -pack

Original author: afrisch
Date: 2005-03-29 15:15:50+00:00
1 open Operators
2 open Builtin_defs
3 open Ident
4
5 let variant_type_ascii l =
6 List.fold_left
7 (fun accu (l,t) ->
8 Types.cup accu
9 (Types.times
10 (Types.cons (Types.atom (Atoms.atom (Atoms.V.mk_ascii l))))
11 (Types.cons t)))
12 Types.empty
13 l
14
15 let record_type_ascii l =
16 Types.record_fields (false,
17 (LabelMap.from_list_disj
18 (List.map (fun (l,t) -> Value.label_ascii l, Types.cons t) l)))
19
20 module Reader = struct
21 let b = Buffer.create 10240
22 let buf = String.create 1024
23
24 let rec read_loop ic =
25 let i = input ic buf 0 (String.length buf) in
26 if i > 0 then (Buffer.add_string b (String.sub buf 0 i); read_loop ic)
27
28 let ic ic =
29 read_loop ic;
30 let s = Buffer.contents b in
31 Buffer.clear b;
32 s
33 end
34
35 let run_process cmd =
36 let (sout,sin,serr) as h = Unix.open_process_full cmd (Unix.environment()) in
37 close_out sin;
38 let sout = Reader.ic sout in
39 let serr = Reader.ic serr in
40 sout,serr, Unix.close_process_full h
41
42 let process_status = function
43 | Unix.WEXITED n ->
44 Value.Pair (Value.atom_ascii "exited", Value.ocaml2cduce_int n)
45 | Unix.WSTOPPED n ->
46 Value.Pair (Value.atom_ascii "stopped", Value.ocaml2cduce_int n)
47 | Unix.WSIGNALED n ->
48 Value.Pair (Value.atom_ascii "signaled", Value.ocaml2cduce_int n)
49
50
51 let system_out =
52 record_type_ascii [
53 "stdout", string_latin1;
54 "stderr", string_latin1;
55 "status", variant_type_ascii [
56 "exited", int;
57 "stopped", int;
58 "signaled", int
59 ]
60 ]
61
62
63 let () = register_fun "system" string_latin1 system_out
64 (fun v ->
65 Location.protect_op "system";
66 let cmd = Value.get_string_latin1 v in
67 let sout,serr,ps = run_process cmd in
68 Value.record_ascii [
69 "stdout", Value.string_latin1 sout;
70 "stderr", Value.string_latin1 serr;
71 "status", process_status ps
72 ]
73 )
74
75 let () = register_fun "exit" byte_int Types.empty
76 (fun v -> Location.protect_op "exit"; exit (Value.cduce2ocaml_int v))
77
78 let exn_not_found =
79 Value.CDuceExn (Value.Atom (Atoms.V.mk_ascii "Not_found"))
80
81 let () = register_fun "getenv" string_latin1 string_latin1
82 (fun e ->
83 Location.protect_op "getenv";
84 let var = Value.get_string_latin1 e in
85 try Value.string_latin1 (Sys.getenv var)
86 with Not_found -> raise exn_not_found);;
87
88 let () = register_fun "argv" nil (Sequence.star string_latin1)
89 (fun e ->
90 Location.protect_op "argv";
91 !Builtin.argv);;

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5