]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Misc.py
BaseTools: Add new RegExp for future use
[mirror_edk2.git] / BaseTools / Source / Python / Common / Misc.py
index 15ad9e4f2eca37851748f578708b3328c1d467dc..7d44fdcf8ba73b42107299e65e36a7b4379659fb 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # Common routines used by all tools\r
 #\r
-# Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
 # 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
@@ -1441,23 +1441,44 @@ def ParseConsoleLog(Filename):
     Opr.close()\r
     Opw.close()\r
 \r
+def IsFieldValueAnArray (Value):\r
+    Value = Value.strip()\r
+    if Value.startswith('GUID') and Value.endswith(')'):\r
+        return True\r
+    if Value.startswith('L"') and Value.endswith('"')  and len(list(Value[2:-1])) > 1:\r
+        return True\r
+    if Value[0] == '"' and Value[-1] == '"' and len(list(Value[1:-1])) > 1:\r
+        return True\r
+    if Value[0] == '{' and Value[-1] == '}':\r
+        return True\r
+    if Value.startswith("L'") and Value.endswith("'") and len(list(Value[2:-1])) > 1:\r
+        return True\r
+    if Value[0] == "'" and Value[-1] == "'" and len(list(Value[1:-1])) > 1:\r
+        return True\r
+    return False\r
+\r
 def AnalyzePcdExpression(Setting):\r
     Setting = Setting.strip()\r
-    # There might be escaped quote in a string: \", \\\"\r
-    Data = Setting.replace('\\\\', '//').replace('\\\"', '\\\'')\r
+    # There might be escaped quote in a string: \", \\\" , \', \\\'\r
+    Data = Setting\r
     # There might be '|' in string and in ( ... | ... ), replace it with '-'\r
     NewStr = ''\r
-    InStr = False\r
+    InSingleQuoteStr = False\r
+    InDoubleQuoteStr = False\r
     Pair = 0\r
-    for ch in Data:\r
-        if ch == '"':\r
-            InStr = not InStr\r
-        elif ch == '(' and not InStr:\r
+    for Index, ch in enumerate(Data):\r
+        if ch == '"' and not InSingleQuoteStr:\r
+            if Data[Index - 1] != '\\':\r
+                InDoubleQuoteStr = not InDoubleQuoteStr\r
+        elif ch == "'" and not InDoubleQuoteStr:\r
+            if Data[Index - 1] != '\\':\r
+                InSingleQuoteStr = not InSingleQuoteStr\r
+        elif ch == '(' and not (InSingleQuoteStr or InDoubleQuoteStr):\r
             Pair += 1\r
-        elif ch == ')' and not InStr:\r
+        elif ch == ')' and not (InSingleQuoteStr or InDoubleQuoteStr):\r
             Pair -= 1\r
 \r
-        if (Pair > 0 or InStr) and ch == TAB_VALUE_SPLIT:\r
+        if (Pair > 0 or InSingleQuoteStr or InDoubleQuoteStr) and ch == TAB_VALUE_SPLIT:\r
             NewStr += '-'\r
         else:\r
             NewStr += ch\r
@@ -1535,16 +1556,10 @@ def ParseFieldValue (Value):
     if Value.startswith('GUID') and Value.endswith(')'):\r
         Value = Value.split('(', 1)[1][:-1].strip()\r
         if Value[0] == '{' and Value[-1] == '}':\r
-            Value = Value[1:-1].strip()\r
-            Value = Value.split('{', 1)\r
-            Value = ['%02x' % int(Item, 16) for Item in (Value[0] + Value[1][:-1]).split(',')]\r
-            if len(Value[0]) != 8:\r
-                Value[0] = '%08X' % int(Value[0], 16)\r
-            if len(Value[1]) != 4:\r
-                Value[1] = '%04X' % int(Value[1], 16)\r
-            if len(Value[2]) != 4:\r
-                Value[2] = '%04X' % int(Value[2], 16)\r
-            Value = '-'.join(Value[0:3]) + '-' + ''.join(Value[3:5]) + '-' + ''.join(Value[5:11])\r
+            TmpValue = GuidStructureStringToGuidString(Value)\r
+            if len(TmpValue) == 0:\r
+                raise BadExpression("Invalid GUID value string %s" % Value)\r
+            Value = TmpValue\r
         if Value[0] == '"' and Value[-1] == '"':\r
             Value = Value[1:-1]\r
         try:\r
