This repository has been archived by the owner on Jun 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
/
common.ml
1244 lines (1013 loc) · 36.4 KB
/
common.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Yoann Padioleau
*
* Copyright (C) 1998-2013 Yoann Padioleau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file license.txt.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
* license.txt for more details.
*)
(*###########################################################################*)
(* Prelude *)
(*###########################################################################*)
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* The following functions should be in their respective sections but
* because some functions in some sections use functions in other
* sections, and because I don't want to take care of the order of
* those sections, of those dependencies, I put the functions causing
* dependency problem here. C is better than OCaml on this with the
* ability to declare prototypes, enabling some form of forward
* reference.
*)
let (+>) o f = f o
let spf = Printf.sprintf
exception Timeout
exception UnixExit of int
let rec drop n xs =
match (n,xs) with
| (0,_) -> xs
| (_,[]) -> failwith "drop: not enough"
| (n,x::xs) -> drop (n-1) xs
let take n xs =
let rec next n xs acc =
match (n,xs) with
| (0,_) -> List.rev acc
| (_,[]) -> failwith "Common.take: not enough"
| (n,x::xs) -> next (n-1) xs (x::acc) in
next n xs []
let rec enum_orig x n =
if x = n then [n] else x::enum_orig (x+1) n
let enum x n =
if not(x <= n)
then failwith (Printf.sprintf "bad values in enum, expect %d <= %d" x n);
let rec enum_aux acc x n =
if x = n then n::acc else enum_aux (x::acc) (x+1) n
in
List.rev (enum_aux [] x n)
let push v l =
l := v :: !l
let debugger = ref false
let unwind_protect f cleanup =
if !debugger then f () else
try f ()
with e -> begin cleanup e; raise e end
let finalize f cleanup =
(* bug: we can not just call f in debugger mode because
* this change the semantic of the program. I originally
* put this code below:
* if !debugger then f () else
* because I wanted some errors to pop-out to the top so I can
* debug them but because now I use save_excursion and finalize
* quite a lot this changes too much the semantic.
* TODO: maybe I should not use save_excursion so much ? maybe
* -debugger helps see code that I should refactor ?
*)
try
let res = f () in
cleanup ();
res
with e ->
cleanup ();
raise e
let (unlines: string list -> string) = fun s ->
(String.concat "\n" s) ^ "\n"
let (lines: string -> string list) = fun s ->
let rec lines_aux = function
| [] -> []
| [x] -> if x = "" then [] else [x]
| x::xs ->
x::lines_aux xs
in
Str.split_delim (Str.regexp "\n") s +> lines_aux
let save_excursion reference newv f =
let old = !reference in
reference := newv;
finalize f (fun _ -> reference := old;)
let memoized ?(use_cache=true) h k f =
if not use_cache
then f ()
else
try Hashtbl.find h k
with Not_found ->
let v = f () in
begin
Hashtbl.add h k v;
v
end
exception Todo
exception Impossible
exception Multi_found (* to be consistent with Not_found *)
let exn_to_s exn =
Printexc.to_string exn
(*###########################################################################*)
(* Basic features *)
(*###########################################################################*)
(*****************************************************************************)
(* Debugging/logging *)
(*****************************************************************************)
let pr s =
print_string s;
print_string "\n";
flush stdout
let pr2 s =
prerr_string s;
prerr_string "\n";
flush stderr
let pr_xxxxxxxxxxxxxxxxx () =
pr "-----------------------------------------------------------------------"
let pr2_xxxxxxxxxxxxxxxxx () =
pr2 "-----------------------------------------------------------------------"
let _already_printed = Hashtbl.create 101
let disable_pr2_once = ref false
let xxx_once f s =
if !disable_pr2_once then pr2 s
else
if not (Hashtbl.mem _already_printed s)
then begin
Hashtbl.add _already_printed s true;
f ("(ONCE) " ^ s);
end
let pr2_once s = xxx_once pr2 s
(* start of dumper.ml *)
(* Dump an OCaml value into a printable string.
* By Richard W.M. Jones (rich@annexia.org).
* dumper.ml 1.2 2005/02/06 12:38:21 rich Exp
*)
open Printf
open Obj
let rec dump2 r =
if is_int r then
string_of_int (magic r : int)
else ( (* Block. *)
let rec get_fields acc = function
| 0 -> acc
| n -> let n = n-1 in get_fields (field r n :: acc) n
in
let rec is_list r =
if is_int r then (
if (magic r : int) = 0 then true (* [] *)
else false
) else (
let s = size r and t = tag r in
if t = 0 && s = 2 then is_list (field r 1) (* h :: t *)
else false
)
in
let rec get_list r =
if is_int r then []
else let h = field r 0 and t = get_list (field r 1) in h :: t
in
let opaque name =
(* XXX In future, print the address of value 'r'. Not possible in
* pure OCaml at the moment.
*)
"<" ^ name ^ ">"
in
let s = size r and t = tag r in
(* From the tag, determine the type of block. *)
if is_list r then ( (* List. *)
let fields = get_list r in
"[" ^ String.concat "; " (List.map dump2 fields) ^ "]"
)
else if t = 0 then ( (* Tuple, array, record. *)
let fields = get_fields [] s in
"(" ^ String.concat ", " (List.map dump2 fields) ^ ")"
)
(* Note that [lazy_tag .. forward_tag] are < no_scan_tag. Not
* clear if very large constructed values could have the same
* tag. XXX *)
else if t = lazy_tag then opaque "lazy"
else if t = closure_tag then opaque "closure"
else if t = object_tag then ( (* Object. *)
let fields = get_fields [] s in
let clasz, id, slots =
match fields with h::h'::t -> h, h', t | _ -> assert false in
(* No information on decoding the class (first field). So just print
* out the ID and the slots.
*)
"Object #" ^ dump2 id ^
" (" ^ String.concat ", " (List.map dump2 slots) ^ ")"
)
else if t = infix_tag then opaque "infix"
else if t = forward_tag then opaque "forward"
else if t < no_scan_tag then ( (* Constructed value. *)
let fields = get_fields [] s in
"Tag" ^ string_of_int t ^
" (" ^ String.concat ", " (List.map dump2 fields) ^ ")"
)
else if t = string_tag then (
"\"" ^ String.escaped (magic r : string) ^ "\""
)
else if t = double_tag then (
string_of_float (magic r : float)
)
else if t = abstract_tag then opaque "abstract"
else if t = custom_tag then opaque "custom"
else if t = final_tag then opaque "final"
else failwith ("dump: impossible tag (" ^ string_of_int t ^ ")")
)
let dump v = dump2 (repr v)
(* end of dumper.ml *)
(*
let (dump : 'a -> string) = fun x ->
Dumper.dump x
*)
let pr2_gen x = pr2 (dump x)
(*****************************************************************************)
(* Profiling *)
(*****************************************************************************)
type prof = PALL | PNONE | PSOME of string list
let profile = ref PNONE
let show_trace_profile = ref false
let check_profile category =
match !profile with
PALL -> true
| PNONE -> false
| PSOME l -> List.mem category l
let _profile_table = ref (Hashtbl.create 100)
let adjust_profile_entry category difftime =
let (xtime, xcount) =
(try Hashtbl.find !_profile_table category
with Not_found ->
let xtime = ref 0.0 in
let xcount = ref 0 in
Hashtbl.add !_profile_table category (xtime, xcount);
(xtime, xcount)
) in
xtime := !xtime +. difftime;
xcount := !xcount + 1;
()
let profile_start category = failwith "todo"
let profile_end category = failwith "todo"
(* subtil: don't forget to give all argumens to f, otherwise partial app
* and will profile nothing.
*
* todo: try also detect when complexity augment each time, so can
* detect the situation for a function gets worse and worse ?
*)
let profile_code category f =
if not (check_profile category)
then f ()
else begin
if !show_trace_profile then pr2 (spf "> %s" category);
let t = Unix.gettimeofday () in
let res, prefix =
try Some (f ()), ""
with Timeout -> None, "*"
in
let category = prefix ^ category in (* add a '*' to indicate timeout func *)
let t' = Unix.gettimeofday () in
if !show_trace_profile then pr2 (spf "< %s" category);
adjust_profile_entry category (t' -. t);
(match res with
| Some res -> res
| None -> raise Timeout
);
end
let _is_in_exclusif = ref (None: string option)
let profile_code_exclusif category f =
if not (check_profile category)
then f ()
else begin
match !_is_in_exclusif with
| Some s ->
failwith (spf "profile_code_exclusif: %s but already in %s " category s);
| None ->
_is_in_exclusif := (Some category);
finalize
(fun () ->
profile_code category f
)
(fun () ->
_is_in_exclusif := None
)
end
let profile_code_inside_exclusif_ok category f =
failwith "Todo"
let (with_open_stringbuf: (((string -> unit) * Buffer.t) -> unit) -> string) =
fun f ->
let buf = Buffer.create 1000 in
let pr s = Buffer.add_string buf (s ^ "\n") in
f (pr, buf);
Buffer.contents buf
(* todo: also put % ? also add % to see if coherent numbers *)
let profile_diagnostic () =
if !profile = PNONE then "" else
let xs =
Hashtbl.fold (fun k v acc -> (k,v)::acc) !_profile_table []
+> List.sort (fun (k1, (t1,n1)) (k2, (t2,n2)) -> compare t2 t1)
in
with_open_stringbuf (fun (pr,_) ->
pr "---------------------";
pr "profiling result";
pr "---------------------";
xs +> List.iter (fun (k, (t,n)) ->
pr (Printf.sprintf "%-40s : %10.3f sec %10d count" k !t !n)
)
)
let report_if_take_time timethreshold s f =
let t = Unix.gettimeofday () in
let res = f () in
let t' = Unix.gettimeofday () in
if (t' -. t > float_of_int timethreshold)
then pr2 (Printf.sprintf "Note: processing took %7.1fs: %s" (t' -. t) s);
res
let profile_code2 category f =
profile_code category (fun () ->
if !profile = PALL
then pr2 ("starting: " ^ category);
let t = Unix.gettimeofday () in
let res = f () in
let t' = Unix.gettimeofday () in
if !profile = PALL
then pr2 (spf "ending: %s, %fs" category (t' -. t));
res
)
(*****************************************************************************)
(* Test *)
(*****************************************************************************)
(* See OUnit *)
(*****************************************************************************)
(* Persistence *)
(*****************************************************************************)
let get_value filename =
let chan = open_in filename in
let x = input_value chan in (* <=> Marshal.from_channel *)
(close_in chan; x)
let write_value valu filename =
let chan = open_out filename in
(output_value chan valu; (* <=> Marshal.to_channel *)
(* Marshal.to_channel chan valu [Marshal.Closures]; *)
close_out chan)
(*****************************************************************************)
(* Composition/Control *)
(*****************************************************************************)
(*****************************************************************************)
(* Error managment *)
(*****************************************************************************)
(*****************************************************************************)
(* Arguments/options and command line (cocci and acomment) *)
(*****************************************************************************)
(*
* todo? isn't unison or scott-mcpeak-lib-in-cil handles that kind of
* stuff better ? That is the need to localize command line argument
* while still being able to gathering them. Same for logging.
* Similiar to the type prof = PALL | PNONE | PSOME of string list.
* Same spirit of fine grain config in log4j ?
*
* todo? how mercurial/cvs/git manage command line options ? because they
* all have a kind of DSL around arguments with some common options,
* specific options, conventions, etc.
*
*
* todo? generate the corresponding noxxx options ?
* todo? generate list of options and show their value ?
*
* todo? make it possible to set this value via a config file ?
*
*
*)
type arg_spec_full = Arg.key * Arg.spec * Arg.doc
type cmdline_options = arg_spec_full list
(* the format is a list of triples:
* (title of section * (optional) explanation of sections * options)
*)
type options_with_title = string * string * arg_spec_full list
type cmdline_sections = options_with_title list
(* ---------------------------------------------------------------------- *)
(* now I use argv as I like at the call sites to show that
* this function internally use argv.
*)
let parse_options options usage_msg argv =
let args = ref [] in
(try
Arg.parse_argv argv options (fun file -> args := file::!args) usage_msg;
args := List.rev !args;
!args
with
| Arg.Bad msg -> Printf.eprintf "%s" msg; exit 2
| Arg.Help msg -> Printf.printf "%s" msg; exit 0
)
let usage usage_msg options =
Arg.usage (Arg.align options) usage_msg
(* for coccinelle *)
(* If you don't want the -help and --help that are appended by Arg.align *)
let arg_align2 xs =
Arg.align xs +> List.rev +> drop 2 +> List.rev
let short_usage usage_msg ~short_opt =
usage usage_msg short_opt
let long_usage usage_msg ~short_opt ~long_opt =
pr usage_msg;
pr "";
let all_options_with_title =
(("main options", "", short_opt)::long_opt) in
all_options_with_title +> List.iter
(fun (title, explanations, xs) ->
pr title;
pr_xxxxxxxxxxxxxxxxx();
if explanations <> ""
then begin pr explanations; pr "" end;
arg_align2 xs +> List.iter (fun (key,action,s) ->
pr (" " ^ key ^ s)
);
pr "";
);
()
(* copy paste of Arg.parse. Don't want the default -help msg *)
let arg_parse2 l msg short_usage_fun =
let args = ref [] in
let f = (fun file -> args := file::!args) in
let l = Arg.align l in
(try begin
Arg.parse_argv Sys.argv l f msg;
args := List.rev !args;
!args
end
with
| Arg.Bad msg -> (* eprintf "%s" msg; exit 2; *)
let xs = lines msg in
(* take only head, it's where the error msg is *)
pr2 (List.hd xs);
short_usage_fun();
raise (UnixExit (2))
| Arg.Help msg -> (* printf "%s" msg; exit 0; *)
raise Impossible (* -help is specified in speclist *)
)
(* ---------------------------------------------------------------------- *)
type flag_spec = Arg.key * Arg.spec * Arg.doc
type action_spec = Arg.key * Arg.doc * action_func
and action_func = (string list -> unit)
type cmdline_actions = action_spec list
exception WrongNumberOfArguments
let options_of_actions action_ref actions =
actions +> List.map (fun (key, doc, _func) ->
(key, (Arg.Unit (fun () -> action_ref := key)), doc)
)
let (action_list: cmdline_actions -> Arg.key list) = fun xs ->
List.map (fun (a,b,c) -> a) xs
let (do_action: Arg.key -> string list (* args *) -> cmdline_actions -> unit) =
fun key args xs ->
let assoc = xs +> List.map (fun (a,b,c) -> (a,c)) in
let action_func = List.assoc key assoc in
action_func args
(* todo? if have a function with default argument ? would like a
* mk_action_0_or_1_arg ?
*)
let mk_action_0_arg f =
(function
| [] -> f ()
| _ -> raise WrongNumberOfArguments
)
let mk_action_1_arg f =
(function
| [file] -> f file
| _ -> raise WrongNumberOfArguments
)
let mk_action_2_arg f =
(function
| [file1;file2] -> f file1 file2
| _ -> raise WrongNumberOfArguments
)
let mk_action_3_arg f =
(function
| [file1;file2;file3] -> f file1 file2 file3
| _ -> raise WrongNumberOfArguments
)
let mk_action_4_arg f =
(function
| [file1;file2;file3;file4] -> f file1 file2 file3 file4
| _ -> raise WrongNumberOfArguments
)
let mk_action_n_arg f = f
(*****************************************************************************)
(* Equality *)
(*****************************************************************************)
let (=|=) : int -> int -> bool = (=)
let (=<=) : char -> char -> bool = (=)
let (=$=) : string -> string -> bool = (=)
let (=:=) : bool -> bool -> bool = (=)
let (=*=) = (=)
(*###########################################################################*)
(* Basic types *)
(*###########################################################################*)
(*****************************************************************************)
(* Bool *)
(*****************************************************************************)
(*****************************************************************************)
(* Char *)
(*****************************************************************************)
(*****************************************************************************)
(* Num *)
(*****************************************************************************)
(*****************************************************************************)
(* Tuples *)
(*****************************************************************************)
(*****************************************************************************)
(* Maybe *)
(*****************************************************************************)
(* type 'a maybe = Just of 'a | None *)
let map_opt f = function
| None -> None
| Some x -> Some (f x)
let do_option f = function
| None -> ()
| Some x -> f x
let opt = do_option
(* not sure why but can't use let (?:) a b = ... then at use time ocaml yells*)
let (|||) a b =
match a with
| Some x -> x
| None -> b
type ('a,'b) either = Left of 'a | Right of 'b
(* with sexp *)
type ('a, 'b, 'c) either3 = Left3 of 'a | Middle3 of 'b | Right3 of 'c
(* with sexp *)
let partition_either f l =
let rec part_either left right = function
| [] -> (List.rev left, List.rev right)
| x :: l ->
(match f x with
| Left e -> part_either (e :: left) right l
| Right e -> part_either left (e :: right) l) in
part_either [] [] l
let partition_either3 f l =
let rec part_either left middle right = function
| [] -> (List.rev left, List.rev middle, List.rev right)
| x :: l ->
(match f x with
| Left3 e -> part_either (e :: left) middle right l
| Middle3 e -> part_either left (e :: middle) right l
| Right3 e -> part_either left middle (e :: right) l) in
part_either [] [] [] l
let rec filter_some = function
| [] -> []
| None :: l -> filter_some l
| Some e :: l -> e :: filter_some l
let map_filter f xs = xs +> List.map f +> filter_some
let rec find_some p = function
| [] -> raise Not_found
| x :: l ->
match p x with
| Some v -> v
| None -> find_some p l
let rec find_some_opt p = function
| [] -> None
| x :: l ->
match p x with
| Some v -> Some v
| None -> find_some_opt p l
(*****************************************************************************)
(* Regexp, can also use PCRE *)
(*****************************************************************************)
let (matched: int -> string -> string) = fun i s ->
Str.matched_group i s
let matched1 = fun s -> matched 1 s
let matched2 = fun s -> (matched 1 s, matched 2 s)
let matched3 = fun s -> (matched 1 s, matched 2 s, matched 3 s)
let matched4 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s)
let matched5 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s, matched 5 s)
let matched6 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s, matched 5 s, matched 6 s)
let matched7 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s, matched 5 s, matched 6 s, matched 7 s)
let _memo_compiled_regexp = Hashtbl.create 101
let candidate_match_func s re =
(* old: Str.string_match (Str.regexp re) s 0 *)
let compile_re =
memoized _memo_compiled_regexp re (fun () -> Str.regexp re)
in
Str.string_match compile_re s 0
let match_func s re =
profile_code "Common.=~" (fun () -> candidate_match_func s re)
let (=~) s re =
match_func s re
let split sep s = Str.split (Str.regexp sep) s
let join sep xs = String.concat sep xs
(*****************************************************************************)
(* Strings *)
(*****************************************************************************)
(* ruby *)
let i_to_s = string_of_int
let s_to_i = int_of_string
let null_string s =
s =$= ""
(*****************************************************************************)
(* Filenames *)
(*****************************************************************************)
type filename = string (* TODO could check that exist :) type sux *)
(* with sexp *)
type dirname = string (* TODO could check that exist :) type sux *)
(* with sexp *)
(* file or dir *)
type path = string
let chop_dirsymbol = function
| s when s =~ "\\(.*\\)/$" -> matched1 s
| s -> s
(* pre: prj_path must not contain regexp symbol *)
let filename_without_leading_path prj_path s =
let prj_path = chop_dirsymbol prj_path in
if s =$= prj_path
then "."
else
if s =~ ("^" ^ prj_path ^ "/\\(.*\\)$")
then matched1 s
else
failwith
(spf "cant find filename_without_project_path: %s %s" prj_path s)
let readable ~root s =
filename_without_leading_path root s
let is_directory file =
(Unix.stat file).Unix.st_kind =*= Unix.S_DIR
(*****************************************************************************)
(* Dates *)
(*****************************************************************************)
(*****************************************************************************)
(* Lines/words/strings *)
(*****************************************************************************)
(*****************************************************************************)
(* Process/Files *)
(*****************************************************************************)
let command2 s = ignore(Sys.command s)
exception CmdError of Unix.process_status * string
let process_output_to_list2 ?(verbose=false) command =
let chan = Unix.open_process_in command in
let res = ref ([] : string list) in
let rec process_otl_aux () =
let e = input_line chan in
res := e::!res;
if verbose then pr2 e;
process_otl_aux() in
try process_otl_aux ()
with End_of_file ->
let stat = Unix.close_process_in chan in (List.rev !res,stat)
let cmd_to_list ?verbose command =
let (l,exit_status) = process_output_to_list2 ?verbose command in
match exit_status with
| Unix.WEXITED 0 -> l
| _ -> raise (CmdError (exit_status,
(spf "CMD = %s, RESULT = %s"
command (String.concat "\n" l))))
let cmd_to_list_and_status = process_output_to_list2
(* tail recursive efficient version *)
let cat file =
let chan = open_in file in
let rec cat_aux acc () =
(* cant do input_line chan::aux() cos ocaml eval from right to left ! *)
let (b, l) = try (true, input_line chan) with End_of_file -> (false, "") in
if b
then cat_aux (l::acc) ()
else acc
in
cat_aux [] () +> List.rev +> (fun x -> close_in chan; x)
let read_file file =
let ic = open_in file in
let size = in_channel_length ic in
let buf = String.create size in
really_input ic buf 0 size;
close_in ic;
buf
let write_file ~file s =
let chan = open_out file in
(output_string chan s; close_out chan)
(* could be in control section too *)
let filemtime file =
(Unix.stat file).Unix.st_mtime
external c_realpath: string -> string option = "caml_realpath"
let realpath2 path =
match c_realpath path with
| Some s -> s
| None -> failwith (spf "problem with realpath on %s" path)
let realpath path =
profile_code "Common.realpath" (fun () -> realpath2 path)
(* Why a use_cache argument ? because sometimes want disable it but dont
* want put the cache_computation funcall in comment, so just easier to
* pass this extra option.
*)
let cache_computation2 ?(verbose=false) ?(use_cache=true) file ext_cache f =
if not use_cache
then f ()
else begin
if not (Sys.file_exists file)
then begin
pr2 ("WARNING: cache_computation: can't find file " ^ file);
pr2 ("defaulting to calling the function");
f ()
end else begin
let file_cache = (file ^ ext_cache) in
if Sys.file_exists file_cache &&
filemtime file_cache >= filemtime file
then begin
if verbose then pr2 ("using cache: " ^ file_cache);
get_value file_cache
end
else begin
let res = f () in
write_value res file_cache;
res
end
end
end
let cache_computation ?verbose ?use_cache a b c =
profile_code "Common.cache_computation" (fun () ->
cache_computation2 ?verbose ?use_cache a b c)
(* emacs/lisp inspiration (eric cooper and yaron minsky use that too) *)
let (with_open_outfile: filename -> (((string -> unit) * out_channel) -> 'a) -> 'a) =
fun file f ->
let chan = open_out file in
let pr s = output_string chan s in
unwind_protect (fun () ->
let res = f (pr, chan) in
close_out chan;
res)
(fun e -> close_out chan)
let (with_open_infile: filename -> ((in_channel) -> 'a) -> 'a) = fun file f ->
let chan = open_in file in
unwind_protect (fun () ->
let res = f chan in
close_in chan;
res)
(fun e -> close_in chan)
(* now in prelude:
* exception Timeout
*)
(* it seems that the toplevel block such signals, even with this explicit
* command :(
* let _ = Unix.sigprocmask Unix.SIG_UNBLOCK [Sys.sigalrm]
*)
(* could be in Control section *)
(* subtil: have to make sure that timeout is not intercepted before here, so
* avoid exn handle such as try (...) with _ -> cos timeout will not bubble up
* enough. In such case, add a case before such as
* with Timeout -> raise Timeout | _ -> ...
*
* question: can we have a signal and so exn when in a exn handler ?
*)
let timeout_function ?(verbose=false) timeoutval = fun f ->
try
begin
Sys.set_signal Sys.sigalrm (Sys.Signal_handle (fun _ -> raise Timeout ));
ignore(Unix.alarm timeoutval);
let x = f () in
ignore(Unix.alarm 0);
x
end
with Timeout ->
begin
if verbose then pr2 "timeout (we abort)";
raise Timeout;
end
| e ->
(* subtil: important to disable the alarm before relaunching the exn,
* otherwise the alarm is still running.
*
* robust?: and if alarm launched after the log (...) ?
* Maybe signals are disabled when process an exception handler ?
*)
begin
ignore(Unix.alarm 0);
(* log ("exn while in transaction (we abort too, even if ...) = " ^
Printexc.to_string e);
*)
if verbose then pr2 "exn while in timeout_function";
raise e
end
(* creation of tmp files, a la gcc *)
let _temp_files_created = ref ([] : filename list)
(* ex: new_temp_file "cocci" ".c" will give "/tmp/cocci-3252-434465.c" *)
let new_temp_file prefix suffix =
let processid = i_to_s (Unix.getpid ()) in
let tmp_file = Filename.temp_file (prefix ^ "-" ^ processid ^ "-") suffix in
push tmp_file _temp_files_created;
tmp_file
let save_tmp_files = ref false
let erase_temp_files () =
if not !save_tmp_files then begin
!_temp_files_created +> List.iter (fun s ->
(* pr2 ("erasing: " ^ s); *)
command2 ("rm -f " ^ s)
);
_temp_files_created := []
end
let erase_this_temp_file f =
if not !save_tmp_files then begin
_temp_files_created :=
List.filter (function x -> not (x =$= f)) !_temp_files_created;
command2 ("rm -f " ^ f)
end
(*###########################################################################*)
(* Collection-like types *)
(*###########################################################################*)
(*****************************************************************************)
(* List *)
(*****************************************************************************)
let exclude p xs =
List.filter (fun x -> not (p x)) xs
let rec (span: ('a -> bool) -> 'a list -> 'a list * 'a list) =
fun p -> function
| [] -> ([], [])
| x::xs ->
if p x then
let (l1, l2) = span p xs in
(x::l1, l2)
else ([], x::xs)
let rec take_safe n xs =
match (n,xs) with