Skip to content

Commit

Permalink
chore: update ocamlformat
Browse files Browse the repository at this point in the history
and switch to janestreet style

Signed-off-by: Rudi Grinberg <[email protected]>

<!-- ps-id: 2f84c3c4-e878-47f5-9177-32373bd8d671 -->
  • Loading branch information
rgrinberg committed Sep 26, 2023
1 parent ae268a3 commit 0c643cf
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 154 deletions.
23 changes: 3 additions & 20 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
version=0.19.0
profile=conventional
break-separators=before
dock-collection-brackets=false
break-sequences=true
doc-comments=before
field-space=loose
let-and=sparse
sequence-style=terminator
type-decl=sparse
wrap-comments=true
if-then-else=k-r
let-and=sparse
space-around-records
space-around-lists
space-around-arrays
cases-exp-indent=2
break-cases=all
indicate-nested-or-patterns=unsafe-no
parse-docstrings=true
version=0.26.0
profile=janestreet
ocaml-version=4.08.0
5 changes: 3 additions & 2 deletions bench/bench.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let%bench "exec true with Spawn.spawn" =
Spawn.spawn () ~prog:"/bin/true" ~argv:[ "true" ] |> Unix.waitpid []
;;

let%bench "exec true with Caml.Unix.create_process" =
Unix.create_process "/bin/true" [| "true" |] Unix.stdin Unix.stdout
Unix.stderr
Unix.create_process "/bin/true" [| "true" |] Unix.stdin Unix.stdout Unix.stderr
|> Unix.waitpid []
;;
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
};
devShells.default = pkgs.mkShell {
inputsFrom = pkgs.lib.attrValues packages;
buildInputs = with pkgs.ocamlPackages; [ ocaml-lsp pkgs.ocamlformat_0_19_0 ];
buildInputs = with pkgs.ocamlPackages; [ ocaml-lsp pkgs.ocamlformat_0_26_0 ];
};
});
}
138 changes: 71 additions & 67 deletions src/spawn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ module Unix_backend = struct
unsuccessful.
In the end we decided not to default to [vfork] on OSX. *)
if is_osx then
Fork
else
Vfork
if is_osx then Fork else Vfork
;;
end

module type Env = sig
Expand All @@ -44,54 +42,50 @@ module Env_win32 : Env = struct
type t = string

let of_list env =
let len =
List.fold_left env ~init:1 ~f:(fun acc s -> acc + String.length s + 1)
in
let len = List.fold_left env ~init:1 ~f:(fun acc s -> acc + String.length s + 1) in
let buf = Buffer.create len in
List.iter env ~f:(fun s ->
Buffer.add_string buf s;
Buffer.add_char buf '\000');
Buffer.add_string buf s;
Buffer.add_char buf '\000');
Buffer.add_char buf '\000';
Buffer.contents buf
;;
end

module Env_unix : Env = struct
type t = string list

let no_null s =
if String.contains s '\000' then
Printf.ksprintf invalid_arg
"Spawn.Env.of_list: NUL bytes are not allowed in the environment but \
found one in %S"
if String.contains s '\000'
then
Printf.ksprintf
invalid_arg
"Spawn.Env.of_list: NUL bytes are not allowed in the environment but found one \
in %S"
s
;;

let of_list l =
List.iter l ~f:no_null;
l
;;
end

module Env : Env = (val if Sys.win32 then
(module Env_win32)
else
(module Env_unix) : Env)
module Env : Env = (val if Sys.win32 then (module Env_win32) else (module Env_unix) : Env)

module Pgid = struct
type t = int

let new_process_group = 0

let of_pid = function
| 0 ->
raise (Invalid_argument "bad pid: 0 (hint: use [Pgid.new_process_group])")
| t ->
if t < 0 then
raise (Invalid_argument ("bad pid: " ^ string_of_int t))
else
t
| 0 -> raise (Invalid_argument "bad pid: 0 (hint: use [Pgid.new_process_group])")
| t -> if t < 0 then raise (Invalid_argument ("bad pid: " ^ string_of_int t)) else t
;;
end

external spawn_unix :
env:Env.t option
external spawn_unix
: env:Env.t option
-> cwd:Working_dir.t
-> prog:string
-> argv:string list
Expand All @@ -101,29 +95,38 @@ external spawn_unix :
-> use_vfork:bool
-> setpgid:int option
-> sigprocmask:(Unix.sigprocmask_command * int list) option
-> int = "spawn_unix_byte" "spawn_unix"
-> int
= "spawn_unix_byte" "spawn_unix"

external spawn_windows :
env:Env.t option
external spawn_windows
: env:Env.t option
-> cwd:string option
-> prog:string
-> cmdline:string
-> stdin:Unix.file_descr
-> stdout:Unix.file_descr
-> stderr:Unix.file_descr
-> int = "spawn_windows_byte" "spawn_windows"
-> int
= "spawn_windows_byte" "spawn_windows"

let maybe_quote f =
if
String.contains f ' ' || String.contains f '\"' || String.contains f '\t'
|| f = ""
then
Filename.quote f
else
f

