3 external measurestr
: int -> string -> float = "ml_measure_string";;
11 type platform
= | Punknown
| Plinux
| Posx
| Psun
| Pbsd
;;
13 let asciilower = let auld = Char.code 'A'
- Char.code '
a'
in
15 | ('A'
..'Z'
) as c
-> Char.code c
- auld |> Char.chr
19 let tempfailureretry f
a =
21 try f
a with Unix.Unix_error
(Unix.EINTR
, _
, _
) -> 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
;;
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
);;
53 type t
= private string
54 val of_string
: string -> t
55 val to_string
: t
-> string
65 let (~
<) = Opaque.of_string;;
66 let (~
>) = Opaque.to_string;;
68 let int_of_string_with_suffix s =
69 let l = String.length
s in
75 | 'k'
| 'K'
-> String.sub
s 0 p, 10
76 | 'm'
| 'M'
-> String.sub
s 0 p, 20
77 | '
g'
| 'G'
-> String.sub
s 0 p, 30
81 let n = int_of_string
s1 in
82 let m = n lsl shift
in
84 then error "value too large"
88 let string_with_suffix_of_int n =
92 let units = [(30, "G"); (20, "M"); (10, "K")] in
98 then string_of_int
h ^
s
100 let s = Printf.sprintf
"_%03d%s" h s in
106 let rec find = function
108 | (shift
, suffix
) :: rest
->
109 if (n land ((1 lsl shift
) - 1)) = 0
110 then prettyint (n lsr shift
) ^ suffix
116 let color_of_string s =
117 Scanf.sscanf
s "%d/%d/%d" (fun r
g b ->
118 (float r
/. 255.0, float g /. 255.0, float b /. 255.0)
122 let rgba_of_string s =
124 s "%d/%d/%d/%d" (fun r
g b a ->
125 (float r
/. 255.0, float g /. 255.0, float b /. 255.0, float a /. 255.0)
129 let color_to_string (r
, g, b) =
130 let r = truncate
(r *. 255.0)
131 and g = truncate
(g *. 255.0)
132 and b = truncate
(b *. 255.0) in
133 Printf.sprintf
"%d/%d/%d" r g b
136 let rgba_to_string (r, g, b, a) =
137 let r = truncate
(r *. 255.0)
138 and g = truncate
(g *. 255.0)
139 and b = truncate
(b *. 255.0)
140 and a = truncate
(a *. 255.0) in
141 Printf.sprintf
"%d/%d/%d/%d" r g b a
145 if Filename.is_relative path
147 let cwd = Sys.getcwd
() in
148 if Filename.is_implicit path
149 then Filename.concat
cwd path
150 else Filename.concat
cwd (Filename.basename path
)
162 try tempfailureretry Unix.close fd
163 with exn
-> f
@@ exntos exn
167 let getenvwithdef name def
=
168 match Sys.getenv name
with
170 | exception Not_found
-> def
173 let newlinere = Str.regexp
"[\r\n]";;
174 let percentsre = Str.regexp
"%s";;
175 let whitere = Str.regexp
"[ \t]";;
180 let b = Buffer.create
(String.length
s + 1) in
181 Buffer.add_string
b s;
186 let btod b = if b then 1 else 0;;
188 let splitatchar s c
= let open String
in
190 | pos
-> sub
s 0 pos
, sub
s (pos
+1) (length
s - pos
- 1)
191 | exception Not_found
-> s, E.s
194 let boundastep h step
=
196 then bound step ~
-h 0
200 let withoutlastutf8 s =
201 let len = String.length
s in
209 let b = Char.code
s.[pos
] in
210 if b land 0b11000000 = 0b11000000
215 if Char.code
s.[len-1] land 0x80 = 0
219 String.sub
s 0 first;
224 let b = Buffer.create
l in
225 let s = Bytes.create
l in
227 let n = tempfailureretry (Unix.read fd
s 0) l in
229 then Buffer.contents
b
231 Buffer.add_subbytes
b s 0 n;
238 let filecontents path
=
239 let fd = Unix.openfile path
[Unix.O_RDONLY
] 0o0
in
240 match fdcontents fd with
242 error "failed to read contents of %s: %s" path
@@ exntos exn
244 Ne.clo fd @@ error "failed to close descriptor for %s: %s" path
;
248 let getcmdoutput errfun cmd
=
249 let reperror fmt
= Printf.kprintf errfun fmt
in
250 let clofail s e
= error "failed to close %s: %s" s e
in
251 match Unix.pipe
() with
253 reperror "pipe failed: %s" @@ exntos exn
;
256 match spawn cmd
[r, -1; w
, 1] with
258 reperror "failed to execute %S: %s" cmd
@@ exntos exn
;
261 Ne.clo w
@@ clofail "write end of the pipe";
263 match Unix.waitpid
[] pid
with
265 reperror "waitpid on %S %d failed: %s" cmd pid
@@ exntos exn
;
267 | _pid
, Unix.WEXITED
0 ->
269 match fdcontents r with
271 reperror "failed to read output of %S: %s" cmd
@@ exntos exn
;
274 let l = String.length
s in
275 if l > 0 && s.[l-1] = '
\n'
276 then String.sub
s 0 (l-1)
279 | _pid
, Unix.WEXITED
n ->
280 reperror "%S exited with error code %d" cmd
n;
282 | _pid
, Unix.WSIGNALED
n ->
283 reperror "%S was killed with signal %d" cmd
n;
285 | _pid
, Unix.WSTOPPED
n ->
286 reperror "%S was stopped by signal %d" cmd
n;
289 Ne.clo r @@ clofail "read end of the pipe";
294 let re = Str.regexp
{|.*\
(\
(https?\
|ftp\
|mailto\
|file\
)://[^
]+\
).*|} in
296 if Str.string_match
re s 0
297 then Str.matched_group
1 s
301 let substratis s pos subs
=
302 let subslen = String.length subs
in
303 if String.length
s - pos
>= subslen
305 let rec cmp i
= i
= subslen || (s.[pos
+i
] = subs
.[i
]) && cmp (i
+1)
310 let w8 s pos i
= Bytes.set
s pos
(Char.chr
(i
land 0xff));;
311 let r8 s pos
= Char.code
(Bytes.get
s pos
);;
315 w8 s (pos
+1) (i
lsr 8)
320 w16 s (pos
+2) (i
lsr 16)
324 let rb pos1
= Char.code
(Bytes.get
s (pos
+ pos1
)) in
325 (rb 0) lor ((rb 1) lsl 8)
330 i - ((i land 0x8000) lsl 1)
334 let rb pos1
= Char.code
(Bytes.get
s (pos
+ pos1
)) in
335 let l = (rb 0) lor ((rb 1) lsl 8)
336 and u
= (rb 2) lor ((rb 3) lsl 8) in
341 if Sys.word_size
> 32
343 let rb pos1
= Char.code
(Bytes.get
s (pos
+ pos1
)) in
344 let v0 = rb 0 and v1
= rb 1 and v2
= rb 2 and v3
= rb 3 in
345 let v = v0 lor (v1
lsl 8) lor (v2
lsl 16) lor (v3
lsl 24) in
348 else (v - (1 lsl 32))
349 else failwith
"r32s: not implemented for word_size <= 32"
352 let vlog fmt
= Format.ksprintf ignore fmt
;;
354 let pipef ?
(closew
=true) cap f cmd
=
355 match Unix.pipe
() with
356 | exception exn
-> dolog "%s cannot create pipe: %S" cap
@@ exntos exn
358 begin match spawn cmd
[r, 0; w
, -1] with
359 | exception exn
-> dolog "%s: cannot execute %S: %s" cap cmd
@@ exntos exn
362 Ne.clo r (dolog "%s failed to close r: %s" cap
);
363 if closew
then Ne.clo w
(dolog "%s failed to close w: %s" cap
);
366 let selstring selcmd
s =
367 pipef "selstring" (fun w
->
369 let l = String.length
s in
370 let bytes = Bytes.unsafe_of_string
s in
371 let n = tempfailureretry (Unix.write w
bytes 0) l in
373 then dolog "failed to write %d characters to sel pipe, wrote %d" l n;
374 with exn
-> dolog "failed to write to sel pipe: %s" @@ exntos exn