+package org.tianocore.migration;\r
+\r
+import java.io.*;\r
+import java.util.*;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+public class SourceFileReplacer {\r
+ SourceFileReplacer(String path, ModuleInfo moduleinfo, Database database, UI fp) {\r
+ modulepath = path;\r
+ mi = moduleinfo;\r
+ db = database;\r
+ ui = fp;\r
+ }\r
+ private String modulepath;\r
+ private ModuleInfo mi;\r
+ private Database db;\r
+ private UI ui;\r
+ private boolean showdetails = false;\r
+ \r
+ private class r8tor9 {\r
+ r8tor9(String r8, String r9) {\r
+ r8thing = r8;\r
+ r9thing = r9;\r
+ }\r
+ public String r8thing;\r
+ public String r9thing;\r
+ }\r
+ \r
+ // these sets are used only for printing log of the changes in current file\r
+ private Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
+ private Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
+ private Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
+ private Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
+ private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
+ private Set<String> filer8only = new HashSet<String>();\r
+ \r
+ private String r8only = "EfiLibInstallDriverBinding " +\r
+ "EfiLibInstallAllDriverProtocols " +\r
+ "EfiLibCompareLanguage " +\r
+ "BufToHexString " +\r
+ "EfiStrTrim " +\r
+ "EfiValueToHexStr " +\r
+ "HexStringToBuf " +\r
+ "IsHexDigit " +\r
+ "NibbleToHexChar " +\r
+ "GetHob " +\r
+ "GetHobListSize " +\r
+ "GetHobVersion " +\r
+ "GetHobBootMode " +\r
+ "GetCpuHobInfo " +\r
+ "GetDxeCoreHobInfo " +\r
+ "GetNextFirmwareVolumeHob " +\r
+ "GetNextGuidHob " +\r
+ "GetPalEntryHobInfo " +\r
+ "GetIoPortSpaceAddressHobInfo ";\r
+ \r
+ public void flush() throws Exception {\r
+ PrintWriter outfile;\r
+ String temp = null;\r
+ if (ui.yesOrNo("Change Source Code is to be doing . See details ?")) {\r
+ showdetails = true;\r
+ }\r
+ File tempdir = new File(modulepath + File.separator + "result" + File.separator);\r
+ if (!tempdir.exists()) tempdir.mkdir();\r
+ String[] list = new File(modulepath + File.separator + "temp").list(); //what I change is the non-local .h commented-out files\r
+ for (int i = 0 ; i < list.length ; i++) {\r
+ if (list[i].contains(".c")) {\r
+ ui.println("\nModifying file : " + list[i]);\r
+ outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + list[i])));\r
+ outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + list[i]));\r
+ outfile.flush();\r
+ outfile.close();\r
+ } else {\r
+ if (list[i].contains(".h")) {\r
+ temp = list[i];\r
+ } else if (list[i].contains(".C")) {\r
+ temp = list[i].replaceFirst(".C", ".c");\r
+ } else if (list[i].contains(".H")) {\r
+ temp = list[i].replaceFirst(".H", ".h");\r
+ } else {\r
+ continue;\r
+ }\r
+ ui.println("\nCopying file : " + temp);\r
+ outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + temp)));\r
+ outfile.append(sourcefiletostring(modulepath + File.separator + "temp" + File.separator + list[i]));\r
+ outfile.flush();\r
+ outfile.close();\r
+ }\r
+ }\r
+\r
+ if (!mi.hashr8only.isEmpty()) {\r
+ addr8only();\r
+ }\r
+ }\r
+ \r
+ private void addr8only() throws Exception {\r
+ String paragraph = null;\r
+ String line = sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c");\r
+ PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c")));\r
+ PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h")));\r
+ //outfile1.append("#include \"R8Lib.h\"\n\n");\r
+ //outfile2.append("#include \"R8Lib.h\"\n\n");\r
+ Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);\r
+ Matcher mtrr8only = ptnr8only.matcher(line);\r
+ Matcher mtrr8onlyhead;\r
+ while (mtrr8only.find()) {\r
+ if (mi.hashr8only.contains(mtrr8only.group(2))) {\r
+ paragraph = mtrr8only.group();\r
+ outfile1.append(paragraph + "\n\n");\r
+ if (mtrr8only.group(1).length() != 0) {\r
+ mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
+ }\r
+ //generate R8lib.h\r
+ while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
+ paragraph = mtrr8onlyhead.replaceAll(";");\r
+ }\r
+ outfile2.append(paragraph + "\n\n");\r
+ }\r
+ }\r
+ outfile1.flush();\r
+ outfile1.close();\r
+ outfile2.flush();\r
+ outfile2.close();\r
+ }\r
+ \r
+ private String sourcefiletostring(String filename) throws Exception {\r
+ BufferedReader rd = new BufferedReader(new FileReader(filename));\r
+ StringBuffer wholefile = new StringBuffer();\r
+ String line;\r
+ while ((line = rd.readLine()) != null) {\r
+ wholefile.append(line + "\n");\r
+ }\r
+ return wholefile.toString();\r
+ }\r
+ \r
+ // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
+ private String sourcefilereplace(String filename) throws Exception {\r
+ BufferedReader rd = new BufferedReader(new FileReader(filename));\r
+ StringBuffer wholefile = new StringBuffer();\r
+ String line;\r
+ String r8thing;\r
+ String r9thing;\r
+ r8tor9 temp;\r
+ boolean addr8 = false;\r
+\r
+ Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !\r
+ //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);\r
+\r
+ while ((line = rd.readLine()) != null) {\r
+ wholefile.append(line + "\n");\r
+ }\r
+ line = wholefile.toString();\r
+ \r
+ // replace BS -> gBS , RT -> gRT\r
+ Matcher mat = pat.matcher(line);\r
+ if (mat.find()) { // add a library here\r
+ ui.println("Converting all BS->gBS,RT->gRT");\r
+ line = mat.replaceAll("g$1$2$3"); //unknown correctiveness\r
+ }\r
+ mat.reset();\r
+ while (mat.find()) {\r
+ if (mat.group(1).matches("BS")) {\r
+ mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
+ }\r
+ if (mat.group(1).matches("RT")) {\r
+ mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");\r
+ }\r
+ }\r
+ /*\r
+ // remove EFI_DRIVER_ENTRY_POINT\r
+ Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
+ Matcher matentrypoint = patentrypoint.matcher(line);\r
+ if (matentrypoint.find()) {\r
+ ui.println("Deleting Entry_Point");\r
+ line = matentrypoint.replaceAll("");\r
+ }\r
+ */\r
+ // start replacing names\r
+ Iterator<String> it;\r
+ // Converting non-locla function\r
+ it = mi.hashnonlocalfunc.iterator();\r
+ while (it.hasNext()) {\r
+ r8thing = it.next();\r
+ if (r8thing.matches("EfiInitializeDriverLib")) { //s\r
+ mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p\r
+ mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e\r
+ } else if (r8thing.matches("DxeInitializeDriverLib")) { //c\r
+ mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i\r
+ mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a\r
+ mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l\r
+ } else { //\r
+ mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here\r
+ }\r
+ \r
+ if ((r9thing = db.getR9Func(r8thing)) != null) {\r
+ if (!r8thing.equals(r9thing)) {\r
+ if (line.contains(r8thing)) {\r
+ line = line.replaceAll(r8thing, r9thing);\r
+ filefunc.add(new r8tor9(r8thing, r9thing));\r
+ Iterator<r8tor9> rt = filefunc.iterator();\r
+ while (rt.hasNext()) {\r
+ temp = rt.next();\r
+ if (r8only.contains(temp.r8thing)) {\r
+ mi.localmodulesources.add("R8Lib.h");\r
+ mi.localmodulesources.add("R8Lib.c");\r
+ mi.localmoduleheaders.add("R8Lib.h");\r
+ filer8only.add(r8thing);\r
+ mi.hashr8only.add(r8thing);\r
+ addr8 = true;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ } //is any of the guids changed?\r
+ if (addr8 == true) {\r
+ line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");\r
+ }\r
+ \r
+ // Converting macro\r
+ it = mi.hashnonlocalmacro.iterator();\r
+ while (it.hasNext()) { //macros are all assumed MdePkg currently\r
+ r8thing = it.next();\r
+ //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); \r
+ if ((r9thing = db.getR9Macro(r8thing)) != null) {\r
+ if (line.contains(r8thing)) {\r
+ line = line.replaceAll(r8thing, r9thing);\r
+ filemacro.add(new r8tor9(r8thing, r9thing));\r
+ }\r
+ }\r
+ }\r
+\r
+ // Converting guid\r
+ replaceGuid(line, mi.guid, "guid", fileguid);\r
+ replaceGuid(line, mi.ppi, "ppi", fileppi);\r
+ replaceGuid(line, mi.protocol, "protocol", fileprotocol);\r
+\r
+ // Converting Pei\r
+ // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%\r
+ Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);\r
+ if (mi.moduletype.contains("PEIM")) {\r
+ Matcher mtrpei = ptnpei.matcher(line);\r
+ while (mtrpei.find()) { // ! add a library here !\r
+ line = mtrpei.replaceAll("PeiServices$1#%$2");\r
+ mi.hashrequiredr9libs.add("PeiServicesLib");\r
+ }\r
+ mtrpei.reset();\r
+ if (line.contains("PeiServicesCopyMem")) {\r
+ line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r
+ mi.hashrequiredr9libs.add("BaseMemoryLib");\r
+ }\r
+ if (line.contains("PeiServicesSetMem")) {\r
+ line = line.replaceAll("PeiServicesSetMem#%", "SetMem");\r
+ mi.hashrequiredr9libs.add("BaseMemoryLib");\r
+ }\r
+\r
+ // Second , find all #% to drop the arg "PeiServices"\r
+ Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);\r
+ Matcher mtrpeiarg = ptnpeiarg.matcher(line);\r
+ while (mtrpeiarg.find()) {\r
+ line = mtrpeiarg.replaceAll("$1");\r
+ }\r
+ }\r
+ \r
+ Matcher mtrmac;\r
+ mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);\r
+ if (mtrmac.find()) {\r
+ line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
+ }\r
+ mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);\r
+ if (mtrmac.find()) {\r
+ line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
+ }\r
+ mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);\r
+ if (mtrmac.find()) {\r
+ line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
+ }\r
+ mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);\r
+ if (mtrmac.find()) {\r
+ line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
+ }\r
+ if (line.contains("EFI_UINTN_ALIGN_MASK")) {\r
+ line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
+ }\r
+\r
+ show(filefunc, "function");\r
+ show(filemacro, "macro");\r
+ show(fileguid, "guid");\r
+ show(fileppi, "ppi");\r
+ show(fileprotocol, "protocol");\r
+ if (!filer8only.isEmpty()) {\r
+ ui.println("Converting r8only : " + filer8only);\r
+ }\r
+\r
+ filefunc.clear();\r
+ filemacro.clear();\r
+ fileguid.clear();\r
+ fileppi.clear();\r
+ fileprotocol.clear();\r
+ filer8only.clear();\r
+ \r
+ return line;\r
+ }\r
+ \r
+ private void show(Set<r8tor9> hash, String sh) {\r
+ Iterator<r8tor9> it = hash.iterator();\r
+ r8tor9 temp;\r
+ if (showdetails) {\r
+ if (!hash.isEmpty()) {\r
+ ui.print("Converting " + sh + " : ");\r
+ while (it.hasNext()) {\r
+ temp = it.next();\r
+ ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
+ }\r
+ ui.println("");\r
+ }\r
+ }\r
+ }\r
+ \r
+ private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
+ Iterator<String> it;\r
+ String r8thing;\r
+ String r9thing;\r
+ it = hash.iterator();\r
+ while (it.hasNext()) {\r
+ r8thing = it.next();\r
+ if ((r9thing = db.getR9Guidname(r8thing)) != null) {\r
+ if (!r8thing.equals(r9thing)) {\r
+ if (line.contains(r8thing)) {\r
+ line = line.replaceAll(r8thing, r9thing);\r
+ filehash.add(new r8tor9(r8thing, r9thing));\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r