let spawn_windows ~env ~cwd ~prog ~argv ~stdin ~stdout ~stderr ~use_vfork:_
~setpgid:_ ~sigprocmask:_ =
if String.contains f ' ' || String.contains f '\"' || String.contains f '\t' || f = ""
then Filename.quote f
else f
;;

let spawn_windows
~env
~cwd
~prog
~argv
~stdin
~stdout
~stderr
~use_vfork:_
~setpgid:_
~sigprocmask:_
=
let cwd =
match (cwd : Working_dir.t) with
| Path p -> Some p
Expand All @@ -132,48 +135,49 @@ let spawn_windows ~env ~cwd ~prog ~argv ~stdin ~stdout ~stderr ~use_vfork:_
in
let cmdline = String.concat (List.map argv ~f:maybe_quote) ~sep:" " in
let prog =
match (Filename.is_relative prog, cwd) with
match Filename.is_relative prog, cwd with
| true, Some p -> Filename.concat p prog
| _ -> prog
in
spawn_windows ~env ~cwd ~prog ~cmdline ~stdin ~stdout ~stderr
;;

let no_null s =
if String.contains s '\000' then
Printf.ksprintf invalid_arg
"Spawn.spawn: NUL bytes are not allowed in any of the arguments but \
found one in %S"
if String.contains s '\000'
then
Printf.ksprintf
invalid_arg
"Spawn.spawn: NUL bytes are not allowed in any of the arguments but found one in %S"
s

let spawn ?env ?(cwd = Working_dir.Inherit) ~prog ~argv ?(stdin = Unix.stdin)
?(stdout = Unix.stdout) ?(stderr = Unix.stderr)
?(unix_backend = Unix_backend.default) ?setpgid ?sigprocmask () =
;;

let spawn
?env
?(cwd = Working_dir.Inherit)
~prog
~argv
?(stdin = Unix.stdin)
?(stdout = Unix.stdout)
?(stderr = Unix.stderr)
?(unix_backend = Unix_backend.default)
?setpgid
?sigprocmask
()
=
(match cwd with
| Path s -> no_null s
| Fd _
| Inherit ->
());
| Path s -> no_null s
| Fd _ | Inherit -> ());
no_null prog;
List.iter argv ~f:no_null;
let backend =
if Sys.win32 then
spawn_windows
else
spawn_unix
in
let backend = if Sys.win32 then spawn_windows else spawn_unix in
let use_vfork =
match unix_backend with
| Vfork -> true
| Fork -> false
in
backend ~env ~cwd ~prog ~argv ~stdin ~stdout ~stderr ~use_vfork ~setpgid
~sigprocmask
backend ~env ~cwd ~prog ~argv ~stdin ~stdout ~stderr ~use_vfork ~setpgid ~sigprocmask
;;

external safe_pipe : unit -> Unix.file_descr * Unix.file_descr = "spawn_pipe"

let safe_pipe =
if Sys.win32 then
fun () ->
Unix.pipe ~cloexec:true ()
else
safe_pipe
let safe_pipe = if Sys.win32 then fun () -> Unix.pipe ~cloexec:true () else safe_pipe
8 changes: 4 additions & 4 deletions src/spawn.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

module Working_dir : sig
type t =
| Path of string (** Path in the filesystem *)
| Path of string (** Path in the filesystem *)
| Fd of Unix.file_descr
(** File descriptor pointing to a directory. Not supported on Windows. *)
| Inherit (** Inherit the working directory of the current process *)
| Inherit (** Inherit the working directory of the current process *)
end

module Unix_backend : sig
Expand Down Expand Up @@ -110,8 +110,8 @@ end
[unix_backend] describes what backend to use on Unix. If set to [Default],
[vfork] is used unless the environment variable [SPAWN_USE_FORK] is set. On
Windows, [CreateProcess] is used. *)
val spawn :
?env:Env.t
val spawn
: ?env:Env.t
-> ?cwd:Working_dir.t (* default: [Inherit] *)
-> prog:string
-> argv:string list
Expand Down
4 changes: 2 additions & 2 deletions test/exe/list_files.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let () =
Sys.readdir "." |> Array.to_list |> List.sort String.compare
|> List.iter print_endline
Sys.readdir "." |> Array.to_list |> List.sort String.compare |> List.iter print_endline
;;
1 change: 1 addition & 0 deletions test/exe/print_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ let () =
match Sys.getenv "FOO" with
| exception _ -> print_endline "None"
| str -> Printf.printf "Some %S\n" str
;;
6 changes: 4 additions & 2 deletions test/pgid_test/checkpgid.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
external getpgid : int -> int = "test_getpgid"

let main =
if not Sys.win32 then
if not Sys.win32
then (
let pid = Unix.getpid () in
let pgid = getpgid pid in
if pid <> pgid then failwith "pgid and pid not equal"
if pid <> pgid then failwith "pgid and pid not equal")
;;
Loading

0 comments on commit 0c643cf

Please sign in to comment.