1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
|
/** @file
Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
SimpleFileParsing.c
Abstract:
Generic but simple file parsing routines.
--*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "CommonLib.h"
#include "EfiUtilityMsgs.h"
#include "SimpleFileParsing.h"
#ifndef MAX_PATH
#define MAX_PATH 255
#endif
//
// just in case we get in an endless loop.
//
#define MAX_NEST_DEPTH 20
//
// number of wchars
//
#define MAX_STRING_IDENTIFIER_NAME 100
#define T_CHAR_SPACE ' '
#define T_CHAR_NULL 0
#define T_CHAR_CR '\r'
#define T_CHAR_TAB '\t'
#define T_CHAR_LF '\n'
#define T_CHAR_SLASH '/'
#define T_CHAR_BACKSLASH '\\'
#define T_CHAR_DOUBLE_QUOTE '"'
#define T_CHAR_LC_X 'x'
#define T_CHAR_0 '0'
#define T_CHAR_STAR '*'
//
// We keep a linked list of these for the source files we process
//
typedef struct _SOURCE_FILE {
FILE *Fptr;
CHAR8 *FileBuffer;
CHAR8 *FileBufferPtr;
UINTN FileSize;
CHAR8 FileName[MAX_PATH];
UINTN LineNum;
BOOLEAN EndOfFile;
BOOLEAN SkipToHash;
struct _SOURCE_FILE *Previous;
struct _SOURCE_FILE *Next;
CHAR8 ControlCharacter;
} SOURCE_FILE;
typedef struct {
CHAR8 *FileBufferPtr;
} FILE_POSITION;
//
// Keep all our module globals in this structure
//
STATIC struct {
SOURCE_FILE SourceFile;
BOOLEAN VerboseFile;
BOOLEAN VerboseToken;
} mGlobals;
STATIC
UINTN
t_strcmp (
CHAR8 *Buffer,
CHAR8 *Str
);
STATIC
UINTN
t_strncmp (
CHAR8 *Str1,
CHAR8 *Str2,
INTN Len
);
STATIC
UINTN
t_strlen (
CHAR8 *Str
);
STATIC
VOID
RewindFile (
SOURCE_FILE *SourceFile
);
STATIC
BOOLEAN
IsWhiteSpace (
SOURCE_FILE *SourceFile
);
STATIC
UINTN
SkipWhiteSpace (
SOURCE_FILE *SourceFile
);
STATIC
BOOLEAN
EndOfFile (
SOURCE_FILE *SourceFile
);
STATIC
VOID
PreprocessFile (
SOURCE_FILE *SourceFile
);
STATIC
CHAR8 *
t_strcpy (
CHAR8 *Dest,
CHAR8 *Src
);
STATIC
STATUS
ProcessIncludeFile (
SOURCE_FILE *SourceFile,
SOURCE_FILE *ParentSourceFile
);
STATIC
STATUS
ProcessFile (
SOURCE_FILE *SourceFile
);
STATIC
STATUS
GetFilePosition (
FILE_POSITION *Fpos
);
STATIC
STATUS
SetFilePosition (
FILE_POSITION *Fpos
);
STATUS
SFPInit (
VOID
)
/*++
Routine Description:
Arguments:
None.
Returns:
STATUS_SUCCESS always
--*/
{
memset ((VOID *) &mGlobals, 0, sizeof (mGlobals));
return STATUS_SUCCESS;
}
UINTN
SFPGetLineNumber (
VOID
)
/*++
Routine Description:
Return the line number of the file we're parsing. Used
for error reporting purposes.
Arguments:
None.
Returns:
The line number, or 0 if no file is being processed
--*/
{
return mGlobals.SourceFile.LineNum;
}
CHAR8 *
SFPGetFileName (
VOID
)
/*++
Routine Description:
Return the name of the file we're parsing. Used
for error reporting purposes.
Arguments:
None.
Returns:
A pointer to the file name. Null if no file is being
processed.
--*/
{
if (mGlobals.SourceFile.FileName[0]) {
return mGlobals.SourceFile.FileName;
}
return NULL;
}
STATUS
SFPOpenFile (
CHAR8 *FileName
)
/*++
Routine Description:
Open a file for parsing.
Arguments:
FileName - name of the file to parse
Returns:
--*/
{
STATUS Status;
t_strcpy (mGlobals.SourceFile.FileName, FileName);
Status = ProcessIncludeFile (&mGlobals.SourceFile, NULL);
return Status;
}
BOOLEAN
SFPIsToken (
CHAR8 *Str
)
/*++
Routine Description:
Check to see if the specified token is found at
the current position in the input file.
Arguments:
Str - the token to look for
Returns:
TRUE - the token is next
FALSE - the token is not next
Notes:
We do a simple string comparison on this function. It is
the responsibility of the caller to ensure that the token
is not a subset of some other token.
The file pointer is advanced past the token in the input file.
--*/
{
UINTN Len;
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
if ((Len = t_strcmp (mGlobals.SourceFile.FileBufferPtr, Str)) > 0) {
mGlobals.SourceFile.FileBufferPtr += Len;
if (mGlobals.VerboseToken) {
printf ("Token: '%s'\n", Str);
}
return TRUE;
}
return FALSE;
}
BOOLEAN
SFPIsKeyword (
CHAR8 *Str
)
/*++
Routine Description:
Check to see if the specified keyword is found at
the current position in the input file.
Arguments:
Str - keyword to look for
Returns:
TRUE - the keyword is next
FALSE - the keyword is not next
Notes:
A keyword is defined as a "special" string that has a non-alphanumeric
character following it.
--*/
{
UINTN Len;
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
if ((Len = t_strcmp (mGlobals.SourceFile.FileBufferPtr, Str)) > 0) {
if (isalnum ((int)mGlobals.SourceFile.FileBufferPtr[Len])) {
return FALSE;
}
mGlobals.SourceFile.FileBufferPtr += Len;
if (mGlobals.VerboseToken) {
printf ("Token: '%s'\n", Str);
}
return TRUE;
}
return FALSE;
}
BOOLEAN
SFPGetNextToken (
CHAR8 *Str,
UINTN Len
)
/*++
Routine Description:
Get the next token from the input stream.
Arguments:
Str - pointer to a copy of the next token
Len - size of buffer pointed to by Str
Returns:
TRUE - next token successfully returned
FALSE - otherwise
Notes:
Preceeding white space is ignored.
The parser's buffer pointer is advanced past the end of the
token.
--*/
{
UINTN Index;
CHAR8 TempChar;
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
//
// Have to have enough string for at least one char and a null-terminator
//
if (Len < 2) {
return FALSE;
}
//
// Look at the first character. If it's an identifier, then treat it
// as such
//
TempChar = mGlobals.SourceFile.FileBufferPtr[0];
if (((TempChar >= 'a') && (TempChar <= 'z')) || ((TempChar >= 'A') && (TempChar <= 'Z')) || (TempChar == '_')) {
Str[0] = TempChar;
mGlobals.SourceFile.FileBufferPtr++;
Index = 1;
while (!EndOfFile (&mGlobals.SourceFile) && (Index < Len)) {
TempChar = mGlobals.SourceFile.FileBufferPtr[0];
if (((TempChar >= 'a') && (TempChar <= 'z')) ||
((TempChar >= 'A') && (TempChar <= 'Z')) ||
((TempChar >= '0') && (TempChar <= '9')) ||
(TempChar == '_')
) {
Str[Index] = mGlobals.SourceFile.FileBufferPtr[0];
mGlobals.SourceFile.FileBufferPtr++;
Index++;
} else {
//
// Invalid character for symbol name, so break out
//
break;
}
}
//
// Null terminate and return success
//
Str[Index] = 0;
return TRUE;
} else if ((TempChar == ')') || (TempChar == '(') || (TempChar == '*')) {
Str[0] = mGlobals.SourceFile.FileBufferPtr[0];
mGlobals.SourceFile.FileBufferPtr++;
Str[1] = 0;
return TRUE;
} else {
//
// Everything else is white-space (or EOF) separated
//
Index = 0;
while (!EndOfFile (&mGlobals.SourceFile) && (Index < Len)) {
if (IsWhiteSpace (&mGlobals.SourceFile)) {
if (Index > 0) {
Str[Index] = 0;
return TRUE;
}
return FALSE;
} else {
Str[Index] = mGlobals.SourceFile.FileBufferPtr[0];
mGlobals.SourceFile.FileBufferPtr++;
Index++;
}
}
//
// See if we just ran out of file contents, but did find a token
//
if ((Index > 0) && EndOfFile (&mGlobals.SourceFile)) {
Str[Index] = 0;
return TRUE;
}
}
return FALSE;
}
BOOLEAN
SFPGetGuidToken (
CHAR8 *Str,
UINT32 Len
)
/*++
Routine Description:
Parse a GUID from the input stream. Stop when you discover white space.
Arguments:
Str - pointer to a copy of the next token
Len - size of buffer pointed to by Str
Returns:
TRUE - GUID string returned successfully
FALSE - otherwise
--*/
{
UINT32 Index;
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
Index = 0;
while (!EndOfFile (&mGlobals.SourceFile) && (Index < Len)) {
if (IsWhiteSpace (&mGlobals.SourceFile)) {
if (Index > 0) {
Str[Index] = 0;
return TRUE;
}
return FALSE;
} else {
Str[Index] = mGlobals.SourceFile.FileBufferPtr[0];
mGlobals.SourceFile.FileBufferPtr++;
Index++;
}
}
return FALSE;
}
BOOLEAN
SFPSkipToToken (
CHAR8 *Str
)
{
UINTN Len;
CHAR8 *SavePos;
Len = t_strlen (Str);
SavePos = mGlobals.SourceFile.FileBufferPtr;
SkipWhiteSpace (&mGlobals.SourceFile);
while (!EndOfFile (&mGlobals.SourceFile)) {
if (t_strncmp (Str, mGlobals.SourceFile.FileBufferPtr, Len) == 0) {
mGlobals.SourceFile.FileBufferPtr += Len;
return TRUE;
}
mGlobals.SourceFile.FileBufferPtr++;
SkipWhiteSpace (&mGlobals.SourceFile);
}
mGlobals.SourceFile.FileBufferPtr = SavePos;
return FALSE;
}
BOOLEAN
SFPGetNumber (
UINTN *Value
)
/*++
Routine Description:
Check the token at the current file position for a numeric value.
May be either decimal or hex.
Arguments:
Value - pointer where to store the value
Returns:
FALSE - current token is not a number
TRUE - current token is a number
--*/
{
unsigned Val;
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
if (isdigit ((int)mGlobals.SourceFile.FileBufferPtr[0])) {
//
// Check for hex value
//
if ((mGlobals.SourceFile.FileBufferPtr[0] == T_CHAR_0) && (mGlobals.SourceFile.FileBufferPtr[1] == T_CHAR_LC_X)) {
if (!isxdigit ((int)mGlobals.SourceFile.FileBufferPtr[2])) {
return FALSE;
}
mGlobals.SourceFile.FileBufferPtr += 2;
sscanf (mGlobals.SourceFile.FileBufferPtr, "%x", &Val);
*Value = (UINT32) Val;
while (isxdigit ((int)mGlobals.SourceFile.FileBufferPtr[0])) {
mGlobals.SourceFile.FileBufferPtr++;
}
return TRUE;
} else {
*Value = atoi (mGlobals.SourceFile.FileBufferPtr);
while (isdigit ((int)mGlobals.SourceFile.FileBufferPtr[0])) {
mGlobals.SourceFile.FileBufferPtr++;
}
return TRUE;
}
} else {
return FALSE;
}
}
STATUS
SFPCloseFile (
VOID
)
/*++
Routine Description:
Close the file being parsed.
Arguments:
None.
Returns:
STATUS_SUCCESS - the file was closed
STATUS_ERROR - no file is currently open
--*/
{
if (mGlobals.SourceFile.FileBuffer != NULL) {
free (mGlobals.SourceFile.FileBuffer);
memset (&mGlobals.SourceFile, 0, sizeof (mGlobals.SourceFile));
return STATUS_SUCCESS;
}
return STATUS_ERROR;
}
STATIC
STATUS
ProcessIncludeFile (
SOURCE_FILE *SourceFile,
SOURCE_FILE *ParentSourceFile
)
/*++
Routine Description:
Given a source file, open the file and parse it
Arguments:
SourceFile - name of file to parse
ParentSourceFile - for error reporting purposes, the file that #included SourceFile.
Returns:
Standard status.
--*/
{
STATIC UINTN NestDepth = 0;
CHAR8 FoundFileName[MAX_PATH];
STATUS Status;
Status = STATUS_SUCCESS;
NestDepth++;
//
// Print the file being processed. Indent so you can tell the include nesting
// depth.
//
if (mGlobals.VerboseFile) {
fprintf (stdout, "%*cProcessing file '%s'\n", (int)NestDepth * 2, ' ', SourceFile->FileName);
fprintf (stdout, "Parent source file = '%s'\n", ParentSourceFile->FileName);
}
//
// Make sure we didn't exceed our maximum nesting depth
//
if (NestDepth > MAX_NEST_DEPTH) {
Error (NULL, 0, 3001, "Not Supported", "%s exceeeds max nesting depth (%u)", SourceFile->FileName, (unsigned) NestDepth);
Status = STATUS_ERROR;
goto Finish;
}
//
// Try to open the file locally, and if that fails try along our include paths.
//
strcpy (FoundFileName, SourceFile->FileName);
if ((SourceFile->Fptr = fopen (LongFilePath (FoundFileName), "rb")) == NULL) {
return STATUS_ERROR;
}
//
// Process the file found
//
ProcessFile (SourceFile);
Finish:
//
// Close open files and return status
//
if (SourceFile->Fptr != NULL) {
fclose (SourceFile->Fptr);
SourceFile->Fptr = NULL;
}
return Status;
}
STATIC
STATUS
ProcessFile (
SOURCE_FILE *SourceFile
)
/*++
Routine Description:
Given a source file that's been opened, read the contents into an internal
buffer and pre-process it to remove comments.
Arguments:
SourceFile - structure containing info on the file to process
Returns:
Standard status.
--*/
{
//
// Get the file size, and then read the entire thing into memory.
// Allocate extra space for a terminator character.
//
fseek (SourceFile->Fptr, 0, SEEK_END);
SourceFile->FileSize = ftell (SourceFile->Fptr);
if (mGlobals.VerboseFile) {
printf ("FileSize = %u (0x%X)\n", (unsigned) SourceFile->FileSize, (unsigned) SourceFile->FileSize);
}
fseek (SourceFile->Fptr, 0, SEEK_SET);
SourceFile->FileBuffer = (CHAR8 *) malloc (SourceFile->FileSize + sizeof (CHAR8 ));
if (SourceFile->FileBuffer == NULL) {
Error (NULL, 0, 4001, "Resource: memory cannot be allocated", NULL);
return STATUS_ERROR;
}
fread ((VOID *) SourceFile->FileBuffer, SourceFile->FileSize, 1, SourceFile->Fptr);
SourceFile->FileBuffer[(SourceFile->FileSize / sizeof (CHAR8 ))] = T_CHAR_NULL;
//
// Pre-process the file to replace comments with spaces
//
PreprocessFile (SourceFile);
SourceFile->LineNum = 1;
return STATUS_SUCCESS;
}
STATIC
VOID
PreprocessFile (
SOURCE_FILE *SourceFile
)
/*++
Routine Description:
Preprocess a file to replace all carriage returns with NULLs so
we can print lines (as part of error messages) from the file to the screen.
Arguments:
SourceFile - structure that we use to keep track of an input file.
Returns:
Nothing.
--*/
{
BOOLEAN InComment;
BOOLEAN SlashSlashComment;
int LineNum;
RewindFile (SourceFile);
InComment = FALSE;
SlashSlashComment = FALSE;
while (!EndOfFile (SourceFile)) {
//
// If a line-feed, then no longer in a comment if we're in a // comment
//
if (SourceFile->FileBufferPtr[0] == T_CHAR_LF) {
SourceFile->FileBufferPtr++;
SourceFile->LineNum++;
if (InComment && SlashSlashComment) {
InComment = FALSE;
SlashSlashComment = FALSE;
}
} else if (SourceFile->FileBufferPtr[0] == T_CHAR_CR) {
//
// Replace all carriage returns with a NULL so we can print stuff
//
SourceFile->FileBufferPtr[0] = 0;
SourceFile->FileBufferPtr++;
//
// Check for */ comment end
//
} else if (InComment &&
!SlashSlashComment &&
(SourceFile->FileBufferPtr[0] == T_CHAR_STAR) &&
(SourceFile->FileBufferPtr[1] == T_CHAR_SLASH)
) {
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
InComment = FALSE;
} else if (InComment) {
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
//
// Check for // comments
//
} else if ((SourceFile->FileBufferPtr[0] == T_CHAR_SLASH) && (SourceFile->FileBufferPtr[1] == T_CHAR_SLASH)) {
InComment = TRUE;
SlashSlashComment = TRUE;
//
// Check for /* comment start
//
} else if ((SourceFile->FileBufferPtr[0] == T_CHAR_SLASH) && (SourceFile->FileBufferPtr[1] == T_CHAR_STAR)) {
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
SlashSlashComment = FALSE;
InComment = TRUE;
} else {
SourceFile->FileBufferPtr++;
}
}
//
// Could check for end-of-file and still in a comment, but
// should not be necessary. So just restore the file pointers.
//
RewindFile (SourceFile);
//
// Dump the reformatted file if verbose mode
//
if (mGlobals.VerboseFile) {
LineNum = 1;
printf ("%04d: ", LineNum);
while (!EndOfFile (SourceFile)) {
if (SourceFile->FileBufferPtr[0] == T_CHAR_LF) {
printf ("'\n%04d: '", ++LineNum);
} else {
printf ("%c", SourceFile->FileBufferPtr[0]);
}
SourceFile->FileBufferPtr++;
}
printf ("'\n");
printf ("FileSize = %u (0x%X)\n", (unsigned)SourceFile->FileSize, (unsigned)SourceFile->FileSize);
RewindFile (SourceFile);
}
}
BOOLEAN
SFPGetQuotedString (
CHAR8 *Str,
INTN Length
)
/*++
Routine Description:
Retrieve a quoted-string from the input file.
Arguments:
Str - pointer to a copy of the quoted string parsed
Length - size of buffer pointed to by Str
Returns:
TRUE - next token in input stream was a quoted string, and
the string value was returned in Str
FALSE - otherwise
--*/
{
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
if (mGlobals.SourceFile.FileBufferPtr[0] == T_CHAR_DOUBLE_QUOTE) {
mGlobals.SourceFile.FileBufferPtr++;
while (Length > 0) {
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
//
// Check for closing quote
//
if (mGlobals.SourceFile.FileBufferPtr[0] == T_CHAR_DOUBLE_QUOTE) {
mGlobals.SourceFile.FileBufferPtr++;
*Str = 0;
return TRUE;
}
*Str = mGlobals.SourceFile.FileBufferPtr[0];
Str++;
Length--;
mGlobals.SourceFile.FileBufferPtr++;
}
}
//
// First character was not a quote, or the input string length was
// insufficient to contain the quoted string, so return failure code.
//
return FALSE;
}
BOOLEAN
SFPIsEOF (
VOID
)
/*++
Routine Description:
Return TRUE of FALSE to indicate whether or not we've reached the end of the
file we're parsing.
Arguments:
NA
Returns:
TRUE - EOF reached
FALSE - otherwise
--*/
{
SkipWhiteSpace (&mGlobals.SourceFile);
return EndOfFile (&mGlobals.SourceFile);
}
#if 0
STATIC
CHAR8 *
GetQuotedString (
SOURCE_FILE *SourceFile,
BOOLEAN Optional
)
{
CHAR8 *String;
CHAR8 *Start;
CHAR8 *Ptr;
UINTN Len;
BOOLEAN PreviousBackslash;
if (SourceFile->FileBufferPtr[0] != T_CHAR_DOUBLE_QUOTE) {
if (Optional == FALSE) {
Error (SourceFile->FileName, SourceFile->LineNum, 0, "expected quoted string", "%S", SourceFile->FileBufferPtr);
}
return NULL;
}
Len = 0;
SourceFile->FileBufferPtr++;
Start = Ptr = SourceFile->FileBufferPtr;
PreviousBackslash = FALSE;
while (!EndOfFile (SourceFile)) {
if ((SourceFile->FileBufferPtr[0] == T_CHAR_DOUBLE_QUOTE) && (PreviousBackslash == FALSE)) {
break;
} else if (SourceFile->FileBufferPtr[0] == T_CHAR_CR) {
Warning (SourceFile->FileName, SourceFile->LineNum, 0, "carriage return found in quoted string", "%S", Start);
PreviousBackslash = FALSE;
} else if (SourceFile->FileBufferPtr[0] == T_CHAR_BACKSLASH) {
PreviousBackslash = TRUE;
} else {
PreviousBackslash = FALSE;
}
SourceFile->FileBufferPtr++;
Len++;
}
if (SourceFile->FileBufferPtr[0] != T_CHAR_DOUBLE_QUOTE) {
Warning (SourceFile->FileName, SourceFile->LineNum, 0, "missing closing quote on string", "%S", Start);
} else {
SourceFile->FileBufferPtr++;
}
//
// Now allocate memory for the string and save it off
//
String = (CHAR8 *) malloc ((Len + 1) * sizeof (CHAR8 ));
if (String == NULL) {
Error (NULL, 0, 4001, "Resource: memory cannot be allocated", NULL);
return NULL;
}
//
// Copy the string from the file buffer to the local copy.
// We do no reformatting of it whatsoever at this point.
//
Ptr = String;
while (Len > 0) {
*Ptr = *Start;
Start++;
Ptr++;
Len--;
}
*Ptr = 0;
return String;
}
#endif
STATIC
BOOLEAN
EndOfFile (
SOURCE_FILE *SourceFile
)
{
//
// The file buffer pointer will typically get updated before the End-of-file flag in the
// source file structure, so check it first.
//
if (SourceFile->FileBufferPtr >= SourceFile->FileBuffer + SourceFile->FileSize / sizeof (CHAR8 )) {
SourceFile->EndOfFile = TRUE;
return TRUE;
}
if (SourceFile->EndOfFile) {
return TRUE;
}
return FALSE;
}
#if 0
STATIC
VOID
ProcessTokenInclude (
SOURCE_FILE *SourceFile
)
{
CHAR8 IncludeFileName[MAX_PATH];
CHAR8 *To;
UINTN Len;
BOOLEAN ReportedError;
SOURCE_FILE IncludedSourceFile;
ReportedError = FALSE;
if (SkipWhiteSpace (SourceFile) == 0) {
Warning (SourceFile->FileName, SourceFile->LineNum, 0, "expected whitespace following #include keyword", NULL);
}
//
// Should be quoted file name
//
if (SourceFile->FileBufferPtr[0] != T_CHAR_DOUBLE_QUOTE) {
Error (SourceFile->FileName, SourceFile->LineNum, 0, "expected quoted include file name", NULL);
goto FailDone;
}
SourceFile->FileBufferPtr++;
//
// Copy the filename as ascii to our local string
//
To = IncludeFileName;
Len = 0;
while (!EndOfFile (SourceFile)) {
if ((SourceFile->FileBufferPtr[0] == T_CHAR_CR) || (SourceFile->FileBufferPtr[0] == T_CHAR_LF)) {
Error (SourceFile->FileName, SourceFile->LineNum, 0, "end-of-line found in quoted include file name", NULL);
goto FailDone;
}
if (SourceFile->FileBufferPtr[0] == T_CHAR_DOUBLE_QUOTE) {
SourceFile->FileBufferPtr++;
break;
}
//
// If too long, then report the error once and process until the closing quote
//
Len++;
if (!ReportedError && (Len >= sizeof (IncludeFileName))) {
Error (SourceFile->FileName, SourceFile->LineNum, 0, "length of include file name exceeds limit", NULL);
ReportedError = TRUE;
}
if (!ReportedError) {
*To = (CHAR8 ) SourceFile->FileBufferPtr[0];
To++;
}
SourceFile->FileBufferPtr++;
}
if (!ReportedError) {
*To = 0;
memset ((CHAR8 *) &IncludedSourceFile, 0, sizeof (SOURCE_FILE));
strcpy (IncludedSourceFile.FileName, IncludeFileName);
ProcessIncludeFile (&IncludedSourceFile, SourceFile);
}
return ;
FailDone:
//
// Error recovery -- skip to next #
//
SourceFile->SkipToHash = TRUE;
}
#endif
STATIC
BOOLEAN
IsWhiteSpace (
SOURCE_FILE *SourceFile
)
{
switch (*SourceFile->FileBufferPtr) {
case T_CHAR_NULL:
case T_CHAR_CR:
case T_CHAR_SPACE:
case T_CHAR_TAB:
case T_CHAR_LF:
return TRUE;
default:
return FALSE;
}
}
UINTN
SkipWhiteSpace (
SOURCE_FILE *SourceFile
)
{
UINTN Count;
Count = 0;
while (!EndOfFile (SourceFile)) {
Count++;
switch (*SourceFile->FileBufferPtr) {
case T_CHAR_NULL:
case T_CHAR_CR:
case T_CHAR_SPACE:
case T_CHAR_TAB:
SourceFile->FileBufferPtr++;
break;
case T_CHAR_LF:
SourceFile->FileBufferPtr++;
SourceFile->LineNum++;
break;
default:
return Count - 1;
}
}
//
// Some tokens require trailing whitespace. If we're at the end of the
// file, then we count that as well.
//
if ((Count == 0) && (EndOfFile (SourceFile))) {
Count++;
}
return Count;
}
STATIC
UINTN
t_strcmp (
CHAR8 *Buffer,
CHAR8 *Str
)
/*++
Routine Description:
Compare two strings for equality. The string pointed to by 'Buffer' may or may not be null-terminated,
so only compare up to the length of Str.
Arguments:
Buffer - pointer to first (possibly not null-terminated) string
Str - pointer to null-terminated string to compare to Buffer
Returns:
Number of bytes matched if exact match
0 if Buffer does not start with Str
--*/
{
UINTN Len;
Len = 0;
while (*Str && (*Str == *Buffer)) {
Buffer++;
Str++;
Len++;
}
if (*Str) {
return 0;
}
return Len;
}
STATIC
UINTN
t_strlen (
CHAR8 *Str
)
{
UINTN Len;
Len = 0;
while (*Str) {
Len++;
Str++;
}
return Len;
}
STATIC
UINTN
t_strncmp (
CHAR8 *Str1,
CHAR8 *Str2,
INTN Len
)
{
while (Len > 0) {
if (*Str1 != *Str2) {
return Len;
}
Len--;
Str1++;
Str2++;
}
return 0;
}
STATIC
CHAR8 *
t_strcpy (
CHAR8 *Dest,
CHAR8 *Src
)
{
CHAR8 *SaveDest;
SaveDest = Dest;
while (*Src) {
*Dest = *Src;
Dest++;
Src++;
}
*Dest = 0;
return SaveDest;
}
STATIC
VOID
RewindFile (
SOURCE_FILE *SourceFile
)
{
SourceFile->LineNum = 1;
SourceFile->FileBufferPtr = SourceFile->FileBuffer;
SourceFile->EndOfFile = 0;
}
STATIC
UINT32
GetHexChars (
CHAR8 *Buffer,
UINT32 BufferLen
)
{
UINT32 Len;
Len = 0;
while (!EndOfFile (&mGlobals.SourceFile) && (BufferLen > 0)) {
if (isxdigit ((int)mGlobals.SourceFile.FileBufferPtr[0])) {
*Buffer = mGlobals.SourceFile.FileBufferPtr[0];
Buffer++;
Len++;
BufferLen--;
mGlobals.SourceFile.FileBufferPtr++;
} else {
break;
}
}
//
// Null terminate if we can
//
if ((Len > 0) && (BufferLen > 0)) {
*Buffer = 0;
}
return Len;
}
BOOLEAN
SFPGetGuid (
INTN GuidStyle,
EFI_GUID *Value
)
/*++
Routine Description:
Parse a GUID from the input stream. Stop when you discover white space.
Arguments:
GuidStyle - Style of the following GUID token
Value - pointer to EFI_GUID struct for output
Returns:
TRUE - GUID string parsed successfully
FALSE - otherwise
GUID styles
Style[0] 12345678-1234-5678-AAAA-BBBBCCCCDDDD
--*/
{
unsigned Value32;
UINT32 Index;
FILE_POSITION FPos;
CHAR8 TempString[20];
CHAR8 TempString2[3];
CHAR8 *From;
CHAR8 *To;
UINT32 Len;
BOOLEAN Status;
Status = FALSE;
//
// Skip white space, then start parsing
//
SkipWhiteSpace (&mGlobals.SourceFile);
GetFilePosition (&FPos);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
if (GuidStyle == PARSE_GUID_STYLE_5_FIELDS) {
//
// Style[0] 12345678-1234-5678-AAAA-BBBBCCCCDDDD
//
Len = GetHexChars (TempString, sizeof (TempString));
if ((Len == 0) || (Len > 8)) {
goto Done;
}
sscanf (TempString, "%x", &Value32);
Value->Data1 = Value32;
//
// Next two UINT16 fields
//
if (mGlobals.SourceFile.FileBufferPtr[0] != '-') {
goto Done;
}
mGlobals.SourceFile.FileBufferPtr++;
Len = GetHexChars (TempString, sizeof (TempString));
if ((Len == 0) || (Len > 4)) {
goto Done;
}
sscanf (TempString, "%x", &Value32);
Value->Data2 = (UINT16) Value32;
if (mGlobals.SourceFile.FileBufferPtr[0] != '-') {
goto Done;
}
mGlobals.SourceFile.FileBufferPtr++;
Len = GetHexChars (TempString, sizeof (TempString));
if ((Len == 0) || (Len > 4)) {
goto Done;
}
sscanf (TempString, "%x", &Value32);
Value->Data3 = (UINT16) Value32;
//
// Parse the "AAAA" as two bytes
//
if (mGlobals.SourceFile.FileBufferPtr[0] != '-') {
goto Done;
}
mGlobals.SourceFile.FileBufferPtr++;
Len = GetHexChars (TempString, sizeof (TempString));
if ((Len == 0) || (Len > 4)) {
goto Done;
}
sscanf (TempString, "%x", &Value32);
Value->Data4[0] = (UINT8) (Value32 >> 8);
Value->Data4[1] = (UINT8) Value32;
if (mGlobals.SourceFile.FileBufferPtr[0] != '-') {
goto Done;
}
mGlobals.SourceFile.FileBufferPtr++;
//
// Read the last 6 bytes of the GUID
//
//
Len = GetHexChars (TempString, sizeof (TempString));
if ((Len == 0) || (Len > 12)) {
goto Done;
}
//
// Insert leading 0's to make life easier
//
if (Len != 12) {
From = TempString + Len - 1;
To = TempString + 11;
TempString[12] = 0;
while (From >= TempString) {
*To = *From;
To--;
From--;
}
while (To >= TempString) {
*To = '0';
To--;
}
}
//
// Now parse each byte
//
TempString2[2] = 0;
for (Index = 0; Index < 6; Index++) {
//
// Copy the two characters from the input string to something
// we can parse.
//
TempString2[0] = TempString[Index * 2];
TempString2[1] = TempString[Index * 2 + 1];
sscanf (TempString2, "%x", &Value32);
Value->Data4[Index + 2] = (UINT8) Value32;
}
Status = TRUE;
} else {
//
// Unsupported GUID style
//
return FALSE;
}
Done:
if (Status == FALSE) {
SetFilePosition (&FPos);
}
return Status;
}
STATIC
STATUS
GetFilePosition (
FILE_POSITION *Fpos
)
{
Fpos->FileBufferPtr = mGlobals.SourceFile.FileBufferPtr;
return STATUS_SUCCESS;
}
STATIC
STATUS
SetFilePosition (
FILE_POSITION *Fpos
)
{
//
// Should check range of pointer
//
mGlobals.SourceFile.FileBufferPtr = Fpos->FileBufferPtr;
return STATUS_SUCCESS;
}
|