4 #include "VfrCompiler.h"
8 CVfrCompiler::SET_RUN_STATUS (
9 IN COMPILER_RUN_STATUS Status
16 CVfrCompiler::IS_RUN_STATUS (
17 IN COMPILER_RUN_STATUS Status
20 return mRunStatus
== Status
;
24 CVfrCompiler::OptionInitialization (
31 mOptions
.VfrFileName
[0] = '\0';
32 mOptions
.RecordListFile
[0] = '\0';
33 mOptions
.CreateRecordListFile
= FALSE
;
34 mOptions
.CreateIfrPkgFile
= FALSE
;
35 mOptions
.PkgOutputFileName
[0] = '\0';
36 mOptions
.COutputFileName
[0] = '\0';
37 mOptions
.OutputDirectory
[0] = '\0';
38 mOptions
.PreprocessorOutputFileName
[0] = '\0';
39 mOptions
.VfrBaseFileName
[0] = '\0';
40 mOptions
.IncludePaths
= NULL
;
41 mOptions
.CPreprocessorOptions
= NULL
;
43 for (Index
= 1; (Index
< Argc
) && (Argv
[Index
][0] == '-'); Index
++) {
44 if ((_stricmp(Argv
[Index
], "-?") == 0) || (_stricmp(Argv
[Index
], "-h") == 0)) {
46 SET_RUN_STATUS (STATUS_DEAD
);
48 } else if (_stricmp(Argv
[Index
], "-l") == 0) {
49 mOptions
.CreateRecordListFile
= TRUE
;
50 gCIfrRecordInfoDB
.TurnOn ();
51 } else if (_stricmp(Argv
[Index
], "-i") == 0) {
53 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
54 printf ("%s -i - missing path argument\n", PROGRAM_NAME
);
58 AppendIncludePath(Argv
[Index
]);
59 } else if (_stricmp(Argv
[Index
], "-od") == 0) {
61 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
62 printf ("%s -od - missing output directory name\n", PROGRAM_NAME
);
65 strcpy (mOptions
.OutputDirectory
, Argv
[Index
]);
66 } else if (_stricmp(Argv
[Index
], "-ibin") == 0) {
67 mOptions
.CreateIfrPkgFile
= TRUE
;
68 } else if (_stricmp(Argv
[Index
], "-nostrings") == 0) {
69 } else if (_stricmp(Argv
[Index
], "-ppflag") == 0) {
71 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
72 printf ("%s -od - missing C-preprocessor argument\n", PROGRAM_NAME
);
76 AppendCPreprocessorOptions (Argv
[Index
]);
78 printf ("%s unrecognized option %s\n", PROGRAM_NAME
, Argv
[Index
]);
84 if (Index
!= Argc
- 1) {
85 printf ("%s must specify VFR file name", PROGRAM_NAME
);
89 strcpy (mOptions
.VfrFileName
, Argv
[Index
]);
92 if (SetBaseFileName() != 0) {
95 if (SetPkgOutputFileName () != 0) {
98 if (SetCOutputFileName() != 0) {
101 if (SetPreprocessorOutputFileName () != 0) {
104 if (SetRecordListFileName () != 0) {
110 SET_RUN_STATUS (STATUS_FAILED
);
112 mOptions
.VfrFileName
[0] = '\0';
113 mOptions
.RecordListFile
[0] = '\0';
114 mOptions
.CreateRecordListFile
= FALSE
;
115 mOptions
.CreateIfrPkgFile
= FALSE
;
116 mOptions
.PkgOutputFileName
[0] = '\0';
117 mOptions
.COutputFileName
[0] = '\0';
118 mOptions
.OutputDirectory
[0] = '\0';
119 mOptions
.PreprocessorOutputFileName
[0] = '\0';
120 mOptions
.VfrBaseFileName
[0] = '\0';
121 if (mOptions
.IncludePaths
!= NULL
) {
122 delete mOptions
.IncludePaths
;
123 mOptions
.IncludePaths
= NULL
;
125 if (mOptions
.CPreprocessorOptions
!= NULL
) {
126 delete mOptions
.CPreprocessorOptions
;
127 mOptions
.CPreprocessorOptions
= NULL
;
132 CVfrCompiler::AppendIncludePath (
137 INT8
*IncludePaths
= NULL
;
139 Len
= strlen (" -I ") + strlen (PathStr
) + 1;
140 if (mOptions
.IncludePaths
!= NULL
) {
141 Len
+= strlen (mOptions
.IncludePaths
);
143 IncludePaths
= new INT8
[Len
];
144 if (IncludePaths
== NULL
) {
145 printf ("%s memory allocation failure\n", PROGRAM_NAME
);
148 IncludePaths
[0] = '\0';
149 if (mOptions
.IncludePaths
!= NULL
) {
150 strcat (IncludePaths
, mOptions
.IncludePaths
);
152 strcat (IncludePaths
, " -I ");
153 strcat (IncludePaths
, PathStr
);
154 if (mOptions
.IncludePaths
!= NULL
) {
155 delete mOptions
.IncludePaths
;
157 mOptions
.IncludePaths
= IncludePaths
;
161 CVfrCompiler::AppendCPreprocessorOptions (
168 Len
= strlen (Options
) + strlen (" ") + 1;
169 if (mOptions
.CPreprocessorOptions
!= NULL
) {
170 Len
+= strlen (mOptions
.CPreprocessorOptions
);
174 printf ("%s memory allocation failure\n", PROGRAM_NAME
);
178 if (mOptions
.CPreprocessorOptions
!= NULL
) {
179 strcat (Opt
, mOptions
.CPreprocessorOptions
);
182 strcat (Opt
, Options
);
183 if (mOptions
.CPreprocessorOptions
!= NULL
) {
184 delete mOptions
.CPreprocessorOptions
;
186 mOptions
.CPreprocessorOptions
= Opt
;
190 CVfrCompiler::SetBaseFileName (
194 INT8
*pFileName
, *pPath
, *pExt
;
196 if (mOptions
.VfrFileName
[0] == '\0') {
200 pFileName
= mOptions
.VfrFileName
;
201 while ((pPath
= strchr (pFileName
, '\\')) != NULL
) {
202 pFileName
= pPath
+ 1;
205 if (pFileName
== NULL
) {
209 if ((pExt
= strchr (pFileName
, '.')) == NULL
) {
213 strncpy (mOptions
.VfrBaseFileName
, pFileName
, pExt
- pFileName
);
214 mOptions
.VfrBaseFileName
[pExt
- pFileName
] = '\0';
220 CVfrCompiler::SetPkgOutputFileName (
224 if (mOptions
.VfrBaseFileName
[0] == '\0') {
228 strcpy (mOptions
.PkgOutputFileName
, mOptions
.OutputDirectory
);
229 strcat (mOptions
.PkgOutputFileName
, mOptions
.VfrBaseFileName
);
230 strcat (mOptions
.PkgOutputFileName
, VFR_PACKAGE_FILENAME_EXTENSION
);
236 CVfrCompiler::SetCOutputFileName (
240 if (mOptions
.VfrBaseFileName
[0] == '\0') {
244 strcpy (mOptions
.COutputFileName
, mOptions
.OutputDirectory
);
245 strcat (mOptions
.COutputFileName
, mOptions
.VfrBaseFileName
);
246 strcat (mOptions
.COutputFileName
, ".c");
252 CVfrCompiler::SetPreprocessorOutputFileName (
256 if (mOptions
.VfrBaseFileName
[0] == '\0') {
260 strcpy (mOptions
.PreprocessorOutputFileName
, mOptions
.OutputDirectory
);
261 strcat (mOptions
.PreprocessorOutputFileName
, mOptions
.VfrBaseFileName
);
262 strcat (mOptions
.PreprocessorOutputFileName
, VFR_PREPROCESS_FILENAME_EXTENSION
);
268 CVfrCompiler::SetRecordListFileName (
272 if (mOptions
.VfrBaseFileName
[0] == '\0') {
276 strcpy (mOptions
.RecordListFile
, mOptions
.OutputDirectory
);
277 strcat (mOptions
.RecordListFile
, mOptions
.VfrBaseFileName
);
278 strcat (mOptions
.RecordListFile
, VFR_RECORDLIST_FILENAME_EXTENSION
);
283 CVfrCompiler::CVfrCompiler (
288 mPreProcessCmd
= PREPROCESSOR_COMMAND
;
289 mPreProcessOpt
= PREPROCESSOR_OPTIONS
;
291 OptionInitialization(Argc
, Argv
);
293 if ((IS_RUN_STATUS(STATUS_FAILED
)) || (IS_RUN_STATUS(STATUS_DEAD
))) {
297 SET_RUN_STATUS(STATUS_INITIALIZED
);
300 CVfrCompiler::~CVfrCompiler (
304 if (mOptions
.IncludePaths
!= NULL
) {
305 delete mOptions
.IncludePaths
;
306 mOptions
.IncludePaths
= NULL
;
309 if (mOptions
.CPreprocessorOptions
!= NULL
) {
310 delete mOptions
.CPreprocessorOptions
;
311 mOptions
.CPreprocessorOptions
= NULL
;
314 SET_RUN_STATUS(STATUS_DEAD
);
318 CVfrCompiler::Usage (
323 CONST INT8
*Help
[] = {
325 "VfrCompile version " VFR_COMPILER_VERSION
,
327 " Usage: VfrCompile {options} [VfrFile]",
329 " where options include:",
330 " -? or -h prints this help",
331 " -l create an output IFR listing file",
332 " -i IncPath add IncPath to the search path for VFR included files",
333 " -od OutputDir deposit all output files to directory OutputDir (default=cwd)",
334 " -ibin create an IFR HII pack file"
335 " -ppflag C-preprocessor argument",
336 " where parameters include:",
337 " VfrFile name of the input VFR script file",
341 for (Index
= 0; Help
[Index
] != NULL
; Index
++) {
342 fprintf (stdout
, "%s\n", Help
[Index
]);
347 CVfrCompiler::PreProcess (
351 FILE *pVfrFile
= NULL
;
353 INT8
*PreProcessCmd
= NULL
;
355 if (!IS_RUN_STATUS(STATUS_INITIALIZED
)) {
359 if ((pVfrFile
= fopen (mOptions
.VfrFileName
, "r")) == NULL
) {
360 printf ("%s could not open input VFR file - %s\n", PROGRAM_NAME
, mOptions
.VfrFileName
);
365 CmdLen
= strlen (mPreProcessCmd
) + strlen (mPreProcessOpt
) +
366 strlen (mOptions
.VfrFileName
) + strlen (mOptions
.PreprocessorOutputFileName
);
367 if (mOptions
.CPreprocessorOptions
!= NULL
) {
368 CmdLen
+= strlen (mOptions
.CPreprocessorOptions
);
370 if (mOptions
.IncludePaths
!= NULL
) {
371 CmdLen
+= strlen (mOptions
.IncludePaths
);
374 PreProcessCmd
= new INT8
[CmdLen
+ 10];
375 if (PreProcessCmd
== NULL
) {
376 printf ("%s could not allocate memory\n", PROGRAM_NAME
);
379 strcpy (PreProcessCmd
, mPreProcessCmd
), strcat (PreProcessCmd
, " ");
380 strcat (PreProcessCmd
, mPreProcessOpt
), strcat (PreProcessCmd
, " ");
381 if (mOptions
.IncludePaths
!= NULL
) {
382 strcat (PreProcessCmd
, mOptions
.IncludePaths
), strcat (PreProcessCmd
, " ");
384 if (mOptions
.CPreprocessorOptions
!= NULL
) {
385 strcat (PreProcessCmd
, mOptions
.CPreprocessorOptions
), strcat (PreProcessCmd
, " ");
387 strcat (PreProcessCmd
, mOptions
.VfrFileName
), strcat (PreProcessCmd
, " > ");
388 strcat (PreProcessCmd
, mOptions
.PreprocessorOutputFileName
);
390 if (system (PreProcessCmd
) != 0) {
391 printf ("%s failed to spawn C preprocessor on VFR file \n\t - %s\n", PROGRAM_NAME
, PreProcessCmd
);
395 delete PreProcessCmd
;
396 SET_RUN_STATUS (STATUS_PREPROCESSED
);
400 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
401 SET_RUN_STATUS (STATUS_FAILED
);
403 delete PreProcessCmd
;
406 extern UINT8
VfrParserStart (IN
FILE *);
409 CVfrCompiler::Compile (
413 FILE *VfrFile
= NULL
;
415 if (!IS_RUN_STATUS(STATUS_PREPROCESSED
)) {
419 if ((VfrFile
= fopen (mOptions
.PreprocessorOutputFileName
, "r")) == NULL
) {
420 printf ("%s failed to open input VFR preprocessor output file - %s\n", PROGRAM_NAME
, mOptions
.PreprocessorOutputFileName
);
424 if (VfrParserStart (VfrFile
) != 0) {
430 if (gCFormPkg
.HavePendingUnassigned () == TRUE
) {
431 gCFormPkg
.PendingAssignPrintAll ();
435 SET_RUN_STATUS (STATUS_COMPILEED
);
439 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
440 printf ("%s compile error!\n", PROGRAM_NAME
);
441 SET_RUN_STATUS (STATUS_FAILED
);
443 if (VfrFile
!= NULL
) {
449 CVfrCompiler::GenBinary (
455 if (!IS_RUN_STATUS(STATUS_COMPILEED
)) {
459 if (mOptions
.CreateIfrPkgFile
== TRUE
) {
460 if ((pFile
= fopen (mOptions
.PkgOutputFileName
, "wb")) == NULL
) {
461 printf ("can not open PkgFileName\n", mOptions
.PkgOutputFileName
);
464 if (gCFormPkg
.BuildPkg (pFile
) != VFR_RETURN_SUCCESS
) {
471 SET_RUN_STATUS (STATUS_GENBINARY
);
475 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
476 SET_RUN_STATUS (STATUS_FAILED
);
480 static const char *gSourceFileHeader
[] = {
482 "// DO NOT EDIT -- auto-generated file",
484 "// This file is generated by the vfrcompiler utility",
490 CVfrCompiler::GenCFile (
497 if (!IS_RUN_STATUS(STATUS_GENBINARY
)) {
501 if ((pFile
= fopen (mOptions
.COutputFileName
, "w")) == NULL
) {
502 printf ("failed to open output C file - %s\n", mOptions
.COutputFileName
);
506 for (Index
= 0; gSourceFileHeader
[Index
] != NULL
; Index
++) {
507 fprintf (pFile
, "%s\n", gSourceFileHeader
[Index
]);
510 gCVfrBufferConfig
.OutputCFile (pFile
, mOptions
.VfrBaseFileName
);
512 if (gCFormPkg
.GenCFile (mOptions
.VfrBaseFileName
, pFile
) != VFR_RETURN_SUCCESS
) {
518 SET_RUN_STATUS (STATUS_FINISHED
);
522 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
523 SET_RUN_STATUS (STATUS_FAILED
);
528 CVfrCompiler::GenRecordListFile (
532 FILE *pInFile
= NULL
;
533 FILE *pOutFile
= NULL
;
534 INT8 LineBuf
[MAX_LINE_LEN
];
537 if (mOptions
.CreateRecordListFile
== TRUE
) {
538 if ((mOptions
.PreprocessorOutputFileName
[0] == '\0') || (mOptions
.RecordListFile
[0] == '\0')) {
542 if ((pInFile
= fopen (mOptions
.PreprocessorOutputFileName
, "r")) == NULL
) {
543 printf ("%s failed to open input VFR preprocessor output file - %s\n", PROGRAM_NAME
, mOptions
.PreprocessorOutputFileName
);
547 if ((pOutFile
= fopen (mOptions
.RecordListFile
, "w")) == NULL
) {
548 printf ("%s failed to open record list file for writing - %s\n", PROGRAM_NAME
, mOptions
.RecordListFile
);
552 fprintf (pOutFile
, "//\n// VFR compiler version " VFR_COMPILER_VERSION
"\n//\n");
554 while (!feof (pInFile
)) {
555 if (fgets (LineBuf
, MAX_LINE_LEN
, pInFile
) != NULL
) {
556 fprintf (pOutFile
, "%s", LineBuf
);
558 gCIfrRecordInfoDB
.IfrRecordOutput (pOutFile
, LineNo
);
578 COMPILER_RUN_STATUS Status
;
579 CVfrCompiler
Compiler(Argc
, Argv
);
581 Compiler
.PreProcess();
583 Compiler
.GenBinary();
585 Compiler
.GenRecordListFile ();
587 Status
= Compiler
.RunStatus ();
588 if ((Status
== STATUS_DEAD
) || (Status
== STATUS_FAILED
)) {