+++ /dev/null
-/** @file\r
- \r
- Copyright (c) 2006, Intel Corporation\r
- All rights reserved. This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
- \r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
- \r
- **/\r
-package org.tianocore.migration;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.File;\r
-import java.io.FileReader;\r
-import java.io.StringReader;\r
-import java.util.Iterator;\r
-import java.util.regex.Matcher;\r
-import java.util.regex.Pattern;\r
-\r
-import org.tianocore.FilenameDocument;\r
-import org.tianocore.ModuleSurfaceAreaDocument;\r
-import org.tianocore.MsaHeaderDocument;\r
-import org.tianocore.SourceFilesDocument;\r
-\r
-public final class ModuleReader implements Common.ForDoAll {\r
- private static final ModuleReader modulereader = new ModuleReader();\r
-\r
- private ModuleInfo mi;\r
-\r
- private final CommentLaplace commentlaplace = new CommentLaplace();\r
-\r
- private static final Pattern ptninfequation = Pattern\r
- .compile("([^\\s]*)\\s*=\\s*([^\\s]*)");\r
-\r
- private static final Pattern ptnsection = Pattern.compile(\r
- "\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern.MULTILINE);\r
-\r
- private static final Pattern ptnfilename = Pattern.compile("[^\\s]+");\r
-\r
- public final void ModuleScan() throws Exception {\r
- Common.toDoAll(mi.modulepath, ModuleInfo.class.getMethod("enroll",\r
- String.class), mi, null, Common.FILE);\r
-\r
- // inf&msa\r
- String filename = null;\r
- if (mi.msaorinf.isEmpty()) {\r
- MigrationTool.ui.println("No INF nor MSA file found!");\r
- System.exit(0);\r
- } else {\r
- if (mi.msaorinf.size() == 1) {\r
- filename = (String) mi.msaorinf.toArray()[0];\r
- } else {\r
- filename = MigrationTool.ui.choose(\r
- "Found .inf or .msa file for module\n" + mi.modulepath\r
- + "\nChoose one Please", mi.msaorinf.toArray());\r
- }\r
- }\r
-\r
- if (filename.contains(".inf")) {\r
- readInf(filename);\r
- } else if (filename.contains(".msa")) {\r
- readMsa(filename);\r
- }\r
- // inf&msa\r
-\r
- preProcessModule();\r
- }\r
-\r
- private final void readMsa(String name) throws Exception {\r
- ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory\r
- .parse(new File(mi.modulepath + File.separator + name));\r
- ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc\r
- .getModuleSurfaceArea();\r
- MsaHeaderDocument.MsaHeader msaheader = msa.getMsaHeader();\r
-\r
- mi.modulename = msaheader.getModuleName();\r
- mi.guidvalue = msaheader.getGuidValue();\r
- mi.moduletype = msaheader.getModuleType().toString(); // ???\r
-\r
- SourceFilesDocument.SourceFiles sourcefiles = msa.getSourceFiles();\r
-\r
- String temp;\r
- Iterator<FilenameDocument.Filename> li = sourcefiles.getFilenameList()\r
- .iterator();\r
- while (li.hasNext()) {\r
- if (!mi.localmodulesources.contains(temp = li.next().toString())) {\r
- System.out.println("Source File Missing! : " + temp);\r
- }\r
- }\r
- }\r
-\r
- private final String extractLicense(String wholeline) throws Exception {\r
- String tempLine;\r
- String license = null;\r
-\r
- BufferedReader rd = new BufferedReader(new StringReader(wholeline));\r
- while ((tempLine = rd.readLine()) != null) {\r
- if (tempLine.contains("#")) {\r
- if (tempLine.contains("Copyright")) {\r
- //\r
- // Find license info.\r
- // \r
- license = "";\r
- while ((tempLine = rd.readLine()) != null) {\r
- if (!tempLine.contains("#")\r
- || tempLine.contains("Module Name:")\r
- || tempLine.contains("Abstract:")) {\r
- //\r
- // We assume license ends here.\r
- // \r
- break;\r
- }\r
- license += " "\r
- + tempLine\r
- .replaceAll("\\s*[#]\\s*(.*)", "$1\n");\r
- }\r
- break;\r
- }\r
- }\r
- }\r
- return license;\r
- }\r
-\r
- private final void readInf(String name) throws Exception {\r
- System.out.println("\nParsing INF file: " + name);\r
- String wholeline;\r
- Matcher mtrinfequation;\r
- Matcher mtrsection;\r
- Matcher mtrfilename;\r
-\r
- wholeline = Common.file2string(mi.modulepath + File.separator + name);\r
- mi.license = extractLicense(wholeline);\r
- mtrsection = ptnsection.matcher(wholeline);\r
- while (mtrsection.find()) {\r
- if (mtrsection.group(1).matches("defines")) {\r
- mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
- while (mtrinfequation.find()) {\r
- if (mtrinfequation.group(1).matches("BASE_NAME")) {\r
- mi.modulename = mtrinfequation.group(2);\r
- }\r
- if (mtrinfequation.group(1).matches("FILE_GUID")) {\r
- mi.guidvalue = mtrinfequation.group(2);\r
- }\r
- if (mtrinfequation.group(1).matches("COMPONENT_TYPE")) {\r
- mi.moduletype = mtrinfequation.group(2);\r
- if (mi.moduletype.matches("LIBRARY")) {\r
- mi.isLibrary = true;\r
- }\r
- }\r
- }\r
- }\r
- if (mtrsection.group(1).contains("nmake.")) {\r
- mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
- while (mtrinfequation.find()) {\r
- if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {\r
- mi.entrypoint = mtrinfequation.group(2);\r
- }\r
- if (mtrinfequation.group(1).matches("DPX_SOURCE")) {\r
- if (!mi.localmodulesources.contains(mtrinfequation\r
- .group(2))) {\r
- MigrationTool.ui.println("DPX File Missing! : "\r
- + mtrinfequation.group(2));\r
- }\r
- }\r
- }\r
- }\r
- if (mtrsection.group(1).contains("sources.")) {\r
- mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
- while (mtrfilename.find()) {\r
- mi.infsources.add(mtrfilename.group());\r
- if (!mi.localmodulesources.contains(mtrfilename.group())) {\r
- MigrationTool.ui\r
- .println("Warn: Source File Missing! : "\r
- + mtrfilename.group());\r
- }\r
- }\r
- }\r
- if (mtrsection.group(1).matches("includes.")) {\r
- mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
- while (mtrfilename.find()) {\r
- mi.infincludes.add(mtrfilename.group());\r
- }\r
- }\r
- }\r
- }\r
-\r
- private final void preProcessModule() throws Exception {\r
- // according to .inf file, add extraordinary includes and sourcefiles\r
- Common.dirCopy(mi.modulepath, mi.temppath); // collect all\r
- // Laplace.namechange to\r
- // here???\r
-\r
- if (!mi.infincludes.isEmpty()) {\r
- Iterator<String> it = mi.infincludes.iterator();\r
- String tempincludename = null;\r
- while (it.hasNext()) {\r
- tempincludename = it.next();\r
- if (tempincludename.contains("..")) {\r
- Matcher mtr = Common.PTNSEPARATER.matcher(tempincludename);\r
- if (mtr.find() && !mtr.group(2).matches(".")) {\r
- Common.oneLevelDirCopy(mi.modulepath.replaceAll(\r
- Common.STRSEPARATER, "$1")\r
- + File.separator + mtr.group(2), mi.temppath,\r
- ".h");\r
- } else {\r
- Common.oneLevelDirCopy(mi.modulepath.replaceAll(\r
- Common.STRSEPARATER, "$1"), mi.temppath, ".h");\r
- }\r
- }\r
- }\r
- }\r
- if (!mi.infsources.isEmpty()) {\r
- Iterator<String> it = mi.infsources.iterator();\r
- String tempsourcename = null;\r
- while (it.hasNext()) {\r
- tempsourcename = it.next();\r
- if (tempsourcename.contains("..")) {\r
- Common.ensureDir(mi.temppath + File.separator\r
- + "MT_Parent_Sources");\r
- Matcher mtr = Common.PTNSEPARATER.matcher(tempsourcename);\r
- if (mtr.find()) {\r
- Common.fileCopy(mi.modulepath.replaceAll(\r
- Common.STRSEPARATER, "$1")\r
- + File.separator + mtr.group(2), mi.temppath\r
- + File.separator + "MT_Parent_Sources"\r
- + File.separator + mtr.group(2));\r
- }\r
- }\r
- }\r
- }\r
-\r
- Common.toDoAll(mi.temppath, this, Common.FILE);\r
-\r
- parsePreProcessedSourceCode();\r
-\r
- }\r
-\r
- private final void parsePreProcessedSourceCode() throws Exception {\r
- BufferedReader rd = null;\r
- String ifile = null;\r
- String line = null;\r
- String temp = null;\r
-\r
- Iterator<String> ii = mi.localmodulesources.iterator();\r
- while (ii.hasNext()) {\r
- temp = ii.next();\r
- if (temp.contains(".c") || temp.contains(".dxs")) {\r
- mi.preprocessedccodes.add(temp);\r
- }\r
- }\r
-\r
- ii = mi.preprocessedccodes.iterator();\r
-\r
- Pattern patefifuncc = Pattern.compile(\r
- "g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)", Pattern.MULTILINE);\r
- Matcher matguid;\r
- Matcher matfuncc;\r
- Matcher matfuncd;\r
- Matcher matenclosereplace;\r
- Matcher matefifuncc;\r
- Matcher matmacro;\r
-\r
- while (ii.hasNext()) {\r
- StringBuffer wholefile = new StringBuffer();\r
- ifile = ii.next();\r
- rd = new BufferedReader(new FileReader(mi.temppath + File.separator\r
- + ifile));\r
- while ((line = rd.readLine()) != null) {\r
- wholefile.append(line + '\n');\r
- }\r
- line = wholefile.toString();\r
-\r
- // find guid\r
- matguid = Guid.ptnguid.matcher(line); // several ways to implement\r
- // this , which one is\r
- // faster ? :\r
- while (matguid.find()) { // 1.currently , find once , then call\r
- // to identify which is it\r
- if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use\r
- // 3\r
- // different\r
- // matchers\r
- // ,\r
- // search\r
- // 3\r
- // times\r
- // to\r
- // find\r
- // each\r
- // matguid.appendReplacement(result,\r
- // MigrationTool.db.getR9Guidname(temp)); // search the\r
- // database for all 3 kinds of guids , high cost\r
- }\r
- }\r
- // matguid.appendTail(result);\r
- // line = result.toString();\r
-\r
- // find EFI call in form of '->' , many\r
- // 'gUnicodeCollationInterface->' like things are not changed\r
- // This item is not simply replaced , special operation is required.\r
- matefifuncc = patefifuncc.matcher(line);\r
- while (matefifuncc.find()) {\r
- mi.hashEFIcall.add(matefifuncc.group(2));\r
- }\r
-\r
- // find function call\r
- matfuncc = Func.ptnfuncc.matcher(line);\r
- while (matfuncc.find()) {\r
- if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {\r
- // MigrationTool.ui.println(ifile + " dofunc " + temp);\r
- // matfuncc.appendReplacement(result,\r
- // MigrationTool.db.getR9Func(temp));\r
- }\r
- }\r
- // matfuncc.appendTail(result);\r
- // line = result.toString();\r
-\r
- // find macro\r
- matmacro = Macro.ptntmacro.matcher(line);\r
- while (matmacro.find()) {\r
- if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {\r
- }\r
- }\r
-\r
- // find function definition\r
- // replace all {} to @\r
- while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r
- line = matenclosereplace.replaceAll("@");\r
- }\r
-\r
- matfuncd = Func.ptnfuncd.matcher(line);\r
- while (matfuncd.find()) {\r
- if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {\r
- }\r
- }\r
- }\r
-\r
- // op on hash\r
- Iterator<String> funcci = mi.hashfuncc.iterator();\r
- while (funcci.hasNext()) {\r
- if (!mi.hashfuncd.contains(temp = funcci.next())\r
- && !mi.hashEFIcall.contains(temp)) {\r
- mi.hashnonlocalfunc.add(temp); // this set contains both\r
- // changed and not changed items\r
- }\r
- }\r
- }\r
-\r
- public class CommentLaplace extends Common.Laplace {\r
- public String operation(String wholeline) {\r
- StringBuffer wholebuffer = new StringBuffer();\r
- String templine = null;\r
- Pattern ptnincludefile = Pattern.compile("[\"<](.*[.]h)[\">]");\r
- Pattern ptninclude = Pattern.compile("#include\\s*(.*)");\r
- Matcher mtrinclude = ptninclude.matcher(wholeline);\r
- Matcher mtrincludefile = null;\r
- while (mtrinclude.find()) {\r
- mtrincludefile = ptnincludefile.matcher(mtrinclude.group(1));\r
- if (mtrincludefile.find()\r
- && mi.localmodulesources.contains(mtrincludefile\r
- .group(1))) {\r
- templine = mtrinclude.group();\r
- } else {\r
- String line = mtrinclude.group().toLowerCase();\r
- if (line.contains("pal.h")) {\r
- templine = "#include <IndustryStandard/Pal.h>\n";\r
- } else if (line.contains("sal.h")) {\r
- templine = "#include <IndustryStandard/Sal.h>\n";\r
- } else if (line.contains("pci22.h")) {\r
- templine = "#include <IndustryStandard/Pci22.h>\n";\r
- } else if (line.contains("pci23.h")) {\r
- templine = "#include <IndustryStandard/Pci23.h>\n";\r
- } else if (line.contains("pci30.h")) {\r
- templine = "#include <IndustryStandard/Pci30.h>\n";\r
- } else if (line.contains("pci.h")) {\r
- templine = "#include <IndustryStandard/Pci.h>\n";\r
- } else if (line.contains("acpi.h")) {\r
- templine = "#include <IndustryStandard/Acpi.h>\n";\r
- } else if (line.contains("scsi.h")) {\r
- templine = "#include <IndustryStandard/Scsi.h>\n";\r
- } else if (line.contains("usb.h")) {\r
- templine = "#include <IndustryStandard/Usb.h>\n";\r
- } else {\r
- templine = MigrationTool.MIGRATIONCOMMENT\r
- + mtrinclude.group();\r
- }\r
- }\r
- mtrinclude.appendReplacement(wholebuffer, templine);\r
- }\r
- mtrinclude.appendTail(wholebuffer);\r
- return wholebuffer.toString();\r
- }\r
-\r
- public boolean recognize(String filename) {\r
- return filename.contains(".c") || filename.contains(".h")\r
- || filename.contains(".dxs");\r
- }\r
-\r
- public String namechange(String oldname) {\r
- return oldname;\r
- }\r
- }\r
-\r
- // -----------------------------------ForDoAll-----------------------------------//\r
- public void run(String filepath) throws Exception {\r
- String name = mi.temppath + File.separator\r
- + filepath.replace(mi.temppath + File.separator, "");\r
- if (commentlaplace.recognize(name)) {\r
- commentlaplace.transform(name, name);\r
- }\r
- }\r
-\r
- public boolean filter(File dir) {\r
- return true;\r
- }\r
-\r
- // -----------------------------------ForDoAll-----------------------------------//\r
-\r
- public final void setModuleInfo(ModuleInfo m) {\r
- mi = m;\r
- }\r
-\r
- public static final void aimAt(ModuleInfo mi) throws Exception {\r
- modulereader.setModuleInfo(mi);\r
- modulereader.ModuleScan();\r
- }\r
-}\r