1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
(*
* This file is part of Coccinelle, licensed under the terms of the GPL v2.
* See copyright.txt in the Coccinelle source code for more information.
* The Coccinelle source code can be obtained at http://coccinelle.lip6.fr
*)
(**
* This script adds a legal header to various kinds of files.
* To apply it to a directory, change to this directory and do:
* ocaml str.cma path/to/licensify-new.ml
*)
let lines =
["This file is part of Coccinelle, licensed under the terms of the GPL v2.";
"See copyright.txt in the Coccinelle source code for more information.";
"The Coccinelle source code can be obtained at http://coccinelle.lip6.fr";
]
let comment_lines =
List.map (function x -> if x <> "" then " * "^x else " *") lines
let cpp_lines = "/*" :: comment_lines @ [" */"]
let ml_lines = "(*" :: comment_lines @ [" *)"]
let make_lines = (List.map (function x -> if x <> "" then "# "^x else "#") lines)
let c_lines = (List.map (function x -> if x <> "" then "// "^x else "//") lines)
let do_one file =
let lines =
if Filename.check_suffix file ".cocci" then c_lines else
if Filename.check_suffix file ".mly" then cpp_lines else
if Filename.check_suffix file ".ml" then ml_lines else
if Filename.check_suffix file ".mli" then ml_lines else
if Filename.check_suffix file ".mll" then ml_lines else
if Filename.check_suffix file ".pl" then make_lines else
if Filename.basename file = "Makefile" then make_lines else
failwith (Printf.sprintf "unknown file type: %s" file) in
let tmpfl = Filename.temp_file "cocci_licence" "orig" in
let _ = Sys.command (Printf.sprintf "cp %s %s" file tmpfl) in
let o = open_out file in
List.iter (function l -> Printf.fprintf o "%s\n" l) lines;
Printf.fprintf o "\n";
close_out o;
let _ = Sys.command (Printf.sprintf "cat %s >> %s" tmpfl file) in
Sys.remove tmpfl
(* pad's modif *)
let (+>) o f = f o
let cat file =
let chan = open_in file in
let rec cat_aux acc () =
(* can't do input_line chan::aux() cos ocaml eval from right to left ! *)
let (b, l) = try (true, input_line chan) with End_of_file -> (false, "") in
if b
then cat_aux (l::acc) ()
else acc
in
cat_aux [] () +> List.rev +> (fun x -> close_in chan; x)
let rec process dir =
let files =
try
List.map (function fl -> dir^"/"^fl)
(Array.to_list(Sys.readdir dir))
with Sys_error _ -> [] in
List.iter (function file ->
try
let xs = cat file in
if List.exists (fun s ->
s = "* This file is part of Coccinelle."
||
s = "# This file is part of Coccinelle."
||
s = "// This file is part of Coccinelle."
||
Str.string_match (Str.regexp_string "Copyright") s 0
) xs
then print_string ("already processed: " ^ file ^ "\n")
else begin
do_one file;
print_string ("processed: " ^ file ^ "\n");
end
with _ ->
print_string ("skipped: " ^ file ^ "\n");
()
) files;
(* pad: no recursive call in directory List.iter process files *)
()
let _ = process "."
|