]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/MigrationTools/org/tianocore/migration/ModuleReader.java
remove unnecessary check for NULL pointer.
[mirror_edk2.git] / Tools / Java / Source / MigrationTools / org / tianocore / migration / ModuleReader.java
1 /** @file
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13 package org.tianocore.migration;
14
15 import java.io.BufferedReader;
16 import java.io.File;
17 import java.io.FileReader;
18 import java.io.StringReader;
19 import java.util.Iterator;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 import org.tianocore.FilenameDocument;
24 import org.tianocore.ModuleSurfaceAreaDocument;
25 import org.tianocore.MsaHeaderDocument;
26 import org.tianocore.SourceFilesDocument;
27
28 public final class ModuleReader implements Common.ForDoAll {
29 private static final ModuleReader modulereader = new ModuleReader();
30
31 private ModuleInfo mi;
32
33 private final CommentLaplace commentlaplace = new CommentLaplace();
34
35 private static final Pattern ptninfequation = Pattern
36 .compile("([^\\s]*)\\s*=\\s*([^\\s]*)");
37
38 private static final Pattern ptnsection = Pattern.compile(
39 "\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern.MULTILINE);
40
41 private static final Pattern ptnfilename = Pattern.compile("[^\\s]+");
42
43 public final void ModuleScan() throws Exception {
44 Common.toDoAll(mi.modulepath, ModuleInfo.class.getMethod("enroll",
45 String.class), mi, null, Common.FILE);
46
47 // inf&msa
48 String filename = null;
49 if (mi.msaorinf.isEmpty()) {
50 MigrationTool.ui.println("No INF nor MSA file found!");
51 System.exit(0);
52 } else {
53 if (mi.msaorinf.size() == 1) {
54 filename = (String) mi.msaorinf.toArray()[0];
55 } else {
56 filename = MigrationTool.ui.choose(
57 "Found .inf or .msa file for module\n" + mi.modulepath
58 + "\nChoose one Please", mi.msaorinf.toArray());
59 }
60 }
61
62 if (filename.contains(".inf")) {
63 readInf(filename);
64 } else if (filename.contains(".msa")) {
65 readMsa(filename);
66 }
67 // inf&msa
68
69 preProcessModule();
70 }
71
72 private final void readMsa(String name) throws Exception {
73 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory
74 .parse(new File(mi.modulepath + File.separator + name));
75 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc
76 .getModuleSurfaceArea();
77 MsaHeaderDocument.MsaHeader msaheader = msa.getMsaHeader();
78
79 mi.modulename = msaheader.getModuleName();
80 mi.guidvalue = msaheader.getGuidValue();
81 mi.moduletype = msaheader.getModuleType().toString(); // ???
82
83 SourceFilesDocument.SourceFiles sourcefiles = msa.getSourceFiles();
84
85 String temp;
86 Iterator<FilenameDocument.Filename> li = sourcefiles.getFilenameList()
87 .iterator();
88 while (li.hasNext()) {
89 if (!mi.localmodulesources.contains(temp = li.next().toString())) {
90 System.out.println("Source File Missing! : " + temp);
91 }
92 }
93 }
94
95 private final String extractLicense(String wholeline) throws Exception {
96 String tempLine;
97 String license = null;
98
99 BufferedReader rd = new BufferedReader(new StringReader(wholeline));
100 while ((tempLine = rd.readLine()) != null) {
101 if (tempLine.contains("#")) {
102 if (tempLine.contains("Copyright")) {
103 //
104 // Find license info.
105 //
106 license = "";
107 while ((tempLine = rd.readLine()) != null) {
108 if (!tempLine.contains("#")
109 || tempLine.contains("Module Name:")
110 || tempLine.contains("Abstract:")) {
111 //
112 // We assume license ends here.
113 //
114 break;
115 }
116 license += " "
117 + tempLine
118 .replaceAll("\\s*[#]\\s*(.*)", "$1\n");
119 }
120 break;
121 }
122 }
123 }
124 return license;
125 }
126
127 private final void readInf(String name) throws Exception {
128 System.out.println("\nParsing INF file: " + name);
129 String wholeline;
130 Matcher mtrinfequation;
131 Matcher mtrsection;
132 Matcher mtrfilename;
133
134 wholeline = Common.file2string(mi.modulepath + File.separator + name);
135 mi.license = extractLicense(wholeline);
136 mtrsection = ptnsection.matcher(wholeline);
137 while (mtrsection.find()) {
138 if (mtrsection.group(1).matches("defines")) {
139 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));
140 while (mtrinfequation.find()) {
141 if (mtrinfequation.group(1).matches("BASE_NAME")) {
142 mi.modulename = mtrinfequation.group(2);
143 }
144 if (mtrinfequation.group(1).matches("FILE_GUID")) {
145 mi.guidvalue = mtrinfequation.group(2);
146 }
147 if (mtrinfequation.group(1).matches("COMPONENT_TYPE")) {
148 mi.moduletype = mtrinfequation.group(2);
149 if (mi.moduletype.matches("LIBRARY")) {
150 mi.isLibrary = true;
151 }
152 }
153 }
154 }
155 if (mtrsection.group(1).contains("nmake.")) {
156 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));
157 while (mtrinfequation.find()) {
158 if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {
159 mi.entrypoint = mtrinfequation.group(2);
160 }
161 if (mtrinfequation.group(1).matches("DPX_SOURCE")) {
162 if (!mi.localmodulesources.contains(mtrinfequation
163 .group(2))) {
164 MigrationTool.ui.println("DPX File Missing! : "
165 + mtrinfequation.group(2));
166 }
167 }
168 }
169 }
170 if (mtrsection.group(1).contains("sources.")) {
171 mtrfilename = ptnfilename.matcher(mtrsection.group(2));
172 while (mtrfilename.find()) {
173 mi.infsources.add(mtrfilename.group());
174 if (!mi.localmodulesources.contains(mtrfilename.group())) {
175 MigrationTool.ui
176 .println("Warn: Source File Missing! : "
177 + mtrfilename.group());
178 }
179 }
180 }
181 if (mtrsection.group(1).matches("includes.")) {
182 mtrfilename = ptnfilename.matcher(mtrsection.group(2));
183 while (mtrfilename.find()) {
184 mi.infincludes.add(mtrfilename.group());
185 }
186 }
187 }
188 }
189
190 private final void preProcessModule() throws Exception {
191 // according to .inf file, add extraordinary includes and sourcefiles
192 Common.dirCopy(mi.modulepath, mi.temppath); // collect all
193 // Laplace.namechange to
194 // here???
195
196 if (!mi.infincludes.isEmpty()) {
197 Iterator<String> it = mi.infincludes.iterator();
198 String tempincludename = null;
199 while (it.hasNext()) {
200 tempincludename = it.next();
201 if (tempincludename.contains("..")) {
202 Matcher mtr = Common.PTNSEPARATER.matcher(tempincludename);
203 if (mtr.find() && !mtr.group(2).matches(".")) {
204 Common.oneLevelDirCopy(mi.modulepath.replaceAll(
205 Common.STRSEPARATER, "$1")
206 + File.separator + mtr.group(2), mi.temppath,
207 ".h");
208 } else {
209 Common.oneLevelDirCopy(mi.modulepath.replaceAll(
210 Common.STRSEPARATER, "$1"), mi.temppath, ".h");
211 }
212 }
213 }
214 }
215 if (!mi.infsources.isEmpty()) {
216 Iterator<String> it = mi.infsources.iterator();
217 String tempsourcename = null;
218 while (it.hasNext()) {
219 tempsourcename = it.next();
220 if (tempsourcename.contains("..")) {
221 Common.ensureDir(mi.temppath + File.separator
222 + "MT_Parent_Sources");
223 Matcher mtr = Common.PTNSEPARATER.matcher(tempsourcename);
224 if (mtr.find()) {
225 Common.fileCopy(mi.modulepath.replaceAll(
226 Common.STRSEPARATER, "$1")
227 + File.separator + mtr.group(2), mi.temppath
228 + File.separator + "MT_Parent_Sources"
229 + File.separator + mtr.group(2));
230 }
231 }
232 }
233 }
234
235 Common.toDoAll(mi.temppath, this, Common.FILE);
236
237 parsePreProcessedSourceCode();
238
239 }
240
241 private final void parsePreProcessedSourceCode() throws Exception {
242 BufferedReader rd = null;
243 String ifile = null;
244 String line = null;
245 String temp = null;
246
247 Iterator<String> ii = mi.localmodulesources.iterator();
248 while (ii.hasNext()) {
249 temp = ii.next();
250 if (temp.contains(".c") || temp.contains(".dxs")) {
251 mi.preprocessedccodes.add(temp);
252 }
253 }
254
255 ii = mi.preprocessedccodes.iterator();
256
257 Pattern patefifuncc = Pattern.compile(
258 "g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)", Pattern.MULTILINE);
259 Matcher matguid;
260 Matcher matfuncc;
261 Matcher matfuncd;
262 Matcher matenclosereplace;
263 Matcher matefifuncc;
264 Matcher matmacro;
265
266 while (ii.hasNext()) {
267 StringBuffer wholefile = new StringBuffer();
268 ifile = ii.next();
269 rd = new BufferedReader(new FileReader(mi.temppath + File.separator
270 + ifile));
271 while ((line = rd.readLine()) != null) {
272 wholefile.append(line + '\n');
273 }
274 line = wholefile.toString();
275
276 // find guid
277 matguid = Guid.ptnguid.matcher(line); // several ways to implement
278 // this , which one is
279 // faster ? :
280 while (matguid.find()) { // 1.currently , find once , then call
281 // to identify which is it
282 if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use
283 // 3
284 // different
285 // matchers
286 // ,
287 // search
288 // 3
289 // times
290 // to
291 // find
292 // each
293 // matguid.appendReplacement(result,
294 // MigrationTool.db.getR9Guidname(temp)); // search the
295 // database for all 3 kinds of guids , high cost
296 }
297 }
298 // matguid.appendTail(result);
299 // line = result.toString();
300
301 // find EFI call in form of '->' , many
302 // 'gUnicodeCollationInterface->' like things are not changed
303 // This item is not simply replaced , special operation is required.
304 matefifuncc = patefifuncc.matcher(line);
305 while (matefifuncc.find()) {
306 mi.hashEFIcall.add(matefifuncc.group(2));
307 }
308
309 // find function call
310 matfuncc = Func.ptnfuncc.matcher(line);
311 while (matfuncc.find()) {
312 if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {
313 // MigrationTool.ui.println(ifile + " dofunc " + temp);
314 // matfuncc.appendReplacement(result,
315 // MigrationTool.db.getR9Func(temp));
316 }
317 }
318 // matfuncc.appendTail(result);
319 // line = result.toString();
320
321 // find macro
322 matmacro = Macro.ptntmacro.matcher(line);
323 while (matmacro.find()) {
324 if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {
325 }
326 }
327
328 // find function definition
329 // replace all {} to @
330 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {
331 line = matenclosereplace.replaceAll("@");
332 }
333
334 matfuncd = Func.ptnfuncd.matcher(line);
335 while (matfuncd.find()) {
336 if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {
337 }
338 }
339 }
340
341 // op on hash
342 Iterator<String> funcci = mi.hashfuncc.iterator();
343 while (funcci.hasNext()) {
344 if (!mi.hashfuncd.contains(temp = funcci.next())
345 && !mi.hashEFIcall.contains(temp)) {
346 mi.hashnonlocalfunc.add(temp); // this set contains both
347 // changed and not changed items
348 }
349 }
350 }
351
352 public class CommentLaplace extends Common.Laplace {
353 public String operation(String wholeline) {
354 StringBuffer wholebuffer = new StringBuffer();
355 String templine = null;
356 Pattern ptnincludefile = Pattern.compile("[\"<](.*[.]h)[\">]");
357 Pattern ptninclude = Pattern.compile("#include\\s*(.*)");
358 Matcher mtrinclude = ptninclude.matcher(wholeline);
359 Matcher mtrincludefile = null;
360 while (mtrinclude.find()) {
361 mtrincludefile = ptnincludefile.matcher(mtrinclude.group(1));
362 if (mtrincludefile.find()
363 && mi.localmodulesources.contains(mtrincludefile
364 .group(1))) {
365 templine = mtrinclude.group();
366 } else {
367 String line = mtrinclude.group().toLowerCase();
368 if (line.contains("pal.h")) {
369 templine = "#include <IndustryStandard/Pal.h>\n";
370 } else if (line.contains("sal.h")) {
371 templine = "#include <IndustryStandard/Sal.h>\n";
372 } else if (line.contains("pci22.h")) {
373 templine = "#include <IndustryStandard/Pci22.h>\n";
374 } else if (line.contains("pci23.h")) {
375 templine = "#include <IndustryStandard/Pci23.h>\n";
376 } else if (line.contains("pci30.h")) {
377 templine = "#include <IndustryStandard/Pci30.h>\n";
378 } else if (line.contains("pci.h")) {
379 templine = "#include <IndustryStandard/Pci.h>\n";
380 } else if (line.contains("acpi.h")) {
381 templine = "#include <IndustryStandard/Acpi.h>\n";
382 } else if (line.contains("scsi.h")) {
383 templine = "#include <IndustryStandard/Scsi.h>\n";
384 } else if (line.contains("usb.h")) {
385 templine = "#include <IndustryStandard/Usb.h>\n";
386 } else {
387 templine = MigrationTool.MIGRATIONCOMMENT
388 + mtrinclude.group();
389 }
390 }
391 mtrinclude.appendReplacement(wholebuffer, templine);
392 }
393 mtrinclude.appendTail(wholebuffer);
394 return wholebuffer.toString();
395 }
396
397 public boolean recognize(String filename) {
398 return filename.contains(".c") || filename.contains(".h")
399 || filename.contains(".dxs");
400 }
401
402 public String namechange(String oldname) {
403 return oldname;
404 }
405 }
406
407 // -----------------------------------ForDoAll-----------------------------------//
408 public void run(String filepath) throws Exception {
409 String name = mi.temppath + File.separator
410 + filepath.replace(mi.temppath + File.separator, "");
411 if (commentlaplace.recognize(name)) {
412 commentlaplace.transform(name, name);
413 }
414 }
415
416 public boolean filter(File dir) {
417 return true;
418 }
419
420 // -----------------------------------ForDoAll-----------------------------------//
421
422 public final void setModuleInfo(ModuleInfo m) {
423 mi = m;
424 }
425
426 public static final void aimAt(ModuleInfo mi) throws Exception {
427 modulereader.setModuleInfo(mi);
428 modulereader.ModuleScan();
429 }
430 }