Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / tools / licensify.ml
1 let lines =
2 ["Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen";
3 "Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix";
4 "This file is part of Coccinelle.";
5 "";
6 "Coccinelle is free software: you can redistribute it and/or modify";
7 "it under the terms of the GNU General Public License as published by";
8 "the Free Software Foundation, according to version 2 of the License.";
9 "";
10 "Coccinelle is distributed in the hope that it will be useful,";
11 "but WITHOUT ANY WARRANTY; without even the implied warranty of";
12 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the";
13 "GNU General Public License for more details.";
14 "";
15 "You should have received a copy of the GNU General Public License";
16 "along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.";
17 "";
18 "The authors reserve the right to distribute this or future versions of";
19 "Coccinelle under other licenses."
20 ]
21
22
23 let comment_lines =
24 List.map (function x -> if x <> "" then " * "^x else " *") lines
25
26 let cpp_lines = "/*" :: comment_lines @ [" */"]
27
28 let ml_lines = "(*" :: comment_lines @ [" *)"]
29
30 let make_lines = (List.map (function x -> if x <> "" then "# "^x else "#") lines)
31 let c_lines = (List.map (function x -> if x <> "" then "// "^x else "//") lines)
32
33 let do_one file =
34 let lines =
35 if Filename.check_suffix file ".cocci" then c_lines else
36 if Filename.check_suffix file ".mly" then cpp_lines else
37 if Filename.check_suffix file ".ml" then ml_lines else
38 if Filename.check_suffix file ".mli" then ml_lines else
39 if Filename.check_suffix file ".mll" then ml_lines else
40 if Filename.check_suffix file ".pl" then make_lines else
41 if Filename.basename file = "Makefile" then make_lines else
42 failwith (Printf.sprintf "unknown file type: %s" file) in
43 let _ = Sys.command (Printf.sprintf "cp %s /tmp/tmpfl" file) in
44 let o = open_out file in
45 List.iter (function l -> Printf.fprintf o "%s\n" l) lines;
46 Printf.fprintf o "\n";
47 Printf.fprintf o "\n";
48 close_out o;
49 let _ = Sys.command (Printf.sprintf "cat /tmp/tmpfl >> %s" file) in
50 ()
51
52 (* pad's modif *)
53 let (+>) o f = f o
54 let cat file =
55 let chan = open_in file in
56 let rec cat_aux acc () =
57 (* cant do input_line chan::aux() cos ocaml eval from right to left ! *)
58 let (b, l) = try (true, input_line chan) with End_of_file -> (false, "") in
59 if b
60 then cat_aux (l::acc) ()
61 else acc
62 in
63 cat_aux [] () +> List.rev +> (fun x -> close_in chan; x)
64
65
66 let rec process dir =
67 let files =
68 try
69 List.map (function fl -> dir^"/"^fl)
70 (Array.to_list(Sys.readdir dir))
71 with Sys_error _ -> [] in
72 List.iter (function file ->
73 try
74 let xs = cat file in
75 if List.exists (fun s ->
76 s = "* This file is part of Coccinelle."
77 ||
78 s = "# This file is part of Coccinelle."
79 ||
80 s = "// This file is part of Coccinelle."
81 ||
82 Str.string_match (Str.regexp_string "Copyright") s 0
83 ) xs
84 then print_string ("already processed: " ^ file ^ "\n")
85 else begin
86 do_one file;
87 print_string ("processed: " ^ file ^ "\n");
88 end
89 with _ ->
90 print_string ("skipped: " ^ file ^ "\n");
91 ()
92 ) files;
93 (* pad: no recursive call in directory List.iter process files *)
94 ()
95
96 let _ = process "."