@@ -1555,7 +1570,13 @@ def ParseFieldValue (Value):
         return Value, 16\r
     if Value.startswith('L"') and Value.endswith('"'):\r
         # Unicode String\r
-        List = list(Value[2:-1])\r
+        # translate escape character\r
+        Value = Value[1:]\r
+        try:\r
+            Value = eval(Value)\r
+        except:\r
+            Value = Value[1:-1]\r
+        List = list(Value)\r
         List.reverse()\r
         Value = 0\r
         for Char in List:\r
@@ -1563,7 +1584,12 @@ def ParseFieldValue (Value):
         return Value, (len(List) + 1) * 2\r
     if Value.startswith('"') and Value.endswith('"'):\r
         # ASCII String\r
-        List = list(Value[1:-1])\r
+        # translate escape character\r
+        try:\r
+            Value = eval(Value)\r
+        except:\r
+            Value = Value[1:-1]\r
+        List = list(Value)\r
         List.reverse()\r
         Value = 0\r
         for Char in List:\r
@@ -1571,7 +1597,15 @@ def ParseFieldValue (Value):
         return Value, len(List) + 1\r
     if Value.startswith("L'") and Value.endswith("'"):\r
         # Unicode Character Constant\r
-        List = list(Value[2:-1])\r
+        # translate escape character\r
+        Value = Value[1:]\r
+        try:\r
+            Value = eval(Value)\r
+        except:\r
+            Value = Value[1:-1]\r
+        List = list(Value)\r
+        if len(List) == 0:\r
+            raise BadExpression('Length %s is %s' % (Value, len(List)))\r
         List.reverse()\r
         Value = 0\r
         for Char in List:\r
@@ -1579,7 +1613,14 @@ def ParseFieldValue (Value):
         return Value, len(List) * 2\r
     if Value.startswith("'") and Value.endswith("'"):\r
         # Character constant\r
-        List = list(Value[1:-1])\r
+        # translate escape character\r
+        try:\r
+            Value = eval(Value)\r
+        except:\r
+            Value = Value[1:-1]\r
+        List = list(Value)\r
+        if len(List) == 0:\r
+            raise BadExpression('Length %s is %s' % (Value, len(List)))\r
         List.reverse()\r
         Value = 0\r
         for Char in List:\r
@@ -1599,7 +1640,8 @@ def ParseFieldValue (Value):
                 Value = (Value << 8) | ((ItemValue >> 8 * I) & 0xff)\r
         return Value, RetSize\r
     if Value.startswith('DEVICE_PATH(') and Value.endswith(')'):\r
-        Value = Value.split('"')[1]\r
+        Value = Value.replace("DEVICE_PATH(", '').rstrip(')')\r
+        Value = Value.strip().strip('"')\r
         return ParseDevPathValue(Value)\r
     if Value.lower().startswith('0x'):\r
         Value = int(Value, 16)\r
@@ -1678,14 +1720,6 @@ def AnalyzeDscPcd(Setting, PcdType, DataType=''):
             Type = DataType\r
         if len(FieldList) > 2:\r
             Size = FieldList[2]\r
-        else:\r
-            if Type == 'VOID*':\r
-                if Value.startswith("L"):\r
-                    Size = str((len(Value)- 3 + 1) * 2)\r
-                elif Value.startswith("{"):\r
-                    Size = str(len(Value.split(",")))\r
-                else:\r
-                    Size = str(len(Value) -2 + 1 )\r
         if DataType == "":\r
             IsValid = (len(FieldList) <= 1)\r
         else:\r
