]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Sample/Tools/Source/UefiVfrCompile/VfrError.cpp
1) Sync EdkCompatibilityPkg with EDK 1.04. The changes includes:
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / UefiVfrCompile / VfrError.cpp
1 #include "stdio.h"
2 #include "string.h"
3 #include "stdlib.h"
4 #include "VfrError.h"
5
6 static SVFR_ERROR_HANDLE VFR_ERROR_HANDLE_TABLE [] = {
7 { VFR_RETURN_SUCCESS, NULL },
8 { VFR_RETURN_ERROR_SKIPED, NULL },
9 { VFR_RETURN_FATAL_ERROR, "fatal error!!" },
10
11 { VFR_RETURN_MISMATCHED, "unexpected token" },
12 { VFR_RETURN_INVALID_PARAMETER, "Invalid parameter" },
13 { VFR_RETURN_OUT_FOR_RESOURCES, "system out of memory" },
14 { VFR_RETURN_UNSUPPORTED, "unsupported" },
15 { VFR_RETURN_REDEFINED, "already defined" },
16 { VFR_RETURN_FORMID_REDEFINED, "form id already defined" },
17 { VFR_RETURN_QUESTIONID_REDEFINED, "question id already defined" },
18 { VFR_RETURN_VARSTOREID_REDEFINED, "varstore id already defined" },
19 { VFR_RETURN_UNDEFINED, "undefined" },
20 { VFR_RETURN_VAR_NOTDEFINED_BY_QUESTION, "some variable has not defined by a question"},
21 { VFR_RETURN_GET_EFIVARSTORE_ERROR, "get efi varstore error"},
22 { VFR_RETURN_EFIVARSTORE_USE_ERROR, "can not use the efi varstore like this" },
23 { VFR_RETURN_EFIVARSTORE_SIZE_ERROR, "unsupport efi varstore size should be <= 8 bytes" },
24 { VFR_RETURN_GET_NVVARSTORE_ERROR, "get name value varstore error" },
25 { VFR_RETURN_QVAR_REUSE, "variable reused by more than one question" },
26 { VFR_RETURN_FLAGS_UNSUPPORTED, "flags unsupported" },
27 { VFR_RETURN_ERROR_ARRARY_NUM, "array number error" },
28 { VFR_RETURN_DATA_STRING_ERROR, "data field string error or not support"},
29 { VFR_RETURN_CODEUNDEFINED, "Undefined Error Code" }
30 };
31
32 CVfrErrorHandle::CVfrErrorHandle (
33 VOID
34 )
35 {
36 mScopeRecordListHead = NULL;
37 mScopeRecordListTail = NULL;
38 mVfrErrorHandleTable = VFR_ERROR_HANDLE_TABLE;
39 }
40
41 CVfrErrorHandle::~CVfrErrorHandle (
42 VOID
43 )
44 {
45 SVfrFileScopeRecord *pNode = NULL;
46
47 while (mScopeRecordListHead != NULL) {
48 pNode = mScopeRecordListHead;
49 mScopeRecordListHead = mScopeRecordListHead->mNext;
50 delete pNode;
51 }
52
53 mScopeRecordListHead = NULL;
54 mScopeRecordListTail = NULL;
55 mVfrErrorHandleTable = NULL;
56 }
57
58 SVfrFileScopeRecord::SVfrFileScopeRecord (
59 IN INT8 *Record,
60 IN UINT32 LineNum
61 )
62 {
63 UINT32 Index;
64 INT8 *FileName = NULL;
65 INT8 *Str = NULL;
66
67 mWholeScopeLine = LineNum;
68 mNext = NULL;
69
70 Str = strchr (Record, ' ');
71 mScopeLineStart = atoi (++Str);
72
73 Str = strchr (Str, '\"');
74 FileName = ++Str;
75
76 while((Str = strstr (FileName, "\\\\")) != NULL) {
77 FileName = Str + 2;
78 }
79 if ((mFileName = new INT8[strlen(FileName)]) != NULL) {
80 for (Index = 0; FileName[Index] != '\"'; Index++) {
81 mFileName[Index] = FileName[Index];
82 }
83 mFileName[Index] = '\0';
84 }
85
86 return;
87 }
88
89 SVfrFileScopeRecord::~SVfrFileScopeRecord (
90 VOID
91 )
92 {
93 if (mFileName != NULL) {
94 delete mFileName;
95 }
96 }
97
98 VOID
99 CVfrErrorHandle::ParseFileScopeRecord (
100 IN INT8 *Record,
101 IN UINT32 WholeScopeLine
102 )
103 {
104 INT8 *FullPathName = NULL;
105 SVfrFileScopeRecord *pNode = NULL;
106
107 if (Record == NULL) {
108 return;
109 }
110
111 if ((pNode = new SVfrFileScopeRecord(Record, WholeScopeLine)) == NULL) {
112 return;
113 }
114
115 if (mScopeRecordListHead == NULL) {
116 mScopeRecordListTail = mScopeRecordListHead = pNode;
117 } else {
118 mScopeRecordListTail->mNext = pNode;
119 mScopeRecordListTail = pNode;
120 }
121 }
122
123 VOID
124 CVfrErrorHandle::GetFileNameLineNum (
125 IN UINT32 LineNum,
126 OUT INT8 **FileName,
127 OUT UINT32 *FileLine
128 )
129 {
130 SVfrFileScopeRecord *pNode = NULL;
131
132 if ((FileName == NULL) || (FileLine == NULL)) {
133 return;
134 }
135
136 *FileName = NULL;
137 *FileLine = 0xFFFFFFFF;
138
139 for (pNode = mScopeRecordListHead; pNode->mNext != NULL; pNode = pNode->mNext) {
140 if ((LineNum > pNode->mWholeScopeLine) && (pNode->mNext->mWholeScopeLine > LineNum)) {
141 *FileName = pNode->mFileName;
142 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
143 return ;
144 }
145 }
146
147 *FileName = pNode->mFileName;
148 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
149 }
150
151 VOID
152 CVfrErrorHandle::PrintError (
153 IN UINT32 LineNum,
154 IN INT8 *TokName,
155 IN INT8 *ErrorMsg
156 )
157 {
158 INT8 *FileName = NULL;
159 UINT32 FileLine;
160
161 GetFileNameLineNum (LineNum, &FileName, &FileLine);
162 printf ("%s line %d: error %s %s\n", FileName, FileLine, TokName, ErrorMsg);
163 }
164
165 UINT8
166 CVfrErrorHandle::HandleError (
167 IN EFI_VFR_RETURN_CODE ErrorCode,
168 IN UINT32 LineNum,
169 IN INT8 *TokName
170 )
171 {
172 UINT32 Index;
173 INT8 *FileName = NULL;
174 UINT32 FileLine;
175 INT8 *ErrorMsg = NULL;
176
177 if (mVfrErrorHandleTable == NULL) {
178 return 1;
179 }
180
181 for (Index = 0; mVfrErrorHandleTable[Index].mErrorCode != VFR_RETURN_CODEUNDEFINED; Index++) {
182 if (ErrorCode == mVfrErrorHandleTable[Index].mErrorCode) {
183 ErrorMsg = mVfrErrorHandleTable[Index].mErrorMsg;
184 break;
185 }
186 }
187
188 if (ErrorMsg != NULL) {
189 GetFileNameLineNum (LineNum, &FileName, &FileLine);
190 printf ("%s line %d: error %s %s\n", FileName, FileLine, TokName, ErrorMsg);
191 return 1;
192 } else {
193 return 0;
194 }
195 }
196
197 CVfrErrorHandle gCVfrErrorHandle;