2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
28 String.concat
"" (Str.split
(Str.regexp
"[ ]+") s
)
30 let parse_line fp l n
=
34 if Str.string_match
(Str.regexp
"#") l
0
35 then None
(* comment line *)
37 let top_split = Str.split
(Str.regexp
":") l
in
39 cocci
::first
::others
->
40 let rec loop tag
= function
43 String.concat
"\\ " (Str.split
(Str.regexp
"[ ]+") x) in
46 let splitted = Str.split
(Str.regexp
"[ ]+") first
in
47 (match List.rev
splitted with
49 let rest = loop new_tag
rest in
50 (tag
,String.concat
"\\ " (List.rev info
))::rest
51 | _
-> failwith
"bad element")
52 | _
-> failwith
"no data" in
53 Some
(cocci
,loop (drop_spaces first
) others
)
54 | _
-> failwith
(Printf.sprintf
"bad line: %s" l
)
56 let collect_lines fp i
=
61 (match parse_line fp
(input_line i
) !ln with
65 else lines := l
::!lines
68 try loop() with End_of_file
-> !lines
70 (* --------------------------------------------------------------------- *)
74 let lines = ref ([] : string list
) in
76 let l = input_line
i in
77 (if not
(Str.string_match
(Str.regexp
"#") l 0)
78 then lines := l :: !lines);
80 (try loop() with End_of_file
-> ());
84 (* --------------------------------------------------------------------- *)
85 (* same info, different categories *)
87 let discard_ambiguous lines =
88 let rec loop = function
90 | (cocci
,tags
)::rest ->
93 (function (cocci2
,tags2
) -> tags
= tags2
&& not
(cocci
= cocci2
))
96 [] -> (cocci
,tags
)::loop rest
98 Printf.printf
"ignoring ambiguity:\n";
100 (function (cocci
,tags
) ->
101 Printf.printf
"%s: %s\n" cocci
104 (function (tag
,tagval
) ->
105 Printf.sprintf
"%s: %s" tag tagval
)
107 ((cocci
,tags
)::same
);
111 (* --------------------------------------------------------------------- *)
112 (* only actually collects the rightmost element into ors *)
114 let split_or (cocci
,line
) =
115 let rev = List.rev line
in
116 (cocci
,List.rev(List.tl
rev), List.hd
rev)
118 let collect_ors fp
lines =
119 let rec loop = function
120 [] -> failwith
"no lines"
122 let (c
,k
,v
) = split_or line
in
125 let (c
,k
,v
) = split_or line
in
126 let ((c1
,k1
,v1
),rest) = loop xs
in
127 if c
= c1
&& k
= k1
&& not
(k
= [])
130 then ((c1
,k1
,v1
),rest)
131 else ((c1
,k1
,v
::v1
),rest)
132 else ((c
,k
,[v
]),((c1
,k1
,v1
)::rest)) in
133 let ((c
,k
,v
),rest) = loop lines in
134 let res = (c
,k
,v
)::rest in
139 [] -> failwith
"not possible"
140 | [x] -> (c
,k
@v
) :: prev
143 Printf.sprintf "%s:(%s)" tag
146 (List.map (function (_,vl) -> vl) v))) in
148 Printf.sprintf "%s: %s %s" c
149 (String.concat " " (List.map (function (k,v) -> k^":"^v) k))
151 if true (*List.mem attempt fp*)
154 Printf.sprintf
"\\\\\\\\\\(%s\\\\\\\\\\)"
155 (String.concat
"\\\\\\\\\\|"
157 (List.map
(function (_
,vl
) -> vl
) v
))) in
158 (c
,k
@[(tag
,vs)]) :: prev
159 else (List.map
(function vi
-> (c
,k
@[vi
])) v
) @ prev
)
162 (* --------------------------------------------------------------------- *)
165 let _ = Sys.command s
in
168 let created = ref ([] : (string * (string list
ref * out_channel
)) list
)
170 let mktag n
= Printf.sprintf
"x%d" n
172 let created_files = ref ([] : (string * int ref) list
)
174 let process_line env
(cocci
,tags
) =
175 let files = List.filter
(function (c
,f
) -> c
= cocci
) env
in
177 (function (_,cocci_file
) ->
178 let resdir = Filename.chop_extension cocci_file
in
179 (if not
(Sys.file_exists cocci_file
)
180 then failwith
"no cocci file");
182 try List.assoc
resdir !created
185 if Sys.file_exists
resdir
189 "test %s -nt %s && /bin/rm -r -f %s && mkdir %s"
190 cocci_file
resdir resdir resdir)
191 else command (Printf.sprintf
"mkdir %s" resdir);
192 let files = Printf.sprintf
"%s/files" resdir in
193 let o = open_out
files in
194 Printf.fprintf
o "all: real_all\n\n";
195 let cell = ((ref []),o) in
196 created := (resdir,cell) :: !created;
199 let temp_file = Filename.temp_file cocci
".cocci" in
200 command (Printf.sprintf
"cp %s %s" cocci_file
temp_file);
203 [] -> failwith
"no tags"
204 | (_,first_tag_val)::_ ->
206 try List.assoc
first_tag_val !created_files
209 created_files := (first_tag_val,c)::!created_files;
214 else Printf.sprintf
"%s%d" first_tag_val !cell in
216 (function (tag
,tagval
) ->
218 (Printf.sprintf
"sed s+%s+%s+ %s > %s_out; cp %s_out %s"
219 tag tagval
temp_file temp_file temp_file temp_file))
222 (Printf.sprintf
"mv %s %s/%s.cocci" temp_file resdir first_tag_val);
223 Printf.fprintf
o "%s.out:\n\tmono_spatch_linux %s.cocci ${ARGS}\n\n"
224 first_tag_val first_tag_val;
225 n
:= (first_tag_val^
".out") :: !n
)
228 (* --------------------------------------------------------------------- *)
230 let rec mkenv = function
232 | [_] -> failwith
"required arguments: file (category x cocci file)*"
233 | category
::cocci
::rest ->
234 if Filename.check_suffix cocci
".cocci"
235 then (category
,cocci
)::mkenv rest
236 else failwith
"required arguments: file (category x cocci file)*"
238 let rec upto = function
240 | n
-> (mktag (n
-1)) :: (upto (n
-1))
244 List.partition
(function "-no_ors" -> true | _ -> false)
245 (Array.to_list
Sys.argv
) in
247 match List.tl args
with
249 let rec loop prev
= function
256 let _ = Str.search_forward
(Str.regexp
".cocci") x 0 in
259 else ([],prev
::x::xs
)
261 let (fp
,env
) = loop x xs
in
264 else (prev
::fp
,env
) in
265 let (fp
,env
) = loop "" env
in
267 | _ -> failwith
"one argument expected" in
268 let fp = List.fold_left
(@) [] (List.map
process_fp fp) in
269 let i = open_in file
in
270 let lines = collect_lines fp i in
271 let lines = if no_ors
= [] then collect_ors fp lines else lines in
273 let lines = discard_ambiguous lines in
274 List.iter
(process_line env
) lines;
276 (function (resdir,(n
,o)) ->
277 Printf.fprintf
o "real_all: %s\n"
278 (String.concat
" " (List.rev !n
));
279 Printf.fprintf
o "\tcat %s > completed\n"
280 (String.concat
" " (List.rev !n
));