|
| 1 | +(* |
| 2 | + * $Id$ |
| 3 | + * Copyright (c) 2007, Hugues Cassé <[email protected]> |
| 4 | + * |
| 5 | + * Pretty printer of C files. |
| 6 | + *) |
| 7 | + |
| 8 | +open Frontc |
| 9 | + |
| 10 | + |
| 11 | +(* Options *) |
| 12 | +let banner = |
| 13 | + "printc V1.0 (10/01/07)\n" ^ |
| 14 | + "Copyright (c) 2007, Hugues Cassé <[email protected]>\n\n" ^ |
| 15 | + "SYNTAX:\tprintc [options] files...\n" ^ |
| 16 | + "\tprintc [options] --\n" |
| 17 | +let args: parsing_arg list ref = ref [] |
| 18 | +let files: string list ref = ref [] |
| 19 | +let out_file = ref "" |
| 20 | +let from_stdin = ref false |
| 21 | + |
| 22 | + |
| 23 | +(* Options scanning *) |
| 24 | +let opts = [ |
| 25 | + ("-o", Arg.Set_string out_file, |
| 26 | + "Output to the given file."); |
| 27 | + ("-pp", Arg.Unit (fun _ -> args := USE_CPP :: !args), |
| 28 | + "Preprocess the input files."); |
| 29 | + ("-nogcc", Arg.Unit (fun _ -> args := (GCC_SUPPORT false) :: !args), |
| 30 | + "Do not use the GCC extensions."); |
| 31 | + ("-proc", Arg.String (fun cpp -> args := (PREPROC cpp) :: !args), |
| 32 | + "Use the given preprocessor"); |
| 33 | + ("-i", Arg.String (fun file -> args := (INCLUDE file) :: !args), |
| 34 | + "Include the given file."); |
| 35 | + ("-I", Arg.String (fun dir -> args := (INCLUDE_DIR dir) :: !args), |
| 36 | + "Include retrieval directory"); |
| 37 | + ("-D", Arg.String (fun def -> args := (DEF def) :: !args), |
| 38 | + "Pass this definition to the preprocessor."); |
| 39 | + ("-U", Arg.String (fun undef -> args := (UNDEF undef) :: !args), |
| 40 | + "Pass this undefinition to the preprocessor."); |
| 41 | + ("--", Arg.Set from_stdin, |
| 42 | + "Takes input from standard input."); |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +(* Main Program *) |
| 47 | +let _ = |
| 48 | + |
| 49 | + (* Parse arguments *) |
| 50 | + Arg.parse opts (fun file -> files := file :: !files) banner; |
| 51 | + |
| 52 | + (* Get the output *) |
| 53 | + let (output, close) = |
| 54 | + if !out_file = "" then (stdout,false) |
| 55 | + else ((open_out !out_file), true) in |
| 56 | + |
| 57 | + |
| 58 | + (* Process the input *) |
| 59 | + let process opts = |
| 60 | + match Frontc.parse opts with |
| 61 | + PARSING_ERROR -> () |
| 62 | + | PARSING_OK file -> |
| 63 | + Cprint.print output file in |
| 64 | + |
| 65 | + (* Process the inputs *) |
| 66 | + let _ = |
| 67 | + if !from_stdin || !files = [] |
| 68 | + then process !args |
| 69 | + else |
| 70 | + List.iter (fun file -> process ((FROM_FILE file) :: !args)) !files in |
| 71 | + |
| 72 | + (* Close the output if needed *) |
| 73 | + if close then close_out output |
| 74 | + |
| 75 | + |
0 commit comments