text
stringlengths
12
786k
type popen_process = Process of in_channel * out_channel | Process_in of in_channel | Process_out of out_channel | Process_full of in_channel * out_channel * in_channel
let popen_processes = ( Hashtbl . create 7 : ( popen_process , int ) Hashtbl . t )
let open_proc prog cmdline optenv proc input output error = let pid = win_create_process prog cmdline optenv input output error in Hashtbl . add popen_processes proc pid
let open_process_cmdline_in prog cmdline = let ( in_read , in_write ) = pipe ~ cloexec : true ( ) in let inchan = in_channel_of_descr in_read in begin try open_proc prog cmdline None ( Process_in inchan ) stdin in_write stderr with e -> close_in inchan ; close in_write ; raise e end ; close in_write ; inchan
let open_process_cmdline_out prog cmdline = let ( out_read , out_write ) = pipe ~ cloexec : true ( ) in let outchan = out_channel_of_descr out_write in begin try open_proc prog cmdline None ( Process_out outchan ) out_read stdout stderr with e -> close_out outchan ; close out_read ; raise e end ; close out_read ; outchan
let open_process_cmdline prog cmdline = let ( in_read , in_write ) = pipe ~ cloexec : true ( ) in let ( out_read , out_write ) = try pipe ~ cloexec : true ( ) with e -> close in_read ; close in_write ; raise e in let inchan = in_channel_of_descr in_read in let outchan = out_channel_of_descr out_write in begin try open_proc prog cmdline None ( Process ( inchan , outchan ) ) out_read in_write stderr with e -> close out_read ; close out_write ; close in_read ; close in_write ; raise e end ; close out_read ; close in_write ; ( inchan , outchan )
let open_process_cmdline_full prog cmdline env = let ( in_read , in_write ) = pipe ~ cloexec : true ( ) in let ( out_read , out_write ) = try pipe ~ cloexec : true ( ) with e -> close in_read ; close in_write ; raise e in let ( err_read , err_write ) = try pipe ~ cloexec : true ( ) with e -> close in_read ; close in_write ; close out_read ; close out_write ; raise e in let inchan = in_channel_of_descr in_read in let outchan = out_channel_of_descr out_write in let errchan = in_channel_of_descr err_read in begin try open_proc prog cmdline ( Some ( make_process_env env ) ) ( Process_full ( inchan , outchan , errchan ) ) out_read in_write err_write with e -> close out_read ; close out_write ; close in_read ; close in_write ; close err_read ; close err_write ; raise e end ; close out_read ; close in_write ; close err_write ; ( inchan , outchan , errchan )
let open_process_args_in prog args = open_process_cmdline_in prog ( make_cmdline args )
let open_process_args_out prog args = open_process_cmdline_out prog ( make_cmdline args )
let open_process_args prog args = open_process_cmdline prog ( make_cmdline args )
let open_process_args_full prog args = open_process_cmdline_full prog ( make_cmdline args )
let open_process_shell fn cmd = let shell = try Sys . getenv " COMSPEC " with Not_found -> raise ( Unix_error ( ENOEXEC , " open_process_shell " , cmd ) ) in fn shell ( shell ^ " / c " ^ cmd )
let open_process_in cmd = open_process_shell open_process_cmdline_in cmd
let open_process_out cmd = open_process_shell open_process_cmdline_out cmd
let open_process cmd = open_process_shell open_process_cmdline cmd
let open_process_full cmd = open_process_shell open_process_cmdline_full cmd
let find_proc_id fun_name proc = try Hashtbl . find popen_processes proc with Not_found -> raise ( Unix_error ( EBADF , fun_name , " " ) )
let remove_proc_id proc = Hashtbl . remove popen_processes proc
let process_in_pid inchan = find_proc_id " process_in_pid " ( Process_in inchan )
let process_out_pid outchan = find_proc_id " process_out_pid " ( Process_out outchan )
let process_pid ( inchan , outchan ) = find_proc_id " process_pid " ( Process ( inchan , outchan ) )
let process_full_pid ( inchan , outchan , errchan ) = find_proc_id " process_full_pid " ( Process_full ( inchan , outchan , errchan ) )
let close_process_in inchan = let proc = Process_in inchan in let pid = find_proc_id " close_process_in " proc in remove_proc_id proc ; close_in inchan ; snd ( waitpid [ ] pid )
let close_process_out outchan = let proc = Process_out outchan in let pid = find_proc_id " close_process_out " proc in remove_proc_id proc ; close_out outchan ; snd ( waitpid [ ] pid )
let close_process ( inchan , outchan ) = let proc = Process ( inchan , outchan ) in let pid = find_proc_id " close_process " proc in remove_proc_id proc ; close_in inchan ; close_out outchan ; snd ( waitpid [ ] pid )
let close_process_full ( inchan , outchan , errchan ) = let proc = Process_full ( inchan , outchan , errchan ) in let pid = find_proc_id " close_process_full " proc in remove_proc_id proc ; close_in inchan ; close_out outchan ; close_in errchan ; snd ( waitpid [ ] pid ) file_descr list -> file_descr list -> file_descr list -> float -> file_descr list * file_descr list * file_descr list = " unix_select "
let open_connection sockaddr = let sock = socket ~ cloexec : true ( domain_of_sockaddr sockaddr ) SOCK_STREAM 0 in try connect sock sockaddr ; ( in_channel_of_descr sock , out_channel_of_descr sock ) with exn -> close sock ; raise exn
let shutdown_connection inchan = shutdown ( descr_of_in_channel inchan ) SHUTDOWN_SEND
let establish_server _server_fun _sockaddr = invalid_arg " Unix . establish_server not implemented "
type terminal_io = { mutable c_ignbrk : bool ; mutable c_brkint : bool ; mutable c_ignpar : bool ; mutable c_parmrk : bool ; mutable c_inpck : bool ; mutable c_istrip : bool ; mutable c_inlcr : bool ; mutable c_igncr : bool ; mutable c_icrnl : bool ; mutable c_ixon : bool ; mutable c_ixoff : bool ; mutable c_opost : bool ; mutable c_obaud : int ; mutable c_ibaud : int ; mutable c_csize : int ; mutable c_cstopb : int ; mutable c_cread : bool ; mutable c_parenb : bool ; mutable c_parodd : bool ; mutable c_hupcl : bool ; mutable c_clocal : bool ; mutable c_isig : bool ; mutable c_icanon : bool ; mutable c_noflsh : bool ; mutable c_echo : bool ; mutable c_echoe : bool ; mutable c_echok : bool ; mutable c_echonl : bool ; mutable c_vintr : char ; mutable c_vquit : char ; mutable c_verase : char ; mutable c_vkill : char ; mutable c_veof : char ; mutable c_veol : char ; mutable c_vmin : int ; mutable c_vtime : int ; mutable c_vstart : char ; mutable c_vstop : char }
type setattr_when = TCSANOW | TCSADRAIN | TCSAFLUSH
let tcgetattr _fd = invalid_arg " Unix . tcgetattr not implemented "
let tcsetattr _fd _wh = invalid_arg " Unix . tcsetattr not implemented "
let tcsendbreak _fd _n = invalid_arg " Unix . tcsendbreak not implemented "
let tcdrain _fd = invalid_arg " Unix . tcdrain not implemented "
type flush_queue = TCIFLUSH | TCOFLUSH | TCIOFLUSH
let tcflush _fd _q = invalid_arg " Unix . tcflush not implemented "
type flow_action = TCOOFF | TCOON | TCIOFF | TCION
let tcflow _fd _fl = invalid_arg " Unix . tcflow not implemented "
let setsid ( ) = invalid_arg " Unix . setsid not implemented "
let to_http service region req = let uri = Uri . add_query_params ( Uri . of_string ( Aws . Util . of_option_exn ( Endpoints . url_of service region ) ) ) ( List . append [ ( " Version " , [ " 2016 - 11 - 15 " ] ) ; ( " Action " , [ " UnmonitorInstances " ] ) ] ( Util . drop_empty ( Uri . query_of_encoded ( Query . render ( UnmonitorInstancesRequest . to_query req ) ) ) ) ) in ( ` POST , uri , [ ] )
let of_http body = try let xml = Ezxmlm . from_string body in let resp = Xml . member " UnmonitorInstancesResponse " ( snd xml ) in try Util . or_error ( Util . option_bind resp UnmonitorInstancesResult . parse ) ( let open Error in BadResponse { body ; message = " Could not find well formed UnmonitorInstancesResult . " } ) with | Xml . RequiredFieldMissing msg -> let open Error in ` Error ( BadResponse { body ; message = ( " Error parsing UnmonitorInstancesResult - missing field in body or children : " ^ msg ) } ) with | Failure msg -> ` Error ( let open Error in BadResponse { body ; message = ( " Error parsing xml : " ^ msg ) } )
let parse_error code err = let errors = [ ] @ Errors_internal . common in match Errors_internal . of_string err with | Some var -> if ( List . mem var errors ) && ( ( match Errors_internal . to_http_code var with | Some var -> var = code | None -> true ) ) then Some var else None | None -> None
let debug = ref false
module Unpack_one = struct type ( ' a , ' state ) unpack_result = [ ` Ok of ' a * int | ` Not_enough_data of ' state * int | ` Invalid_data of Error . t ] type ( ' a , ' state ) unpack = state : ' state -> buf : Bigstring . t -> pos : int -> len : int -> ( ' a , ' state ) unpack_result type ' a t = T : { initial_state : ' state ; unpack : ( ' a , ' state ) unpack } -> ' a t let create ~ initial_state ~ unpack = T { initial_state ; unpack } include Monad . Make ( struct type nonrec ' a t = ' a t let return v = T { initial_state = ( ) ; unpack = ( fun ~ state ( ) : ~ buf : _ ~ pos : _ ~ len : _ -> ` Ok ( v , 0 ) ) } let map ' ( t : ' a t ) ~ f = let T { initial_state ; unpack } = t in T { initial_state ; unpack = ( fun ~ state ~ buf ~ pos ~ len -> match unpack ~ state ~ buf ~ pos ~ len with | ` Invalid_data _ | ` Not_enough_data _ as x -> x | ` Ok ( a , pos ) -> ` Ok ( f a , pos ) ) } ; ; let map = ` Custom map ' let bind = let module State = struct type ( ' sa , ' b ) t = | A : ' sa -> ( ' sa , _ ) t | B : ' sb * ( ' b , ' sb ) unpack -> ( _ , ' b ) t end in let open State in let do_b ~ na sb ( ub : ( _ , _ ) unpack ) ~ buf ~ pos ~ len = match ub ~ state : sb ~ buf ~ pos ~ len with | ` Invalid_data _ as x -> x | ` Not_enough_data ( sb , nb ) -> ` Not_enough_data ( B ( sb , ub ) , nb + na ) | ` Ok ( b , nb ) -> ` Ok ( b , na + nb ) in fun ( T a ) ~ f -> let do_a sa ~ buf ~ pos ~ len = match a . unpack ~ state : sa ~ buf ~ pos ~ len with | ` Invalid_data _ as x -> x | ` Not_enough_data ( sa , n ) -> ` Not_enough_data ( A sa , n ) | ` Ok ( a , na ) -> let T b = f a in do_b ~ na b . initial_state b . unpack ~ buf ~ pos ( : pos + na ) ~ len ( : len - na ) in T { initial_state = A a . initial_state ; unpack = ( fun ~ state ~ buf ~ pos ~ len -> match state with | A sa -> do_a sa ~ buf ~ pos ~ len | B ( sb , ub ) -> do_b ~ na : 0 sb ub ~ buf ~ pos ~ len ) } ; ; end ) let create_bin_prot_internal bin_prot_reader ~ reader_expects_size_header = let header_length = Bin_prot . Utils . size_header_length in let not_enough_data = ` Not_enough_data ( ( ) , 0 ) in let pos_ref = ref 0 in let invalid_data message a sexp_of_a = ` Invalid_data ( Error . create message a sexp_of_a ) in let read bin_reader buf ~ pos ~ len = pos_ref := pos ; let result = bin_reader buf ~ pos_ref in if ! pos_ref <> pos + len then invalid_data " pos_ref <> pos + len " ( ! pos_ref , pos , len ) ( [ % sexp_of : int * int * int ] ) else ` Ok result in T { initial_state = ( ) ; unpack = fun ~ state ( ) : ~ buf ~ pos ~ len -> if header_length > len then not_enough_data else begin match read Bin_prot . Utils . bin_read_size_header buf ~ pos ~ len : header_length with | ` Invalid_data _ as x -> x | ` Ok element_length -> if element_length < 0 then invalid_data " negative element length % d " element_length [ % sexp_of : int ] else begin if element_length > len - header_length then not_enough_data else begin let pos = match reader_expects_size_header with | true -> pos | false -> pos + header_length in let len = match reader_expects_size_header with | true -> header_length + element_length | false -> element_length in match read bin_prot_reader . Bin_prot . Type_class . read buf ~ pos ~ len with | ` Invalid_data _ as x -> x | ` Ok result -> ` Ok ( result , header_length + element_length ) end end end } ; ; let create_bin_prot bin_prot_reader = create_bin_prot_internal bin_prot_reader ~ reader_expects_size_header : false ; ; let bin_blob = create_bin_prot_internal Bin_prot . Blob . Opaque . Bigstring . bin_reader_t ~ reader_expects_size_header : true ; ; let sexp = let module Parse_pos = Sexp . Parse_pos in let initial_state ~ pos ~ len buf = Sexp . parse_bigstring buf ~ len ~ parse_pos ( : Parse_pos . create ~ buf_pos : pos ( ) ) in T { initial_state ; unpack = fun ~ state ~ buf ~ pos ~ len -> try begin match state ~ pos ~ len buf with | Cont ( _state , k ) -> ` Not_enough_data ( k , len ) | Done ( sexp , parse_pos ) -> ` Ok ( sexp , parse_pos . Parse_pos . buf_pos - pos ) end with exn -> ` Invalid_data ( Error . of_exn exn ) } ; ; let char = T { initial_state = ( ) ; unpack = fun ~ state ( ) : ~ buf ~ pos ~ len -> if len < 1 then ` Not_enough_data ( ( ) , 0 ) else ` Ok ( Bigstring . get buf pos , 1 ) } ; ; module type Equal = sig type t [ @@ deriving sexp_of ] val equal : t -> t -> bool end let expect ( type a ) ( T u ) ( module E : Equal with type t = a ) expected = T { initial_state = u . initial_state ; unpack = fun ~ state ~ buf ~ pos ~ len -> match u . unpack ~ state ~ buf ~ pos ~ len with | ` Invalid_data _ | ` Not_enough_data _ as x -> x | ` Ok ( parsed , n ) -> if E . equal expected parsed then ` Ok ( ( ) , n ) else ` Invalid_data ( Error . create " parsed does not match expected " ( ) ( fun ( ) -> [ % sexp { parsed = ( parsed : E . t ) ; expected = ( expected : E . t ) } ] ) ) } ; ; let expect_char = expect char ( module Char ) let newline = expect_char ' \ n ' end
type ( ' a , ' state ) alive = { mutable state : ' state ; mutable state_is_initial : bool ; initial_state : ' state ; unpack : ( ( ' a , ' state ) Unpack_one . unpack [ @ sexp . opaque ] ) ; mutable buf : Bigstring . t ; mutable pos : int ; mutable len : int }
type ' a alive_or_dead = | Alive : ( ' a , _ ) alive -> ' a alive_or_dead | Dead of Error . t
type ' a t = { mutable alive_or_dead : ' a alive_or_dead }
let invariant _ t = try match t . alive_or_dead with | Dead _ -> ( ) | Alive alive -> assert ( alive . pos >= 0 ) ; assert ( alive . len >= 0 ) ; if alive . len = 0 then assert ( alive . pos = 0 ) ; if alive . state_is_initial then assert ( phys_equal alive . state alive . initial_state ) ; assert ( alive . pos + alive . len <= Bigstring . length alive . buf ) ; with exn -> failwiths ~ here [ :% here ] " invariant failed " ( exn , t ) [ % sexp_of : exn * _ t ] ; ;
let create ( Unpack_one . T { initial_state ; unpack } ) = { alive_or_dead = Alive { state = initial_state ; state_is_initial = true ; initial_state ; unpack ; buf = Bigstring . create 1 ; pos = 0 ; len = 0 } ; } ; ;
let create_bin_prot bin_prot_reader = create ( Unpack_one . create_bin_prot bin_prot_reader ) ; ;
let is_empty t = match t . alive_or_dead with | Dead error -> Error error | Alive alive -> Ok ( alive . state_is_initial && alive . len = 0 ) ; ;
let is_available t len = let input_start = t . pos + t . len in let available = Bigstring . length t . buf - input_start in available >= len ; ;
let ensure_available t len = if not ( is_available t len ) then begin let new_buf = Bigstring . create ( max ( t . len + len ) ( 2 * Bigstring . length t . buf ) ) in Bigstring . blito ~ src : t . buf ~ src_pos : t . pos ~ src_len : t . len ~ dst : new_buf ( ) ; t . pos <- 0 ; t . buf <- new_buf ; assert ( is_available t len ) ; end ; ; ;
let feed_gen buf_length ( blit_buf_to_bigstring : ( _ , _ ) Blit . blito ) ? pos ? len t buf = if ! debug then invariant ignore t ; match t . alive_or_dead with | Dead e -> Error e | Alive t -> let ( src_pos , src_len ) = Ordered_collection_common . get_pos_len_exn ( ) ? pos ? len ~ total_length ( : buf_length buf ) in ensure_available t src_len ; blit_buf_to_bigstring ~ src : buf ~ src_pos ~ src_len ~ dst : t . buf ~ dst_pos ( : t . pos + t . len ) ( ) ; t . len <- t . len + src_len ; Ok ( ) ; ; ;
let feed ? pos ? len t buf = feed_gen Bigstring . length Bigstring . blito ? pos ? len t buf ; ;
let feed_string ? pos ? len t buf = feed_gen String . length Bigstring . From_string . blito ? pos ? len t buf ; ;
let feed_bytes ? pos ? len t buf = feed_gen Bytes . length Bigstring . From_bytes . blito ? pos ? len t buf ; ;
let error t e = t . alive_or_dead <- Dead e ; Error e ; ;
let consume alive ~ num_bytes = alive . pos <- alive . pos + num_bytes ; alive . len <- alive . len - num_bytes ; ; ;
let rec unpack_iter_loop t alive ~ f = if alive . len = 0 then begin alive . pos <- 0 ; Ok ( ) ; end else begin match alive . unpack ~ buf : alive . buf ~ pos : alive . pos ~ len : alive . len ~ state : alive . state with | exception exn -> error t ( Error . create " unpack error " exn [ % sexp_of : Exn . t ] ) | unpack_result -> match unpack_result with | ` Invalid_data e -> error t ( Error . tag e ~ tag " : invalid data " ) | ` Ok ( one , num_bytes ) -> if num_bytes < 0 || num_bytes > alive . len then error t ( Error . create " unpack consumed invalid amount " num_bytes [ % sexp_of : int ] ) else if num_bytes = 0 && alive . state_is_initial then error t ( Error . of_string " \ unpack returned a value but consumed 0 bytes without partially unpacked data " ) else begin consume alive ~ num_bytes ; alive . state <- alive . initial_state ; alive . state_is_initial <- true ; match f one with | exception exn -> error t ( Error . create " ~ f supplied to Unpack_buffer . unpack_iter raised " exn [ % sexp_of : exn ] ) | _ -> unpack_iter_loop t alive ~ f ; end ; | ` Not_enough_data ( state , num_bytes ) -> if num_bytes < 0 || num_bytes > alive . len then error t ( Error . create " partial unpack consumed invalid amount " num_bytes [ % sexp_of : int ] ) else begin consume alive ~ num_bytes ; alive . state <- state ; alive . state_is_initial <- false ; if alive . len > 0 then Bigstring . blito ~ src : alive . buf ~ src_pos : alive . pos ~ src_len : alive . len ~ dst : alive . buf ( ) ; alive . pos <- 0 ; Ok ( ) ; end end ; ;
let unpack_iter t ~ f = if ! debug then invariant ignore t ; match t . alive_or_dead with | Dead e -> Error e | Alive alive -> unpack_iter_loop t alive ~ f ; ;
let unpack_into t q = unpack_iter t ~ f ( : Queue . enqueue q )
let input_closed_error = Error . of_string " input closed "
let input_closed_in_the_middle_of_data_error = Error . of_string " input closed in the middle of data " ; ;
let unpack_error error = Error . create " unpack error " error [ % sexp_of : Error . t ]
module Unpack_iter_result = struct type ' a t = | Input_closed | Input_closed_in_the_middle_of_data of ' a Unpack_buffer . t | Unpack_error of Error . t [ @@ deriving sexp_of ] let to_error : _ t -> Error . t = function | Input_closed -> input_closed_error | Input_closed_in_the_middle_of_data _ -> input_closed_in_the_middle_of_data_error | Unpack_error error -> unpack_error error ; ; end
module Unpack_result = struct type ' a t = | Input_closed | Input_closed_in_the_middle_of_data of ' a Unpack_buffer . t | Output_closed | Unpack_error of Error . t [ @@ deriving sexp_of ] let to_error : _ t -> Error . t = function | Input_closed -> input_closed_error | Input_closed_in_the_middle_of_data _ -> input_closed_in_the_middle_of_data_error | Output_closed -> Error . of_string " output closed " | Unpack_error error -> unpack_error error ; ; let eof unpack_buffer = match Unpack_buffer . is_empty unpack_buffer with | Error error -> Unpack_error error | Ok true -> Input_closed | Ok false -> Input_closed_in_the_middle_of_data unpack_buffer ; ; let of_unpack_iter_result : _ Unpack_iter_result . t -> _ t = function | Input_closed -> Input_closed | Input_closed_in_the_middle_of_data x -> Input_closed_in_the_middle_of_data x | Unpack_error e -> Unpack_error e ; ; end
module Unpack_from = struct type t = | Pipe of string Pipe . Reader . t | Reader of Reader . t end
module Unpack_to = struct type ' a t = | Iter of ( ' a -> unit ) | Pipe of ' a Pipe . Writer . t [ @@ deriving sexp_of ] end
let unpack_all ( ~ from : Unpack_from . t ) ( ~ to_ : _ Unpack_to . t ) ~ using : unpack_buffer = let unpack_all_available = match to_ with | Iter f -> fun ( ) -> ( match Unpack_buffer . unpack_iter unpack_buffer ~ f with | Ok ( ) -> return ` Continue | Error error -> return ( ` Stop ( Unpack_result . Unpack_error error ) ) ) | Pipe output_writer -> let f a = if Pipe . is_closed output_writer then failwith " output closed " ; Pipe . write_without_pushback output_writer a in fun ( ) -> ( match Unpack_buffer . unpack_iter unpack_buffer ~ f with | Ok ( ) -> Pipe . pushback output_writer >>| fun ( ) -> ` Continue | Error error -> return ( ` Stop ( if Pipe . is_closed output_writer then Unpack_result . Output_closed else Unpack_result . Unpack_error error ) ) ) in let finished_with_input = match from with | Reader input -> try_with ( fun ( ) -> Reader . read_one_chunk_at_a_time input ~ handle_chunk ( : fun buf ~ pos ~ len -> match Unpack_buffer . feed unpack_buffer buf ~ pos ~ len with | Error error -> return ( ` Stop ( Unpack_result . Unpack_error error ) ) | Ok ( ) -> unpack_all_available ( ) ) ) >>| ( function | Error exn -> Unpack_result . Unpack_error ( Error . of_exn exn ) | Ok ( ` Stopped result ) -> result | Ok ` Eof -> Unpack_result . eof unpack_buffer | Ok ( ` Eof_with_unconsumed_data _ ) -> assert false ) | Pipe input -> Deferred . repeat_until_finished ( ) ( fun ( ) -> Pipe . read ' input >>= function | ` Eof -> return ( ` Finished ( Unpack_result . eof unpack_buffer ) ) | ` Ok q -> ( match Queue . iter q ~ f ( : fun string -> match Unpack_buffer . feed_string unpack_buffer string with | Ok ( ) -> ( ) | Error error -> Error . raise error ) with | exception exn -> return ( ` Finished ( Unpack_result . Unpack_error ( Error . of_exn exn ) ) ) | ( ) -> unpack_all_available ( ) >>| ( function | ` Continue -> ` Repeat ( ) | ` Stop z -> ` Finished z ) ) ) in match to_ with | Iter _ -> finished_with_input | Pipe output -> choose [ choice finished_with_input Fn . id ; choice ( Pipe . closed output ) ( fun ( ) -> Unpack_result . Output_closed ) ] ; ;
let unpack_into_pipe ~ from ~ using = let output_reader , output_writer = Pipe . create ( ) in let result = unpack_all ~ from ~ to_ ( : Pipe output_writer ) ~ using >>| fun result -> Pipe . close output_writer ; result in output_reader , result ; ;
let unpack_iter ~ from ~ using ~ f = unpack_all ~ from ~ to_ ( : Iter f ) ~ using >>| function | Input_closed -> Unpack_iter_result . Input_closed | Input_closed_in_the_middle_of_data x -> Input_closed_in_the_middle_of_data x | Unpack_error x -> Unpack_error x | Output_closed as t -> failwiths ~ here [ :% here ] " Unpack_sequence . unpack_iter got unexpected value " t [ % sexp_of : _ Unpack_result . t ] ; ; ( module struct module Unpack_result = struct include Unpack_result let compare _compare_a t1 t2 = match t1 , t2 with | Input_closed , Input_closed -> 0 | Input_closed_in_the_middle_of_data _ , Input_closed_in_the_middle_of_data _ -> 0 | Output_closed , Output_closed -> 0 | Unpack_error e_l , Unpack_error e_r -> Error . compare e_l e_r | ( ( Input_closed | Input_closed_in_the_middle_of_data _ | Output_closed | Unpack_error _ ) , _ ) -> - 1 ; ; end let pack bin_writer values = List . map values ~ f ( : fun value -> Bin_prot . Utils . bin_dump ~ header : true bin_writer value |> Bigstring . to_string ) |> String . concat ; ; let break_into_pieces string ~ of_size = let rec loop start_idx = if start_idx < String . length string then ( let next_idx = Int . min ( start_idx + of_size ) ( String . length string ) in let this_slice = String . slice string start_idx next_idx in this_slice :: loop next_idx ) else [ ] in loop 0 ; ; let % test_unit _ = [ % test_result : string list ] ( break_into_pieces " foobarx " ~ of_size : 2 ) ~ expect [ : " fo " ; " ob " ; " ar " ; " x " ] ; ; module Value = struct type t = { a : string ; b : int } [ @@ deriving bin_io , compare , sexp ] let unpack_buffer ( ) = Unpack_buffer . create_bin_prot bin_reader_t let create seed = let char = Char . of_int_exn ( seed + Char . to_int ' a ' ) in { a = String . make seed char ; b = seed } ; ; let pack ts = pack bin_writer_t ts let bogus_data = let bogus_size = 10 in let buf = Bigstring . init ( Bin_prot . Utils . size_header_length + bogus_size ) ~ f ( : const ' \ 000 ' ) in ignore ( Bin_prot . Utils . bin_write_size_header buf ~ pos : 0 bogus_size : int ) ; Bigstring . to_string buf ; ; let % test_unit _ = let unpack_buffer = unpack_buffer ( ) in ok_exn ( Unpack_buffer . feed_string unpack_buffer bogus_data ) ; let q = Queue . create ( ) in match Unpack_buffer . unpack_into unpack_buffer q with | Ok ( ) -> assert false | Error _ -> assert ( Queue . is_empty q ) ; ; let partial_data = String . make 1 ' ' ; ; let % test_unit _ = let unpack_buffer = unpack_buffer ( ) in ok_exn ( Unpack_buffer . feed_string unpack_buffer partial_data ) ; let q = Queue . create ( ) in match Unpack_buffer . unpack_into unpack_buffer q with | Ok ( ) -> assert ( Queue . is_empty q ) | Error _ -> assert false ; ; end let values n = List . init n ~ f : Value . create let test_size = 50 let ( >>= ) deferred f = let timeout = sec 10 . in Clock . with_timeout timeout deferred >>| ( function | ` Timeout -> failwithf " ! unpack_sequence . ml : Deferred took more than { % Time . Span } " timeout ( ) | ` Result result -> result ) >>= f ; ; let ( >>| ) deferred f = deferred >>= fun x -> return ( f x ) let setup_string_pipe_reader ( ) = let input_r , input_w = Pipe . create ( ) in let output , finished = unpack_into_pipe ~ from ( : Pipe input_r ) ~ using ( : Value . unpack_buffer ( ) ) in return ( input_w , output , finished ) ; ; let setup_iter ( ) = let input_r , input_w = Pipe . create ( ) in let output_r , output_w = Pipe . create ( ) in let finished = unpack_iter ~ from ( : Pipe input_r ) ~ using ( : Value . unpack_buffer ( ) ) ~ f ( : fun a -> Pipe . write_without_pushback output_w a ) >>| Unpack_result . of_unpack_iter_result in return ( input_w , output_r , finished ) ; ; let setup_reader ( ) = let pipe_info = Info . of_string " unpack sequence test " in let input_r , input_w = Pipe . create ( ) in Reader . of_pipe pipe_info input_r >>= fun reader -> let pipe , finished = unpack_into_pipe ~ from ( : Reader reader ) ~ using ( : Unpack_buffer . create_bin_prot Value . bin_reader_t ) in return ( input_w , pipe , finished ) ; ; let run_tests ( ? only_supports_output_to_pipe = false ) test_fn = Thread_safe . block_on_async_exn ( fun ( ) -> Deferred . List . iter ( [ setup_reader ; setup_string_pipe_reader ] @ if only_supports_output_to_pipe then [ ] else [ setup_iter ] ) ~ f ( : fun setup -> setup ( ) >>= fun ( input , output , finished ) -> test_fn input output finished ) ) ; ; let % test_unit " test various full reads " = run_tests ( fun input output finished -> Deferred . repeat_until_finished ( values test_size ) ( fun values -> match values with | [ ] -> Pipe . close input ; finished >>= fun result -> [ % test_result : Value . t Unpack_result . t ] result ~ expect : Unpack_result . Input_closed ; return ( ` Finished ( ) ) | _ :: rest -> let data = Value . pack values in Deferred . repeat_until_finished 1 ( fun of_size -> if of_size >= String . length data then return ( ` Finished ( ) ) else ( let pieces = break_into_pieces data ~ of_size in Pipe . transfer_in_without_pushback input ~ from ( : Queue . of_list pieces ) ; Pipe . read_exactly output ~ num_values ( : List . length values ) >>| function | ` Eof | ` Fewer _ -> assert false | ` Exactly queue -> [ % test_result : Value . t list ] ( Queue . to_list queue ) ~ expect : values ; ` Repeat ( of_size + 1 ) ) ) >>= fun ( ) -> return ( ` Repeat rest ) ) ) ; ; let % test_unit " input closed in middle of read " = run_tests ( fun input output finished -> let values = values test_size in let buffer = Value . pack values ^ Value . partial_data in Pipe . write_without_pushback input buffer ; Pipe . read_exactly output ~ num_values ( : List . length values ) >>= function | ` Eof | ` Fewer _ -> assert false | ` Exactly queue -> [ % test_result : Value . t list ] ( Queue . to_list queue ) ~ expect : values ; Pipe . close input ; finished >>= fun result -> [ % test_result : Value . t Unpack_result . t ] result ~ expect ( : Input_closed_in_the_middle_of_data ( Value . unpack_buffer ( ) ) ) ; Deferred . unit ) ; ; let % test_unit " output pipe closed " = run_tests ~ only_supports_output_to_pipe : true ( fun _input output finished -> Pipe . close_read output ; Pipe . read ' output >>= function | ` Ok _ -> assert false | ` Eof -> finished >>= fun result -> [ % test_result : Value . t Unpack_result . t ] result ~ expect : Output_closed ; Deferred . unit ) ; ; let % test_unit " bad bin - io data " = run_tests ( fun input output finished -> let values = values test_size in let buffer = Value . pack values ^ Value . bogus_data in Pipe . write_without_pushback input buffer ; Pipe . read_exactly output ~ num_values ( : List . length values ) >>= function | ` Eof | ` Fewer _ -> assert false | ` Exactly queue -> [ % test_result : Value . t list ] ( Queue . to_list queue ) ~ expect : values ; finished >>| ( function | Unpack_error _ -> ( ) | _ -> assert false ) ) ; ; end ) ; ;
let sexps = let a x = Sexp . Atom x in let l x = Sexp . List x in [ a " " ; a " hello " ; l [ ] ; l [ a " " ] ; l [ a " " ; a " hello " ] ] ; ;
let test ( ) = Unix . pipe ( Info . of_string " unpack_sequence_test " ) >>= fun ( ` Reader reader_fd , ` Writer writer_fd ) -> let string_writer = Writer . create writer_fd in let sexp_reader , unpack_result = Unpack_sequence . unpack_into_pipe ~ from ( : Reader ( Reader . create reader_fd ) ) ~ using ( : Unpack_buffer . create Unpack_buffer . Unpack_one . sexp ) in List . iter sexps ~ f ( : fun sexp -> Writer . write string_writer ( Sexp . to_string sexp ) ) ; let rec loop_sexps sexps = match sexps with | [ ] -> don ' t_wait_for ( Writer . close string_writer ) | sexp :: sexps -> let packed = Sexp . to_string sexp in let rec loop_bytes i = if i = String . length packed then loop_sexps sexps else ( Writer . write string_writer ( String . sub packed ~ pos : i ~ len : 1 ) ; after ( sec 0 . 001 ) >>> fun ( ) -> loop_bytes ( i + 1 ) ) in loop_bytes 0 in loop_sexps sexps ; let all = Pipe . read_all sexp_reader in unpack_result >>= fun result -> ( match result with | Input_closed -> ( ) | r -> Error . raise ( Unpack_sequence . Unpack_result . to_error r ) ) ; all >>= fun all -> assert ( sexps @ sexps = Queue . to_list all ) ; Fd . close reader_fd ; ;
let tests = [ " Unpack_Sequence_test " , test ]
let display ( ) = glClear [ GL_COLOR_BUFFER_BIT ] ; glFlush ( ) ; ; ;
let reshape ~ width : w ~ height : h = glViewport 0 0 w h ; glMatrixMode GL_PROJECTION ; glLoadIdentity ( ) ; gluPerspective 45 . 0 ( float w . / float h ) 1 . 0 100 . 0 ; glMatrixMode GL_MODELVIEW ; glLoadIdentity ( ) ; ; ;
let mouse ~ button ~ state ~ x ~ y = match button with | GLUT_LEFT_BUTTON -> if state = GLUT_DOWN then begin let viewport = glGetInteger4 Get . GL_VIEWPORT in let a , b , c , d = viewport in let viewport = [ | a ; b ; c ; d ] | in let mvmatrix = glGetMatrix Get . GL_MODELVIEW_MATRIX in let projmatrix = glGetMatrix Get . GL_PROJECTION_MATRIX in let realy = viewport . ( 3 ) - y - 1 in Printf . printf " Coordinates at cursor are ( % 4d , % 4d ) \ n " %! x realy ; let wx , wy , wz = gluUnProject ( float x ) ( float realy ) 0 . 0 mvmatrix projmatrix viewport in Printf . printf " World coords at z = 0 . 0 are ( % f , % f , % f ) \ n " %! wx wy wz ; let wx , wy , wz = gluUnProject ( float x ) ( float realy ) 1 . 0 mvmatrix projmatrix viewport in Printf . printf " World coords at z = 1 . 0 are ( % f , % f , % f ) \ n " %! wx wy wz ; end | GLUT_RIGHT_BUTTON -> if state = GLUT_DOWN then exit ( 0 ) ; | _ -> ( ) ; ;
let keyboard ~ key ~ x ~ y = match key with | ' \ 027 ' -> exit ( 0 ) ; | _ -> ( ) ; ;
let ( ) = let _ = glutInit Sys . argv in glutInitDisplayMode [ GLUT_SINGLE ; GLUT_RGB ] ; glutInitWindowSize 500 500 ; glutInitWindowPosition 100 100 ; let _ = glutCreateWindow Sys . argv . ( 0 ) in glutDisplayFunc ~ display ; glutReshapeFunc ~ reshape ; glutKeyboardFunc ~ keyboard ; glutMouseFunc ~ mouse ; glutMainLoop ( ) ; ; ;
module M : sig type t = private [ ` Bar of ' a | ` Foo ] as ' a val bar : t type t = [ ` Bar of ' a | ` Foo ] as ' a let bar = ` Bar ` Foo end ; ; [ %% expect { |
module M : sig type t = private [ ` Bar of ' a | ` Foo ] as ' a val bar : t end } ] |
let y = match ( M . bar :> [ ` Bar of ' a | ` Foo ] as ' a ) with | ` Bar x -> x | ` Foo -> assert false ; ; [ %% expect { | } ] |
let y = match ( M . bar :> [ ` Bar of M . t | ` Foo ] ) with | ` Bar x -> x | ` Foo -> assert false ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type M . t = [ ` Bar of M . t | ` Foo ] is not a subtype of M . t } ] |
module F ( X : sig end ) : sig type s = private [ ` Bar of ' a | ` Foo ] as ' a val from : M . t -> s val to_ : s -> M . t type s = M . t let from x = x let to_ x = x end ; ; [ %% expect { |
module F : functor ( X : sig end ) -> sig type s = private [ ` Bar of ' a | ` Foo ] as ' a val from : M . t -> s val to_ : s -> M . t end } ] |
module N = F ( struct end ) ; ; [ %% expect { |
module N : sig type s = private [ ` Bar of ' a | ` Foo ] as ' a val from : M . t -> s val to_ : s -> M . t end } ] |
let y = match ( N . from M . bar :> [ ` Bar of N . s | ` Foo ] ) with | ` Bar x -> N . to_ x | ` Foo -> assert false ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type N . s = [ ` Bar of N . s | ` Foo ] is not a subtype of N . s } ] |
let rec exp { e_desc = desc } = match desc with | Eapp ( _ , e , e_list ) -> ( not ( Ztypes . is_combinatorial ( List . length e_list ) e . e_typ ) ) || ( exp e ) || ( List . exists exp e_list ) | Erecord_access ( e , _ ) | Etypeconstraint ( e , _ ) -> exp e | Erecord ( f_e_list ) -> List . exists ( fun ( _ , e ) -> exp e ) f_e_list | Erecord_with ( e , f_e_list ) -> exp e || List . exists ( fun ( _ , e ) -> exp e ) f_e_list | Eseq ( e1 , e2 ) -> ( exp e1 ) || ( exp e2 ) | Elocal _ | Elast _ | Econst _ | Econstr0 _ | Eglobal _ | Eperiod _ | Eop _ -> false | Elet _ | Eblock _ -> true | Econstr1 ( _ , e_list ) | Etuple ( e_list ) -> List . exists exp e_list | Epresent _ | Ematch _ -> assert false
let rec equation { eq_desc = desc } = match desc with | EQeq ( _ , e ) | EQinit ( _ , e ) | EQder ( _ , e , None , [ ] ) | EQpluseq ( _ , e ) -> exp e | EQmatch ( _ , e , m_h_list ) -> exp e || List . exists | EQreset ( eq_list , e ) -> exp e || List . exists equation eq_list | EQand ( eq_list ) | EQbefore ( eq_list ) -> List . exists equation eq_list | EQforall { for_index = i_list ; for_init = init_list ; for_body = b_eq_list } -> let index { desc = desc } = match desc with | Einput ( _ , e ) -> exp e | Eoutput _ -> false | Eindex ( _ , e1 , e2 ) -> exp e1 || exp e2 in let init { desc = desc } = match desc with | Einit_last ( _ , e ) -> exp e in List . exists index i_list || List . exists init init_list || | EQder _ | EQnext _ | EQautomaton _ | EQpresent _ | EQemit _ | EQblock _ -> assert false ( List . exists ( fun { l_eq = eq_list } -> List . exists equation eq_list ) l_list ) || List . exists equation eq_list
let ( ) = init ( )
module type Basics = sig type t val add : t -> t -> t val sub : t -> t -> t val mul : t -> t -> t val div : t -> t -> t val rem : t -> t -> t val max_int : t val logand : t -> t -> t val logor : t -> t -> t val logxor : t -> t -> t val shift_left : t -> int -> t val shift_right : t -> int -> t val of_int : int -> t val to_int : t -> int val of_int64 : int64 -> t val to_int64 : t -> int64 val of_string : string -> t val to_string : t -> string val to_hexstring : t -> string end
module type Extras = sig type t val zero : t val one : t val lognot : t -> t val succ : t -> t val pred : t -> t val compare : t -> t -> int val equal : t -> t -> bool val max : t -> t -> t val min : t -> t -> t val of_string_opt : string -> t option val pp : Format . formatter -> t -> unit val pp_hex : Format . formatter -> t -> unit end
module type Infix = sig type t val ( ) + : t -> t -> t val ( ) - : t -> t -> t val ( * ) : t -> t -> t val ( ) / : t -> t -> t val ( mod ) : t -> t -> t val ( land ) : t -> t -> t val ( lor ) : t -> t -> t val ( lxor ) : t -> t -> t val ( lsl ) : t -> int -> t val ( lsr ) : t -> int -> t end
module type S = sig include Basics include Extras with type t := t module Infix : Infix with type t := t end
module MakeInfix ( B : Basics ) = struct open B let ( ) + = add let ( ) - = sub let ( * ) = mul let ( ) / = div let ( mod ) = rem let ( land ) = logand let ( lor ) = logor let ( lxor ) = logxor let ( lsl ) = shift_left let ( lsr ) = shift_right end
module Extras ( Basics : Basics ) : Extras with type t := Basics . t = struct open Basics let zero = of_int 0 let one = of_int 1 let succ n = add n one let pred n = sub n one let lognot n = logxor n max_int let compare ( x : t ) ( y : t ) = Stdlib . compare x y let equal ( x : t ) ( y : t ) = Stdlib . ( ) = x y let max ( x : t ) ( y : t ) = Stdlib . max x y let min ( x : t ) ( y : t ) = Stdlib . min x y let of_string_opt ( s : string ) = try Some ( of_string s ) with Failure _ -> None let pp fmt x = Format . fprintf fmt " % s " ( to_string x ) let pp_hex fmt x = Format . fprintf fmt " % s " ( to_hexstring x ) end
module UInt8 : S with type t = private int = struct module B = struct type t = int let max_int = 255 let add : t -> t -> t = fun x y -> ( x + y ) land max_int let sub : t -> t -> t = fun x y -> ( x - y ) land max_int let mul : t -> t -> t = fun x y -> ( x * y ) land max_int let div : t -> t -> t = ( ) / let rem : t -> t -> t = ( mod ) let logand : t -> t -> t = ( land ) let logor : t -> t -> t = ( lor ) let logxor : t -> t -> t = ( lxor ) let shift_left : t -> int -> t = fun x y -> ( x lsl y ) land max_int let shift_right : t -> int -> t = ( lsr ) let of_int ( x : int ) : t = x land max_int external to_int : t -> int = " % identity " let of_int64 : int64 -> t = fun x -> of_int ( Int64 . to_int x ) let to_int64 : t -> int64 = fun x -> Int64 . of_int ( to_int x ) external of_string : string -> t = " integers_uint8_of_string " let to_string : t -> string = string_of_int let to_hexstring : t -> string = format_int " % x " end include B include Extras ( B ) module Infix = MakeInfix ( B ) end