| 1 |
(**************************************************************************) |
| 2 |
(* *) |
| 3 |
(* Copyright 2012-2020 OCamlPro *) |
| 4 |
(* Copyright 2012 INRIA *) |
| 5 |
(* *) |
| 6 |
(* All rights reserved. This file is distributed under the terms of the *) |
| 7 |
(* GNU Lesser General Public License version 2.1, with the special *) |
| 8 |
(* exception on linking described in the file LICENSE. *) |
| 9 |
(* *) |
| 10 |
(**************************************************************************) |
| 11 |
|
| 12 |
module type SET = sig |
| 13 |
include Set.S |
| 14 |
val map: (elt -> elt) -> t -> t |
| 15 |
val is_singleton: t -> bool |
| 16 |
val choose_one : t -> elt |
| 17 |
val choose_opt : t -> elt option |
| 18 |
val of_list: elt list -> t |
| 19 |
val to_list_map: (elt -> 'b) -> t -> 'b list |
| 20 |
val to_string: t -> string |
| 21 |
val to_json: t OpamJson.encoder |
| 22 |
val of_json: t OpamJson.decoder |
| 23 |
val find: (elt -> bool) -> t -> elt |
| 24 |
val find_opt: (elt -> bool) -> t -> elt option |
| 25 |
val safe_add: elt -> t -> t |
| 26 |
val fixpoint: (elt -> t) -> t -> t |
| 27 |
val map_reduce: ?default:'a -> (elt -> 'a) -> ('a -> 'a -> 'a) -> t -> 'a |
| 28 |
|
| 29 |
module Op : sig |
| 30 |
val (++): t -> t -> t |
| 31 |
val (--): t -> t -> t |
| 32 |
val (%%): t -> t -> t |
| 33 |
end |
| 34 |
end |
| 35 |
module type MAP = sig |
| 36 |
include Map.S |
| 37 |
val to_string: ('a -> string) -> 'a t -> string |
| 38 |
val to_json: 'a OpamJson.encoder -> 'a t OpamJson.encoder |
| 39 |
val of_json: 'a OpamJson.decoder -> 'a t OpamJson.decoder |
| 40 |
val keys: 'a t -> key list |
| 41 |
val values: 'a t -> 'a list |
| 42 |
val find_opt: key -> 'a t -> 'a option |
| 43 |
val choose_opt: 'a t -> (key * 'a) option |
| 44 |
val union: ('a -> 'a -> 'a) -> 'a t -> 'a t -> 'a t |
| 45 |
val is_singleton: 'a t -> bool |
| 46 |
val of_list: (key * 'a) list -> 'a t |
| 47 |
val safe_add: key -> 'a -> 'a t -> 'a t |
| 48 |
val update: key -> ('a -> 'a) -> 'a -> 'a t -> 'a t |
| 49 |
val map_reduce: |
| 50 |
?default:'b -> (key -> 'a -> 'b) -> ('b -> 'b -> 'b) -> 'a t -> 'b |
| 51 |
val filter_map: (key -> 'a -> 'b option) -> 'a t -> 'b t |
| 52 |
end |
| 53 |
module type ABSTRACT = sig |
| 54 |
type t |
| 55 |
val compare: t -> t -> int |
| 56 |
val equal: t -> t -> bool |
| 57 |
val of_string: string -> t |
| 58 |
val to_string: t -> string |
| 59 |
val to_json: t OpamJson.encoder |
| 60 |
val of_json: t OpamJson.decoder |
| 61 |
module Set: SET with type elt = t |
| 62 |
module Map: MAP with type key = t |
| 63 |
end |
| 64 |
|
| 65 |
module type OrderedType = sig |
| 66 |
include Set.OrderedType |
| 67 |
val to_string: t -> string |
| 68 |
val to_json: t OpamJson.encoder |
| 69 |
val of_json: t OpamJson.decoder |
| 70 |
end |
| 71 |
|
| 72 |
module OpamCompare = struct |
| 73 |
external compare : 't -> 't -> int = "%compare" |
| 74 |
external equal : 't -> 't -> bool = "%equal" |
| 75 |
external (=) : 't -> 't -> bool = "%equal" |
| 76 |
external (<>) : 't -> 't -> bool = "%notequal" |
| 77 |
external (<) : 't -> 't -> bool = "%lessthan" |
| 78 |
external (>) : 't -> 't -> bool = "%greaterthan" |
| 79 |
external (<=) : 't -> 't -> bool = "%lessequal" |
| 80 |
external (>=) : 't -> 't -> bool = "%greaterequal" |
| 81 |
end |
| 82 |
|
| 83 |
let max_print = 100 |
| 84 |
|
| 85 |
module OpamList = struct |
| 86 |
|
| 87 |
let cons x xs = x :: xs |
| 88 |
|
| 89 |
let concat_map ?(left="") ?(right="") ?nil ?last_sep sep f = |
| 90 |
let last_sep = match last_sep with None -> sep | Some sep -> sep in |
| 91 |
function |
| 92 |
| [] -> (match nil with Some s -> s | None -> left^right) |
| 93 |
| l -> |
| 94 |
let seplen = String.length sep in |
| 95 |
let strs,len = |
| 96 |
List.fold_left (fun (strs,len) x -> |
| 97 |
let s = f x in s::strs, String.length s + seplen + len) |
| 98 |
([], String.length left + String.length right - seplen) |
| 99 |
l |
| 100 |
in |
| 101 |
let len = match l with |
| 102 |
| _::_::_ -> len + String.length last_sep - seplen |
| 103 |
| _ -> len |
| 104 |
in |
| 105 |
let buf = Bytes.create len in |
| 106 |
let prepend i s = |
| 107 |
let slen = String.length s in |
| 108 |
Bytes.blit_string s 0 buf (i - slen) slen; |
| 109 |
i - slen |
| 110 |
in |
| 111 |
let pos = prepend len right in |
| 112 |
let pos = prepend pos (List.hd strs) in |
| 113 |
let pos = |
| 114 |
List.fold_left (fun (pos, cur_sep) s -> (prepend (prepend pos cur_sep) s, sep)) |
| 115 |
(pos, last_sep) (List.tl strs) |> fst |
| 116 |
in |
| 117 |
let pos = prepend pos left in |
| 118 |
assert (pos = 0); |
| 119 |
Bytes.to_string buf |
| 120 |
|
| 121 |
let rec find_opt f = function |
| 122 |
| [] -> None |
| 123 |
| x::r -> if f x then Some x else find_opt f r |
| 124 |
|
| 125 |
let to_string f = |
| 126 |
concat_map ~left:"{ " ~right:" }" ~nil:"{}" ", " f |
| 127 |
|
| 128 |
let rec remove_duplicates eq = function |
| 129 |
| a::(b::_ as r) when eq a b -> remove_duplicates eq r |
| 130 |
| a::r -> a::remove_duplicates eq r |
| 131 |
| [] -> [] |
| 132 |
|
| 133 |
let sort_nodup cmp l = |
| 134 |
remove_duplicates (fun a b -> cmp a b = 0) (List.sort cmp l) |
| 135 |
|
| 136 |
let filter_map f l = |
| 137 |
let rec loop accu = function |
| 138 |
| [] -> List.rev accu |
| 139 |
| h :: t -> |
| 140 |
match f h with |
| 141 |
| None -> loop accu t |
| 142 |
| Some x -> loop (x::accu) t in |
| 143 |
loop [] l |
| 144 |
|
| 145 |
let filter_some l = filter_map (fun x -> x) l |
| 146 |
|
| 147 |
let rec find_map f = function |
| 148 |
| [] -> raise Not_found |
| 149 |
| x::r -> match f x with |
| 150 |
| Some r -> r |
| 151 |
| None -> find_map f r |
| 152 |
|
| 153 |
let rec find_map_opt f = function |
| 154 |
| [] -> None |
| 155 |
| x::r -> match f x with |
| 156 |
| Some r -> Some r |
| 157 |
| None -> find_map_opt f r |
| 158 |
|
| 159 |
let insert comp x l = |
| 160 |
let rec aux = function |
| 161 |
| [] -> [x] |
| 162 |
| h::t when comp h x < 0 -> h::aux t |
| 163 |
| l -> x :: l in |
| 164 |
aux l |
| 165 |
|
| 166 |
let rec insert_at index value = function |
| 167 |
| [] -> [value] |
| 168 |
| l when index <= 0 -> value :: l |
| 169 |
| x::l -> x :: insert_at (index - 1) value l |
| 170 |
|
| 171 |
let rec assoc eq x = function |
| 172 |
| [] -> raise Not_found |
| 173 |
| (a,b)::r -> if eq a x then b else assoc eq x r |
| 174 |
|
| 175 |
let rec assoc_opt eq x = function |
| 176 |
| [] -> None |
| 177 |
| (a,b)::l -> if eq a x then Some b else assoc_opt eq x l |
| 178 |
|
| 179 |
let pick f l = |
| 180 |
let rec aux acc = function |
| 181 |
| [] -> None, l |
| 182 |
| x::l -> |
| 183 |
if f x then Some x, List.rev_append acc l |
| 184 |
else aux (x::acc) l |
| 185 |
in |
| 186 |
aux [] l |
| 187 |
|
| 188 |
let pick_assoc eq x l = |
| 189 |
let rec aux acc = function |
| 190 |
| [] -> None, l |
| 191 |
| (k,v) as b::r -> |
| 192 |
if eq k x then Some v, List.rev_append acc r |
| 193 |
else aux (b::acc) r |
| 194 |
in |
| 195 |
aux [] l |
| 196 |
|
| 197 |
let rec mem_assoc eq x = function |
| 198 |
| [] -> false |
| 199 |
| (a,_)::r -> eq a x || mem_assoc eq x r |
| 200 |
|
| 201 |
let update_assoc eq k v l = |
| 202 |
let rec aux acc = function |
| 203 |
| [] -> List.rev ((k,v)::acc) |
| 204 |
| (k1,_) as b::r -> |
| 205 |
if eq k1 k then List.rev_append acc ((k,v)::r) |
| 206 |
else aux (b::acc) r |
| 207 |
in |
| 208 |
aux [] l |
| 209 |
|
| 210 |
let remove_assoc eq k l = |
| 211 |
let rec aux acc = function |
| 212 |
| [] -> List.rev acc |
| 213 |
| (k1,_) as b::r -> |
| 214 |
if eq k1 k then List.rev_append acc r |
| 215 |
else aux (b::acc) r |
| 216 |
in |
| 217 |
aux [] l |
| 218 |
|
| 219 |
let fold_left_map f s l = |
| 220 |
let s, l_rev = |
| 221 |
List.fold_left (fun (s, l_rev) x -> |
| 222 |
let s, y = f s x in |
| 223 |
s, y :: l_rev) |
| 224 |
(s, []) l |
| 225 |
in |
| 226 |
s, List.rev l_rev |
| 227 |
|
| 228 |
end |
| 229 |
|
| 230 |
|
| 231 |
module Set = struct |
| 232 |
|
| 233 |
module Make (O : OrderedType) = struct |
| 234 |
|
| 235 |
module S = Set.Make(O) |
| 236 |
|
| 237 |
include S |
| 238 |
|
| 239 |
let fold f set i = |
| 240 |
let r = ref i in |
| 241 |
S.iter (fun elt -> |
| 242 |
r := f elt !r |
| 243 |
) set; |
| 244 |
!r |
| 245 |
|
| 246 |
let is_singleton s = |
| 247 |
not (is_empty s) && |
| 248 |
min_elt s == max_elt s |
| 249 |
|
| 250 |
let choose_one s = |
| 251 |
if is_empty s then raise Not_found |
| 252 |
else if is_singleton s then choose s |
| 253 |
else failwith "choose_one" |
| 254 |
|
| 255 |
let choose_opt s = |
| 256 |
try Some (choose s) with Not_found -> None |
| 257 |
|
| 258 |
let of_list l = |
| 259 |
List.fold_left (fun set e -> add e set) empty l |
| 260 |
|
| 261 |
let to_list_map f set = |
| 262 |
fold (fun x acc -> f x :: acc) set [] |
| 263 |
|
| 264 |
let to_string s = |
| 265 |
if S.cardinal s > max_print then |
| 266 |
Printf.sprintf "%d elements" (S.cardinal s) |
| 267 |
else |
| 268 |
let l = S.fold (fun nv l -> O.to_string nv :: l) s [] in |
| 269 |
OpamList.to_string (fun x -> x) (List.rev l) |
| 270 |
|
| 271 |
let map f t = |
| 272 |
S.fold (fun e set -> S.add (f e) set) t S.empty |
| 273 |
|
| 274 |
exception Found of elt |
| 275 |
|
| 276 |
let find_opt fn t = |
| 277 |
try iter (fun x -> if fn x then raise (Found x)) t; None |
| 278 |
with Found x -> Some x |
| 279 |
|
| 280 |
let find fn t = |
| 281 |
match find_opt fn t with |
| 282 |
| Some x -> x |
| 283 |
| None -> raise Not_found |
| 284 |
|
| 285 |
let to_json t = |
| 286 |
let elements = S.elements t in |
| 287 |
let jsons = List.map O.to_json elements in |
| 288 |
`A jsons |
| 289 |
|
| 290 |
let of_json = function |
| 291 |
| `A jsons -> |
| 292 |
begin try |
| 293 |
let get = function |
| 294 |
| None -> raise Not_found |
| 295 |
| Some v -> v in |
| 296 |
let elems = List.map get (List.map O.of_json jsons) in |
| 297 |
Some (S.of_list elems) |
| 298 |
with Not_found -> None |
| 299 |
end |
| 300 |
| _ -> None |
| 301 |
|
| 302 |
module Op = struct |
| 303 |
let (++) = union |
| 304 |
let (--) = diff |
| 305 |
let (%%) = inter |
| 306 |
end |
| 307 |
|
| 308 |
let safe_add elt t = |
| 309 |
if mem elt t |
| 310 |
then failwith (Printf.sprintf "duplicate entry %s" (O.to_string elt)) |
| 311 |
else add elt t |
| 312 |
|
| 313 |
let fixpoint f = |
| 314 |
let open Op in |
| 315 |
let rec aux fullset curset = |
| 316 |
if is_empty curset then fullset else |
| 317 |
let newset = fold (fun nv set -> set ++ f nv) curset empty in |
| 318 |
let fullset = fullset ++ curset in |
| 319 |
aux fullset (newset -- fullset) |
| 320 |
in |
| 321 |
aux empty |
| 322 |
|
| 323 |
let map_reduce ?default f op t = |
| 324 |
match choose_opt t with |
| 325 |
| Some x -> |
| 326 |
fold (fun x acc -> op acc (f x)) (remove x t) (f x) |
| 327 |
| None -> |
| 328 |
match default with |
| 329 |
| Some d -> d |
| 330 |
| None -> invalid_arg "Set.map_reduce" |
| 331 |
|
| 332 |
end |
| 333 |
|
| 334 |
end |
| 335 |
|
| 336 |
module Map = struct |
| 337 |
|
| 338 |
module Make (O : OrderedType) = struct |
| 339 |
|
| 340 |
module M = Map.Make(O) |
| 341 |
|
| 342 |
include M |
| 343 |
|
| 344 |
let fold f map i = |
| 345 |
let r = ref i in |
| 346 |
M.iter (fun key value-> |
| 347 |
r:= f key value !r |
| 348 |
) map; |
| 349 |
!r |
| 350 |
|
| 351 |
let map f map = |
| 352 |
fold (fun key value map -> |
| 353 |
add key (f value) map |
| 354 |
) map empty |
| 355 |
|
| 356 |
let mapi f map = |
| 357 |
fold (fun key value map -> |
| 358 |
add key (f key value) map |
| 359 |
) map empty |
| 360 |
|
| 361 |
let filter_map f map = |
| 362 |
fold (fun key value map -> |
| 363 |
match f key value with |
| 364 |
| Some value -> add key value map |
| 365 |
| None -> map |
| 366 |
) map empty |
| 367 |
|
| 368 |
let values map = |
| 369 |
List.rev (M.fold (fun _ v acc -> v :: acc) map []) |
| 370 |
|
| 371 |
let keys map = |
| 372 |
List.rev (M.fold (fun k _ acc -> k :: acc) map []) |
| 373 |
|
| 374 |
let union f m1 m2 = |
| 375 |
M.merge (fun _ a b -> match a, b with |
| 376 |
| Some _ as s, None | None, (Some _ as s) -> s |
| 377 |
| Some v1, Some v2 -> Some (f v1 v2) |
| 378 |
| None, None -> assert false) |
| 379 |
m1 m2 |
| 380 |
|
| 381 |
let is_singleton s = |
| 382 |
not (is_empty s) && |
| 383 |
fst (min_binding s) == fst (max_binding s) |
| 384 |
|
| 385 |
let to_string string_of_value m = |
| 386 |
if M.cardinal m > max_print then |
| 387 |
Printf.sprintf "%d elements" (M.cardinal m) |
| 388 |
else |
| 389 |
let s (k,v) = Printf.sprintf "%s:%s" (O.to_string k) (string_of_value v) in |
| 390 |
let l = fold (fun k v l -> s (k,v)::l) m [] in |
| 391 |
OpamList.to_string (fun x -> x) l |
| 392 |
|
| 393 |
let of_list l = |
| 394 |
List.fold_left (fun map (k,v) -> add k v map) empty l |
| 395 |
|
| 396 |
let to_json json_of_value t = |
| 397 |
let bindings = M.bindings t in |
| 398 |
let jsons = List.map (fun (k,v) -> |
| 399 |
`O [ ("key" , O.to_json k); |
| 400 |
("value", json_of_value v) ] |
| 401 |
) bindings in |
| 402 |
`A jsons |
| 403 |
|
| 404 |
let of_json value_of_json = function |
| 405 |
| `A jsons -> |
| 406 |
begin try |
| 407 |
let get_pair = function |
| 408 |
| `O binding -> |
| 409 |
begin match |
| 410 |
O.of_json (OpamList.assoc String.equal "key" binding), |
| 411 |
value_of_json (OpamList.assoc String.equal "value" binding) |
| 412 |
with |
| 413 |
| Some key, Some value -> (key, value) |
| 414 |
| _ -> raise Not_found |
| 415 |
end |
| 416 |
| _ -> raise Not_found in |
| 417 |
let pairs = List.map get_pair jsons in |
| 418 |
Some (of_list pairs) |
| 419 |
with Not_found -> None |
| 420 |
end |
| 421 |
| _ -> None |
| 422 |
|
| 423 |
let find_opt k map = try Some (find k map) with Not_found -> None |
| 424 |
|
| 425 |
let choose_opt m = |
| 426 |
try Some (choose m) with Not_found -> None |
| 427 |
|
| 428 |
let safe_add k v map = |
| 429 |
if mem k map |
| 430 |
then failwith (Printf.sprintf "duplicate entry %s" (O.to_string k)) |
| 431 |
else add k v map |
| 432 |
|
| 433 |
let update k f zero map = |
| 434 |
let v = try find k map with Not_found -> zero in |
| 435 |
add k (f v) map |
| 436 |
|
| 437 |
let map_reduce ?default f op t = |
| 438 |
match choose_opt t with |
| 439 |
| Some (k, v) -> |
| 440 |
fold (fun k v acc -> op acc (f k v)) (remove k t) (f k v) |
| 441 |
| None -> |
| 442 |
match default with |
| 443 |
| Some d -> d |
| 444 |
| None -> invalid_arg "Map.map_reduce" |
| 445 |
end |
| 446 |
|
| 447 |
end |
| 448 |
|
| 449 |
module AbstractString = struct |
| 450 |
type t = string |
| 451 |
let compare = String.compare |
| 452 |
let equal = String.equal |
| 453 |
let of_string x = x |
| 454 |
let to_string x = x |
| 455 |
let to_json x = `String x |
| 456 |
let of_json = function |
| 457 |
| `String x -> Some x |
| 458 |
| _ -> None |
| 459 |
module O = struct |
| 460 |
type t = string |
| 461 |
let to_string = to_string |
| 462 |
let compare = compare |
| 463 |
let to_json = to_json |
| 464 |
let of_json = of_json |
| 465 |
end |
| 466 |
module Set = Set.Make(O) |
| 467 |
module Map = Map.Make(O) |
| 468 |
end |
| 469 |
|
| 470 |
|
| 471 |
module OInt = struct |
| 472 |
type t = int |
| 473 |
let compare = Int.compare |
| 474 |
let to_string = string_of_int |
| 475 |
let to_json i = `String (string_of_int i) |
| 476 |
let of_json = function |
| 477 |
| `String s -> (try Some (int_of_string s) with _ -> None) |
| 478 |
| _ -> None |
| 479 |
end |
| 480 |
|
| 481 |
module IntMap = Map.Make(OInt) |
| 482 |
module IntSet = Set.Make(OInt) |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
module Option = struct |
| 487 |
let map f = function |
| 488 |
| None -> None |
| 489 |
| Some x -> Some (f x) |
| 490 |
|
| 491 |
let iter f = function |
| 492 |
| None -> () |
| 493 |
| Some x -> f x |
| 494 |
|
| 495 |
let default dft = function |
| 496 |
| None -> dft |
| 497 |
| Some x -> x |
| 498 |
|
| 499 |
let default_map dft = function |
| 500 |
| None -> dft |
| 501 |
| some -> some |
| 502 |
|
| 503 |
let replace f = function |
| 504 |
| None -> None |
| 505 |
| Some x -> f x |
| 506 |
|
| 507 |
let map_default f dft = function |
| 508 |
| None -> dft |
| 509 |
| Some x -> f x |
| 510 |
|
| 511 |
let compare cmp o1 o2 = |
| 512 |
match o1,o2 with |
| 513 |
| None, None -> 0 |
| 514 |
| Some _, None -> 1 |
| 515 |
| None, Some _ -> -1 |
| 516 |
| Some x1, Some x2 -> cmp x1 x2 |
| 517 |
|
| 518 |
let equal f o1 o2 = |
| 519 |
match o1, o2 with |
| 520 |
| Some o1, Some o2 -> f o1 o2 |
| 521 |
| None, None -> true |
| 522 |
| _ , _ -> false |
| 523 |
|
| 524 |
let to_string ?(none="") f = function |
| 525 |
| Some x -> f x |
| 526 |
| None -> none |
| 527 |
|
| 528 |
let to_list = function |
| 529 |
| None -> [] |
| 530 |
| Some x -> [x] |
| 531 |
|
| 532 |
let some x = Some x |
| 533 |
let none _ = None |
| 534 |
|
| 535 |
let of_Not_found f x = |
| 536 |
try Some (f x) with Not_found -> None |
| 537 |
|
| 538 |
module Op = struct |
| 539 |
let (>>=) = function |
| 540 |
| None -> fun _ -> None |
| 541 |
| Some x -> fun f -> f x |
| 542 |
let (>>|) opt f = map f opt |
| 543 |
let (>>+) opt f = match opt with |
| 544 |
| None -> f () |
| 545 |
| some -> some |
| 546 |
let (+!) opt dft = default dft opt |
| 547 |
let (++) = function |
| 548 |
| None -> fun opt -> opt |
| 549 |
| some -> fun _ -> some |
| 550 |
end |
| 551 |
end |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
module OpamString = struct |
| 556 |
|
| 557 |
module OString = struct |
| 558 |
type t = string |
| 559 |
let compare = String.compare |
| 560 |
let to_string x = x |
| 561 |
let to_json x = `String x |
| 562 |
let of_json = function |
| 563 |
| `String s -> Some s |
| 564 |
| _ -> None |
| 565 |
end |
| 566 |
|
| 567 |
module StringSet = Set.Make(OString) |
| 568 |
module StringMap = Map.Make(OString) |
| 569 |
|
| 570 |
module SetSet = Set.Make(StringSet) |
| 571 |
module SetMap = Map.Make(StringSet) |
| 572 |
|
| 573 |
module Set = StringSet |
| 574 |
module Map = StringMap |
| 575 |
|
| 576 |
let starts_with ~prefix s = |
| 577 |
let x = String.length prefix in |
| 578 |
let n = String.length s in |
| 579 |
n >= x && |
| 580 |
let rec chk i = i >= x || prefix.[i] = s.[i] && chk (i+1) in |
| 581 |
chk 0 |
| 582 |
|
| 583 |
let ends_with ~suffix s = |
| 584 |
let x = String.length suffix in |
| 585 |
let n = String.length s in |
| 586 |
n >= x && |
| 587 |
let rec chk i = i >= x || suffix.[i] = s.[i+n-x] && chk (i+1) in |
| 588 |
chk 0 |
| 589 |
|
| 590 |
let for_all f s = |
| 591 |
let len = String.length s in |
| 592 |
let rec aux i = i >= len || f s.[i] && aux (i+1) in |
| 593 |
aux 0 |
| 594 |
|
| 595 |
let contains_char s c = |
| 596 |
try let _ = String.index s c in true |
| 597 |
with Not_found -> false |
| 598 |
|
| 599 |
let contains ~sub = |
| 600 |
Re.(execp (compile (str sub))) |
| 601 |
|
| 602 |
let exact_match re s = |
| 603 |
try |
| 604 |
let subs = Re.exec re s in |
| 605 |
let subs = Array.to_list (Re.Group.all_offset subs) in |
| 606 |
let n = String.length s in |
| 607 |
let subs = List.filter (fun (s,e) -> s=0 && e=n) subs in |
| 608 |
List.length subs > 0 |
| 609 |
with Not_found -> |
| 610 |
false |
| 611 |
|
| 612 |
let find_from f s i = |
| 613 |
let l = String.length s in |
| 614 |
if i < 0 || i > l then |
| 615 |
invalid_arg "find_from" |
| 616 |
else |
| 617 |
let rec g i = |
| 618 |
if i < l then |
| 619 |
if f s.[i] then |
| 620 |
i |
| 621 |
else |
| 622 |
g (succ i) |
| 623 |
else |
| 624 |
raise Not_found in |
| 625 |
g i |
| 626 |
|
| 627 |
let map f s = |
| 628 |
let len = String.length s in |
| 629 |
let b = Bytes.create len in |
| 630 |
for i = 0 to len - 1 do Bytes.set b i (f s.[i]) done; |
| 631 |
Bytes.to_string b |
| 632 |
|
| 633 |
let is_whitespace = function |
| 634 |
| ' ' | '\t' | '\r' | '\n' -> true |
| 635 |
| _ -> false |
| 636 |
|
| 637 |
let strip str = |
| 638 |
let p = ref 0 in |
| 639 |
let l = String.length str in |
| 640 |
while !p < l && is_whitespace (String.unsafe_get str !p) do |
| 641 |
incr p; |
| 642 |
done; |
| 643 |
let p = !p in |
| 644 |
let l = ref (l - 1) in |
| 645 |
while !l >= p && is_whitespace (String.unsafe_get str !l) do |
| 646 |
decr l; |
| 647 |
done; |
| 648 |
String.sub str p (!l - p + 1) |
| 649 |
|
| 650 |
let strip_right str = |
| 651 |
let rec aux i = |
| 652 |
if i < 0 || not (is_whitespace str.[i]) then i else aux (i-1) |
| 653 |
in |
| 654 |
let l = String.length str in |
| 655 |
let i = aux (l-1) in |
| 656 |
if i = l - 1 then str |
| 657 |
else String.sub str 0 (i+1) |
| 658 |
|
| 659 |
let sub_at n s = |
| 660 |
if String.length s <= n then |
| 661 |
s |
| 662 |
else |
| 663 |
String.sub s 0 n |
| 664 |
|
| 665 |
let remove_prefix ~prefix s = |
| 666 |
if starts_with ~prefix s then |
| 667 |
let x = String.length prefix in |
| 668 |
let n = String.length s in |
| 669 |
String.sub s x (n - x) |
| 670 |
else |
| 671 |
s |
| 672 |
|
| 673 |
let remove_suffix ~suffix s = |
| 674 |
if ends_with ~suffix s then |
| 675 |
let x = String.length suffix in |
| 676 |
let n = String.length s in |
| 677 |
String.sub s 0 (n - x) |
| 678 |
else |
| 679 |
s |
| 680 |
|
| 681 |
let cut_at_aux fn s sep = |
| 682 |
try |
| 683 |
let i = fn s sep in |
| 684 |
let name = String.sub s 0 i in |
| 685 |
let version = String.sub s (i+1) (String.length s - i - 1) in |
| 686 |
Some (name, version) |
| 687 |
with Invalid_argument _ | Not_found -> |
| 688 |
None |
| 689 |
|
| 690 |
let cut_at = cut_at_aux String.index |
| 691 |
|
| 692 |
let rcut_at = cut_at_aux String.rindex |
| 693 |
|
| 694 |
let split s c = |
| 695 |
(* old compat version (Re 1.2.0) |
| 696 |
{[Re_str.split (Re_str.regexp (Printf.sprintf "[%c]+" c)) s]} *) |
| 697 |
Re.(split (compile (rep1 (char c)))) s |
| 698 |
|
| 699 |
let split_delim s c = |
| 700 |
let tokens = Re.(split_full (compile (char c)) s) in |
| 701 |
let rec aux acc = function |
| 702 |
| [] -> acc |
| 703 |
| (`Delim _)::[] -> ""::acc |
| 704 |
| (`Text s)::tl -> aux (s::acc) tl |
| 705 |
| (`Delim _)::tl -> aux acc tl |
| 706 |
in |
| 707 |
let acc0 = |
| 708 |
match tokens with |
| 709 |
| (`Delim _)::_ -> [""] |
| 710 |
|_ -> [] |
| 711 |
in List.rev (aux acc0 tokens) |
| 712 |
|
| 713 |
let fold_left f acc s = |
| 714 |
let acc = ref acc in |
| 715 |
for i = 0 to String.length s - 1 do acc := f !acc s.[i] done; |
| 716 |
!acc |
| 717 |
|
| 718 |
let compare_case s1 s2 = |
| 719 |
let l1 = String.length s1 and l2 = String.length s2 in |
| 720 |
let len = min l1 l2 in |
| 721 |
let rec aux i = |
| 722 |
if i < len then |
| 723 |
let c1 = s1.[i] and c2 = s2.[i] in |
| 724 |
match Char.compare (Char.lowercase_ascii c1) (Char.lowercase_ascii c2) |
| 725 |
with |
| 726 |
| 0 -> |
| 727 |
(match Char.compare c1 c2 with |
| 728 |
| 0 -> aux (i+1) |
| 729 |
| c -> c) |
| 730 |
| c -> c |
| 731 |
else |
| 732 |
if l1 < l2 then -1 |
| 733 |
else if l1 > l2 then 1 |
| 734 |
else 0 |
| 735 |
in |
| 736 |
aux 0 |
| 737 |
|
| 738 |
let is_prefix_of ~from ~full s = |
| 739 |
let length_s = String.length s in |
| 740 |
let length_full = String.length full in |
| 741 |
if from < 0 || from > length_full then |
| 742 |
invalid_arg "is_prefix_of" |
| 743 |
else |
| 744 |
length_s <= length_full |
| 745 |
&& length_s > from |
| 746 |
&& String.sub full 0 length_s = s |
| 747 |
|
| 748 |
let is_hex s = |
| 749 |
try |
| 750 |
String.iter (function |
| 751 |
| '0'..'9' | 'A'..'F' | 'a'..'f' -> () |
| 752 |
| _ -> raise Exit) |
| 753 |
s; |
| 754 |
true |
| 755 |
with Exit -> false |
| 756 |
|
| 757 |
end |
| 758 |
|
| 759 |
type warning_printer = |
| 760 |
{mutable warning : 'a . ('a, unit, string, unit) format4 -> 'a} |
| 761 |
let console = ref {warning = fun fmt -> Printf.ksprintf prerr_string fmt} |
| 762 |
|
| 763 |
module Env = struct |
| 764 |
|
| 765 |
(* Remove from a c-separated list of string the one with the given prefix *) |
| 766 |
let reset_value ~prefix c v = |
| 767 |
let v = OpamString.split v c in |
| 768 |
List.filter (fun v -> not (OpamString.starts_with ~prefix v)) v |
| 769 |
|
| 770 |
(* Split the list in two according to the first occurrence of the string |
| 771 |
starting with the given prefix. |
| 772 |
*) |
| 773 |
let cut_value ~prefix c v = |
| 774 |
let v = OpamString.split v c in |
| 775 |
let rec aux before = |
| 776 |
function |
| 777 |
| [] -> [], List.rev before |
| 778 |
| curr::after when OpamString.starts_with ~prefix curr -> |
| 779 |
before, after |
| 780 |
| curr::after -> aux (curr::before) after |
| 781 |
in aux [] v |
| 782 |
|
| 783 |
let escape_single_quotes ?(using_backslashes=false) = |
| 784 |
if using_backslashes then |
| 785 |
Re.(replace (compile (set "\\\'")) ~f:(fun g -> "\\"^Group.get g 0)) |
| 786 |
else |
| 787 |
Re.(replace_string (compile (char '\'')) ~by:"'\"'\"'") |
| 788 |
|
| 789 |
let escape_powershell = |
| 790 |
(* escape single quotes with two single quotes. |
| 791 |
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.1 *) |
| 792 |
Re.(replace_string (compile (char '\'')) ~by:"''") |
| 793 |
|
| 794 |
module Name = struct |
| 795 |
module M = struct |
| 796 |
include AbstractString |
| 797 |
|
| 798 |
let compare = |
| 799 |
if Sys.win32 then |
| 800 |
fun l r -> |
| 801 |
String.(compare (lowercase_ascii l) (lowercase_ascii r)) |
| 802 |
else |
| 803 |
String.compare |
| 804 |
end |
| 805 |
|
| 806 |
type t = string |
| 807 |
|
| 808 |
let of_string = M.of_string |
| 809 |
let to_string = M.to_string |
| 810 |
let of_json = M.of_json |
| 811 |
let to_json = M.to_json |
| 812 |
let compare = M.compare |
| 813 |
|
| 814 |
let equal = |
| 815 |
if Sys.win32 then |
| 816 |
fun l r -> |
| 817 |
String.(equal (lowercase_ascii l) (lowercase_ascii r)) |
| 818 |
else |
| 819 |
String.equal |
| 820 |
|
| 821 |
let equal_string = equal |
| 822 |
|
| 823 |
module Set = Set.Make(M) |
| 824 |
module Map = Map.Make(M) |
| 825 |
end |
| 826 |
|
| 827 |
let to_list env = |
| 828 |
List.rev_map (fun s -> |
| 829 |
match OpamString.cut_at s '=' with |
| 830 |
| None -> s, "" |
| 831 |
| Some p -> p) |
| 832 |
(Array.to_list env) |
| 833 |
|
| 834 |
let raw_env = Unix.environment |
| 835 |
|
| 836 |
let list = |
| 837 |
let lazy_env = lazy (to_list (raw_env ())) in |
| 838 |
fun () -> Lazy.force lazy_env |
| 839 |
|
| 840 |
let cyg_env cygbin = |
| 841 |
let env = raw_env () in |
| 842 |
let f v = |
| 843 |
match OpamString.cut_at v '=' with |
| 844 |
| Some (path, c) when Name.equal_string path "path" -> |
| 845 |
Printf.sprintf "%s=%s;%s" |
| 846 |
path cygbin c |
| 847 |
| _ -> v |
| 848 |
in |
| 849 |
Array.map f env |
| 850 |
|
| 851 |
let get_full n = List.find (fun (k,_) -> Name.equal k n) (list ()) |
| 852 |
|
| 853 |
let get n = snd (get_full n) |
| 854 |
|
| 855 |
let getopt = Option.of_Not_found get |
| 856 |
|
| 857 |
let getopt_full n = |
| 858 |
try let (n, v) = get_full n in (n, Some v) |
| 859 |
with Not_found -> (n, None) |
| 860 |
end |
| 861 |
|
| 862 |
|
| 863 |
(** To use when catching default exceptions: ensures we don't catch fatal errors |
| 864 |
like C-c *) |
| 865 |
let fatal e = match e with |
| 866 |
| Sys.Break -> prerr_newline (); raise e |
| 867 |
| Assert_failure _ | Match_failure _ -> raise e |
| 868 |
| _ -> () |
| 869 |
|
| 870 |
module OpamSys = struct |
| 871 |
|
| 872 |
let path_sep = if Sys.win32 then ';' else ':' |
| 873 |
|
| 874 |
let split_path_variable ?(clean=true) = |
| 875 |
if Sys.win32 then fun path -> |
| 876 |
let length = String.length path in |
| 877 |
let rec f acc index current last normal = |
| 878 |
if index = length then |
| 879 |
let current = current ^ String.sub path last (index - last) in |
| 880 |
List.rev (if current <> "" then current::acc else acc) |
| 881 |
else let c = path.[index] |
| 882 |
and next = succ index in |
| 883 |
if c = ';' && normal || c = '"' then |
| 884 |
let current = current ^ String.sub path last (index - last) in |
| 885 |
if c = '"' then |
| 886 |
f acc next current next (not normal) |
| 887 |
else |
| 888 |
let acc = if current = "" then acc else current::acc in |
| 889 |
f acc next "" next true |
| 890 |
else |
| 891 |
f acc next current last normal in |
| 892 |
f [] 0 "" 0 true |
| 893 |
else fun path -> |
| 894 |
let split = if clean then OpamString.split else OpamString.split_delim in |
| 895 |
split path path_sep |
| 896 |
|
| 897 |
let with_process_in cmd args f = |
| 898 |
if Sys.win32 then |
| 899 |
assert false; |
| 900 |
let path = split_path_variable (Env.get "PATH") in |
| 901 |
let cmd = |
| 902 |
List.find Sys.file_exists (List.map (fun d -> Filename.concat d cmd) path) |
| 903 |
in |
| 904 |
let ic = Unix.open_process_in (cmd^" "^args) in |
| 905 |
try |
| 906 |
let r = f ic in |
| 907 |
ignore (Unix.close_process_in ic) ; r |
| 908 |
with exn -> |
| 909 |
ignore (Unix.close_process_in ic) ; raise exn |
| 910 |
|
| 911 |
let tty_out = Unix.isatty Unix.stdout |
| 912 |
|
| 913 |
let tty_in = Unix.isatty Unix.stdin |
| 914 |
|
| 915 |
let default_columns = lazy ( |
| 916 |
let default = 16_000_000 in |
| 917 |
let cols = |
| 918 |
try int_of_string (Env.get "COLUMNS") with |
| 919 |
| Not_found |
| 920 |
| Failure _ -> default |
| 921 |
in |
| 922 |
if cols > 0 then cols else default |
| 923 |
) |
| 924 |
|
| 925 |
let get_terminal_columns () = |
| 926 |
let fallback = 80 in |
| 927 |
let cols = |
| 928 |
try (* terminfo *) |
| 929 |
with_process_in "tput" "cols" |
| 930 |
(fun ic -> int_of_string (input_line ic)) |
| 931 |
with |
| 932 |
| Unix.Unix_error _ | Sys_error _ | Failure _ | End_of_file | Not_found -> |
| 933 |
try (* GNU stty *) |
| 934 |
with_process_in "stty" "size" |
| 935 |
(fun ic -> |
| 936 |
match OpamString.split (input_line ic) ' ' with |
| 937 |
| [_ ; v] -> int_of_string v |
| 938 |
| _ -> failwith "stty") |
| 939 |
with |
| 940 |
| Unix.Unix_error _ | Sys_error _ | Failure _ |
| 941 |
| End_of_file | Not_found -> fallback |
| 942 |
in |
| 943 |
if cols > 0 then cols else fallback |
| 944 |
|
| 945 |
let win32_get_console_width default_columns = |
| 946 |
try |
| 947 |
let hConsoleOutput = OpamStubs.(getStdHandle STD_OUTPUT_HANDLE) in |
| 948 |
let {OpamStubs.size = (width, _); _} = |
| 949 |
OpamStubs.getConsoleScreenBufferInfo hConsoleOutput |
| 950 |
in |
| 951 |
width |
| 952 |
with Not_found -> |
| 953 |
Lazy.force default_columns |
| 954 |
|
| 955 |
let terminal_columns = |
| 956 |
let v = ref (lazy (get_terminal_columns ())) in |
| 957 |
let () = |
| 958 |
try Sys.set_signal 28 (* SIGWINCH *) |
| 959 |
(Sys.Signal_handle |
| 960 |
(fun _ -> v := lazy (get_terminal_columns ()))) |
| 961 |
with Invalid_argument _ -> () |
| 962 |
in |
| 963 |
if Sys.win32 then |
| 964 |
fun () -> |
| 965 |
win32_get_console_width default_columns |
| 966 |
else |
| 967 |
fun () -> |
| 968 |
if tty_out |
| 969 |
then Lazy.force !v |
| 970 |
else Lazy.force default_columns |
| 971 |
|
| 972 |
let home = |
| 973 |
(* Note: we ask Unix.getenv instead of Env.get to avoid |
| 974 |
forcing the environment in this function that is used |
| 975 |
before the .init() functions are called -- see |
| 976 |
OpamStateConfig.default. *) |
| 977 |
let home = lazy ( |
| 978 |
try Unix.getenv "HOME" |
| 979 |
with Not_found -> |
| 980 |
if Sys.win32 then |
| 981 |
(* CSIDL_PROFILE = 0x28 *) |
| 982 |
OpamStubs.(shGetFolderPath 0x28 SHGFP_TYPE_CURRENT) |
| 983 |
else |
| 984 |
Sys.getcwd () |
| 985 |
) in |
| 986 |
fun () -> Lazy.force home |
| 987 |
|
| 988 |
let etc () = "/etc" |
| 989 |
|
| 990 |
let uname = |
| 991 |
let memo = Hashtbl.create 7 in |
| 992 |
fun arg -> |
| 993 |
try Hashtbl.find memo arg with Not_found -> |
| 994 |
let r = |
| 995 |
try |
| 996 |
with_process_in "uname" arg |
| 997 |
(fun ic -> Some (OpamString.strip (input_line ic))) |
| 998 |
with Unix.Unix_error _ | Sys_error _ | Not_found -> None |
| 999 |
in |
| 1000 |
Hashtbl.add memo arg r; |
| 1001 |
r |
| 1002 |
|
| 1003 |
let system () = |
| 1004 |
(* CSIDL_SYSTEM = 0x25 *) |
| 1005 |
OpamStubs.(shGetFolderPath 0x25 SHGFP_TYPE_CURRENT) |
| 1006 |
|
| 1007 |
type os = |
| 1008 |
| Darwin |
| 1009 |
| Linux |
| 1010 |
| FreeBSD |
| 1011 |
| OpenBSD |
| 1012 |
| NetBSD |
| 1013 |
| DragonFly |
| 1014 |
| Cygwin |
| 1015 |
| Win32 |
| 1016 |
| Unix |
| 1017 |
| Other of string |
| 1018 |
|
| 1019 |
let os = |
| 1020 |
let os = lazy ( |
| 1021 |
match Sys.os_type with |
| 1022 |
| "Unix" -> begin |
| 1023 |
match uname "-s" with |
| 1024 |
| Some "Darwin" -> Darwin |
| 1025 |
| Some "Linux" -> Linux |
| 1026 |
| Some "FreeBSD" -> FreeBSD |
| 1027 |
| Some "OpenBSD" -> OpenBSD |
| 1028 |
| Some "NetBSD" -> NetBSD |
| 1029 |
| Some "DragonFly" -> DragonFly |
| 1030 |
| _ -> Unix |
| 1031 |
end |
| 1032 |
| "Win32" -> Win32 |
| 1033 |
| "Cygwin" -> Cygwin |
| 1034 |
| s -> Other s |
| 1035 |
) in |
| 1036 |
fun () -> Lazy.force os |
| 1037 |
|
| 1038 |
type powershell_host = Powershell_pwsh | Powershell |
| 1039 |
type shell = SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish |
| 1040 |
| SH_pwsh of powershell_host | SH_cmd |
| 1041 |
|
| 1042 |
let all_shells = |
| 1043 |
[SH_sh; SH_bash; |
| 1044 |
SH_zsh; |
| 1045 |
SH_csh; |
| 1046 |
SH_fish; |
| 1047 |
SH_pwsh Powershell_pwsh; |
| 1048 |
SH_pwsh Powershell; |
| 1049 |
SH_cmd] |
| 1050 |
|
| 1051 |
let windows_default_shell = SH_cmd |
| 1052 |
let unix_default_shell = SH_sh |
| 1053 |
|
| 1054 |
let shell_of_string = function |
| 1055 |
| "tcsh" |
| 1056 |
| "bsd-csh" |
| 1057 |
| "csh" -> Some SH_csh |
| 1058 |
| "zsh" -> Some SH_zsh |
| 1059 |
| "bash" -> Some SH_bash |
| 1060 |
| "fish" -> Some SH_fish |
| 1061 |
| "pwsh" -> Some (SH_pwsh Powershell_pwsh) |
| 1062 |
| "dash" |
| 1063 |
| "sh" -> Some SH_sh |
| 1064 |
| _ -> None |
| 1065 |
|
| 1066 |
let executable_name = |
| 1067 |
if Sys.win32 then |
| 1068 |
fun name -> |
| 1069 |
if Filename.check_suffix name ".exe" then |
| 1070 |
name |
| 1071 |
else |
| 1072 |
name ^ ".exe" |
| 1073 |
else |
| 1074 |
fun x -> x |
| 1075 |
|
| 1076 |
let windows_process_ancestry = Lazy.from_fun OpamStubs.getProcessAncestry |
| 1077 |
|
| 1078 |
type shell_choice = Accept of shell |
| 1079 |
|
| 1080 |
let windows_get_shell = |
| 1081 |
let categorize_process (_, image) = |
| 1082 |
match String.lowercase_ascii (Filename.basename image) with |
| 1083 |
| "powershell.exe" | "powershell_ise.exe" -> |
| 1084 |
Some (Accept (SH_pwsh Powershell)) |
| 1085 |
| "pwsh.exe" -> Some (Accept (SH_pwsh Powershell_pwsh)) |
| 1086 |
| "cmd.exe" -> Some (Accept SH_cmd) |
| 1087 |
| "env.exe" -> Some (Accept SH_sh) |
| 1088 |
| name -> |
| 1089 |
Option.map |
| 1090 |
(fun shell -> Accept shell) |
| 1091 |
(shell_of_string (Filename.chop_suffix name ".exe")) |
| 1092 |
in |
| 1093 |
lazy ( |
| 1094 |
let lazy ancestors = windows_process_ancestry in |
| 1095 |
match OpamList.filter_map categorize_process ancestors with |
| 1096 |
| [] -> None |
| 1097 |
| Accept most_relevant_shell :: _ -> Some most_relevant_shell |
| 1098 |
) |
| 1099 |
|
| 1100 |
let guess_shell_compat () = |
| 1101 |
let parent_guess () = |
| 1102 |
let ppid = Unix.getppid () in |
| 1103 |
let dir = Filename.concat "/proc" (string_of_int ppid) in |
| 1104 |
try |
| 1105 |
Some (Unix.readlink (Filename.concat dir "exe")) |
| 1106 |
with e -> |
| 1107 |
fatal e; |
| 1108 |
try |
| 1109 |
with_process_in "ps" |
| 1110 |
(Printf.sprintf "-p %d -o comm= 2>/dev/null" ppid) |
| 1111 |
(fun ic -> Some (input_line ic)) |
| 1112 |
with |
| 1113 |
| Unix.Unix_error _ | Sys_error _ | Failure _ | End_of_file | Not_found -> |
| 1114 |
try |
| 1115 |
let c = open_in_bin ("/proc/" ^ string_of_int ppid ^ "/cmdline") in |
| 1116 |
begin try |
| 1117 |
let s = input_line c in |
| 1118 |
close_in c; |
| 1119 |
Some (String.sub s 0 (String.index s '\000')) |
| 1120 |
with |
| 1121 |
| Not_found -> |
| 1122 |
None |
| 1123 |
| e -> |
| 1124 |
close_in c; |
| 1125 |
fatal e; None |
| 1126 |
end |
| 1127 |
with e -> |
| 1128 |
fatal e; None |
| 1129 |
in |
| 1130 |
let test shell = shell_of_string (Filename.basename shell) in |
| 1131 |
if Sys.win32 then |
| 1132 |
let shell = |
| 1133 |
match Lazy.force windows_get_shell with |
| 1134 |
| None -> |
| 1135 |
Option.of_Not_found Env.get "SHELL" |> Option.replace test |
| 1136 |
| some -> |
| 1137 |
some |
| 1138 |
in |
| 1139 |
Option.default windows_default_shell shell |
| 1140 |
else |
| 1141 |
let shell = |
| 1142 |
match Option.replace test (parent_guess ()) with |
| 1143 |
| None -> |
| 1144 |
Option.of_Not_found Env.get "SHELL" |> Option.replace test |
| 1145 |
| some -> |
| 1146 |
some |
| 1147 |
in |
| 1148 |
Option.default unix_default_shell shell |
| 1149 |
|
| 1150 |
let guess_dot_profile shell = |
| 1151 |
let home f = |
| 1152 |
try Filename.concat (home ()) f |
| 1153 |
with Not_found -> f in |
| 1154 |
match shell with |
| 1155 |
| SH_fish -> |
| 1156 |
Some (List.fold_left Filename.concat (home ".config") ["fish"; "config.fish"]) |
| 1157 |
| SH_zsh -> Some (home ".zshrc") |
| 1158 |
| SH_bash -> |
| 1159 |
let shell = |
| 1160 |
(try |
| 1161 |
List.find Sys.file_exists [ |
| 1162 |
(* Bash looks up these 3 files in order and only loads the first, |
| 1163 |
for LOGIN shells *) |
| 1164 |
home ".bash_profile"; |
| 1165 |
home ".bash_login"; |
| 1166 |
home ".profile"; |
| 1167 |
(* Bash loads .bashrc INSTEAD, for interactive NON login shells only; |
| 1168 |
but it's often included from the above. |
| 1169 |
We may include our variables in both to be sure ; for now we rely |
| 1170 |
on non-login shells inheriting their env from a login shell |
| 1171 |
somewhere... *) |
| 1172 |
] |
| 1173 |
with Not_found -> |
| 1174 |
(* iff none of the above exist, creating this should be safe *) |
| 1175 |
home ".bash_profile") |
| 1176 |
in |
| 1177 |
Some shell |
| 1178 |
| SH_csh -> |
| 1179 |
let cshrc = home ".cshrc" in |
| 1180 |
let tcshrc = home ".tcshrc" in |
| 1181 |
Some (if Sys.file_exists cshrc then cshrc else tcshrc) |
| 1182 |
| SH_pwsh _ -> |
| 1183 |
None |
| 1184 |
| SH_sh -> Some (home ".profile") |
| 1185 |
| SH_cmd -> |
| 1186 |
None |
| 1187 |
|
| 1188 |
|
| 1189 |
let registered_at_exit = ref [] |
| 1190 |
let at_exit f = |
| 1191 |
Stdlib.at_exit f; |
| 1192 |
registered_at_exit := f :: !registered_at_exit |
| 1193 |
let exec_at_exit () = |
| 1194 |
List.iter |
| 1195 |
(fun f -> try f () with _ -> ()) |
| 1196 |
!registered_at_exit |
| 1197 |
|
| 1198 |
let get_windows_executable_variant = |
| 1199 |
if Sys.win32 then |
| 1200 |
let results = Hashtbl.create 17 in |
| 1201 |
let requires_cygwin cygcheck name = |
| 1202 |
let env = Env.cyg_env (Filename.dirname cygcheck) in |
| 1203 |
let cmd = OpamCompat.Filename.quote_command cygcheck [name] in |
| 1204 |
let ((c, _, _) as process) = Unix.open_process_full cmd env in |
| 1205 |
let rec check_dll platform = |
| 1206 |
match input_line c with |
| 1207 |
| dll -> |
| 1208 |
let tdll = (*String.trim*) dll in |
| 1209 |
if OpamString.ends_with ~suffix:"cygwin1.dll" tdll then |
| 1210 |
if OpamString.starts_with ~prefix:" " dll then |
| 1211 |
check_dll `Cygwin |
| 1212 |
else if platform = `Native then |
| 1213 |
check_dll (`Tainted `Cygwin) |
| 1214 |
else |
| 1215 |
check_dll platform |
| 1216 |
else if OpamString.ends_with ~suffix:"msys-2.0.dll" tdll then |
| 1217 |
if OpamString.starts_with ~prefix:" " dll then |
| 1218 |
check_dll `Msys2 |
| 1219 |
else if platform = `Native then |
| 1220 |
check_dll (`Tainted `Msys2) |
| 1221 |
else |
| 1222 |
check_dll platform |
| 1223 |
else |
| 1224 |
check_dll platform |
| 1225 |
| exception e -> |
| 1226 |
Unix.close_process_full process |> ignore; |
| 1227 |
fatal e; |
| 1228 |
platform |
| 1229 |
in |
| 1230 |
check_dll `Native |
| 1231 |
in |
| 1232 |
fun ~cygbin name -> |
| 1233 |
match cygbin with |
| 1234 |
| Some cygbin -> |
| 1235 |
(let cygcheck = Filename.concat cygbin "cygcheck.exe" in |
| 1236 |
if Filename.is_relative name then |
| 1237 |
requires_cygwin cygcheck name |
| 1238 |
else |
| 1239 |
try Hashtbl.find results (cygcheck, name) |
| 1240 |
with Not_found -> |
| 1241 |
let result = requires_cygwin cygcheck name in |
| 1242 |
Hashtbl.add results (cygcheck, name) result; |
| 1243 |
result) |
| 1244 |
| None -> `Native |
| 1245 |
else |
| 1246 |
fun ~cygbin:_ _ -> `Native |
| 1247 |
|
| 1248 |
let is_cygwin_cygcheck ~cygbin = |
| 1249 |
match cygbin with |
| 1250 |
| Some cygbin -> |
| 1251 |
let cygpath = Filename.concat cygbin "cygpath.exe" in |
| 1252 |
Sys.file_exists cygpath |
| 1253 |
&& (get_windows_executable_variant ~cygbin:(Some cygbin) cygpath = `Cygwin) |
| 1254 |
| None -> false |
| 1255 |
|
| 1256 |
let get_cygwin_variant ~cygbin cmd = |
| 1257 |
(* Treat MSYS2's variant of `cygwin1.dll` called `msys-2.0.dll` equivalently. |
| 1258 |
Confer https://www.msys2.org/wiki/How-does-MSYS2-differ-from-Cygwin/ *) |
| 1259 |
match get_windows_executable_variant ~cygbin cmd with |
| 1260 |
| `Native -> `Native |
| 1261 |
| `Cygwin |
| 1262 |
| `Msys2 -> `Cygwin |
| 1263 |
| `Tainted _ -> `CygLinked |
| 1264 |
|
| 1265 |
let is_cygwin_variant ~cygbin cmd = |
| 1266 |
get_cygwin_variant ~cygbin cmd = `Cygwin |
| 1267 |
|
| 1268 |
exception Exit of int |
| 1269 |
exception Exec of string * string array * string array |
| 1270 |
|
| 1271 |
let exit i = raise (Exit i) |
| 1272 |
|
| 1273 |
type exit_reason = |
| 1274 |
[ `Success | `False | `Bad_arguments | `Not_found | `Aborted | `Locked |
| 1275 |
| `No_solution | `File_error | `Package_operation_error | `Sync_error |
| 1276 |
| `Configuration_error | `Solver_failure | `Internal_error |
| 1277 |
| `User_interrupt ] |
| 1278 |
|
| 1279 |
let exit_codes : (exit_reason * int) list = [ |
| 1280 |
(* Normal return values *) |
| 1281 |
`Success, 0; |
| 1282 |
`False, 1; |
| 1283 |
(* Errors happening in normal use (user related, or impossible requests) *) |
| 1284 |
`Bad_arguments, 2; |
| 1285 |
`Not_found, 5; |
| 1286 |
`Aborted, 10; |
| 1287 |
`Locked, 15; |
| 1288 |
`No_solution, 20; |
| 1289 |
(* Errors related to the database (repository and package definitions) *) |
| 1290 |
`File_error, 30; |
| 1291 |
`Package_operation_error, 31; |
| 1292 |
(* Network related error *) |
| 1293 |
`Sync_error, 40; |
| 1294 |
(* Opam setup error *) |
| 1295 |
`Configuration_error, 50; |
| 1296 |
(* Errors that shouldn't happen and are likely bugs *) |
| 1297 |
`Solver_failure, 60; |
| 1298 |
`Internal_error, 99; |
| 1299 |
(* Received signals *) |
| 1300 |
`User_interrupt, 130; |
| 1301 |
] |
| 1302 |
|
| 1303 |
let get_exit_code reason = OpamList.assoc OpamCompare.equal reason exit_codes |
| 1304 |
|
| 1305 |
let exit_because reason = exit (get_exit_code reason) |
| 1306 |
|
| 1307 |
type nonrec warning_printer = warning_printer = |
| 1308 |
{mutable warning : 'a . ('a, unit, string, unit) format4 -> 'a} |
| 1309 |
|
| 1310 |
let set_warning_printer = |
| 1311 |
let called = ref false in |
| 1312 |
fun printer -> |
| 1313 |
if !called then invalid_arg "Just what do you think you're doing, Dave?"; |
| 1314 |
called := true; |
| 1315 |
console := printer |
| 1316 |
end |
| 1317 |
|
| 1318 |
|
| 1319 |
module Win32 = struct |
| 1320 |
module RegistryHive = struct |
| 1321 |
let to_string = function |
| 1322 |
| OpamStubs.HKEY_CLASSES_ROOT -> "HKEY_CLASSES_ROOT" |
| 1323 |
| OpamStubs.HKEY_CURRENT_CONFIG -> "HKEY_CURRENT_CONFIG" |
| 1324 |
| OpamStubs.HKEY_CURRENT_USER -> "HKEY_CURRENT_USER" |
| 1325 |
| OpamStubs.HKEY_LOCAL_MACHINE -> "HKEY_LOCAL_MACHINE" |
| 1326 |
| OpamStubs.HKEY_USERS -> "HKEY_USERS" |
| 1327 |
|
| 1328 |
let of_string = function |
| 1329 |
| "HKCR" |
| 1330 |
| "HKEY_CLASSES_ROOT" -> OpamStubs.HKEY_CLASSES_ROOT |
| 1331 |
| "HKCC" |
| 1332 |
| "HKEY_CURRENT_CONFIG" -> OpamStubs.HKEY_CURRENT_CONFIG |
| 1333 |
| "HKCU" |
| 1334 |
| "HKEY_CURRENT_USER" -> OpamStubs.HKEY_CURRENT_USER |
| 1335 |
| "HKLM" |
| 1336 |
| "HKEY_LOCAL_MACHINE" -> OpamStubs.HKEY_LOCAL_MACHINE |
| 1337 |
| "HKU" |
| 1338 |
| "HKEY_USERS" -> OpamStubs.HKEY_USERS |
| 1339 |
| _ -> failwith "RegistryHive.of_string" |
| 1340 |
end |
| 1341 |
|
| 1342 |
let (set_parent_pid, parent_putenv) = |
| 1343 |
let ppid = ref (OpamCompat.Lazy.map (function (_::(pid, _)::_) -> pid | _ -> 0l) OpamSys.windows_process_ancestry) in |
| 1344 |
let parent_putenv = lazy ( |
| 1345 |
let {contents = lazy ppid} = ppid in |
| 1346 |
let our_architecture = OpamStubs.getProcessArchitecture None in |
| 1347 |
let their_architecture = OpamStubs.getProcessArchitecture (Some ppid) in |
| 1348 |
let no_opam_putenv = |
| 1349 |
let warning = lazy ( |
| 1350 |
!console.warning "opam-putenv was not found - \ |
| 1351 |
OPAM is unable to alter environment variables"; |
| 1352 |
false) |
| 1353 |
in |
| 1354 |
fun _ _ -> Lazy.force warning |
| 1355 |
in |
| 1356 |
if our_architecture <> their_architecture then |
| 1357 |
match their_architecture with |
| 1358 |
| OpamStubs.ARM | ARM64 | IA64 | Unknown -> |
| 1359 |
(* ARM support not yet implemented - just ensure we don't inject Intel |
| 1360 |
code into an ARM process! *) |
| 1361 |
no_opam_putenv |
| 1362 |
| AMD64 | Intel -> |
| 1363 |
(* |
| 1364 |
* Expect to see opam-putenv.exe in the same directory as opam.exe, |
| 1365 |
* rather than PATH (allow for crazy users like developers who may have |
| 1366 |
* both builds of opam) |
| 1367 |
*) |
| 1368 |
let putenv_exe = |
| 1369 |
Filename.(concat (dirname Sys.executable_name) "opam-putenv.exe") |
| 1370 |
in |
| 1371 |
let ctrl = ref stdout in |
| 1372 |
let quit_putenv () = |
| 1373 |
if !ctrl <> stdout then |
| 1374 |
let () = Printf.fprintf !ctrl "::QUIT\n%!" in |
| 1375 |
ctrl := stdout |
| 1376 |
in |
| 1377 |
at_exit quit_putenv; |
| 1378 |
if Sys.file_exists putenv_exe then |
| 1379 |
fun key value -> |
| 1380 |
if !ctrl = stdout then begin |
| 1381 |
let (inCh, outCh) = Unix.pipe () in |
| 1382 |
let _ = |
| 1383 |
Unix.create_process putenv_exe |
| 1384 |
[| putenv_exe; Int32.to_string ppid |] |
| 1385 |
inCh Unix.stdout Unix.stderr |
| 1386 |
in |
| 1387 |
ctrl := (Unix.out_channel_of_descr outCh); |
| 1388 |
set_binary_mode_out !ctrl true; |
| 1389 |
end; |
| 1390 |
Printf.fprintf !ctrl "%s\n%s\n%!" key value; |
| 1391 |
if key = "::QUIT" then ctrl := stdout; |
| 1392 |
true |
| 1393 |
else |
| 1394 |
no_opam_putenv |
| 1395 |
else |
| 1396 |
function "::QUIT" -> fun _ -> true |
| 1397 |
| key -> OpamStubs.process_putenv ppid key) |
| 1398 |
in |
| 1399 |
((fun pid -> |
| 1400 |
if Lazy.is_val parent_putenv then |
| 1401 |
failwith "Target parent already known"; |
| 1402 |
ppid := Lazy.from_val pid), |
| 1403 |
(fun key -> (Lazy.force parent_putenv) key)) |
| 1404 |
|
| 1405 |
let persistHomeDirectory dir = |
| 1406 |
(* Update our environment *) |
| 1407 |
Unix.putenv "HOME" dir; |
| 1408 |
(* Update our parent's environment *) |
| 1409 |
ignore (parent_putenv "HOME" dir); |
| 1410 |
(* Persist the value to the user's environment *) |
| 1411 |
OpamStubs.(writeRegistry HKEY_CURRENT_USER "Environment" "HOME" REG_SZ dir); |
| 1412 |
(* Broadcast the change (or a reboot would be required) *) |
| 1413 |
(* These constants are defined in WinUser.h *) |
| 1414 |
let hWND_BROADCAST = 0xffffn in |
| 1415 |
let sMTO_ABORTIFHUNG = 0x2 in |
| 1416 |
OpamStubs.(sendMessageTimeout hWND_BROADCAST 5000 sMTO_ABORTIFHUNG |
| 1417 |
WM_SETTINGCHANGE 0 "Environment") |> ignore |
| 1418 |
end |
| 1419 |
|
| 1420 |
|
| 1421 |
module OpamFormat = struct |
| 1422 |
|
| 1423 |
let visual_length_substring s ofs len = |
| 1424 |
let rec aux acc i = |
| 1425 |
if i >= len then acc |
| 1426 |
else match s.[ofs + i] with |
| 1427 |
| '\xc2'..'\xdf' -> aux (acc - min 1 (len - i)) (i + 2) |
| 1428 |
| '\xe0'..'\xef' -> aux (acc - min 2 (len - i)) (i + 3) |
| 1429 |
| '\xf0'..'\xf4' -> aux (acc - min 3 (len - i)) (i + 4) |
| 1430 |
| '\027' -> |
| 1431 |
(try |
| 1432 |
let j = String.index_from s (ofs+i+1) 'm' - ofs in |
| 1433 |
if j > len then acc - (len - i) else |
| 1434 |
aux (acc - (j - i + 1)) (j + 1) |
| 1435 |
with Not_found | Invalid_argument _ -> |
| 1436 |
acc - (len - i)) |
| 1437 |
| _ -> aux acc (i + 1) |
| 1438 |
in |
| 1439 |
aux len 0 |
| 1440 |
|
| 1441 |
let visual_length s = visual_length_substring s 0 (String.length s) |
| 1442 |
|
| 1443 |
let visual_width s = |
| 1444 |
List.fold_left max 0 (List.map visual_length (OpamString.split s '\n')) |
| 1445 |
|
| 1446 |
let cut_at_visual s width = |
| 1447 |
let rec aux extra i = |
| 1448 |
try |
| 1449 |
let j = String.index_from s i '\027' in |
| 1450 |
let k = String.index_from s (j+1) 'm' in |
| 1451 |
if j - extra > width then width + extra |
| 1452 |
else aux (extra + k - j + 1) (k + 1) |
| 1453 |
with Not_found -> min (String.length s) (width + extra) |
| 1454 |
| Invalid_argument _ -> String.length s |
| 1455 |
in |
| 1456 |
let cut_at = aux 0 0 in |
| 1457 |
if cut_at = String.length s then s else |
| 1458 |
let sub = String.sub s 0 cut_at in |
| 1459 |
let rec rem_escapes i = |
| 1460 |
try |
| 1461 |
let j = String.index_from s i '\027' in |
| 1462 |
let k = String.index_from s (j+1) 'm' in |
| 1463 |
String.sub s j (k - j + 1) :: rem_escapes (k+1) |
| 1464 |
with Not_found | Invalid_argument _ -> [] |
| 1465 |
in |
| 1466 |
String.concat "" (sub :: rem_escapes cut_at) |
| 1467 |
|
| 1468 |
let indent_left s ?(visual=s) nb = |
| 1469 |
let nb = nb - String.length visual in |
| 1470 |
if nb <= 0 then |
| 1471 |
s |
| 1472 |
else |
| 1473 |
s ^ String.make nb ' ' |
| 1474 |
|
| 1475 |
let indent_right s ?(visual=s) nb = |
| 1476 |
let nb = nb - String.length visual in |
| 1477 |
if nb <= 0 then |
| 1478 |
s |
| 1479 |
else |
| 1480 |
String.make nb ' ' ^ s |
| 1481 |
|
| 1482 |
let align_table ll = |
| 1483 |
let rec transpose ll = |
| 1484 |
if List.for_all ((=) []) ll then [] else |
| 1485 |
let col, rest = |
| 1486 |
List.fold_left (fun (col,rest) -> function |
| 1487 |
| hd::tl -> hd::col, tl::rest |
| 1488 |
| [] -> ""::col, []::rest) |
| 1489 |
([],[]) ll |
| 1490 |
in |
| 1491 |
List.rev col::transpose (List.rev rest) |
| 1492 |
in |
| 1493 |
let columns = transpose ll in |
| 1494 |
let pad n s = |
| 1495 |
let sn = visual_length s in |
| 1496 |
if sn >= n then s |
| 1497 |
else s ^ (String.make (n - sn) ' ') |
| 1498 |
in |
| 1499 |
let pad_multi n s = |
| 1500 |
match OpamString.split s '\n' with |
| 1501 |
| [] | [_] -> pad n s ^"\n" |
| 1502 |
| ls -> String.concat "\n" (List.map (pad n) ls) |
| 1503 |
in |
| 1504 |
let align sl = |
| 1505 |
let (len, multiline) = |
| 1506 |
List.fold_left (fun (len,ml) s -> |
| 1507 |
if String.contains s '\n' then max len (visual_width s), true |
| 1508 |
else max len (visual_length s), ml) |
| 1509 |
(0, false) sl |
| 1510 |
in |
| 1511 |
List.map (if multiline then pad_multi len else pad len) sl |
| 1512 |
in |
| 1513 |
let rec map_but_last f = function |
| 1514 |
| ([] | [_]) as l -> l |
| 1515 |
| x::r -> f x :: map_but_last f r |
| 1516 |
in |
| 1517 |
transpose (map_but_last align columns) |
| 1518 |
|
| 1519 |
let reformat |
| 1520 |
?(start_column=0) ?(indent=0) ?(width=OpamSys.terminal_columns ()) s = |
| 1521 |
let slen = String.length s in |
| 1522 |
let buf = Buffer.create 1024 in |
| 1523 |
let rec find_nonsp i = |
| 1524 |
if i >= slen then i else |
| 1525 |
match s.[i] with ' ' -> find_nonsp (i+1) | _ -> i |
| 1526 |
in |
| 1527 |
let rec find_split i = |
| 1528 |
if i >= slen then i else |
| 1529 |
match s.[i] with ' ' | '\n' -> i | _ -> find_split (i+1) |
| 1530 |
in |
| 1531 |
let newline i = |
| 1532 |
Buffer.add_char buf '\n'; |
| 1533 |
if i+1 < slen && s.[i+1] <> '\n' then |
| 1534 |
for _i = 1 to indent do Buffer.add_char buf ' ' done |
| 1535 |
in |
| 1536 |
let rec print i col = |
| 1537 |
if i >= slen then () else |
| 1538 |
if s.[i] = '\n' then (newline i; print (i+1) indent) else |
| 1539 |
let j = find_nonsp i in |
| 1540 |
let k = find_split j in |
| 1541 |
let len_visual = visual_length_substring s i (k - i) in |
| 1542 |
if col + len_visual >= width && col > indent then |
| 1543 |
(newline i; |
| 1544 |
Buffer.add_substring buf s j (k - j); |
| 1545 |
print k (indent + len_visual - j + i)) |
| 1546 |
else |
| 1547 |
(Buffer.add_substring buf s i (k - i); |
| 1548 |
print k (col + len_visual)) |
| 1549 |
in |
| 1550 |
print 0 start_column; |
| 1551 |
Buffer.contents buf |
| 1552 |
|
| 1553 |
let itemize ?(bullet=" - ") f = |
| 1554 |
let indent = visual_length bullet in |
| 1555 |
OpamList.concat_map ~left:bullet ~right:"\n" ~nil:"" ("\n"^bullet) |
| 1556 |
(fun s -> reformat ~start_column:indent ~indent (f s)) |
| 1557 |
|
| 1558 |
let rec pretty_list ?(last="and") = function |
| 1559 |
| [] -> "" |
| 1560 |
| [a] -> a |
| 1561 |
| [a;b] -> Printf.sprintf "%s %s %s" a last b |
| 1562 |
| h::t -> Printf.sprintf "%s, %s" h (pretty_list ~last t) |
| 1563 |
|
| 1564 |
let as_aligned_table ?(width=OpamSys.terminal_columns ()) l = |
| 1565 |
let itlen = |
| 1566 |
List.fold_left (fun acc s -> max acc (visual_length s)) |
| 1567 |
0 l |
| 1568 |
in |
| 1569 |
let by_line = (width + 1) / (itlen + 1) in |
| 1570 |
if by_line <= 1 then |
| 1571 |
List.map (fun x -> [x]) l |
| 1572 |
else |
| 1573 |
let rec aux rline n = function |
| 1574 |
| [] -> [List.rev rline] |
| 1575 |
| x::r as line -> |
| 1576 |
if n = 0 then List.rev rline :: aux [] by_line line |
| 1577 |
else aux (x :: rline) (n-1) r |
| 1578 |
in |
| 1579 |
align_table (aux [] by_line l) |
| 1580 |
|
| 1581 |
end |
| 1582 |
|
| 1583 |
|
| 1584 |
module Exn = struct |
| 1585 |
|
| 1586 |
let fatal = fatal |
| 1587 |
|
| 1588 |
let register_backtrace, get_backtrace = |
| 1589 |
let registered_backtrace = ref None in |
| 1590 |
(fun e -> |
| 1591 |
registered_backtrace := |
| 1592 |
match !registered_backtrace with |
| 1593 |
| Some (e1, _) as reg when e1 == e -> reg |
| 1594 |
| _ -> Some (e, Printexc.get_backtrace ())), |
| 1595 |
(fun e -> |
| 1596 |
match !registered_backtrace with |
| 1597 |
| Some(e1,bt) when e1 == e -> bt |
| 1598 |
| _ -> Printexc.get_backtrace ()) |
| 1599 |
|
| 1600 |
let pretty_backtrace e = |
| 1601 |
match get_backtrace e with |
| 1602 |
| "" -> "" |
| 1603 |
| b -> |
| 1604 |
let b = |
| 1605 |
OpamFormat.itemize ~bullet:" " (fun x -> x) (OpamString.split b '\n') |
| 1606 |
in |
| 1607 |
Printf.sprintf "Backtrace:\n%s" b |
| 1608 |
|
| 1609 |
let finalise e f = |
| 1610 |
let bt = Printexc.get_raw_backtrace () in |
| 1611 |
f (); |
| 1612 |
Printexc.raise_with_backtrace e bt |
| 1613 |
|
| 1614 |
let finally f k = |
| 1615 |
match k () with |
| 1616 |
| r -> f (); r |
| 1617 |
| exception e -> finalise e f |
| 1618 |
end |
| 1619 |
|
| 1620 |
|
| 1621 |
module Op = struct |
| 1622 |
|
| 1623 |
let (@@) f x = f x |
| 1624 |
|
| 1625 |
let (|>) x f = f x |
| 1626 |
|
| 1627 |
let (@*) g f x = g (f x) |
| 1628 |
|
| 1629 |
let (@>) f g x = g (f x) |
| 1630 |
|
| 1631 |
end |
| 1632 |
|
| 1633 |
|
| 1634 |
module Config = struct |
| 1635 |
|
| 1636 |
module type Sig = sig |
| 1637 |
|
| 1638 |
type t |
| 1639 |
type 'a options_fun |
| 1640 |
|
| 1641 |
val default: t |
| 1642 |
val set: t -> (unit -> t) options_fun |
| 1643 |
val setk: (t -> 'a) -> t -> 'a options_fun |
| 1644 |
val r: t ref |
| 1645 |
val update: ?noop:_ -> (unit -> unit) options_fun |
| 1646 |
val init: ?noop:_ -> (unit -> unit) options_fun |
| 1647 |
val initk: 'a -> 'a options_fun |
| 1648 |
|
| 1649 |
end |
| 1650 |
|
| 1651 |
type env_var = string |
| 1652 |
|
| 1653 |
type when_ = [ `Always | `Never | `Auto ] |
| 1654 |
type when_ext = [ `Extended | when_ ] |
| 1655 |
type answer = [ `unsafe_yes | `all_yes | `all_no | `ask ] |
| 1656 |
type yes_answer = [ `unsafe_yes | `all_yes ] |
| 1657 |
|
| 1658 |
let env conv var = |
| 1659 |
try Option.map conv (Env.getopt ("OPAM"^var)) |
| 1660 |
with Failure _ -> |
| 1661 |
flush stdout; |
| 1662 |
!console.warning |
| 1663 |
"Invalid value for environment variable OPAM%s, ignored." var; |
| 1664 |
None |
| 1665 |
|
| 1666 |
let bool_of_string s = |
| 1667 |
match String.lowercase_ascii s with |
| 1668 |
| "" | "0" | "no" | "false" -> Some false |
| 1669 |
| "1" | "yes" | "true" -> Some true |
| 1670 |
| _ -> None |
| 1671 |
|
| 1672 |
let bool s = |
| 1673 |
match bool_of_string s with |
| 1674 |
| Some s -> s |
| 1675 |
| None -> failwith "env_bool" |
| 1676 |
|
| 1677 |
let env_bool var = env bool var |
| 1678 |
|
| 1679 |
let env_int var = env int_of_string var |
| 1680 |
|
| 1681 |
type level = int |
| 1682 |
let env_level var = |
| 1683 |
env (function s -> |
| 1684 |
if s = "" then 0 else |
| 1685 |
match bool_of_string s with |
| 1686 |
| Some true -> 0 |
| 1687 |
| Some false -> 1 |
| 1688 |
| None -> int_of_string s) |
| 1689 |
var |
| 1690 |
|
| 1691 |
type sections = int option OpamString.Map.t |
| 1692 |
let env_sections var = |
| 1693 |
env (fun s -> |
| 1694 |
let f map elt = |
| 1695 |
let parse_value (section, value) = |
| 1696 |
try |
| 1697 |
(section, Some (int_of_string value)) |
| 1698 |
with Failure _ -> |
| 1699 |
(section, None) |
| 1700 |
in |
| 1701 |
let (section, level) = |
| 1702 |
Option.map_default parse_value (elt, None) (OpamString.cut_at elt ':') |
| 1703 |
in |
| 1704 |
OpamString.Map.add section level map |
| 1705 |
in |
| 1706 |
List.fold_left f OpamString.Map.empty (OpamString.split s ' ')) var |
| 1707 |
|
| 1708 |
let env_string var = |
| 1709 |
env (fun s -> s) var |
| 1710 |
|
| 1711 |
let env_float var = |
| 1712 |
env float_of_string var |
| 1713 |
|
| 1714 |
let when_ext s = |
| 1715 |
match String.lowercase_ascii s with |
| 1716 |
| "extended" -> `Extended |
| 1717 |
| "always" -> `Always |
| 1718 |
| "never" -> `Never |
| 1719 |
| "auto" -> `Auto |
| 1720 |
| _ -> failwith "env_when" |
| 1721 |
|
| 1722 |
let env_when_ext var = env when_ext var |
| 1723 |
|
| 1724 |
let env_when var = |
| 1725 |
env (fun v -> match when_ext v with |
| 1726 |
| (`Always | `Never | `Auto) as w -> w |
| 1727 |
| `Extended -> failwith "env_when") |
| 1728 |
var |
| 1729 |
|
| 1730 |
let resolve_when ~auto = function |
| 1731 |
| `Always -> true |
| 1732 |
| `Never -> false |
| 1733 |
| `Auto -> Lazy.force auto |
| 1734 |
|
| 1735 |
let answer s = |
| 1736 |
match String.lowercase_ascii s with |
| 1737 |
| "ask" -> `ask |
| 1738 |
| "yes" -> `all_yes |
| 1739 |
| "no" -> `all_no |
| 1740 |
| "unsafe-yes" -> `unsafe_yes |
| 1741 |
| _ -> failwith "env_answer" |
| 1742 |
|
| 1743 |
let env_answer = |
| 1744 |
env (fun s -> |
| 1745 |
try if bool s then `all_yes else `all_no |
| 1746 |
with Failure _ -> answer s) |
| 1747 |
|
| 1748 |
|
| 1749 |
module E = struct |
| 1750 |
type t = .. |
| 1751 |
type t += REMOVED |
| 1752 |
let (r : t list ref) = ref [] |
| 1753 |
|
| 1754 |
let update v = r := v :: !r |
| 1755 |
let updates l = r := l @ !r |
| 1756 |
|
| 1757 |
let find var = OpamList.find_map var !r |
| 1758 |
let value_t var = try Some (find var) with Not_found -> None |
| 1759 |
let value var = |
| 1760 |
let l = lazy (value_t var) in |
| 1761 |
fun () -> Lazy.force l |
| 1762 |
end |
| 1763 |
|
| 1764 |
end |
| 1765 |
|
| 1766 |
module List = OpamList |
| 1767 |
module String = OpamString |
| 1768 |
module Sys = OpamSys |
| 1769 |
module Format = OpamFormat |
| 1770 |
module Compare = OpamCompare |