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);
152 if (mtrsection
.group(1).contains("nmake.")) {
153 mtrinfequation
= ptninfequation
.matcher(mtrsection
.group(2));
154 while (mtrinfequation
.find()) {
155 if (mtrinfequation
.group(1).matches("IMAGE_ENTRY_POINT")) {
156 mi
.entrypoint
= mtrinfequation
.group(2);
158 if (mtrinfequation
.group(1).matches("DPX_SOURCE")) {
159 if (!mi
.localmodulesources
.contains(mtrinfequation
161 MigrationTool
.ui
.println("DPX File Missing! : "
162 + mtrinfequation
.group(2));
167 if (mtrsection
.group(1).contains("sources.")) {
168 mtrfilename
= ptnfilename
.matcher(mtrsection
.group(2));
169 while (mtrfilename
.find()) {
170 mi
.infsources
.add(mtrfilename
.group());
171 if (!mi
.localmodulesources
.contains(mtrfilename
.group())) {
173 .println("Warn: Source File Missing! : "
174 + mtrfilename
.group());
178 if (mtrsection
.group(1).matches("includes.")) {
179 mtrfilename
= ptnfilename
.matcher(mtrsection
.group(2));
180 while (mtrfilename
.find()) {
181 mi
.infincludes
.add(mtrfilename
.group());
187 private final void preProcessModule() throws Exception
{
188 // according to .inf file, add extraordinary includes and sourcefiles
189 Common
.dirCopy(mi
.modulepath
, mi
.temppath
); // collect all
190 // Laplace.namechange to
193 if (!mi
.infincludes
.isEmpty()) {
194 Iterator
<String
> it
= mi
.infincludes
.iterator();
195 String tempincludename
= null;
196 while (it
.hasNext()) {
197 tempincludename
= it
.next();
198 if (tempincludename
.contains("..")) {
199 Matcher mtr
= Common
.PTNSEPARATER
.matcher(tempincludename
);
200 if (mtr
.find() && !mtr
.group(2).matches(".")) {
201 Common
.oneLevelDirCopy(mi
.modulepath
.replaceAll(
202 Common
.STRSEPARATER
, "$1")
203 + File
.separator
+ mtr
.group(2), mi
.temppath
,
206 Common
.oneLevelDirCopy(mi
.modulepath
.replaceAll(
207 Common
.STRSEPARATER
, "$1"), mi
.temppath
, ".h");
212 if (!mi
.infsources
.isEmpty()) {
213 Iterator
<String
> it
= mi
.infsources
.iterator();
214 String tempsourcename
= null;
215 while (it
.hasNext()) {
216 tempsourcename
= it
.next();
217 if (tempsourcename
.contains("..")) {
218 Common
.ensureDir(mi
.temppath
+ File
.separator
219 + "MT_Parent_Sources");
220 Matcher mtr
= Common
.PTNSEPARATER
.matcher(tempsourcename
);
222 Common
.fileCopy(mi
.modulepath
.replaceAll(
223 Common
.STRSEPARATER
, "$1")
224 + File
.separator
+ mtr
.group(2), mi
.temppath
225 + File
.separator
+ "MT_Parent_Sources"
226 + File
.separator
+ mtr
.group(2));
232 Common
.toDoAll(mi
.temppath
, this, Common
.FILE
);
234 parsePreProcessedSourceCode();
238 private final void parsePreProcessedSourceCode() throws Exception
{
239 BufferedReader rd
= null;
244 Iterator
<String
> ii
= mi
.localmodulesources
.iterator();
245 while (ii
.hasNext()) {
247 if (temp
.contains(".c") || temp
.contains(".dxs")) {
248 mi
.preprocessedccodes
.add(temp
);
252 ii
= mi
.preprocessedccodes
.iterator();
254 Pattern patefifuncc
= Pattern
.compile(
255 "g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)", Pattern
.MULTILINE
);
259 Matcher matenclosereplace
;
263 while (ii
.hasNext()) {
264 StringBuffer wholefile
= new StringBuffer();
266 rd
= new BufferedReader(new FileReader(mi
.temppath
+ File
.separator
268 while ((line
= rd
.readLine()) != null) {
269 wholefile
.append(line
+ '\n');
271 line
= wholefile
.toString();
274 matguid
= Guid
.ptnguid
.matcher(line
); // several ways to implement
275 // this , which one is
277 while (matguid
.find()) { // 1.currently , find once , then call
278 // to identify which is it
279 if ((temp
= Guid
.register(matguid
, mi
, MigrationTool
.db
)) != null) { // 2.use
290 // matguid.appendReplacement(result,
291 // MigrationTool.db.getR9Guidname(temp)); // search the
292 // database for all 3 kinds of guids , high cost
295 // matguid.appendTail(result);
296 // line = result.toString();
298 // find EFI call in form of '->' , many
299 // 'gUnicodeCollationInterface->' like things are not changed
300 // This item is not simply replaced , special operation is required.
301 matefifuncc
= patefifuncc
.matcher(line
);
302 while (matefifuncc
.find()) {
303 mi
.hashEFIcall
.add(matefifuncc
.group(2));
306 // find function call
307 matfuncc
= Func
.ptnfuncc
.matcher(line
);
308 while (matfuncc
.find()) {
309 if ((temp
= Func
.register(matfuncc
, mi
, MigrationTool
.db
)) != null) {
310 // MigrationTool.ui.println(ifile + " dofunc " + temp);
311 // matfuncc.appendReplacement(result,
312 // MigrationTool.db.getR9Func(temp));
315 // matfuncc.appendTail(result);
316 // line = result.toString();
319 matmacro
= Macro
.ptntmacro
.matcher(line
);
320 while (matmacro
.find()) {
321 if ((temp
= Macro
.register(matmacro
, mi
, MigrationTool
.db
)) != null) {
325 // find function definition
326 // replace all {} to @
327 while ((matenclosereplace
= Func
.ptnbrace
.matcher(line
)).find()) {
328 line
= matenclosereplace
.replaceAll("@");
331 matfuncd
= Func
.ptnfuncd
.matcher(line
);
332 while (matfuncd
.find()) {
333 if ((temp
= Func
.register(matfuncd
, mi
, MigrationTool
.db
)) != null) {
339 Iterator
<String
> funcci
= mi
.hashfuncc
.iterator();
340 while (funcci
.hasNext()) {
341 if (!mi
.hashfuncd
.contains(temp
= funcci
.next())
342 && !mi
.hashEFIcall
.contains(temp
)) {
343 mi
.hashnonlocalfunc
.add(temp
); // this set contains both
344 // changed and not changed items
349 public class CommentLaplace
extends Common
.Laplace
{
350 public String
operation(String wholeline
) {
351 StringBuffer wholebuffer
= new StringBuffer();
352 String templine
= null;
353 Pattern ptnincludefile
= Pattern
.compile("[\"<](.*[.]h)[\">]");
354 Pattern ptninclude
= Pattern
.compile("#include\\s*(.*)");
355 Matcher mtrinclude
= ptninclude
.matcher(wholeline
);
356 Matcher mtrincludefile
= null;
357 while (mtrinclude
.find()) {
358 mtrincludefile
= ptnincludefile
.matcher(mtrinclude
.group(1));
359 if (mtrincludefile
.find()
360 && mi
.localmodulesources
.contains(mtrincludefile
362 templine
= mtrinclude
.group();
364 templine
= MigrationTool
.MIGRATIONCOMMENT
365 + mtrinclude
.group();
367 mtrinclude
.appendReplacement(wholebuffer
, templine
);
369 mtrinclude
.appendTail(wholebuffer
);
370 return wholebuffer
.toString();
373 public boolean recognize(String filename
) {
374 return filename
.contains(".c") || filename
.contains(".h")
375 || filename
.contains(".dxs");
378 public String
namechange(String oldname
) {
383 // -----------------------------------ForDoAll-----------------------------------//
384 public void run(String filepath
) throws Exception
{
385 String name
= mi
.temppath
+ File
.separator
386 + filepath
.replace(mi
.temppath
+ File
.separator
, "");
387 if (commentlaplace
.recognize(name
)) {
388 commentlaplace
.transform(name
, name
);
392 public boolean filter(File dir
) {
396 // -----------------------------------ForDoAll-----------------------------------//
398 public final void setModuleInfo(ModuleInfo m
) {
402 public static final void aimAt(ModuleInfo mi
) throws Exception
{
403 modulereader
.setModuleInfo(mi
);
404 modulereader
.ModuleScan();