You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.4 KiB
71 lines
2.4 KiB
8 years ago
|
(*
|
||
|
* Copyright (c) 2016 - present Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*)
|
||
|
|
||
8 years ago
|
include Core
|
||
8 years ago
|
|
||
8 years ago
|
module Unix_ = struct
|
||
|
let improve f make_arg_sexps =
|
||
7 years ago
|
try f ()
|
||
|
with Unix.Unix_error (e, s, _) ->
|
||
|
let buf = Buffer.create 100 in
|
||
|
let fmt = Format.formatter_of_buffer buf in
|
||
|
Format.pp_set_margin fmt 10000 ;
|
||
|
Sexp.pp_hum fmt
|
||
|
(Sexp.List
|
||
|
(List.map (make_arg_sexps ()) ~f:(fun (name, value) -> Sexp.List [Sexp.Atom name; value]))) ;
|
||
|
Format.pp_print_flush fmt () ;
|
||
|
let arg_str = Buffer.contents buf in
|
||
|
raise (Unix.Unix_error (e, s, arg_str))
|
||
8 years ago
|
|
||
7 years ago
|
let create_process_redirect ~prog ~args ?(stdin= Unix.stdin) ?(stdout= Unix.stdout)
|
||
|
?(stderr= Unix.stderr) () =
|
||
8 years ago
|
improve
|
||
|
(fun () ->
|
||
7 years ago
|
let prog_args = Array.of_list (prog :: args) in
|
||
|
Caml.UnixLabels.create_process ~prog ~args:prog_args ~stdin ~stdout ~stderr |> Pid.of_int)
|
||
8 years ago
|
(fun () ->
|
||
7 years ago
|
[("prog", Sexp.Atom prog); ("args", Sexplib.Conv.sexp_of_list (fun a -> Sexp.Atom a) args)]
|
||
|
)
|
||
8 years ago
|
|
||
|
let fork_redirect_exec_wait ~prog ~args ?stdin ?stdout ?stderr () =
|
||
|
Unix.waitpid (create_process_redirect ~prog ~args ?stdin ?stdout ?stderr ())
|
||
|
|> Unix.Exit_or_signal.or_error |> ok_exn
|
||
|
end
|
||
|
|
||
8 years ago
|
module List_ = struct
|
||
|
let rec fold_until ~init ~f l =
|
||
7 years ago
|
match (l, init) with
|
||
|
| _, `Stop init' | [], `Continue init'
|
||
|
-> init'
|
||
|
| h :: t, `Continue _
|
||
|
-> fold_until ~init:(f init h) ~f t
|
||
8 years ago
|
end
|
||
|
|
||
8 years ago
|
(* Use Caml.Set since they are serialized using Marshal, and Core.Std.Set includes the comparison
|
||
|
function in its representation, which Marshal cannot (de)serialize. *)
|
||
7 years ago
|
module IntSet = Caml.Set.Make (Int)
|
||
8 years ago
|
|
||
8 years ago
|
(* Compare police: generic compare mostly disabled. *)
|
||
8 years ago
|
let compare = No_polymorphic_compare.compare
|
||
7 years ago
|
|
||
8 years ago
|
let equal = No_polymorphic_compare.equal
|
||
7 years ago
|
|
||
|
let ( = ) = No_polymorphic_compare.( = )
|
||
8 years ago
|
|
||
8 years ago
|
module PVariant = struct
|
||
|
(* Equality for polymorphic variants *)
|
||
7 years ago
|
let ( = ) (v1: [> ]) (v2: [> ]) = Polymorphic_compare.( = ) v1 v2
|
||
8 years ago
|
end
|
||
8 years ago
|
|
||
|
let failwithf fmt =
|
||
7 years ago
|
Format.kfprintf (fun _ -> failwith (Format.flush_str_formatter ())) Format.str_formatter fmt
|
||
8 years ago
|
|
||
|
let invalid_argf fmt =
|
||
7 years ago
|
Format.kfprintf (fun _ -> invalid_arg (Format.flush_str_formatter ())) Format.str_formatter fmt
|