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
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.
13 package org
.tianocore
.migration
;
15 import java
.io
.BufferedReader
;
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
;
23 import org
.tianocore
.FilenameDocument
;
24 import org
.tianocore
.ModuleSurfaceAreaDocument
;
25 import org
.tianocore
.MsaHeaderDocument
;
26 import org
.tianocore
.SourceFilesDocument
;
28 public final class ModuleReader
implements Common
.ForDoAll
{
29 private static final ModuleReader modulereader
= new ModuleReader();
31 private ModuleInfo mi
;
33 private final CommentLaplace commentlaplace
= new CommentLaplace();
35 private static final Pattern ptninfequation
= Pattern
36 .compile("([^\\s]*)\\s*=\\s*([^\\s]*)");
38 private static final Pattern ptnsection
= Pattern
.compile(
39 "\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern
.MULTILINE
);
41 private static final Pattern ptnfilename
= Pattern
.compile("[^\\s]+");
43 public final void ModuleScan() throws Exception
{
44 Common
.toDoAll(mi
.modulepath
, ModuleInfo
.class.getMethod("enroll",
45 String
.class), mi
, null, Common
.FILE
);
48 String filename
= null;
49 if (mi
.msaorinf
.isEmpty()) {
50 MigrationTool
.ui
.println("No INF nor MSA file found!");
53 if (mi
.msaorinf
.size() == 1) {
54 filename
= (String
) mi
.msaorinf
.toArray()[0];
56 filename
= MigrationTool
.ui
.choose(
57 "Found .inf or .msa file for module\n" + mi
.modulepath
58 + "\nChoose one Please", mi
.msaorinf
.toArray());
62 if (filename
.contains(".inf")) {
64 } else if (filename
.contains(".msa")) {
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();
79 mi
.modulename
= msaheader
.getModuleName();
80 mi
.guidvalue
= msaheader
.getGuidValue();
81 mi
.moduletype
= msaheader
.getModuleType().toString(); // ???
83 SourceFilesDocument
.SourceFiles sourcefiles
= msa
.getSourceFiles();
86 Iterator
<FilenameDocument
.Filename
> li
= sourcefiles
.getFilenameList()
88 while (li
.hasNext()) {
89 if (!mi
.localmodulesources
.contains(temp
= li
.next().toString())) {
90 System
.out
.println("Source File Missing! : " + temp
);
95 private final String
extractLicense(String wholeline
) throws Exception
{
97 String license
= null;
99 BufferedReader rd
= new BufferedReader(new StringReader(wholeline
));
100 while ((tempLine
= rd
.readLine()) != null) {
101 if (tempLine
.contains("#")) {
102 if (tempLine
.contains("Copyright")) {
104 // Find license info.
107 while ((tempLine
= rd
.readLine()) != null) {
108 if (!tempLine
.contains("#")
109 || tempLine
.contains("Module Name:")
110 || tempLine
.contains("Abstract:")) {
112 // We assume license ends here.
118 .replaceAll("\\s*[#]\\s*(.*)", "$1\n");
127 private final void readInf(String name
) throws Exception
{
128 System
.out
.println("\nParsing INF file: " + name
);
130 Matcher mtrinfequation
;
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);
144 if (mtrinfequation
.group(1).matches("FILE_GUID")) {
145 mi
.guidvalue
= mtrinfequation
.group(2);
147 if (mtrinfequation
.group(1).matches("COMPONENT_TYPE")) {
148 mi
.moduletype
= mtrinfequation
.group(2);
149 if (mi
.moduletype
.matches("LIBRARY")) {
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);
161 if (mtrinfequation
.group(1).matches("DPX_SOURCE")) {
162 if (!mi
.localmodulesources
.contains(mtrinfequation
164 MigrationTool
.ui
.println("DPX File Missing! : "
165 + mtrinfequation
.group(2));
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())) {
176 .println("Warn: Source File Missing! : "
177 + mtrfilename
.group());
181 if (mtrsection
.group(1).matches("includes.")) {
182 mtrfilename
= ptnfilename
.matcher(mtrsection
.group(2));
183 while (mtrfilename
.find()) {
184 mi
.infincludes
.add(mtrfilename
.group());
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
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
,
209 Common
.oneLevelDirCopy(mi
.modulepath
.replaceAll(
210 Common
.STRSEPARATER
, "$1"), mi
.temppath
, ".h");
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
);
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));
235 Common
.toDoAll(mi
.temppath
, this, Common
.FILE
);
237 parsePreProcessedSourceCode();
241 private final void parsePreProcessedSourceCode() throws Exception
{
242 BufferedReader rd
= null;
247 Iterator
<String
> ii
= mi
.localmodulesources
.iterator();
248 while (ii
.hasNext()) {
250 if (temp
.contains(".c") || temp
.contains(".dxs")) {
251 mi
.preprocessedccodes
.add(temp
);
255 ii
= mi
.preprocessedccodes
.iterator();
257 Pattern patefifuncc
= Pattern
.compile(
258 "g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)", Pattern
.MULTILINE
);
262 Matcher matenclosereplace
;
266 while (ii
.hasNext()) {
267 StringBuffer wholefile
= new StringBuffer();
269 rd
= new BufferedReader(new FileReader(mi
.temppath
+ File
.separator
271 while ((line
= rd
.readLine()) != null) {
272 wholefile
.append(line
+ '\n');
274 line
= wholefile
.toString();
277 matguid
= Guid
.ptnguid
.matcher(line
); // several ways to implement
278 // this , which one is
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
293 // matguid.appendReplacement(result,
294 // MigrationTool.db.getR9Guidname(temp)); // search the
295 // database for all 3 kinds of guids , high cost
298 // matguid.appendTail(result);
299 // line = result.toString();
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));
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));
318 // matfuncc.appendTail(result);
319 // line = result.toString();
322 matmacro
= Macro
.ptntmacro
.matcher(line
);
323 while (matmacro
.find()) {
324 if ((temp
= Macro
.register(matmacro
, mi
, MigrationTool
.db
)) != null) {
328 // find function definition
329 // replace all {} to @
330 while ((matenclosereplace
= Func
.ptnbrace
.matcher(line
)).find()) {
331 line
= matenclosereplace
.replaceAll("@");
334 matfuncd
= Func
.ptnfuncd
.matcher(line
);
335 while (matfuncd
.find()) {
336 if ((temp
= Func
.register(matfuncd
, mi
, MigrationTool
.db
)) != null) {
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
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
365 templine
= mtrinclude
.group();
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";
387 templine
= MigrationTool
.MIGRATIONCOMMENT
388 + mtrinclude
.group();
391 mtrinclude
.appendReplacement(wholebuffer
, templine
);
393 mtrinclude
.appendTail(wholebuffer
);
394 return wholebuffer
.toString();
397 public boolean recognize(String filename
) {
398 return filename
.contains(".c") || filename
.contains(".h")
399 || filename
.contains(".dxs");
402 public String
namechange(String oldname
) {
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
);
416 public boolean filter(File dir
) {
420 // -----------------------------------ForDoAll-----------------------------------//
422 public final void setModuleInfo(ModuleInfo m
) {
426 public static final void aimAt(ModuleInfo mi
) throws Exception
{
427 modulereader
.setModuleInfo(mi
);
428 modulereader
.ModuleScan();