text
stringlengths
0
601k
let getenv_term ( ) = let term_var = " TERM " in get_env term_var
let path_sep = if Sys . win32 then " ; " else " " :
let null_path = if Sys . win32 then " nul " else " / dev / null "
let temp_dir_name = if Sys . win32 then Filename . get_temp_dir_name ( ) else " / tmp "
let getenv_path ( ) = let path_var = " PATH " in get_env path_var
let open_in_no_fail fn = try open_in fn with e -> let e = Printexc . to_string e in Printf . fprintf stderr " Could not open_in : ' % s ' ( % s ) \ n " fn e ; exit 3
let open_in_bin_no_fail fn = try open_in_bin fn with e -> let e = Printexc . to_string e in Printf . fprintf stderr " Could not open_in_bin : ' % s ' ( % s ) \ n " fn e ; exit 3
let close_in_no_fail fn ic = try close_in ic with e -> let e = Printexc . to_string e in Printf . fprintf stderr " Could not close : ' % s ' ( % s ) \ n " fn e ; exit 3
let open_out_no_fail fn = try open_out fn with e -> let e = Printexc . to_string e in Printf . fprintf stderr " Could not open_out : ' % s ' ( % s ) \ n " fn e ; exit 3
let open_out_bin_no_fail fn = try open_out_bin fn with e -> let e = Printexc . to_string e in Printf . fprintf stderr " Could not open_out_bin : ' % s ' ( % s ) \ n " fn e ; exit 3
let close_out_no_fail fn oc = try close_out oc with e -> let e = Printexc . to_string e in Printf . fprintf stderr " Could not close : ' % s ' ( % s ) \ n " fn e ; exit 3
let cat_no_fail filename = let ic = open_in_bin_no_fail filename in let len = in_channel_length ic in let buf = Buffer . create len in Buffer . add_channel buf ic len ; let content = Buffer . contents buf in close_in_no_fail filename ic ; content
let nl_regexp = Str . regexp " [ \ r \ n ] "
let split_lines = Str . split nl_regexp
let string_contains str substring = let re = Str . regexp_string substring in try ( Str . search_forward re str 0 ) >= 0 with Not_found -> false
let exec_read cmd = let ic = Unix . open_process_in cmd in let result = input_line ic in assert ( Unix . close_process_in ic = Unix . WEXITED 0 ) ; result
let exec_read_lines ( ? reverse = false ) cmd = let ic = Unix . open_process_in cmd in let result = ref [ ] in ( try while true do result := input_line ic :: ! result done ; with End_of_file -> ( ) ) ; assert ( Unix . close_process_in ic = Unix . WEXITED 0 ) ; if not reverse then List . rev ! result else ! result
let rec rm_dir_tree path = try begin let stats = Unix . lstat path in match stats . Unix . st_kind with | Unix . S_DIR -> let contents = Sys . readdir path in List . iter ( Array . to_list contents ) ~ f ( : fun name -> let name = Filename . concat path name in rm_dir_tree name ) ; Unix . rmdir path | Unix . S_LNK | Un...
let restart ( ) = let cmd = Sys . argv . ( 0 ) in let argv = Sys . argv in Unix . execv cmd argv
let logname_impl ( ) = match getenv_user ( ) with | Some user -> user | None -> let exec_try_read cmd = let ic = Unix . open_process_in cmd in let out = try Some ( input_line ic ) with End_of_file -> None in let status = Unix . close_process_in ic in match out , status with | Some _ , Unix . WEXITED 0 -> out | _ -> Non...
let logname_ref = ref None
let logname ( ) = if ! logname_ref = None then logname_ref := Some ( logname_impl ( ) ) ; Utils . unsafe_opt ! logname_ref
let with_umask umask f = let old_umask = ref 0 in Utils . with_context ~ enter ( : fun ( ) -> old_umask := Unix . umask umask ) ~ exit ( : fun ( ) -> ignore ( Unix . umask ! old_umask ) ) ~ do_ : f
let with_umask umask f = if Sys . win32 then f ( ) else with_umask umask f
let read_stdin_to_string ( ) = let buf = Buffer . create 4096 in try while true do Buffer . add_string buf ( input_line stdin ) ; Buffer . add_char buf ' \ n ' done ; assert false with End_of_file -> Buffer . contents buf
let read_all ( ? buf_size = 4096 ) ic = let buf = Buffer . create buf_size in ( try while true do let data = Bytes . create buf_size in let bytes_read = input ic data 0 buf_size in if bytes_read = 0 then raise Exit ; Buffer . add_subbytes buf data 0 bytes_read ; done with Exit -> ( ) ) ; Buffer . contents buf
let expanduser path = Str . substitute_first ( Str . regexp " ( [ ] ) " ) ^~\\^/*\\ begin fun s -> match Str . matched_group 1 s with | " " -> begin match getenv_home ( ) with | None -> ( Unix . getpwuid ( Unix . getuid ( ) ) ) . Unix . pw_dir | Some home -> home end | unixname -> try ( Unix . getpwnam unixname ) . Uni...
let executable_path : unit -> string = let executable_path_ = ref None in let dir_sep = Filename . dir_sep . [ 0 ] in let search_path path = let paths = match getenv_path ( ) with | None -> failwith " Unable to determine executable path " | Some paths -> Str . split ( Str . regexp_string path_sep ) paths in let path = ...
let lines_of_in_channel ic = let rec loop accum = match try Some ( input_line ic ) with _e -> None with | None -> List . rev accum | Some ( line ) -> loop ( line :: accum ) in loop [ ]
let lines_of_file filename = let ic = open_in filename in try let result = lines_of_in_channel ic in let _ = close_in ic in result with _ -> close_in ic ; [ ]
let read_file file = let ic = open_in_bin 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 )
let append_file ~ file s = let chan = open_out_gen [ Open_wronly ; Open_append ; Open_creat ] 0o666 file in ( output_string chan s ; close_out chan )
let filemtime file = ( Unix . stat file ) . Unix . st_mtime
let try_touch ~ follow_symlinks file = try if follow_symlinks then Unix . utimes file 0 . 0 0 . 0 else lutimes file with _ -> ( )
let rec mkdir_p = function | " " -> failwith " Unexpected empty directory , should never happen " | d when not ( Sys . file_exists d ) -> mkdir_p ( Filename . dirname d ) ; Unix . mkdir d 0o770 ; | d when Sys . is_directory d -> ( ) | d -> raise ( NotADirectory d )
let mkdir_no_fail dir = with_umask 0 begin fun ( ) -> try Unix . mkdir dir 0o777 with Unix . Unix_error ( Unix . EEXIST , _ , _ ) -> ( ) end
let unlink_no_fail fn = try Unix . unlink fn with Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> ( )
let splitext filename = let root = Filename . chop_extension filename in let root_length = String . length root in let ext_length = String . length filename - root_length - 1 in let ext = String . sub filename ( root_length + 1 ) ext_length in root , ext
let is_test_mode ( ) = try ignore @@ Sys . getenv " HH_TEST_MODE " ; true with _ -> false
let symlink = let win32_symlink source dest = write_file ~ file : dest source in if Sys . win32 then win32_symlink else fun source dest -> Unix . symlink source dest
let make_link_of_timestamped linkname = let open Unix in let dir = Filename . dirname linkname in mkdir_no_fail dir ; let base = Filename . basename linkname in let base , ext = splitext base in let dir = Filename . concat dir ( Printf . sprintf " % ss " ext ) in mkdir_no_fail dir ; let tm = localtime ( time ( ) ) in l...
let setsid = if Sys . win32 then Unix . getpid else Unix . setsid
let set_signal = if not Sys . win32 then Sys . set_signal else ( fun _ _ -> ( ) )
let signal = if not Sys . win32 then ( fun a b -> ignore ( Sys . signal a b ) ) else ( fun _ _ -> ( ) )
let total_ram = get_total_ram ( )
let nbr_procs = nproc ( ) " hh_set_priorities " " handle_of_pid_for_termination "
let terminate_process pid = Unix . kill pid Sys . sigkill
let lstat path = Unix . lstat @@ if Sys . win32 && String_utils . string_ends_with path Filename . dir_sep then String . sub path 0 ( String . length path - 1 ) else path
let normalize_filename_dir_sep = let dir_sep_char = String . get Filename . dir_sep 0 in String . map ( fun c -> if c = dir_sep_char then ' ' / else c )
let name_of_signal = function | s when s = Sys . sigabrt -> " SIGABRT ( Abnormal termination ) " | s when s = Sys . sigalrm -> " SIGALRM ( Timeout ) " | s when s = Sys . sigfpe -> " SIGFPE ( Arithmetic exception ) " | s when s = Sys . sighup -> " SIGHUP ( Hangup on controlling terminal ) " | s when s = Sys . sigill -> ...
type mode = Struct | Sig
let dump_file oc file = let ic = open_in file in let buflen = 8096 in let buf = Bytes . create buflen in let rec loop ( ) = let len = input ic buf 0 buflen in if len <> 0 then ( Printf . fprintf oc " % s " ( Bytes . to_string ( if len = buflen then buf else Bytes . sub buf 0 len ) ) ; loop ( ) ) in loop ( ) ; close_in ...
let opened_modules = [ " Pervasives " ; " Error_monad " ]
let include_ mode oc file = let unit = String . capitalize_ascii ( Filename . chop_extension ( Filename . basename file ) ) in ( match mode with | Struct -> Printf . fprintf oc " module % s = struct \ n " unit | Sig -> Printf . fprintf oc " module % s : sig \ n " unit ) ; Printf . fprintf oc " # 1 % S \ n " file ; dump...
let ( ) = let mode = match Sys . argv . ( 1 ) with " structs " -> Struct | " sigs " -> Sig | _ -> exit 1 in ( match mode with | Struct -> Printf . fprintf stdout " module M = struct \ n " | Sig -> Printf . fprintf stdout " module type T = sig \ n " ) ; for i = 2 to Array . length Sys . argv - 1 do let file = Sys . argv...
module Bool = struct type t = | True | False let instance = function | True -> ( module struct type t = True let to_string = " TRUE " end : Mysql . Enum . Instance ) | False -> ( module struct type t = False let to_string = " FALSE " end : Mysql . Enum . Instance ) end
module Tables = struct # include " tables / user . ml " # include " tables / team . ml " # include " tables / project . ml " # include " tables / team_user . ml " end
let print_params ps = printf " [ \ n " ; %! let print_param = function | Mysql . Param . Bool b -> printf " Bool % b , \ n " %! b | Mysql . Param . Int i -> printf " Int % d , \ n " %! i | Mysql . Param . String s -> printf " String % s , \ n " %! s | Mysql . Param . Enum ( module E ) -> printf " % s , \ n " %! ( E . t...
let run = List . iter ( fun ( query , params ) -> print_endline query ; print_params params ; print_endline " " ) ===
let ( ) = run [ q0 ; q1 ; q2 ; q3 ; q4 ; q5 ; q6 ; q7 ; q8 ; q9 ]
module Tables = struct module User = struct include ( val Mysql . table " users " ) let id = Field . int " id " let name = Field . string " name " end module Team = struct include ( val Mysql . table " teams " ) let id = Field . int " id " let owner = Field . foreign_key " owner_id " ~ references : User . id let name =...
let ( ) = let open Tables in print_endline @@ fst @@ Mysql . ( Expr . ( Select . ( Expr . ( from Team . table |> left_join ( that Team . owner There ) |> right_join ( this Project . leader There ) |> select [ field User . id ( Skip There ) ; field User . name ( Skip There ) ; field Team . id ( Skip ( Skip There ) ) ; f...
let build_result ngroups input = let res = Array . make ( ngroups + 1 ) " " ~ in for i = 0 to ngroups do try res . ( i ) <- Str . matched_group i input with Not_found -> ( ) done ; res
let search_forward re ng input start = try ignore ( Str . search_forward re input start ) ; build_result ng input with Not_found -> [ ] ||
let search_backward re ng input start = try ignore ( Str . search_backward re input start ) ; build_result ng input with Not_found -> [ ] ||
let partial_match re ng input start = if Str . string_partial_match re input start then build_result ng input else [ ] ||
let start_test msg = print_newline ( ) ; printf " % s \ n " msg
let num_failures = ref 0
let test res1 res2 = if res1 = res2 then print_char ' . ' else begin print_string " FAIL " ; incr num_failures end
let test_search_forward r ng s exp = test ( search_forward r ng s 0 ) exp
let test_search_backward r ng s exp = test ( search_backward r ng s ( String . length s ) ) exp
let test_partial_match r ng s exp = test ( partial_match r ng s 0 ) exp
let end_test ( ) = print_newline ( ) ; if ! num_failures = 0 then printf " All tests passed \ n " else begin printf " TEST FAILED : % d failure ( s ) \ n " ! num_failures ; exit 2 end
let automated_test ( ) = start_test " Search for / the quick brown fox " ; / let r = Str . regexp " the quick brown fox " in let n = 0 in test_search_forward r n " the quick brown fox " [ " | the quick brown fox " ] ; | test_search_forward r n " What do you know about the quick brown fox " ? [ " | the quick brown fox "...
let manual_test regexp text = try ignore ( Str . search_forward ( Str . regexp regexp ) text 0 ) ; printf " Matched , " ; begin try for i = 0 to 31 do try let s = Str . matched_group i text in printf " \\% d =% s " i s with Not_found -> ( ) done with Invalid_argument str as exn -> if str " = Str . matched_group " then ...
let _ = if Array . length Sys . argv >= 3 then manual_test Sys . argv . ( 1 ) Sys . argv . ( 2 ) else automated_test ( )
let f_int ( ) = Testprint . string " f " " int " ; let x1 = 1 in let x3 = 3 in let x4 = 4 in let x8 = 8 in let x9 = 9 in let xm3 = ( - 3 ) in let xm8 = ( - 8 ) in let xm11 = ( - 11 ) in Testprint . int " a " ( x3 + x4 ) ; Testprint . int " b " ( x3 - x4 ) ; Testprint . int " c " ( x3 * x4 ) ; Testprint . int " d " ( x8...
let f_int_unclean ( ) = Testprint . string " f " " int_unclean " ; let x3 = 4 * 0x2000_0003 in let x4 = 4 * 0x2000_0005 in let x8 = 4 * 0x2000_0006 in let x9 = 4 * 0x2000_0007 in let xm3 = ( - 4 ) * 0x2000_0002 in let xm8 = ( - 4 ) * 0x2000_0005 in let xm11 = ( - 4 ) * 0x2000_0007 in Testprint . int " a " ( x3 + x4 ) ;...
let f_intval ( ) = Testprint . string " f " " intval " ; let x1 = 1 + 0 in let x3 = 3 + 0 in let x4 = 4 + 0 in let x8 = 8 + 0 in let x9 = 9 + 0 in let xm3 = ( - 3 ) + 0 in let xm8 = ( - 8 ) + 0 in let xm11 = ( - 11 ) + 0 in Testprint . int " a " ( x3 + x4 ) ; Testprint . int " b " ( x3 - x4 ) ; Testprint . int " c " ( ...
let ( ) = f_int ( ) ; f_int_unclean ( ) ; f_intval ( )
let f ( a : int ) ( b : int ) = let boolint x = if x then 1 else 0 in Testprint . int " x0 " ( boolint ( a = b ) ) ; Testprint . int " x1 " ( boolint ( a = a ) ) ; Testprint . int " x2 " ( boolint ( b = a ) ) ; Testprint . int " x3 " ( boolint ( a < b ) ) ; Testprint . int " x4 " ( boolint ( a < a ) ) ; Testprint . int...
let g ( a : int ) ( b : int ) = Testprint . int " x0 " ( if a = b then 1 else 0 ) ; Testprint . int " x1 " ( if a = a then 1 else 0 ) ; Testprint . int " x2 " ( if b = a then 1 else 0 ) ; Testprint . int " x3 " ( if a < b then 1 else 0 ) ; Testprint . int " x4 " ( if a < a then 1 else 0 ) ; Testprint . int " x5 " ( if ...
let h ( ) = let bad_zero = 4 * 0x2000_0000 in f bad_zero 0
let ( ) = f 10 11 ; g 10 11 ; f 10 ( - 11 ) ; g 10 ( - 11 ) ; f ( - 10 ) 11 ; g ( - 10 ) 11 ; f ( - 10 ) ( - 11 ) ; g ( - 10 ) ( - 11 ) ; h ( )
type bdd = One | Zero | Node of bdd * int * int * bdd
let rec eval bdd vars = match bdd with Zero -> false | One -> true | Node ( l , v , _ , h ) -> if vars . ( v ) then eval h vars else eval l vars
let getId bdd = match bdd with Node ( _ , _ , id , _ ) -> id | Zero -> 0 | One -> 1
let initSize_1 = 8 * 1024 - 1
let nodeC = ref 1
let sz_1 = ref initSize_1
let htab = ref ( Array . make ( ! sz_1 + 1 ) [ ] )
let n_items = ref 0
let hashVal x y v = x lsl 1 + y + v lsl 2
let resize newSize = let arr = ! htab in let newSz_1 = newSize - 1 in let newArr = Array . make newSize [ ] in let rec copyBucket bucket = match bucket with [ ] -> ( ) | n :: ns -> match n with | Node ( l , v , _ , h ) -> let ind = hashVal ( getId l ) ( getId h ) v land newSz_1 in newArr . ( ind ) <- ( n :: newArr . ( ...
let rec insert idl idh v ind bucket newNode = if ! n_items <= ! sz_1 then ( ( ! htab ) . ( ind ) <- ( newNode :: bucket ) ; incr n_items ) else ( resize ( ! sz_1 + ! sz_1 + 2 ) ; let ind = hashVal idl idh v land ( ! sz_1 ) in ( ! htab ) . ( ind ) <- newNode :: ( ! htab ) . ( ind ) )
let resetUnique ( ) = ( sz_1 := initSize_1 ; htab := Array . make ( ! sz_1 + 1 ) [ ] ; n_items := 0 ; nodeC := 1 )
let mkNode low v high = let idl = getId low in let idh = getId high in if idl = idh then low else let ind = hashVal idl idh v land ( ! sz_1 ) in let bucket = ( ! htab ) . ( ind ) in let rec lookup b = match b with [ ] -> let n = Node ( low , v , ( incr nodeC ; ! nodeC ) , high ) in insert ( getId low ) ( getId high ) v...
type ordering = LESS | EQUAL | GREATER
let cmpVar ( x : int ) ( y : int ) = if x < y then LESS else if x > y then GREATER else EQUAL