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.
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.
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.
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/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
24 String.concat
"" (Str.split
(Str.regexp
"[ ]+") s
)
26 let parse_line fp l n
=
30 if Str.string_match
(Str.regexp
"#") l
0
31 then None
(* comment line *)
33 let top_split = Str.split
(Str.regexp
":") l
in
35 cocci
::first
::others
->
36 let rec loop tag
= function
39 String.concat
"\\ " (Str.split
(Str.regexp
"[ ]+") x) in
42 let splitted = Str.split
(Str.regexp
"[ ]+") first
in
43 (match List.rev
splitted with
45 let rest = loop new_tag
rest in
46 (tag
,String.concat
"\\ " info
)::rest
47 | _
-> failwith
"bad element")
48 | _
-> failwith
"no data" in
49 Some
(cocci
,loop (drop_spaces first
) others
)
50 | _
-> failwith
(Printf.sprintf
"bad line: %s" l
)
52 let collect_lines fp i
=
57 (match parse_line fp
(input_line i
) !ln with
61 else lines := l
::!lines
64 try loop() with End_of_file
-> !lines
66 (* --------------------------------------------------------------------- *)
70 let lines = ref ([] : string list
) in
72 let l = input_line
i in
73 (if not
(Str.string_match
(Str.regexp
"#") l 0)
74 then lines := l :: !lines);
76 (try loop() with End_of_file
-> ());
80 (* --------------------------------------------------------------------- *)
81 (* same info, different categories *)
83 let discard_ambiguous lines =
84 let rec loop = function
86 | (cocci
,tags
)::rest ->
89 (function (cocci2
,tags2
) -> tags
= tags2
&& not
(cocci
= cocci2
))
92 [] -> (cocci
,tags
)::loop rest
94 Printf.printf
"ignoring ambiguity:\n";
96 (function (cocci
,tags
) ->
97 Printf.printf
"%s: %s\n" cocci
100 (function (tag
,tagval
) ->
101 Printf.sprintf
"%s: %s" tag tagval
)
103 ((cocci
,tags
)::same
);
107 (* --------------------------------------------------------------------- *)
108 (* only actually collects the rightmost element into ors *)
110 let split_or (cocci
,line
) =
111 let rev = List.rev line
in
112 (cocci
,List.rev(List.tl
rev), List.hd
rev)
114 let collect_ors fp
lines =
115 let rec loop = function
116 [] -> failwith
"no lines"
118 let (c
,k
,v
) = split_or line
in
121 let (c
,k
,v
) = split_or line
in
122 let ((c1
,k1
,v1
),rest) = loop xs
in
123 if c
= c1
&& k
= k1
&& not
(k
= [])
126 then ((c1
,k1
,v1
),rest)
127 else ((c1
,k1
,v
::v1
),rest)
128 else ((c
,k
,[v
]),((c1
,k1
,v1
)::rest)) in
129 let ((c
,k
,v
),rest) = loop lines in
130 let res = (c
,k
,v
)::rest in
135 [] -> failwith
"not possible"
136 | [x] -> (c
,k
@v
) :: prev
139 Printf.sprintf "%s:(%s)" tag
142 (List.map (function (_,vl) -> vl) v))) in
144 Printf.sprintf "%s: %s %s" c
145 (String.concat " " (List.map (function (k,v) -> k^":"^v) k))
147 if true (*List.mem attempt fp*)
150 Printf.sprintf
"\\\\\\\\\\(%s\\\\\\\\\\)"
151 (String.concat
"\\\\\\\\\\|"
153 (List.map
(function (_
,vl
) -> vl
) v
))) in
154 (c
,k
@[(tag
,vs)]) :: prev
155 else (List.map
(function vi
-> (c
,k
@[vi
])) v
) @ prev
)
158 (* --------------------------------------------------------------------- *)
161 let _ = Sys.command s
in
164 let created = ref ([] : (string * (int ref * out_channel
)) list
)
166 let mktag n
= Printf.sprintf
"x%d" n
168 let created_files = ref ([] : (string * int ref) list
)
170 let process_line env
(cocci
,tags
) =
171 let files = List.filter
(function (c
,f
) -> c
= cocci
) env
in
173 (function (_,cocci_file
) ->
174 let resdir = Filename.chop_extension cocci_file
in
175 (if not
(Sys.file_exists cocci_file
)
176 then failwith
"no cocci file");
178 try List.assoc
resdir !created
182 (Printf.sprintf
"/bin/rm -r -f %s; mkdir %s" resdir resdir);
183 let files = Printf.sprintf
"%s/files" resdir in
184 let o = open_out
files in
185 Printf.fprintf
o "all: real_all\n\n";
186 let cell = ((ref 0),o) in
187 created := (resdir,cell) :: !created;
190 let temp_file = Filename.temp_file cocci
".cocci" in
191 command (Printf.sprintf
"cp %s %s" cocci_file
temp_file);
194 [] -> failwith
"no tags"
195 | (_,first_tag_val)::_ ->
197 try List.assoc
first_tag_val !created_files
200 created_files := (first_tag_val,c)::!created_files;
205 else Printf.sprintf
"%s%d" first_tag_val !cell in
207 (function (tag
,tagval
) ->
209 (Printf.sprintf
"sed s/%s/%s/ %s > %s_out; cp %s_out %s"
210 tag tagval
temp_file temp_file temp_file temp_file))
213 (Printf.sprintf
"mv %s %s/%s.cocci" temp_file resdir first_tag_val);
214 Printf.fprintf
o "%s:\n\tmono_spatch_linux %s.cocci ${ARGS}\n\n"
215 (mktag !n
) first_tag_val;
219 (* --------------------------------------------------------------------- *)
221 let rec mkenv = function
223 | [_] -> failwith
"required arguments: file (category x cocci file)*"
224 | category
::cocci
::rest ->
225 if Filename.check_suffix cocci
".cocci"
226 then (category
,cocci
)::mkenv rest
227 else failwith
"required arguments: file (category x cocci file)*"
229 let rec upto = function
231 | n
-> (mktag (n
-1)) :: (upto (n
-1))
235 List.partition
(function "-no_ors" -> true | _ -> false)
236 (Array.to_list
Sys.argv
) in
238 match List.tl args
with
240 let rec loop prev
= function
247 let _ = Str.search_forward
(Str.regexp
".cocci") x 0 in
250 else ([],prev
::x::xs
)
252 let (fp
,env
) = loop x xs
in
255 else (prev
::fp
,env
) in
256 let (fp
,env
) = loop "" env
in
258 | _ -> failwith
"one argument expected" in
259 let fp = List.fold_left
(@) [] (List.map
process_fp fp) in
260 let i = open_in file
in
261 let lines = collect_lines fp i in
262 let lines = if no_ors
= [] then collect_ors fp lines else lines in
264 let lines = discard_ambiguous lines in
265 List.iter
(process_line env
) lines;
267 (function (resdir,(n
,o)) ->
268 Printf.fprintf
o "real_all: %s\n"
269 (String.concat
" " (List.rev (upto !n
)));