-l is no more
[llpp.git] / utils.ml
blob159ffc035a8ec79e2cb490bdb33c9f1999bf1622
1 exception Quit;;
3 external measurestr : int -> string -> float = "ml_measure_string";;
5 module E = struct
6 let s = "";;
7 let b = Bytes.empty;;
8 let a = [||];;
9 end;;
11 type platform = | Punknown | Plinux | Pmacos | Pbsd;;
13 let asciilower = let auld = Char.code 'A' - Char.code 'a' in
14 function
15 | ('A'..'Z') as c -> Char.code c - auld |> Char.chr
16 | c -> c
19 let tempfailureretry f a =
20 let rec g () =
21 try f a with Unix.Unix_error (Unix.EINTR, _, _) -> g ()
22 in g ()
25 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
26 external hasdata : Unix.file_descr -> bool = "ml_hasdata";;
27 external toutf8 : int -> string = "ml_keysymtoutf8";;
28 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
29 external spawn : string -> (Unix.file_descr * int) list -> int = "ml_spawn";;
30 external platform : unit -> (platform * string array) = "ml_platform";;
32 let now = Unix.gettimeofday;;
33 let platform, uname = platform ();;
34 let dolog fmt = Format.ksprintf prerr_endline fmt;;
36 let exntos = function
37 | Unix.Unix_error (e, s, a) ->
38 Printf.sprintf "%s(%s) : %s (%d)"
39 s a (Unix.error_message e) (Obj.magic e)
40 | exn -> Printexc.to_string exn
43 let error fmt = Printf.kprintf (fun s -> failwith s) fmt;;
45 module IntSet = Set.Make (struct type t = int let compare = (-) end);;
47 let emptystr s = String.length s = 0;;
48 let nonemptystr s = String.length s > 0;;
49 let bound v minv maxv = max minv (min maxv v);;
51 module Opaque : sig
52 type t = private string
53 val of_string : string -> t
54 val to_string : t -> string
55 end = struct
56 type t = string
57 let of_string s = s
58 let to_string t = t
59 end
62 let (~<) = Opaque.of_string;;
63 let (~>) = Opaque.to_string;;
65 let int_of_string_with_suffix s =
66 let l = String.length s in
67 let s1, shift =
68 if l > 1
69 then
70 let p = l-1 in
71 match s.[p] with
72 | 'k' | 'K' -> String.sub s 0 p, 10
73 | 'm' | 'M' -> String.sub s 0 p, 20
74 | 'g' | 'G' -> String.sub s 0 p, 30
75 | _ -> s, 0
76 else s, 0
78 let n = int_of_string s1 in
79 let m = n lsl shift in
80 if m < 0 || m < n
81 then error "value too large"
82 else m
85 let string_with_suffix_of_int n =
86 if n = 0
87 then "0"
88 else
89 let units = [(30, "G"); (20, "M"); (10, "K")] in
90 let prettyint n =
91 let rec loop s n =
92 let h = n mod 1000 in
93 let n = n / 1000 in
94 if n = 0
95 then string_of_int h ^ s
96 else (
97 let s = Printf.sprintf "_%03d%s" h s in
98 loop s n
101 loop E.s n
103 let rec find = function
104 | [] -> prettyint n
105 | (shift, suffix) :: rest ->
106 if (n land ((1 lsl shift) - 1)) = 0
107 then prettyint (n lsr shift) ^ suffix
108 else find rest
110 find units
113 let color_of_string s =
114 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
115 (float r /. 255.0, float g /. 255.0, float b /. 255.0)
119 let rgba_of_string s =
120 Scanf.sscanf
121 s "%d/%d/%d/%d" (fun r g b a ->
122 (float r /. 255.0, float g /. 255.0, float b /. 255.0, float a /. 255.0)
126 let color_to_string (r, g, b) =
127 let r = truncate (r *. 255.0)
128 and g = truncate (g *. 255.0)
129 and b = truncate (b *. 255.0) in
130 Printf.sprintf "%d/%d/%d" r g b
133 let rgba_to_string (r, g, b, a) =
134 let r = truncate (r *. 255.0)
135 and g = truncate (g *. 255.0)
136 and b = truncate (b *. 255.0)
137 and a = truncate (a *. 255.0) in
138 Printf.sprintf "%d/%d/%d/%d" r g b a
141 let abspath path =
142 if Filename.is_relative path
143 then
144 let cwd = Sys.getcwd () in
145 if Filename.is_implicit path
146 then Filename.concat cwd path
147 else Filename.concat cwd (Filename.basename path)
148 else
149 path
152 module Ne = struct
153 let index s c = try String.index s c with Not_found -> -1;;
154 let clo fd f =
155 try tempfailureretry Unix.close fd
156 with exn -> f @@ exntos exn
158 end;;
160 let getenvwithdef name def =
161 match Sys.getenv name with
162 | env -> env
163 | exception Not_found -> def
166 module Re = struct
167 let crlf = Str.regexp "[\r\n]";;
168 let percent = Str.regexp "%s";;
169 let whitespace = Str.regexp "[ \t]";;
170 end;;
172 let unit () = ();;
174 let addchar s c =
175 let b = Buffer.create (String.length s + 1) in
176 Buffer.add_string b s;
177 Buffer.add_char b c;
178 Buffer.contents b;
181 let btod b = if b then 1 else 0;;
183 let splitatchar s c = let open String in
184 match index s c with
185 | pos -> sub s 0 pos, sub s (pos+1) (length s - pos - 1)
186 | exception Not_found -> s, E.s
189 let boundastep h step =
190 if step < 0
191 then bound step ~-h 0
192 else bound step 0 h
195 let withoutlastutf8 s =
196 let len = String.length s in
197 if len = 0
198 then s
199 else
200 let rec find pos =
201 if pos = 0
202 then pos
203 else
204 let b = Char.code s.[pos] in
205 if b land 0b11000000 = 0b11000000
206 then pos
207 else find (pos-1)
209 let first =
210 if Char.code s.[len-1] land 0x80 = 0
211 then len-1
212 else find (len-1)
214 String.sub s 0 first;
217 let fdcontents fd =
218 let l = 4096 in
219 let b = Buffer.create l in
220 let s = Bytes.create l in
221 let rec loop () =
222 let n = tempfailureretry (Unix.read fd s 0) l in
223 if n = 0
224 then Buffer.contents b
225 else (
226 Buffer.add_subbytes b s 0 n;
227 loop ()
230 loop ()
233 let filecontents path =
234 let fd = Unix.openfile path [Unix.O_RDONLY] 0o0 in
235 match fdcontents fd with
236 | exception exn ->
237 error "failed to read contents of %s: %s" path @@ exntos exn
238 | s ->
239 Ne.clo fd @@ error "failed to close descriptor for %s: %s" path;
243 let getcmdoutput errfun cmd =
244 let reperror fmt = Printf.kprintf errfun fmt in
245 let clofail s e = error "failed to close %s: %s" s e in
246 match Unix.pipe () with
247 | exception exn ->
248 reperror "pipe failed: %s" @@ exntos exn;
250 | (r, w) ->
251 match spawn cmd [r, -1; w, 1] with
252 | exception exn ->
253 reperror "failed to execute %S: %s" cmd @@ exntos exn;
255 | pid ->
256 Ne.clo w @@ clofail "write end of the pipe";
257 let s =
258 match Unix.waitpid [] pid with
259 | exception exn ->
260 reperror "waitpid on %S %d failed: %s" cmd pid @@ exntos exn;
262 | _pid, Unix.WEXITED 0 ->
263 begin
264 match fdcontents r with
265 | exception exn ->
266 reperror "failed to read output of %S: %s" cmd @@ exntos exn;
268 | s ->
269 let l = String.length s in
270 if l > 0 && s.[l-1] = '\n'
271 then String.sub s 0 (l-1)
272 else s
273 end;
274 | _pid, Unix.WEXITED n ->
275 reperror "%S exited with error code %d" cmd n;
277 | _pid, Unix.WSIGNALED n ->
278 reperror "%S was killed with signal %d" cmd n;
280 | _pid, Unix.WSTOPPED n ->
281 reperror "%S was stopped by signal %d" cmd n;
284 Ne.clo r @@ clofail "read end of the pipe";
288 let geturl =
289 let re = Str.regexp {|.*\(\(https?\|ftp\|mailto\|file\)://[^ ]+\).*|} in
290 fun s -> if Str.string_match re s 0
291 then Str.matched_group 1 s
292 else E.s
295 let substratis s pos subs =
296 let subslen = String.length subs in
297 if String.length s - pos >= subslen
298 then
299 let rec cmp i = i = subslen || (s.[pos+i] = subs.[i]) && cmp (i+1)
300 in cmp 0
301 else false
304 let w8 s pos i = Bytes.set s pos (Char.chr (i land 0xff));;
305 let r8 s pos = Char.code (Bytes.get s pos);;
307 let w16 s pos i =
308 w8 s pos i;
309 w8 s (pos+1) (i lsr 8)
312 let w32 s pos i =
313 w16 s pos i;
314 w16 s (pos+2) (i lsr 16)
317 let r16 s pos =
318 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
319 (rb 0) lor ((rb 1) lsl 8)
322 let r16s s pos =
323 let i = r16 s pos in
324 i - ((i land 0x8000) lsl 1)
327 let r32 s pos =
328 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
329 let l = (rb 0) lor ((rb 1) lsl 8)
330 and u = (rb 2) lor ((rb 3) lsl 8) in
331 (u lsl 16) lor l
334 let r32s =
335 if Sys.word_size > 32
336 then fun s pos ->
337 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
338 let v0 = rb 0 and v1 = rb 1 and v2 = rb 2 and v3 = rb 3 in
339 let v = v0 lor (v1 lsl 8) lor (v2 lsl 16) lor (v3 lsl 24) in
340 if v3 land 0x80 = 0
341 then v
342 else (v - (1 lsl 32))
343 else failwith "r32s: not implemented for word_size <= 32"
346 let vlog fmt = Format.ksprintf ignore fmt;;
348 let pipef ?(closew=true) cap f cmd =
349 match Unix.pipe () with
350 | exception exn -> dolog "%s cannot create pipe: %S" cap @@ exntos exn
351 | (r, w) ->
352 begin match spawn cmd [r, 0; w, -1] with
353 | exception exn -> dolog "%s: cannot execute %S: %s" cap cmd @@ exntos exn
354 | _pid -> f w
355 end;
356 Ne.clo r (dolog "%s failed to close r: %s" cap);
357 if closew then Ne.clo w (dolog "%s failed to close w: %s" cap);
360 let selstring selcmd s =
361 pipef "selstring" (fun w ->
363 let l = String.length s in
364 let bytes = Bytes.unsafe_of_string s in
365 let n = tempfailureretry (Unix.write w bytes 0) l in
366 if n != l
367 then dolog "failed to write %d characters to sel pipe, wrote %d" l n;
368 with exn -> dolog "failed to write to sel pipe: %s" @@ exntos exn
369 ) selcmd