Debian Codegraph @

vendor/opam/src/core/opamCompat.ml

raw
1 (**************************************************************************)
2 (* *)
3 (* Copyright 2018-2020 OCamlPro *)
4 (* *)
5 (* All rights reserved. This file is distributed under the terms of the *)
6 (* GNU Lesser General Public License version 2.1, with the special *)
7 (* exception on linking described in the file LICENSE. *)
8 (* *)
9 (**************************************************************************)
10
11 module String = struct
12 [@@@warning "-32"]
13
14 (** NOTE: OCaml >= 4.13 *)
15 let exists p s =
16 let n = String.length s in
17 let rec loop i =
18 if i = n then false
19 else if p (String.unsafe_get s i) then true
20 else loop (succ i) in
21 loop 0
22
23 include Stdlib.String
24 end
25
26 module Either = struct
27 (** NOTE: OCaml >= 4.12 *)
28 type ('a, 'b) t =
29 | Left of 'a
30 | Right of 'b
31 end
32
33 module Unix = struct
34 [@@@warning "-32"]
35
36 (** NOTE: OCaml >= 4.13 *)
37 let realpath s =
38 let getchdir s =
39 let p =
40 try Sys.getcwd ()
41 with Sys_error _ -> Filename.get_temp_dir_name ()
42 in
43 Unix.chdir s;
44 p
45 in
46 try getchdir (getchdir s) with Unix.Unix_error _ -> s
47
48 include Unix
49 end
50
51 module Lazy = struct
52 [@@@warning "-32"]
53
54 (** NOTE: OCaml >= 4.13 *)
55 let map f x =
56 lazy (f (Lazy.force x))
57
58 include Stdlib.Lazy
59 end
60
61 module Filename = struct
62 [@@@warning "-32"]
63
64 let quote s =
65 let l = String.length s in
66 let b = Buffer.create (l + 20) in
67 Buffer.add_char b '\"';
68 let rec loop i =
69 if i = l then Buffer.add_char b '\"' else
70 match s.[i] with
71 | '\"' -> loop_bs 0 i;
72 | '\\' -> loop_bs 0 i;
73 | c -> Buffer.add_char b c; loop (i+1);
74 and loop_bs n i =
75 if i = l then begin
76 Buffer.add_char b '\"';
77 add_bs n;
78 end else begin
79 match s.[i] with
80 | '\"' -> add_bs (2*n+1); Buffer.add_char b '\"'; loop (i+1);
81 | '\\' -> loop_bs (n+1) (i+1);
82 | _ -> add_bs n; loop i
83 end
84 and add_bs n = for _j = 1 to n do Buffer.add_char b '\\'; done
85 in
86 loop 0;
87 Buffer.contents b
88
89 (*
90 Quoting commands for execution by cmd.exe is difficult.
91 1- Each argument is first quoted using the "quote" function above, to
92 protect it against the processing performed by the C runtime system,
93 then cmd.exe's special characters are escaped with '^', using
94 the "quote_cmd" function below. For more details, see
95 https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23
96 2- The command and the redirection files, if any, must be double-quoted
97 in case they contain spaces. This quoting is interpreted by cmd.exe,
98 not by the C runtime system, hence the "quote" function above
99 cannot be used. The two characters we don't know how to quote
100 inside a double-quoted cmd.exe string are double-quote and percent.
101 We just fail if the command name or the redirection file names
102 contain a double quote (not allowed in Windows file names, anyway)
103 or a percent. See function "quote_cmd_filename" below.
104 3- The whole string passed to Sys.command is then enclosed in double
105 quotes, which are immediately stripped by cmd.exe. Otherwise,
106 some of the double quotes from step 2 above can be misparsed.
107 See e.g. https://stackoverflow.com/a/9965141
108 *)
109 let quote_cmd s =
110 let b = Buffer.create (String.length s + 20) in
111 String.iter
112 (fun c ->
113 match c with
114 | '(' | ')' | '!' | '^' | '%' | '\"' | '<' | '>' | '&' | '|' ->
115 Buffer.add_char b '^'; Buffer.add_char b c
116 | _ ->
117 Buffer.add_char b c)
118 s;
119 Buffer.contents b
120
121 let quote_cmd_filename f =
122 if String.contains f '\"' || String.contains f '%' then
123 failwith ("Filename.quote_command: bad file name " ^ f)
124 else if String.contains f ' ' then
125 "\"" ^ f ^ "\""
126 else
127 f
128 (* Redirections in cmd.exe: see https://ss64.com/nt/syntax-redirection.html
129 and https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490982(v=technet.10)
130 *)
131
132 (** NOTE: OCaml >= 4.10 *)
133 let quote_command cmd ?stdin ?stdout ?stderr args =
134 String.concat "" [
135 "\"";
136 quote_cmd_filename cmd;
137 " ";
138 quote_cmd (String.concat " " (List.map quote args));
139 (match stdin with None -> "" | Some f -> " <" ^ quote_cmd_filename f);
140 (match stdout with None -> "" | Some f -> " >" ^ quote_cmd_filename f);
141 (match stderr with None -> "" | Some f ->
142 if stderr = stdout
143 then " 2>&1"
144 else " 2>" ^ quote_cmd_filename f);
145 "\""
146 ]
147
148 include Stdlib.Filename
149 end