(* Supprime toutes les lignes comprises entre les lignes (* BEGIN *) et (* END *) où peut être n'importe quelle chaîne de majuscule sans espaces *) let output_file = ref "" let unpack = ref false let tag = ref None let set_tag t = tag := Some t let exact_match regexp string = (Str.string_match regexp string 0) && (String.length string = Str.match_end ()) let opening_line = ref (Str.regexp "") let ending_line = ref (Str.regexp "") let set_opening_and_ending_lines () = match !tag with | Some tag -> opening_line := Str.regexp ("[ \t]*(\\*[ \t]*BEGIN[ \t]+" ^ tag ^ "[ \t]*\\*)[ \t]*") ; ending_line := Str.regexp ("[ \t]*(\\*[ \t]*END[ \t]+" ^ tag ^ "[ \t]*\\*)[ \t]*") ; | None -> raise Exit let line_check line = if exact_match !opening_line line then Some true else if exact_match !ending_line line then Some false else None let channel_copy src_channel tgt_channel = try while true do output_string tgt_channel ((input_line src_channel)^"\n") done with | End_of_file -> () let cut_off file = let src = open_in_bin file in let (tmp,tgt) = Filename.open_temp_file file ".copy" in let () = channel_copy src tgt in let () = close_in src ; close_out tgt in let src = open_in_bin tmp in let tgt = open_out_bin file in let in_block = ref false in try while true do let line = input_line src in match line_check line with | Some b -> in_block := b | None -> if !unpack || not !in_block then output_string tgt (line^"\n") done with | End_of_file -> ( close_in src ; if !output_file <> "" then close_out tgt ) let () = let filenames = ref [] in let usage = Printf.sprintf "Usage: %s Remove all the blocks of the form (* BEGIN *) ... (* END *) The opening and the ending lines should not contain anything else, yet extra white spaces and tabulations are ignored. WARNING: The given file is thus altered." Sys.argv.(0) in Arg.parse [ ("--", Arg.String set_tag , "Remove all the lines between the given tags") ; ("-u",Arg.Set unpack,"Only remove tags. The blocks are thus unpacked") ; ] (fun filename -> filenames := filename :: !filenames) usage ; try set_opening_and_ending_lines () ; let filenames = List.rev !filenames in List.iter cut_off filenames with Exit -> ()