@@ -1818,10 +1852,10 @@ def CheckPcdDatum(Type, Value):
     if Type == "VOID*":\r
         ValueRe = re.compile(r'\s*L?\".*\"\s*$')\r
         if not (((Value.startswith('L"') or Value.startswith('"')) and Value.endswith('"'))\r
-                or (Value.startswith('{') and Value.endswith('}'))\r
+                or (Value.startswith('{') and Value.endswith('}')) or (Value.startswith("L'") or Value.startswith("'") and Value.endswith("'"))\r
                ):\r
             return False, "Invalid value [%s] of type [%s]; must be in the form of {...} for array"\\r
-                          ", or \"...\" for string, or L\"...\" for unicode string" % (Value, Type)\r
+                          ", \"...\" or \'...\' for string, L\"...\" or L\'...\' for unicode string" % (Value, Type)\r
         elif ValueRe.match(Value):\r
             # Check the chars in UnicodeString or CString is printable\r
             if Value.startswith("L"):\r
@@ -2240,6 +2274,10 @@ class SkuClass():
             GlobalData.gSkuids = (self.SkuIdSet)\r
             if 'COMMON' in GlobalData.gSkuids:\r
                 GlobalData.gSkuids.remove('COMMON')\r
+            if self.SkuUsageType == self.SINGLE:\r
+                if len(GlobalData.gSkuids) != 1:\r
+                    if 'DEFAULT' in GlobalData.gSkuids:\r
+                        GlobalData.gSkuids.remove('DEFAULT')\r
             if GlobalData.gSkuids:\r
                 GlobalData.gSkuids.sort()\r
 \r
@@ -2251,6 +2289,8 @@ class SkuClass():
         return self.__SkuInherit.get(skuname,"DEFAULT")\r
 \r
     def GetSkuChain(self,sku):\r
+        if sku == "DEFAULT":\r
+            return ["DEFAULT"]\r
         skulist = [sku]\r
         nextsku = sku\r
         while 1:\r
@@ -2345,31 +2385,6 @@ def PackRegistryFormatGuid(Guid):
                 int(Guid[4][-2:], 16)\r
                 )\r
 \r
-def BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, Value):\r
-    if PcdDatumType == 'VOID*':\r
-        if Value.startswith('L'):\r
-            if not Value[1]:\r
-                EdkLogger.error("build", FORMAT_INVALID, 'For Void* type PCD, when specify the Value in the command line, please use the following format: "string", L"string", H"{...}"')\r
-            Value = Value[0] + '"' + Value[1:] + '"'\r
-        elif Value.startswith('H'):\r
-            if not Value[1]:\r
-                EdkLogger.error("build", FORMAT_INVALID, 'For Void* type PCD, when specify the Value in the command line, please use the following format: "string", L"string", H"{...}"')\r
-            Value = Value[1:]\r
-        else:\r
-            if not Value[0]:\r
-                EdkLogger.error("build", FORMAT_INVALID, 'For Void* type PCD, when specify the Value in the command line, please use the following format: "string", L"string", H"{...}"')\r
-            Value = '"' + Value + '"'\r
-\r
-    IsValid, Cause = CheckPcdDatum(PcdDatumType, Value)\r
-    if not IsValid:\r
-        EdkLogger.error("build", FORMAT_INVALID, Cause, ExtraData="%s.%s" % (TokenSpaceGuidCName, TokenCName))\r
-    if PcdDatumType == 'BOOLEAN':\r
-        Value = Value.upper()\r
-        if Value == 'TRUE' or Value == '1':\r
-            Value = '1'\r
-        elif Value == 'FALSE' or Value == '0':\r
-            Value = '0'\r
-    return  Value\r
 ##  Get the integer value from string like "14U" or integer like 2\r
 #\r
 #   @param      Input   The object that may be either a integer value or a string\r