open Printf open Ast let input_all ic = let n = in_channel_length ic in let buf = String.create n in really_input ic buf 0 n; close_in ic; buf let phase ir = prerr_endline "- Load input file"; match ir with | Args args -> let display_name = match args.filename with | Some filename -> filename | None -> "" in if args.cpp then let _ = prerr_endline "- Run C preprocessor" in let cpp_out = match args.filename with | Some filename -> Unix.open_process_in ("cpp " ^ filename) | None -> let content = input_all stdin in let (cpp_out, cpp_in) = Unix.open_process "cpp" in output_string cpp_in content; close_out cpp_in; cpp_out in (* Read preprocessed code from cpp's stdout *) let bufsize = 1024 in let newbuf () = String.create bufsize in let rec read_all buf pos = let nread = input cpp_out buf pos bufsize in if nread = 0 then (close_in cpp_out; buf) else read_all (String.concat "" [buf; newbuf ()]) nread in let preprocessed = read_all (newbuf ()) 0 in FileContent (display_name, preprocessed, args) else let infile = match args.filename with | Some filename -> open_in filename | None -> stdin in FileContent (display_name, input_all infile, args) | _ -> raise (InvalidInput "load")