Support -P/--preinclude to pre-INCLUDE a file (#1043)

Fixes #1041

Co-authored-by: ISSOtm <eldredhabert0@gmail.com>
This commit is contained in:
Rangi
2022-09-24 12:37:16 -04:00
committed by GitHub
parent 02923a67f3
commit b8385a50e3
13 changed files with 121 additions and 11 deletions

View File

@@ -42,6 +42,8 @@ size_t maxRecursionDepth;
static unsigned int nbIncPaths = 0;
static char const *includePaths[MAXINCPATHS];
static const char *preIncludeName;
static const char *dumpNodeAndParents(struct FileStackNode const *node)
{
char const *name;
@@ -133,6 +135,11 @@ void fstk_AddIncludePath(char const *path)
includePaths[nbIncPaths++] = str;
}
void fstk_SetPreIncludeFile(char const *path)
{
preIncludeName = path;
}
static void printDep(char const *path)
{
if (dependfile) {
@@ -274,6 +281,7 @@ bool yywrap(void)
lexer_SetState(contextStack->lexerState);
macro_SetUniqueID(contextStack->uniqueID);
return false;
}
@@ -342,6 +350,41 @@ void fstk_RunInclude(char const *path)
contextStack->uniqueID = macro_UndefUniqueID();
}
// Similar to `fstk_RunInclude`, but not subject to `-MG`, and
// calling `lexer_SetState` instead of `lexer_SetStateAtEOL`.
static void runPreIncludeFile(void)
{
if (!preIncludeName)
return;
char *fullPath = NULL;
size_t size = 0;
if (!fstk_FindFile(preIncludeName, &fullPath, &size)) {
free(fullPath);
error("Unable to open included file '%s': %s\n", preIncludeName, strerror(errno));
return;
}
struct FileStackNamedNode *fileInfo = malloc(sizeof(*fileInfo) + size);
if (!fileInfo) {
error("Failed to alloc file info for pre-include: %s\n", strerror(errno));
return;
}
fileInfo->node.type = NODE_FILE;
strcpy(fileInfo->name, fullPath);
free(fullPath);
newContext((struct FileStackNode *)fileInfo);
contextStack->lexerState = lexer_OpenFile(fileInfo->name);
if (!contextStack->lexerState)
fatalerror("Failed to set up lexer for file include\n");
lexer_SetState(contextStack->lexerState);
// We're back at top-level, so most things are reset
contextStack->uniqueID = macro_UndefUniqueID();
}
void fstk_RunMacro(char const *macroName, struct MacroArgs *args)
{
struct Symbol *macro = sym_FindExactSymbol(macroName);
@@ -563,4 +606,6 @@ void fstk_Init(char const *mainPath, size_t maxDepth)
// Make sure that the default of 64 is OK, though
assert(DEPTH_LIMIT >= DEFAULT_MAX_DEPTH);
#undef DEPTH_LIMIT
runPreIncludeFile();
}

View File

@@ -87,7 +87,7 @@ static char *make_escape(char const *str)
}
// Short options
static const char *optstring = "b:D:Eg:Hhi:LlM:o:p:Q:r:VvW:w";
static const char *optstring = "b:D:Eg:Hhi:LlM:o:P:p:Q:r:VvW:w";
// Variables for the long-only options
static int depType; // Variants of `-M`
@@ -116,6 +116,7 @@ static struct option const longopts[] = {
{ "MT", required_argument, &depType, 'T' },
{ "MQ", required_argument, &depType, 'Q' },
{ "output", required_argument, NULL, 'o' },
{ "preinclude", required_argument, NULL, 'P' },
{ "pad-value", required_argument, NULL, 'p' },
{ "q-precision", required_argument, NULL, 'Q' },
{ "recursion-depth", required_argument, NULL, 'r' },
@@ -130,8 +131,8 @@ static void print_usage(void)
fputs(
"Usage: rgbasm [-EHhLlVvw] [-b chars] [-D name[=value]] [-g chars] [-i path]\n"
" [-M depend_file] [-MG] [-MP] [-MT target_file] [-MQ target_file]\n"
" [-o out_file] [-p pad_value] [-Q precision] [-r depth]\n"
" [-W warning] <file>\n"
" [-o out_file] [-P include_file] [-p pad_value] [-Q precision]\n"
" [-r depth] [-W warning] <file>\n"
"Useful options:\n"
" -E, --export-all export all labels\n"
" -M, --dependfile <path> set the output dependency file\n"
@@ -254,6 +255,10 @@ int main(int argc, char *argv[])
out_SetFileName(musl_optarg);
break;
case 'P':
fstk_SetPreIncludeFile(musl_optarg);
break;
unsigned long padByte;
case 'p':
padByte = strtoul(musl_optarg, &ep, 0);