Turn to Regex in ModuleReader.java
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / SourceFileReplacer.java
CommitLineData
b0282412 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
0dc8c589 13package org.tianocore.migration;\r
14\r
15import java.io.*;\r
16import java.util.*;\r
17import java.util.regex.Matcher;\r
18import java.util.regex.Pattern;\r
19\r
20public class SourceFileReplacer {\r
21 SourceFileReplacer(String path, ModuleInfo moduleinfo, Database database, UI fp) {\r
22 modulepath = path;\r
23 mi = moduleinfo;\r
24 db = database;\r
25 ui = fp;\r
26 }\r
27 private String modulepath;\r
28 private ModuleInfo mi;\r
29 private Database db;\r
30 private UI ui;\r
31 private boolean showdetails = false;\r
32 \r
33 private class r8tor9 {\r
34 r8tor9(String r8, String r9) {\r
35 r8thing = r8;\r
36 r9thing = r9;\r
37 }\r
38 public String r8thing;\r
39 public String r9thing;\r
40 }\r
41 \r
42 // these sets are used only for printing log of the changes in current file\r
43 private Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
44 private Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
45 private Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
46 private Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
47 private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
48 private Set<String> filer8only = new HashSet<String>();\r
49 \r
50 private String r8only = "EfiLibInstallDriverBinding " +\r
51 "EfiLibInstallAllDriverProtocols " +\r
52 "EfiLibCompareLanguage " +\r
53 "BufToHexString " +\r
e6a5df3b 54 "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!\r
0dc8c589 55 "EfiValueToHexStr " +\r
56 "HexStringToBuf " +\r
57 "IsHexDigit " +\r
58 "NibbleToHexChar " +\r
59 "GetHob " +\r
60 "GetHobListSize " +\r
61 "GetHobVersion " +\r
62 "GetHobBootMode " +\r
63 "GetCpuHobInfo " +\r
64 "GetDxeCoreHobInfo " +\r
65 "GetNextFirmwareVolumeHob " +\r
66 "GetNextGuidHob " +\r
67 "GetPalEntryHobInfo " +\r
68 "GetIoPortSpaceAddressHobInfo ";\r
69 \r
70 public void flush() throws Exception {\r
71 PrintWriter outfile;\r
e6a5df3b 72 String outname = null;\r
73 String inname = null;\r
f7fee0ea 74 if (ui.yesOrNo("Changes will be made to the Source Code. View details?")) {\r
0dc8c589 75 showdetails = true;\r
76 }\r
e6a5df3b 77 \r
78 Iterator<String> di = mi.localmodulesources.iterator();\r
79 while (di.hasNext()) {\r
80 inname = di.next();\r
81 if (inname.contains(".c") || inname.contains(".C")) {\r
82 if (inname.contains(".C")) {\r
83 outname = inname.replaceFirst(".C", ".c");\r
84 } else {\r
85 outname = inname;\r
86 }\r
f7fee0ea 87 ui.println("\nModifying file: " + inname);\r
90503bad 88 Common.ensureDir(modulepath + File.separator + "result" + File.separator + outname);\r
e6a5df3b 89 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname)));\r
90 outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname));\r
0dc8c589 91 outfile.flush();\r
92 outfile.close();\r
e6a5df3b 93 } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {\r
94 if (inname.contains(".H")) {\r
95 outname = inname.replaceFirst(".H", ".h");\r
0dc8c589 96 } else {\r
e6a5df3b 97 outname = inname;\r
0dc8c589 98 }\r
f7fee0ea 99 ui.println("\nCopying file: " + inname);\r
90503bad 100 Common.ensureDir(modulepath + File.separator + "result" + File.separator + outname);\r
e6a5df3b 101 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname)));\r
90503bad 102 outfile.append(Common.sourcefiletostring(modulepath + File.separator + "temp" + File.separator + inname));\r
0dc8c589 103 outfile.flush();\r
104 outfile.close();\r
105 }\r
106 }\r
107\r
108 if (!mi.hashr8only.isEmpty()) {\r
109 addr8only();\r
110 }\r
111 }\r
112 \r
113 private void addr8only() throws Exception {\r
114 String paragraph = null;\r
90503bad 115 String line = Common.sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c");\r
116 Common.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c");\r
0dc8c589 117 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c")));\r
118 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h")));\r
0dc8c589 119 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);\r
120 Matcher mtrr8only = ptnr8only.matcher(line);\r
121 Matcher mtrr8onlyhead;\r
122 while (mtrr8only.find()) {\r
123 if (mi.hashr8only.contains(mtrr8only.group(2))) {\r
124 paragraph = mtrr8only.group();\r
125 outfile1.append(paragraph + "\n\n");\r
126 if (mtrr8only.group(1).length() != 0) {\r
127 mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
128 }\r
129 //generate R8lib.h\r
130 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
131 paragraph = mtrr8onlyhead.replaceAll(";");\r
132 }\r
133 outfile2.append(paragraph + "\n\n");\r
134 }\r
135 }\r
136 outfile1.flush();\r
137 outfile1.close();\r
138 outfile2.flush();\r
139 outfile2.close();\r
e6a5df3b 140 \r
141 mi.localmodulesources.add("R8Lib.h");\r
142 mi.localmodulesources.add("R8Lib.c");\r
0dc8c589 143 }\r
144 \r
0dc8c589 145 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
146 private String sourcefilereplace(String filename) throws Exception {\r
147 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
148 StringBuffer wholefile = new StringBuffer();\r
149 String line;\r
150 String r8thing;\r
151 String r9thing;\r
152 r8tor9 temp;\r
153 boolean addr8 = false;\r
154\r
155 Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !\r
156 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);\r
157\r
158 while ((line = rd.readLine()) != null) {\r
159 wholefile.append(line + "\n");\r
160 }\r
161 line = wholefile.toString();\r
162 \r
163 // replace BS -> gBS , RT -> gRT\r
164 Matcher mat = pat.matcher(line);\r
165 if (mat.find()) { // add a library here\r
f7fee0ea 166 ui.println("Converting all BS->gBS, RT->gRT");\r
0dc8c589 167 line = mat.replaceAll("g$1$2$3"); //unknown correctiveness\r
168 }\r
169 mat.reset();\r
170 while (mat.find()) {\r
171 if (mat.group(1).matches("BS")) {\r
172 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
173 }\r
174 if (mat.group(1).matches("RT")) {\r
175 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");\r
176 }\r
177 }\r
178 /*\r
179 // remove EFI_DRIVER_ENTRY_POINT\r
180 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
181 Matcher matentrypoint = patentrypoint.matcher(line);\r
182 if (matentrypoint.find()) {\r
183 ui.println("Deleting Entry_Point");\r
184 line = matentrypoint.replaceAll("");\r
185 }\r
186 */\r
187 // start replacing names\r
188 Iterator<String> it;\r
189 // Converting non-locla function\r
190 it = mi.hashnonlocalfunc.iterator();\r
191 while (it.hasNext()) {\r
192 r8thing = it.next();\r
193 if (r8thing.matches("EfiInitializeDriverLib")) { //s\r
194 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p\r
195 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e\r
196 } else if (r8thing.matches("DxeInitializeDriverLib")) { //c\r
197 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i\r
198 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a\r
199 mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l\r
200 } else { //\r
201 mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here\r
202 }\r
203 \r
204 if ((r9thing = db.getR9Func(r8thing)) != null) {\r
205 if (!r8thing.equals(r9thing)) {\r
206 if (line.contains(r8thing)) {\r
207 line = line.replaceAll(r8thing, r9thing);\r
208 filefunc.add(new r8tor9(r8thing, r9thing));\r
209 Iterator<r8tor9> rt = filefunc.iterator();\r
210 while (rt.hasNext()) {\r
211 temp = rt.next();\r
212 if (r8only.contains(temp.r8thing)) {\r
0dc8c589 213 filer8only.add(r8thing);\r
214 mi.hashr8only.add(r8thing);\r
215 addr8 = true;\r
216 }\r
217 }\r
218 }\r
219 }\r
220 }\r
221 } //is any of the guids changed?\r
222 if (addr8 == true) {\r
223 line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");\r
224 }\r
225 \r
226 // Converting macro\r
227 it = mi.hashnonlocalmacro.iterator();\r
228 while (it.hasNext()) { //macros are all assumed MdePkg currently\r
229 r8thing = it.next();\r
230 //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); \r
231 if ((r9thing = db.getR9Macro(r8thing)) != null) {\r
232 if (line.contains(r8thing)) {\r
233 line = line.replaceAll(r8thing, r9thing);\r
234 filemacro.add(new r8tor9(r8thing, r9thing));\r
235 }\r
236 }\r
237 }\r
238\r
239 // Converting guid\r
240 replaceGuid(line, mi.guid, "guid", fileguid);\r
241 replaceGuid(line, mi.ppi, "ppi", fileppi);\r
242 replaceGuid(line, mi.protocol, "protocol", fileprotocol);\r
243\r
244 // Converting Pei\r
245 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%\r
246 Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);\r
247 if (mi.moduletype.contains("PEIM")) {\r
248 Matcher mtrpei = ptnpei.matcher(line);\r
249 while (mtrpei.find()) { // ! add a library here !\r
250 line = mtrpei.replaceAll("PeiServices$1#%$2");\r
251 mi.hashrequiredr9libs.add("PeiServicesLib");\r
252 }\r
253 mtrpei.reset();\r
254 if (line.contains("PeiServicesCopyMem")) {\r
255 line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r
256 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
257 }\r
258 if (line.contains("PeiServicesSetMem")) {\r
259 line = line.replaceAll("PeiServicesSetMem#%", "SetMem");\r
260 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
261 }\r
262\r
263 // Second , find all #% to drop the arg "PeiServices"\r
264 Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);\r
265 Matcher mtrpeiarg = ptnpeiarg.matcher(line);\r
266 while (mtrpeiarg.find()) {\r
267 line = mtrpeiarg.replaceAll("$1");\r
268 }\r
269 }\r
270 \r
271 Matcher mtrmac;\r
272 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);\r
273 if (mtrmac.find()) {\r
274 line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
275 }\r
276 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);\r
277 if (mtrmac.find()) {\r
278 line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
279 }\r
280 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);\r
281 if (mtrmac.find()) {\r
282 line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
283 }\r
284 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);\r
285 if (mtrmac.find()) {\r
286 line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
287 }\r
288 if (line.contains("EFI_UINTN_ALIGN_MASK")) {\r
289 line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
290 }\r
291\r
292 show(filefunc, "function");\r
293 show(filemacro, "macro");\r
294 show(fileguid, "guid");\r
295 show(fileppi, "ppi");\r
296 show(fileprotocol, "protocol");\r
297 if (!filer8only.isEmpty()) {\r
298 ui.println("Converting r8only : " + filer8only);\r
299 }\r
300\r
301 filefunc.clear();\r
302 filemacro.clear();\r
303 fileguid.clear();\r
304 fileppi.clear();\r
305 fileprotocol.clear();\r
306 filer8only.clear();\r
e6a5df3b 307\r
0dc8c589 308 return line;\r
309 }\r
310 \r
311 private void show(Set<r8tor9> hash, String sh) {\r
312 Iterator<r8tor9> it = hash.iterator();\r
313 r8tor9 temp;\r
314 if (showdetails) {\r
315 if (!hash.isEmpty()) {\r
316 ui.print("Converting " + sh + " : ");\r
317 while (it.hasNext()) {\r
318 temp = it.next();\r
319 ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
320 }\r
321 ui.println("");\r
322 }\r
323 }\r
324 }\r
325 \r
326 private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
327 Iterator<String> it;\r
328 String r8thing;\r
329 String r9thing;\r
330 it = hash.iterator();\r
331 while (it.hasNext()) {\r
332 r8thing = it.next();\r
333 if ((r9thing = db.getR9Guidname(r8thing)) != null) {\r
334 if (!r8thing.equals(r9thing)) {\r
335 if (line.contains(r8thing)) {\r
336 line = line.replaceAll(r8thing, r9thing);\r
337 filehash.add(new r8tor9(r8thing, r9thing));\r
338 }\r
339 }\r
340 }\r
341 }\r
342 }\r
343}\r