]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/VfrCompile/VfrError.cpp
Sync EDKII BaseTools to BaseTools project r1903.
[mirror_edk2.git] / BaseTools / Source / C / VfrCompile / VfrError.cpp
1 /** @file
2
3 VfrCompiler error handler.
4
5 Copyright (c) 2004 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "stdio.h"
17 #include "string.h"
18 #include "stdlib.h"
19 #include "VfrError.h"
20 #include "EfiUtilityMsgs.h"
21
22 static SVFR_ERROR_HANDLE VFR_ERROR_HANDLE_TABLE [] = {
23 { VFR_RETURN_SUCCESS, NULL },
24 { VFR_RETURN_ERROR_SKIPED, NULL },
25 { VFR_RETURN_FATAL_ERROR, ": fatal error!!" },
26
27 { VFR_RETURN_MISMATCHED, ": unexpected token" },
28 { VFR_RETURN_INVALID_PARAMETER, ": invalid parameter" },
29 { VFR_RETURN_OUT_FOR_RESOURCES, ": system out of memory" },
30 { VFR_RETURN_UNSUPPORTED, ": unsupported" },
31 { VFR_RETURN_REDEFINED, ": already defined" },
32 { VFR_RETURN_FORMID_REDEFINED, ": form id already defined" },
33 { VFR_RETURN_QUESTIONID_REDEFINED, ": question id already defined" },
34 { VFR_RETURN_VARSTOREID_REDEFINED, ": varstore id already defined" },
35 { VFR_RETURN_UNDEFINED, ": undefined" },
36 { VFR_RETURN_VAR_NOTDEFINED_BY_QUESTION, ": some variable has not defined by a question"},
37 { VFR_RETURN_GET_EFIVARSTORE_ERROR, ": get efi varstore error"},
38 { VFR_RETURN_EFIVARSTORE_USE_ERROR, ": can not use the efi varstore like this" },
39 { VFR_RETURN_EFIVARSTORE_SIZE_ERROR, ": unsupport efi varstore size should be <= 8 bytes" },
40 { VFR_RETURN_GET_NVVARSTORE_ERROR, ": get name value varstore error" },
41 { VFR_RETURN_QVAR_REUSE, ": variable reused by more than one question" },
42 { VFR_RETURN_FLAGS_UNSUPPORTED, ": flags unsupported" },
43 { VFR_RETURN_ERROR_ARRARY_NUM, ": array number error, the valid value is in (0 ~ MAX_INDEX-1) for UEFI vfr and in (1 ~ MAX_INDEX) for Framework Vfr" },
44 { VFR_RETURN_DATA_STRING_ERROR, ": data field string error or not support"},
45 { VFR_RETURN_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
46 { VFR_RETURN_CONSTANT_ONLY, ": only constant is allowed in the expression"},
47 { VFR_RETURN_CODEUNDEFINED, ": undefined Error Code" }
48 };
49
50 CVfrErrorHandle::CVfrErrorHandle (
51 VOID
52 )
53 {
54 mInputFileName = NULL;
55 mScopeRecordListHead = NULL;
56 mScopeRecordListTail = NULL;
57 mVfrErrorHandleTable = VFR_ERROR_HANDLE_TABLE;
58 }
59
60 CVfrErrorHandle::~CVfrErrorHandle (
61 VOID
62 )
63 {
64 SVfrFileScopeRecord *pNode = NULL;
65
66 if (mInputFileName != NULL) {
67 delete mInputFileName;
68 }
69
70 while (mScopeRecordListHead != NULL) {
71 pNode = mScopeRecordListHead;
72 mScopeRecordListHead = mScopeRecordListHead->mNext;
73 delete pNode;
74 }
75
76 mScopeRecordListHead = NULL;
77 mScopeRecordListTail = NULL;
78 mVfrErrorHandleTable = NULL;
79 }
80
81 VOID
82 CVfrErrorHandle::SetInputFile (
83 IN CHAR8 *InputFile
84 )
85 {
86 if (InputFile != NULL) {
87 mInputFileName = new CHAR8[strlen(InputFile) + 1];
88 strcpy (mInputFileName, InputFile);
89 }
90 }
91
92 SVfrFileScopeRecord::SVfrFileScopeRecord (
93 IN CHAR8 *Record,
94 IN UINT32 LineNum
95 )
96 {
97 UINT32 Index;
98 CHAR8 *FileName = NULL;
99 CHAR8 *Str = NULL;
100
101 mWholeScopeLine = LineNum;
102 mNext = NULL;
103
104 Str = strchr (Record, ' ');
105 mScopeLineStart = atoi (++Str);
106
107 Str = strchr (Str, '\"');
108 FileName = ++Str;
109
110 while((Str = strstr (FileName, "\\\\")) != NULL) {
111 FileName = Str + 2;
112 }
113 if ((mFileName = new CHAR8[strlen(FileName)]) != NULL) {
114 for (Index = 0; FileName[Index] != '\"'; Index++) {
115 mFileName[Index] = FileName[Index];
116 }
117 mFileName[Index] = '\0';
118 }
119
120 return;
121 }
122
123 SVfrFileScopeRecord::~SVfrFileScopeRecord (
124 VOID
125 )
126 {
127 if (mFileName != NULL) {
128 delete mFileName;
129 }
130 }
131
132 VOID
133 CVfrErrorHandle::ParseFileScopeRecord (
134 IN CHAR8 *Record,
135 IN UINT32 WholeScopeLine
136 )
137 {
138 SVfrFileScopeRecord *pNode = NULL;
139
140 if (Record == NULL) {
141 return;
142 }
143
144 if ((pNode = new SVfrFileScopeRecord(Record, WholeScopeLine)) == NULL) {
145 return;
146 }
147
148 if (mScopeRecordListHead == NULL) {
149 mScopeRecordListTail = mScopeRecordListHead = pNode;
150 } else {
151 mScopeRecordListTail->mNext = pNode;
152 mScopeRecordListTail = pNode;
153 }
154 }
155
156 VOID
157 CVfrErrorHandle::GetFileNameLineNum (
158 IN UINT32 LineNum,
159 OUT CHAR8 **FileName,
160 OUT UINT32 *FileLine
161 )
162 {
163 SVfrFileScopeRecord *pNode = NULL;
164
165 if ((FileName == NULL) || (FileLine == NULL)) {
166 return;
167 }
168
169 *FileName = NULL;
170 *FileLine = 0xFFFFFFFF;
171
172 //
173 // Some errors occur before scope record list been built.
174 //
175 if (mScopeRecordListHead == NULL) {
176 *FileLine = LineNum;
177 *FileName = mInputFileName;
178 return ;
179 }
180
181 for (pNode = mScopeRecordListHead; pNode->mNext != NULL; pNode = pNode->mNext) {
182 if ((LineNum > pNode->mWholeScopeLine) && (pNode->mNext->mWholeScopeLine > LineNum)) {
183 *FileName = pNode->mFileName;
184 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
185 return ;
186 }
187 }
188
189 *FileName = pNode->mFileName;
190 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
191 }
192
193 VOID
194 CVfrErrorHandle::PrintMsg (
195 IN UINT32 LineNum,
196 IN CHAR8 *TokName,
197 IN CONST CHAR8 *MsgType,
198 IN CONST CHAR8 *ErrorMsg
199 )
200 {
201 CHAR8 *FileName = NULL;
202 UINT32 FileLine;
203
204 if (strncmp ("Warning", MsgType, strlen ("Warning")) == 0) {
205 VerboseMsg ((CHAR8 *) ErrorMsg);
206 return;
207 }
208 GetFileNameLineNum (LineNum, &FileName, &FileLine);
209 Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
210 }
211
212 UINT8
213 CVfrErrorHandle::HandleError (
214 IN EFI_VFR_RETURN_CODE ErrorCode,
215 IN UINT32 LineNum,
216 IN CHAR8 *TokName
217 )
218 {
219 UINT32 Index;
220 CHAR8 *FileName = NULL;
221 UINT32 FileLine;
222 CONST CHAR8 *ErrorMsg = NULL;
223
224 if (mVfrErrorHandleTable == NULL) {
225 return 1;
226 }
227
228 for (Index = 0; mVfrErrorHandleTable[Index].mErrorCode != VFR_RETURN_CODEUNDEFINED; Index++) {
229 if (ErrorCode == mVfrErrorHandleTable[Index].mErrorCode) {
230 ErrorMsg = mVfrErrorHandleTable[Index].mErrorMsg;
231 break;
232 }
233 }
234
235 if (ErrorMsg != NULL) {
236 GetFileNameLineNum (LineNum, &FileName, &FileLine);
237 Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
238 return 1;
239 } else {
240 return 0;
241 }
242 }
243
244 CVfrErrorHandle gCVfrErrorHandle;