]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
Follow changes on grammar in messages by Larry
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / SourceFileReplacer.java
... / ...
CommitLineData
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
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
54 "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!\r
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
72 String outname = null;\r
73 String inname = null;\r
74 if (ui.yesOrNo("Changes will be made to the Source Code. View details?")) {\r
75 showdetails = true;\r
76 }\r
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
87 ui.println("\nModifying file: " + inname);\r
88 mi.ensureDir(modulepath + File.separator + "result" + File.separator + outname);\r
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
91 outfile.flush();\r
92 outfile.close();\r
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
96 } else {\r
97 outname = inname;\r
98 }\r
99 ui.println("\nCopying file: " + inname);\r
100 mi.ensureDir(modulepath + File.separator + "result" + File.separator + outname);\r
101 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname)));\r
102 outfile.append(sourcefiletostring(modulepath + File.separator + "temp" + File.separator + inname));\r
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
115 String line = sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c");\r
116 mi.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c");\r
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
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
140 \r
141 mi.localmodulesources.add("R8Lib.h");\r
142 mi.localmodulesources.add("R8Lib.c");\r
143 }\r
144 \r
145 private String sourcefiletostring(String filename) throws Exception {\r
146 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
147 StringBuffer wholefile = new StringBuffer();\r
148 String line;\r
149 while ((line = rd.readLine()) != null) {\r
150 wholefile.append(line + "\n");\r
151 }\r
152 return wholefile.toString();\r
153 }\r
154 \r
155 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
156 private String sourcefilereplace(String filename) throws Exception {\r
157 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
158 StringBuffer wholefile = new StringBuffer();\r
159 String line;\r
160 String r8thing;\r
161 String r9thing;\r
162 r8tor9 temp;\r
163 boolean addr8 = false;\r
164\r
165 Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !\r
166 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);\r
167\r
168 while ((line = rd.readLine()) != null) {\r
169 wholefile.append(line + "\n");\r
170 }\r
171 line = wholefile.toString();\r
172 \r
173 // replace BS -> gBS , RT -> gRT\r
174 Matcher mat = pat.matcher(line);\r
175 if (mat.find()) { // add a library here\r
176 ui.println("Converting all BS->gBS, RT->gRT");\r
177 line = mat.replaceAll("g$1$2$3"); //unknown correctiveness\r
178 }\r
179 mat.reset();\r
180 while (mat.find()) {\r
181 if (mat.group(1).matches("BS")) {\r
182 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
183 }\r
184 if (mat.group(1).matches("RT")) {\r
185 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");\r
186 }\r
187 }\r
188 /*\r
189 // remove EFI_DRIVER_ENTRY_POINT\r
190 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
191 Matcher matentrypoint = patentrypoint.matcher(line);\r
192 if (matentrypoint.find()) {\r
193 ui.println("Deleting Entry_Point");\r
194 line = matentrypoint.replaceAll("");\r
195 }\r
196 */\r
197 // start replacing names\r
198 Iterator<String> it;\r
199 // Converting non-locla function\r
200 it = mi.hashnonlocalfunc.iterator();\r
201 while (it.hasNext()) {\r
202 r8thing = it.next();\r
203 if (r8thing.matches("EfiInitializeDriverLib")) { //s\r
204 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p\r
205 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e\r
206 } else if (r8thing.matches("DxeInitializeDriverLib")) { //c\r
207 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i\r
208 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a\r
209 mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l\r
210 } else { //\r
211 mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here\r
212 }\r
213 \r
214 if ((r9thing = db.getR9Func(r8thing)) != null) {\r
215 if (!r8thing.equals(r9thing)) {\r
216 if (line.contains(r8thing)) {\r
217 line = line.replaceAll(r8thing, r9thing);\r
218 filefunc.add(new r8tor9(r8thing, r9thing));\r
219 Iterator<r8tor9> rt = filefunc.iterator();\r
220 while (rt.hasNext()) {\r
221 temp = rt.next();\r
222 if (r8only.contains(temp.r8thing)) {\r
223 filer8only.add(r8thing);\r
224 mi.hashr8only.add(r8thing);\r
225 addr8 = true;\r
226 }\r
227 }\r
228 }\r
229 }\r
230 }\r
231 } //is any of the guids changed?\r
232 if (addr8 == true) {\r
233 line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");\r
234 }\r
235 \r
236 // Converting macro\r
237 it = mi.hashnonlocalmacro.iterator();\r
238 while (it.hasNext()) { //macros are all assumed MdePkg currently\r
239 r8thing = it.next();\r
240 //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); \r
241 if ((r9thing = db.getR9Macro(r8thing)) != null) {\r
242 if (line.contains(r8thing)) {\r
243 line = line.replaceAll(r8thing, r9thing);\r
244 filemacro.add(new r8tor9(r8thing, r9thing));\r
245 }\r
246 }\r
247 }\r
248\r
249 // Converting guid\r
250 replaceGuid(line, mi.guid, "guid", fileguid);\r
251 replaceGuid(line, mi.ppi, "ppi", fileppi);\r
252 replaceGuid(line, mi.protocol, "protocol", fileprotocol);\r
253\r
254 // Converting Pei\r
255 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%\r
256 Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);\r
257 if (mi.moduletype.contains("PEIM")) {\r
258 Matcher mtrpei = ptnpei.matcher(line);\r
259 while (mtrpei.find()) { // ! add a library here !\r
260 line = mtrpei.replaceAll("PeiServices$1#%$2");\r
261 mi.hashrequiredr9libs.add("PeiServicesLib");\r
262 }\r
263 mtrpei.reset();\r
264 if (line.contains("PeiServicesCopyMem")) {\r
265 line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r
266 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
267 }\r
268 if (line.contains("PeiServicesSetMem")) {\r
269 line = line.replaceAll("PeiServicesSetMem#%", "SetMem");\r
270 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
271 }\r
272\r
273 // Second , find all #% to drop the arg "PeiServices"\r
274 Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);\r
275 Matcher mtrpeiarg = ptnpeiarg.matcher(line);\r
276 while (mtrpeiarg.find()) {\r
277 line = mtrpeiarg.replaceAll("$1");\r
278 }\r
279 }\r
280 \r
281 Matcher mtrmac;\r
282 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);\r
283 if (mtrmac.find()) {\r
284 line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
285 }\r
286 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);\r
287 if (mtrmac.find()) {\r
288 line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
289 }\r
290 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);\r
291 if (mtrmac.find()) {\r
292 line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
293 }\r
294 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);\r
295 if (mtrmac.find()) {\r
296 line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
297 }\r
298 if (line.contains("EFI_UINTN_ALIGN_MASK")) {\r
299 line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
300 }\r
301\r
302 show(filefunc, "function");\r
303 show(filemacro, "macro");\r
304 show(fileguid, "guid");\r
305 show(fileppi, "ppi");\r
306 show(fileprotocol, "protocol");\r
307 if (!filer8only.isEmpty()) {\r
308 ui.println("Converting r8only : " + filer8only);\r
309 }\r
310\r
311 filefunc.clear();\r
312 filemacro.clear();\r
313 fileguid.clear();\r
314 fileppi.clear();\r
315 fileprotocol.clear();\r
316 filer8only.clear();\r
317\r
318 return line;\r
319 }\r
320 \r
321 private void show(Set<r8tor9> hash, String sh) {\r
322 Iterator<r8tor9> it = hash.iterator();\r
323 r8tor9 temp;\r
324 if (showdetails) {\r
325 if (!hash.isEmpty()) {\r
326 ui.print("Converting " + sh + " : ");\r
327 while (it.hasNext()) {\r
328 temp = it.next();\r
329 ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
330 }\r
331 ui.println("");\r
332 }\r
333 }\r
334 }\r
335 \r
336 private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
337 Iterator<String> it;\r
338 String r8thing;\r
339 String r9thing;\r
340 it = hash.iterator();\r
341 while (it.hasNext()) {\r
342 r8thing = it.next();\r
343 if ((r9thing = db.getR9Guidname(r8thing)) != null) {\r
344 if (!r8thing.equals(r9thing)) {\r
345 if (line.contains(r8thing)) {\r
346 line = line.replaceAll(r8thing, r9thing);\r
347 filehash.add(new r8tor9(r8thing, r9thing));\r
348 }\r
349 }\r
350 }\r
351 }\r
352 }\r
353}\r