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.
29 String.concat
"" (Str.split
(Str.regexp
"[ ]+") s
)
31 let parse_line fp l n
=
35 if Str.string_match
(Str.regexp
"#") l
0
36 then None
(* comment line *)
38 let top_split = Str.split
(Str.regexp
":") l
in
40 cocci
::first
::others
->
41 let rec loop tag
= function
44 String.concat
"\\ " (Str.split
(Str.regexp
"[ ]+") x) in
47 let splitted = Str.split
(Str.regexp
"[ ]+") first
in
48 (match List.rev
splitted with
50 let rest = loop new_tag
rest in
51 (tag
,String.concat
"\\ " (List.rev info
))::rest
52 | _
-> failwith
"bad element")
53 | _
-> failwith
"no data" in
54 Some
(cocci
,loop (drop_spaces first
) others
)
55 | _
-> failwith
(Printf.sprintf
"bad line: %s" l
)
57 let collect_lines fp i
=
62 (match parse_line fp
(input_line i
) !ln with
66 else lines := l
::!lines
69 try loop() with End_of_file
-> !lines
71 (* --------------------------------------------------------------------- *)
75 let lines = ref ([] : string list
) in
77 let l = input_line
i in
78 (if not
(Str.string_match
(Str.regexp
"#") l 0)
79 then lines := l :: !lines);
81 (try loop() with End_of_file
-> ());
85 (* --------------------------------------------------------------------- *)
86 (* same info, different categories *)
88 let discard_ambiguous lines =
89 let rec loop = function
91 | (cocci
,tags
)::rest ->
94 (function (cocci2
,tags2
) -> tags
= tags2
&& not
(cocci
= cocci2
))
97 [] -> (cocci
,tags
)::loop rest
99 Printf.printf
"ignoring ambiguity:\n";
101 (function (cocci
,tags
) ->
102 Printf.printf
"%s: %s\n" cocci
105 (function (tag
,tagval
) ->
106 Printf.sprintf
"%s: %s" tag tagval
)
108 ((cocci
,tags
)::same
);
112 (* --------------------------------------------------------------------- *)
113 (* only actually collects the rightmost element into ors *)
115 let split_or (cocci
,line
) =
116 let rev = List.rev line
in
117 (cocci
,List.rev(List.tl
rev), List.hd
rev)
119 let collect_ors fp
lines =
120 let rec loop = function
121 [] -> failwith
"no lines"
123 let (c
,k
,v
) = split_or line
in
126 let (c
,k
,v
) = split_or line
in
127 let ((c1
,k1
,v1
),rest) = loop xs
in
128 if c
= c1
&& k
= k1
&& not
(k
= [])
131 then ((c1
,k1
,v1
),rest)
132 else ((c1
,k1
,v
::v1
),rest)
133 else ((c
,k
,[v
]),((c1
,k1
,v1
)::rest)) in
134 let ((c
,k
,v
),rest) = loop lines in
135 let res = (c
,k
,v
)::rest in
140 [] -> failwith
"not possible"
141 | [x] -> (c
,k
@v
) :: prev
144 Printf.sprintf "%s:(%s)" tag
147 (List.map (function (_,vl) -> vl) v))) in
149 Printf.sprintf "%s: %s %s" c
150 (String.concat " " (List.map (function (k,v) -> k^":"^v) k))
152 if true (*List.mem attempt fp*)
155 Printf.sprintf
"\\\\\\\\\\(%s\\\\\\\\\\)"
156 (String.concat
"\\\\\\\\\\|"
158 (List.map
(function (_
,vl
) -> vl
) v
))) in
159 (c
,k
@[(tag
,vs)]) :: prev
160 else (List.map
(function vi
-> (c
,k
@[vi
])) v
) @ prev
)
163 (* --------------------------------------------------------------------- *)
166 let _ = Sys.command s
in
169 let created = ref ([] : (string * (string list
ref * out_channel
)) list
)
171 let mktag n
= Printf.sprintf
"x%d" n
173 let created_files = ref ([] : (string * int ref) list
)
175 let process_line env
(cocci
,tags
) =
176 let files = List.filter
(function (c
,f
) -> c
= cocci
) env
in
178 (function (_,cocci_file
) ->
179 let resdir = Filename.chop_extension cocci_file
in
180 (if not
(Sys.file_exists cocci_file
)
181 then failwith
"no cocci file");
183 try List.assoc
resdir !created
186 if Sys.file_exists
resdir
190 "test %s -nt %s && /bin/rm -r -f %s && mkdir %s"
191 cocci_file
resdir resdir resdir)
192 else command (Printf.sprintf
"mkdir %s" resdir);
193 let files = Printf.sprintf
"%s/files" resdir in
194 let o = open_out
files in
195 Printf.fprintf
o "all: real_all\n\n";
196 let cell = ((ref []),o) in
197 created := (resdir,cell) :: !created;
200 let temp_file = Filename.temp_file cocci
".cocci" in
201 command (Printf.sprintf
"cp %s %s" cocci_file
temp_file);
204 [] -> failwith
"no tags"
205 | (_,first_tag_val)::_ ->
207 try List.assoc
first_tag_val !created_files
210 created_files := (first_tag_val,c)::!created_files;
215 else Printf.sprintf
"%s%d" first_tag_val !cell in
217 (function (tag
,tagval
) ->
219 (Printf.sprintf
"sed s+%s+%s+ %s > %s_out; cp %s_out %s"
220 tag tagval
temp_file temp_file temp_file temp_file))
223 (Printf.sprintf
"mv %s %s/%s.cocci" temp_file resdir first_tag_val);
224 Printf.fprintf
o "%s.out:\n\tmono_spatch_linux %s.cocci ${ARGS}\n\n"
225 first_tag_val first_tag_val;
226 n
:= (first_tag_val^
".out") :: !n
)
229 (* --------------------------------------------------------------------- *)
231 let rec mkenv = function
233 | [_] -> failwith
"required arguments: file (category x cocci file)*"
234 | category
::cocci
::rest ->
235 if Filename.check_suffix cocci
".cocci"
236 then (category
,cocci
)::mkenv rest
237 else failwith
"required arguments: file (category x cocci file)*"
239 let rec upto = function
241 | n
-> (mktag (n
-1)) :: (upto (n
-1))
245 List.partition
(function "-no_ors" -> true | _ -> false)
246 (Array.to_list
Sys.argv
) in
248 match List.tl args
with
250 let rec loop prev
= function
257 let _ = Str.search_forward
(Str.regexp
".cocci") x 0 in
260 else ([],prev
::x::xs
)
262 let (fp
,env
) = loop x xs
in
265 else (prev
::fp
,env
) in
266 let (fp
,env
) = loop "" env
in
268 | _ -> failwith
"one argument expected" in
269 let fp = List.fold_left
(@) [] (List.map
process_fp fp) in
270 let i = open_in file
in
271 let lines = collect_lines fp i in
272 let lines = if no_ors
= [] then collect_ors fp lines else lines in
274 let lines = discard_ambiguous lines in
275 List.iter
(process_line env
) lines;
277 (function (resdir,(n
,o)) ->
278 Printf.fprintf
o "real_all: %s\n"
279 (String.concat
" " (List.rev !n
));
280 Printf.fprintf
o "\tcat %s > completed\n"
281 (String.concat
" " (List.rev !n
));