Commit | Line | Data |
---|---|---|
34e49164 C |
1 | let drop_spaces s = |
2 | String.concat "" (Str.split (Str.regexp "[ ]+") s) | |
3 | ||
4 | let parse_line fp l n = | |
5 | if List.mem l fp | |
6 | then None | |
7 | else | |
8 | if Str.string_match (Str.regexp "#") l 0 | |
9 | then None (* comment line *) | |
10 | else | |
11 | let top_split = Str.split (Str.regexp ":") l in | |
12 | match top_split with | |
13 | cocci::first::others -> | |
14 | let rec loop tag = function | |
15 | [x] -> | |
16 | let x = | |
17 | String.concat "\\ " (Str.split (Str.regexp "[ ]+") x) in | |
18 | [(tag,x)] | |
19 | | first::rest -> | |
20 | let splitted = Str.split (Str.regexp "[ ]+") first in | |
21 | (match List.rev splitted with | |
22 | new_tag::info -> | |
23 | let rest = loop new_tag rest in | |
24 | (tag,String.concat "\\ " 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) | |
29 | ||
30 | let collect_lines fp i = | |
31 | let lines = ref [] in | |
32 | let ln = ref 0 in | |
33 | let rec loop _ = | |
34 | ln := !ln + 1; | |
35 | (match parse_line fp (input_line i) !ln with | |
36 | Some l -> | |
37 | if List.mem l !lines | |
38 | then () | |
39 | else lines := l::!lines | |
40 | | None -> ()); | |
41 | loop() in | |
42 | try loop() with End_of_file -> !lines | |
43 | ||
44 | (* --------------------------------------------------------------------- *) | |
45 | ||
46 | let process_fp fl = | |
47 | let i = open_in fl in | |
48 | let lines = ref ([] : string list) in | |
49 | let rec loop _ = | |
50 | let l = input_line i in | |
51 | (if not(Str.string_match (Str.regexp "#") l 0) | |
52 | then lines := l :: !lines); | |
53 | loop() in | |
54 | (try loop() with End_of_file -> ()); | |
55 | close_in i; | |
56 | !lines | |
57 | ||
58 | (* --------------------------------------------------------------------- *) | |
59 | ||
60 | let discard_ambiguous lines = | |
61 | let rec loop = function | |
62 | [] -> [] | |
63 | | (cocci,tags)::rest -> | |
64 | let (same,others) = | |
65 | List.partition | |
66 | (function (cocci2,tags2) -> tags = tags2 && not(cocci = cocci2)) | |
67 | rest in | |
68 | match same with | |
69 | [] -> (cocci,tags)::loop rest | |
70 | | _ -> | |
71 | Printf.printf "ignoring ambiguity:\n"; | |
72 | List.iter | |
73 | (function (cocci,tags) -> | |
74 | Printf.printf "%s: %s\n" cocci | |
75 | (String.concat ", " | |
76 | (List.map | |
77 | (function (tag,tagval) -> | |
78 | Printf.sprintf "%s: %s" tag tagval) | |
79 | tags))) | |
80 | ((cocci,tags)::same); | |
81 | loop others in | |
82 | loop lines | |
83 | ||
84 | (* --------------------------------------------------------------------- *) | |
85 | (* only actually collects the rightmost element into ors *) | |
86 | ||
87 | let split_or (cocci,line) = | |
88 | let rev = List.rev line in | |
89 | (cocci,List.rev(List.tl rev), List.hd rev) | |
90 | ||
91 | let collect_ors fp lines = | |
92 | let rec loop = function | |
93 | [] -> failwith "no lines" | |
94 | | [line] -> | |
95 | let (c,k,v) = split_or line in | |
96 | ((c,k,[v]),[]) | |
97 | | line::xs -> | |
98 | let (c,k,v) = split_or line in | |
99 | let ((c1,k1,v1),rest) = loop xs in | |
978fd7e5 | 100 | if c = c1 && k = k1 && not (k = []) |
34e49164 C |
101 | then |
102 | if List.mem v v1 | |
103 | then ((c1,k1,v1),rest) | |
104 | else ((c1,k1,v::v1),rest) | |
105 | else ((c,k,[v]),((c1,k1,v1)::rest)) in | |
106 | let ((c,k,v),rest) = loop lines in | |
107 | let res = (c,k,v)::rest in | |
108 | List.fold_left | |
109 | (function prev -> | |
110 | function (c,k,v) -> | |
111 | match v with | |
112 | [] -> failwith "not possible" | |
113 | | [x] -> (c,k@v) :: prev | |
114 | | (tag,_)::_ -> | |
91eba41f | 115 | (*let vs = |
34e49164 C |
116 | Printf.sprintf "%s:(%s)" tag |
117 | (String.concat "|" | |
118 | (List.sort compare | |
119 | (List.map (function (_,vl) -> vl) v))) in | |
120 | let attempt = | |
121 | Printf.sprintf "%s: %s %s" c | |
122 | (String.concat " " (List.map (function (k,v) -> k^":"^v) k)) | |
91eba41f C |
123 | vs in*) |
124 | if true (*List.mem attempt fp*) | |
34e49164 C |
125 | then |
126 | let vs = | |
127 | Printf.sprintf "\\\\\\\\\\(%s\\\\\\\\\\)" | |
128 | (String.concat "\\\\\\\\\\|" | |
129 | (List.sort compare | |
130 | (List.map (function (_,vl) -> vl) v))) in | |
131 | (c,k@[(tag,vs)]) :: prev | |
132 | else (List.map (function vi -> (c,k@[vi])) v) @ prev) | |
133 | [] res | |
134 | ||
135 | (* --------------------------------------------------------------------- *) | |
136 | ||
137 | let command s = | |
138 | let _ = Sys.command s in | |
139 | () | |
140 | ||
141 | let created = ref ([] : (string * (int ref * out_channel)) list) | |
142 | ||
143 | let mktag n = Printf.sprintf "x%d" n | |
144 | ||
145 | let created_files = ref ([] : (string * int ref) list) | |
146 | ||
147 | let process_line env (cocci,tags) = | |
148 | let files = List.filter (function (c,f) -> c = cocci) env in | |
149 | List.iter | |
150 | (function (_,cocci_file) -> | |
151 | let resdir = Filename.chop_extension cocci_file in | |
152 | (if not(Sys.file_exists cocci_file) | |
153 | then failwith "no cocci file"); | |
154 | let (n,o) = | |
155 | try List.assoc resdir !created | |
156 | with Not_found -> | |
157 | begin | |
158 | command | |
159 | (Printf.sprintf "/bin/rm -r -f %s; mkdir %s" resdir resdir); | |
160 | let files = Printf.sprintf "%s/files" resdir in | |
161 | let o = open_out files in | |
162 | Printf.fprintf o "all: real_all\n\n"; | |
163 | let cell = ((ref 0),o) in | |
164 | created := (resdir,cell) :: !created; | |
165 | cell | |
166 | end in | |
167 | let temp_file = Filename.temp_file cocci ".cocci" in | |
168 | command (Printf.sprintf "cp %s %s" cocci_file temp_file); | |
169 | let first_tag_val = | |
170 | match tags with | |
171 | [] -> failwith "no tags" | |
172 | | (_,first_tag_val)::_ -> | |
173 | let cell = | |
174 | try List.assoc first_tag_val !created_files | |
175 | with Not_found -> | |
176 | let c = ref (-1) in | |
177 | created_files := (first_tag_val,c)::!created_files; | |
178 | c in | |
179 | cell := !cell + 1; | |
180 | if !cell = 0 | |
181 | then first_tag_val | |
182 | else Printf.sprintf "%s%d" first_tag_val !cell in | |
183 | List.iter | |
184 | (function (tag,tagval) -> | |
185 | command | |
186 | (Printf.sprintf "sed s/%s/%s/ %s > %s_out; cp %s_out %s" | |
187 | tag tagval temp_file temp_file temp_file temp_file)) | |
188 | tags; | |
189 | command | |
190 | (Printf.sprintf "mv %s %s/%s.cocci" temp_file resdir first_tag_val); | |
191 | Printf.fprintf o "%s:\n\tmono_spatch_linux %s.cocci ${ARGS}\n\n" | |
192 | (mktag !n) first_tag_val; | |
193 | n := !n + 1) | |
194 | files | |
195 | ||
196 | (* --------------------------------------------------------------------- *) | |
197 | ||
198 | let rec mkenv = function | |
199 | [] -> [] | |
200 | | [_] -> failwith "required arguments: file (category x cocci file)*" | |
201 | | category::cocci::rest -> | |
202 | if Filename.check_suffix cocci ".cocci" | |
203 | then (category,cocci)::mkenv rest | |
204 | else failwith "required arguments: file (category x cocci file)*" | |
205 | ||
206 | let rec upto = function | |
207 | 0 -> [] | |
208 | | n -> (mktag (n-1)) :: (upto (n-1)) | |
209 | ||
210 | let _ = | |
211 | let (file,fp,env) = | |
212 | match List.tl(Array.to_list Sys.argv) with | |
213 | file::env -> | |
214 | let rec loop prev = function | |
215 | [] -> | |
216 | if prev = "" | |
217 | then ([],[]) | |
218 | else ([prev],[]) | |
219 | | x::xs -> | |
220 | try | |
221 | let _ = Str.search_forward (Str.regexp ".cocci") x 0 in | |
222 | if prev = "" | |
223 | then ([],x::xs) | |
224 | else ([],prev::x::xs) | |
225 | with Not_found -> | |
226 | let (fp,env) = loop x xs in | |
227 | if prev = "" | |
228 | then (fp,env) | |
229 | else (prev::fp,env) in | |
230 | let (fp,env) = loop "" env in | |
231 | (file,fp,mkenv env) | |
232 | | _ -> failwith "one argument expected" in | |
233 | let fp = List.fold_left (@) [] (List.map process_fp fp) in | |
234 | let i = open_in file in | |
235 | let lines = collect_lines fp i in | |
236 | let lines = collect_ors fp lines in | |
237 | close_in i; | |
238 | let lines = discard_ambiguous lines in | |
239 | List.iter (process_line env) lines; | |
240 | List.iter | |
241 | (function (resdir,(n,o)) -> | |
242 | Printf.fprintf o "real_all: %s\n" | |
243 | (String.concat " " (List.rev (upto !n))); | |
244 | close_out o) | |
245 | !created |