2 String.concat
"" (Str.split
(Str.regexp
"[ ]+") s
)
4 let parse_line fp l n
=
8 if Str.string_match
(Str.regexp
"#") l
0
9 then None
(* comment line *)
11 let top_split = Str.split
(Str.regexp
":") l
in
13 cocci
::first
::others
->
14 let rec loop tag
= function
17 String.concat
"\\ " (Str.split
(Str.regexp
"[ ]+") x) in
20 let splitted = Str.split
(Str.regexp
"[ ]+") first
in
21 (match List.rev
splitted with
23 let rest = loop new_tag
rest in
24 (tag
,String.concat
"\\ " (List.rev info
))::rest
25 | _
-> failwith
"bad element")
26 | _
-> failwith
"no data" in
27 Some
(cocci
,loop (drop_spaces first
) others
)
28 | _
-> failwith
(Printf.sprintf
"bad line: %s" l
)
30 let collect_lines fp i
=
35 (match parse_line fp
(input_line i
) !ln with
39 else lines := l
::!lines
42 try loop() with End_of_file
-> !lines
44 (* --------------------------------------------------------------------- *)
48 let lines = ref ([] : string list
) in
50 let l = input_line
i in
51 (if not
(Str.string_match
(Str.regexp
"#") l 0)
52 then lines := l :: !lines);
54 (try loop() with End_of_file
-> ());
58 (* --------------------------------------------------------------------- *)
59 (* same info, different categories *)
61 let discard_ambiguous lines =
62 let rec loop = function
64 | (cocci
,tags
)::rest ->
67 (function (cocci2
,tags2
) -> tags
= tags2
&& not
(cocci
= cocci2
))
70 [] -> (cocci
,tags
)::loop rest
72 Printf.printf
"ignoring ambiguity:\n";
74 (function (cocci
,tags
) ->
75 Printf.printf
"%s: %s\n" cocci
78 (function (tag
,tagval
) ->
79 Printf.sprintf
"%s: %s" tag tagval
)
85 (* --------------------------------------------------------------------- *)
86 (* only actually collects the rightmost element into ors *)
88 let split_or (cocci
,line
) =
89 let rev = List.rev line
in
90 (cocci
,List.rev(List.tl
rev), List.hd
rev)
92 let collect_ors fp
lines =
93 let rec loop = function
94 [] -> failwith
"no lines"
96 let (c
,k
,v
) = split_or line
in
99 let (c
,k
,v
) = split_or line
in
100 let ((c1
,k1
,v1
),rest) = loop xs
in
101 if c
= c1
&& k
= k1
&& not
(k
= [])
104 then ((c1
,k1
,v1
),rest)
105 else ((c1
,k1
,v
::v1
),rest)
106 else ((c
,k
,[v
]),((c1
,k1
,v1
)::rest)) in
107 let ((c
,k
,v
),rest) = loop lines in
108 let res = (c
,k
,v
)::rest in
113 [] -> failwith
"not possible"
114 | [x] -> (c
,k
@v
) :: prev
117 Printf.sprintf "%s:(%s)" tag
120 (List.map (function (_,vl) -> vl) v))) in
122 Printf.sprintf "%s: %s %s" c
123 (String.concat " " (List.map (function (k,v) -> k^":"^v) k))
125 if true (*List.mem attempt fp*)
128 Printf.sprintf
"\\\\\\\\\\(%s\\\\\\\\\\)"
129 (String.concat
"\\\\\\\\\\|"
131 (List.map
(function (_
,vl
) -> vl
) v
))) in
132 (c
,k
@[(tag
,vs)]) :: prev
133 else (List.map
(function vi
-> (c
,k
@[vi
])) v
) @ prev
)
136 (* --------------------------------------------------------------------- *)
139 let _ = Sys.command s
in
142 let created = ref ([] : (string * (string list
ref * out_channel
)) list
)
144 let mktag n
= Printf.sprintf
"x%d" n
146 let created_files = ref ([] : (string * int ref) list
)
148 let process_line env
(cocci
,tags
) =
149 let files = List.filter
(function (c
,f
) -> c
= cocci
) env
in
151 (function (_,cocci_file
) ->
152 let resdir = Filename.chop_extension cocci_file
in
153 (if not
(Sys.file_exists cocci_file
)
154 then failwith
"no cocci file");
156 try List.assoc
resdir !created
159 if Sys.file_exists
resdir
163 "test %s -nt %s && /bin/rm -r -f %s && mkdir %s"
164 cocci_file
resdir resdir resdir)
165 else command (Printf.sprintf
"mkdir %s" resdir);
166 let files = Printf.sprintf
"%s/files" resdir in
167 let o = open_out
files in
168 Printf.fprintf
o "all: real_all\n\n";
169 let cell = ((ref []),o) in
170 created := (resdir,cell) :: !created;
173 let temp_file = Filename.temp_file cocci
".cocci" in
174 command (Printf.sprintf
"cp %s %s" cocci_file
temp_file);
177 [] -> failwith
"no tags"
178 | (_,first_tag_val)::_ ->
180 try List.assoc
first_tag_val !created_files
183 created_files := (first_tag_val,c)::!created_files;
188 else Printf.sprintf
"%s%d" first_tag_val !cell in
190 (function (tag
,tagval
) ->
192 (Printf.sprintf
"sed s+%s+%s+ %s > %s_out; cp %s_out %s"
193 tag tagval
temp_file temp_file temp_file temp_file))
196 (Printf.sprintf
"mv %s %s/%s.cocci" temp_file resdir first_tag_val);
197 Printf.fprintf
o "%s.out:\n\tmono_spatch_linux %s.cocci ${ARGS}\n\n"
198 first_tag_val first_tag_val;
199 n
:= (first_tag_val^
".out") :: !n
)
202 (* --------------------------------------------------------------------- *)
204 let rec mkenv = function
206 | [_] -> failwith
"required arguments: file (category x cocci file)*"
207 | category
::cocci
::rest ->
208 if Filename.check_suffix cocci
".cocci"
209 then (category
,cocci
)::mkenv rest
210 else failwith
"required arguments: file (category x cocci file)*"
212 let rec upto = function
214 | n
-> (mktag (n
-1)) :: (upto (n
-1))
218 List.partition
(function "-no_ors" -> true | _ -> false)
219 (Array.to_list
Sys.argv
) in
221 match List.tl args
with
223 let rec loop prev
= function
230 let _ = Str.search_forward
(Str.regexp
".cocci") x 0 in
233 else ([],prev
::x::xs
)
235 let (fp
,env
) = loop x xs
in
238 else (prev
::fp
,env
) in
239 let (fp
,env
) = loop "" env
in
241 | _ -> failwith
"one argument expected" in
242 let fp = List.fold_left
(@) [] (List.map
process_fp fp) in
243 let i = open_in file
in
244 let lines = collect_lines fp i in
245 let lines = if no_ors
= [] then collect_ors fp lines else lines in
247 let lines = discard_ambiguous lines in
248 List.iter
(process_line env
) lines;
250 (function (resdir,(n
,o)) ->
251 Printf.fprintf
o "real_all: %s\n"
252 (String.concat
" " (List.rev !n
));
253 Printf.fprintf
o "\tcat %s > completed\n"
254 (String.concat
" " (List.rev !n
));