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