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