Fix error message
[llpp.git] / utils.ml
blob76094e4ec5129d905e9d97fa238918d4ecdb35f0
1 module E = struct
2 let s = "";;
3 let b = Bytes.empty;;
4 let a = [||];;
5 end;;
7 type platform = | Punknown | Plinux | Posx | Psun | Pbsd | Pcygwin;;
9 let tempfailureretry f a =
10 let rec g () =
11 try f a with Unix.Unix_error (Unix.EINTR, _, _) -> g ()
12 in g ()
15 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
16 external hasdata : Unix.file_descr -> bool = "ml_hasdata";;
17 external toutf8 : int -> string = "ml_keysymtoutf8";;
18 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
19 external spawn : string -> (Unix.file_descr * int) list -> int = "ml_spawn";;
20 external platform : unit -> (platform * string array) = "ml_platform";;
22 let now = Unix.gettimeofday;;
23 let platform, uname = platform ();;
24 let dolog fmt = Format.ksprintf prerr_endline fmt;;
26 let exntos = function
27 | Unix.Unix_error (e, s, a) -> Printf.sprintf "%s(%s) : %s (%d)"
28 s a (Unix.error_message e) (Obj.magic e)
29 | exn -> Printexc.to_string exn
32 let error fmt = Printf.kprintf failwith fmt;;
34 module IntSet = Set.Make (struct type t = int let compare = (-) end);;
36 let emptystr s = String.length s = 0;;
37 let nonemptystr s = String.length s > 0;;
38 let bound v minv maxv = max minv (min maxv v);;
40 let spawn cmd fda =
41 if platform = Pcygwin
42 then failwith "spawn not implemented under cygwin yet"
43 else spawn cmd fda;
46 module Opaque :
47 sig
48 type t = private string
49 val of_string : string -> t
50 val to_string : t -> string
51 end
53 struct
54 type t = string
55 let of_string s = s
56 let to_string t = t
57 end
60 let (~<) = Opaque.of_string;;
61 let (~>) = Opaque.to_string;;
63 let int_of_string_with_suffix s =
64 let l = String.length s in
65 let s1, shift =
66 if l > 1
67 then
68 let suffix = Char.lowercase s.[l-1] in
69 match suffix with
70 | 'k' -> String.sub s 0 (l-1), 10
71 | 'm' -> String.sub s 0 (l-1), 20
72 | 'g' -> String.sub s 0 (l-1), 30
73 | _ -> s, 0
74 else s, 0
76 let n = int_of_string s1 in
77 let m = n lsl shift in
78 if m < 0 || m < n
79 then error "value too large"
80 else m
83 let string_with_suffix_of_int n =
84 if n = 0
85 then "0"
86 else
87 let units = [(30, "G"); (20, "M"); (10, "K")] in
88 let prettyint n =
89 let rec loop s n =
90 let h = n mod 1000 in
91 let n = n / 1000 in
92 if n = 0
93 then string_of_int h ^ s
94 else (
95 let s = Printf.sprintf "_%03d%s" h s in
96 loop s n
99 loop E.s n
101 let rec find = function
102 | [] -> prettyint n
103 | (shift, suffix) :: rest ->
104 if (n land ((1 lsl shift) - 1)) = 0
105 then prettyint (n lsr shift) ^ suffix
106 else find rest
108 find units
111 let color_of_string s =
112 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
113 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
117 let color_to_string (r, g, b) =
118 let r = truncate (r *. 256.0)
119 and g = truncate (g *. 256.0)
120 and b = truncate (b *. 256.0) in
121 Printf.sprintf "%d/%d/%d" r g b
124 let abspath path =
125 if Filename.is_relative path
126 then
127 let cwd = Sys.getcwd () in
128 if Filename.is_implicit path
129 then Filename.concat cwd path
130 else Filename.concat cwd (Filename.basename path)
131 else
132 path
135 let nindex s c =
136 try String.index s c
137 with Not_found -> -1
140 module Ne = struct
141 let clo fd f =
142 try tempfailureretry Unix.close fd
143 with exn -> f @@ exntos exn
145 end;;
147 let getenvwithdef name def =
148 match Sys.getenv name with
149 | env -> env
150 | exception Not_found -> def
153 let newlinere = Str.regexp "[\r\n]";;
154 let percentsre = Str.regexp "%s";;
155 let whitere = Str.regexp "[ \t]";;
157 let unit () = ();;
159 let addchar s c =
160 let b = Buffer.create (String.length s + 1) in
161 Buffer.add_string b s;
162 Buffer.add_char b c;
163 Buffer.contents b;
166 let btod b = if b then 1 else 0;;
168 let splitatspace s = let open String in
169 match index s ' ' with
170 | pos -> sub s 0 pos :: sub s (pos+1) (length s - pos - 1) :: []
171 | exception Not_found -> [s]
174 let boundastep h step =
175 if step < 0
176 then bound step ~-h 0
177 else bound step 0 h
180 let withoutlastutf8 s =
181 let len = String.length s in
182 if len = 0
183 then s
184 else
185 let rec find pos =
186 if pos = 0
187 then pos
188 else
189 let b = Char.code s.[pos] in
190 if b land 0b11000000 = 0b11000000
191 then pos
192 else find (pos-1)
194 let first =
195 if Char.code s.[len-1] land 0x80 = 0
196 then len-1
197 else find (len-1)
199 String.sub s 0 first;
202 let fdcontents fd =
203 let l = 4096 in
204 let b = Buffer.create l in
205 let s = Bytes.create l in
206 let rec loop () =
207 let n = tempfailureretry (Unix.read fd s 0) l in
208 if n = 0
209 then Buffer.contents b
210 else (
211 Buffer.add_subbytes b s 0 n;
212 loop ()
215 loop ()
218 let filecontents path =
219 let fd = Unix.openfile path [Unix.O_RDONLY] 0o0 in
220 match fdcontents fd with
221 | (exception exn) ->
222 error "failed to read contents of %s: %s" path @@ exntos exn
223 | s ->
224 Ne.clo fd @@ error "failed to close descriptor for %s: %s" path;
228 let getcmdoutput errfun cmd =
229 let reperror fmt = Printf.kprintf errfun fmt in
230 let clofail s e = error "failed to close %s: %s" s e in
231 match Unix.pipe () with
232 | (exception exn) ->
233 reperror "pipe failed: %s" @@ exntos exn;
235 | (r, w) ->
236 match spawn cmd [r, -1; w, 1] with
237 | (exception exn) ->
238 reperror "failed to execute %S: %s" cmd @@ exntos exn;
240 | pid ->
241 Ne.clo w @@ clofail "write end of the pipe";
242 let s =
243 match Unix.waitpid [] pid with
244 | (exception exn) ->
245 reperror "waitpid on %S %d failed: %s" cmd pid @@ exntos exn;
247 | _pid, Unix.WEXITED 0 ->
248 begin
249 match fdcontents r with
250 | (exception exn) ->
251 reperror "failed to read output of %S: %s" cmd @@ exntos exn;
253 | s ->
254 let l = String.length s in
255 if l > 0 && s.[l-1] = '\n'
256 then String.sub s 0 (l-1)
257 else s
258 end;
259 | _pid, Unix.WEXITED n ->
260 reperror "%S exited with error code %d" cmd n;
262 | _pid, Unix.WSIGNALED n ->
263 reperror "%S was killed with signal %d" cmd n;
265 | _pid, Unix.WSTOPPED n ->
266 reperror "%S was stopped by signal %d" cmd n;
269 Ne.clo r @@ clofail "read end of the pipe";
273 let geturl =
274 let re = Str.regexp {|.*\(\(https?\|ftp\|mailto\|file\)://[^ ]+\).*|} in
275 fun s ->
276 if Str.string_match re s 0
277 then Str.matched_group 1 s
278 else E.s