Cleanup code of rgbasm

Follow Linux kernel coding style.

Signed-off-by: Antonio Niño Díaz <antonio_nd@outlook.com>
This commit is contained in:
Antonio Niño Díaz
2018-01-02 03:04:26 +01:00
parent 2ffaf72e39
commit 72f801283d
22 changed files with 2852 additions and 2678 deletions

View File

@@ -19,19 +19,19 @@
#include "types.h"
struct sContext *pFileStack;
struct sSymbol *pCurrentMacro;
YY_BUFFER_STATE CurrentFlexHandle;
FILE *pCurrentFile;
uint32_t nCurrentStatus;
static struct sContext *pFileStack;
static struct sSymbol *pCurrentMacro;
static YY_BUFFER_STATE CurrentFlexHandle;
static FILE *pCurrentFile;
static uint32_t nCurrentStatus;
char tzCurrentFileName[_MAX_PATH + 1];
char IncludePaths[MAXINCPATHS][_MAX_PATH + 1];
int32_t NextIncPath = 0;
uint32_t nMacroCount;
static char IncludePaths[MAXINCPATHS][_MAX_PATH + 1];
static int32_t NextIncPath;
static uint32_t nMacroCount;
char *pCurrentREPTBlock;
uint32_t nCurrentREPTBlockSize;
uint32_t nCurrentREPTBlockCount;
static char *pCurrentREPTBlock;
static uint32_t nCurrentREPTBlockSize;
static uint32_t nCurrentREPTBlockCount;
uint32_t ulMacroReturnValue;
@@ -49,8 +49,7 @@ extern FILE *dependfile;
/*
* Context push and pop
*/
void
pushcontext(void)
static void pushcontext(void)
{
struct sContext **ppFileStack;
@@ -58,36 +57,37 @@ pushcontext(void)
while (*ppFileStack)
ppFileStack = &((*ppFileStack)->pNext);
if ((*ppFileStack = malloc(sizeof(struct sContext))) != NULL) {
(*ppFileStack)->FlexHandle = CurrentFlexHandle;
(*ppFileStack)->pNext = NULL;
strcpy((char *) (*ppFileStack)->tzFileName,
(char *) tzCurrentFileName);
(*ppFileStack)->nLine = nLineNo;
switch ((*ppFileStack)->nStatus = nCurrentStatus) {
case STAT_isMacroArg:
case STAT_isMacro:
sym_SaveCurrentMacroArgs((*ppFileStack)->tzMacroArgs);
(*ppFileStack)->pMacro = pCurrentMacro;
break;
case STAT_isInclude:
(*ppFileStack)->pFile = pCurrentFile;
break;
case STAT_isREPTBlock:
sym_SaveCurrentMacroArgs((*ppFileStack)->tzMacroArgs);
(*ppFileStack)->pREPTBlock = pCurrentREPTBlock;
(*ppFileStack)->nREPTBlockSize = nCurrentREPTBlockSize;
(*ppFileStack)->nREPTBlockCount =
nCurrentREPTBlockCount;
break;
}
nLineNo = 0;
} else
*ppFileStack = malloc(sizeof(struct sContext));
if (*ppFileStack == NULL)
fatalerror("No memory for context");
(*ppFileStack)->FlexHandle = CurrentFlexHandle;
(*ppFileStack)->pNext = NULL;
strcpy((char *)(*ppFileStack)->tzFileName, (char *)tzCurrentFileName);
(*ppFileStack)->nLine = nLineNo;
switch ((*ppFileStack)->nStatus = nCurrentStatus) {
case STAT_isMacroArg:
case STAT_isMacro:
sym_SaveCurrentMacroArgs((*ppFileStack)->tzMacroArgs);
(*ppFileStack)->pMacro = pCurrentMacro;
break;
case STAT_isInclude:
(*ppFileStack)->pFile = pCurrentFile;
break;
case STAT_isREPTBlock:
sym_SaveCurrentMacroArgs((*ppFileStack)->tzMacroArgs);
(*ppFileStack)->pREPTBlock = pCurrentREPTBlock;
(*ppFileStack)->nREPTBlockSize = nCurrentREPTBlockSize;
(*ppFileStack)->nREPTBlockCount = nCurrentREPTBlockCount;
break;
}
nLineNo = 0;
}
static int32_t
popcontext(void)
static int32_t popcontext(void)
{
struct sContext *pLastFile, **ppLastFile;
@@ -95,63 +95,67 @@ popcontext(void)
if (--nCurrentREPTBlockCount) {
yy_delete_buffer(CurrentFlexHandle);
CurrentFlexHandle =
yy_scan_bytes(pCurrentREPTBlock,
nCurrentREPTBlockSize);
yy_scan_bytes(pCurrentREPTBlock,
nCurrentREPTBlockSize);
yy_switch_to_buffer(CurrentFlexHandle);
sym_UseCurrentMacroArgs();
sym_SetMacroArgID(nMacroCount++);
sym_UseNewMacroArgs();
return (0);
return 0;
}
}
if ((pLastFile = pFileStack) != NULL) {
ppLastFile = &pFileStack;
while (pLastFile->pNext) {
ppLastFile = &(pLastFile->pNext);
pLastFile = *ppLastFile;
}
yy_delete_buffer(CurrentFlexHandle);
nLineNo = pLastFile->nLine;
if (nCurrentStatus == STAT_isInclude)
fclose(pCurrentFile);
if (nCurrentStatus == STAT_isMacro) {
sym_FreeCurrentMacroArgs();
nLineNo += 1;
}
if (nCurrentStatus == STAT_isREPTBlock)
nLineNo += 1;
pLastFile = pFileStack;
if (pLastFile == NULL)
return 1;
CurrentFlexHandle = pLastFile->FlexHandle;
strcpy((char *) tzCurrentFileName,
(char *) pLastFile->tzFileName);
switch (nCurrentStatus = pLastFile->nStatus) {
case STAT_isMacroArg:
case STAT_isMacro:
sym_RestoreCurrentMacroArgs(pLastFile->tzMacroArgs);
pCurrentMacro = pLastFile->pMacro;
break;
case STAT_isInclude:
pCurrentFile = pLastFile->pFile;
break;
case STAT_isREPTBlock:
sym_RestoreCurrentMacroArgs(pLastFile->tzMacroArgs);
pCurrentREPTBlock = pLastFile->pREPTBlock;
nCurrentREPTBlockSize = pLastFile->nREPTBlockSize;
nCurrentREPTBlockCount = pLastFile->nREPTBlockCount;
break;
}
ppLastFile = &pFileStack;
while (pLastFile->pNext) {
ppLastFile = &(pLastFile->pNext);
pLastFile = *ppLastFile;
}
free(*ppLastFile);
*ppLastFile = NULL;
yy_switch_to_buffer(CurrentFlexHandle);
return (0);
} else
return (1);
yy_delete_buffer(CurrentFlexHandle);
nLineNo = pLastFile->nLine;
if (nCurrentStatus == STAT_isInclude)
fclose(pCurrentFile);
if (nCurrentStatus == STAT_isMacro) {
sym_FreeCurrentMacroArgs();
nLineNo += 1;
}
if (nCurrentStatus == STAT_isREPTBlock)
nLineNo += 1;
CurrentFlexHandle = pLastFile->FlexHandle;
strcpy((char *)tzCurrentFileName, (char *)pLastFile->tzFileName);
switch (nCurrentStatus = pLastFile->nStatus) {
case STAT_isMacroArg:
case STAT_isMacro:
sym_RestoreCurrentMacroArgs(pLastFile->tzMacroArgs);
pCurrentMacro = pLastFile->pMacro;
break;
case STAT_isInclude:
pCurrentFile = pLastFile->pFile;
break;
case STAT_isREPTBlock:
sym_RestoreCurrentMacroArgs(pLastFile->tzMacroArgs);
pCurrentREPTBlock = pLastFile->pREPTBlock;
nCurrentREPTBlockSize = pLastFile->nREPTBlockSize;
nCurrentREPTBlockCount = pLastFile->nREPTBlockCount;
break;
}
free(*ppLastFile);
*ppLastFile = NULL;
yy_switch_to_buffer(CurrentFlexHandle);
return 0;
}
int32_t
fstk_GetLine(void)
int32_t fstk_GetLine(void)
{
struct sContext *pLastFile, **ppLastFile;
@@ -167,7 +171,9 @@ fstk_GetLine(void)
break; /* Peek top file of the stack */
}
if ((pLastFile = pFileStack) != NULL) {
pLastFile = pFileStack;
if (pLastFile != NULL) {
ppLastFile = &pFileStack;
while (pLastFile->pNext) {
ppLastFile = &(pLastFile->pNext);
@@ -176,24 +182,24 @@ fstk_GetLine(void)
return pLastFile->nLine;
}
/* This is only reached if the lexer is in REPT or MACRO mode but there
* are no saved contexts with the origin of said REPT or MACRO. */
fatalerror("fstk_GetLine: Internal error.");
/*
* This is only reached if the lexer is in REPT or MACRO mode but there
* are no saved contexts with the origin of said REPT or MACRO.
*/
fatalerror("%s: Internal error.", __func__);
}
int
yywrap(void)
int yywrap(void)
{
return (popcontext());
return popcontext();
}
/*
* Dump the context stack to stderr
*/
void
fstk_Dump(void)
void fstk_Dump(void)
{
struct sContext *pLastFile;
const struct sContext *pLastFile;
pLastFile = pFileStack;
@@ -209,46 +215,44 @@ fstk_Dump(void)
/*
* Extra includepath stuff
*/
void
fstk_AddIncludePath(char *s)
void fstk_AddIncludePath(char *s)
{
if (NextIncPath == MAXINCPATHS) {
if (NextIncPath == MAXINCPATHS)
fatalerror("Too many include directories passed from command line");
return;
}
if (strlcpy(IncludePaths[NextIncPath++], s, _MAX_PATH) >= _MAX_PATH) {
fatalerror("Include path too long '%s'",s);
return;
}
if (strlcpy(IncludePaths[NextIncPath++], s, _MAX_PATH) >= _MAX_PATH)
fatalerror("Include path too long '%s'", s);
}
FILE *
fstk_FindFile(char *fname)
FILE *fstk_FindFile(char *fname)
{
char path[_MAX_PATH];
int32_t i;
FILE *f;
if ((f = fopen(fname, "rb")) != NULL || errno != ENOENT) {
if (dependfile) {
f = fopen(fname, "rb");
if (f != NULL || errno != ENOENT) {
if (dependfile)
fprintf(dependfile, "%s: %s\n", tzObjectname, fname);
}
return f;
}
for (i = 0; i < NextIncPath; ++i) {
if (strlcpy(path, IncludePaths[i], sizeof path) >=
sizeof path) {
if (strlcpy(path, IncludePaths[i], sizeof(path))
>= sizeof(path))
continue;
}
if (strlcat(path, fname, sizeof path) >= sizeof path) {
continue;
}
if ((f = fopen(path, "rb")) != NULL || errno != ENOENT) {
if (strlcat(path, fname, sizeof(path)) >= sizeof(path))
continue;
f = fopen(path, "rb");
if (f != NULL || errno != ENOENT) {
if (dependfile) {
fprintf(dependfile, "%s: %s\n", tzObjectname, path);
fprintf(dependfile, "%s: %s\n", tzObjectname,
path);
}
return f;
}
@@ -261,17 +265,12 @@ fstk_FindFile(char *fname)
/*
* Set up an include file for parsing
*/
void
fstk_RunInclude(char *tzFileName)
void fstk_RunInclude(char *tzFileName)
{
FILE *f;
FILE *f = fstk_FindFile(tzFileName);
f = fstk_FindFile(tzFileName);
if (f == NULL) {
err(1, "Unable to open included file '%s'",
tzFileName);
}
if (f == NULL)
err(1, "Unable to open included file '%s'", tzFileName);
pushcontext();
nLineNo = 1;
@@ -281,7 +280,7 @@ fstk_RunInclude(char *tzFileName)
CurrentFlexHandle = yy_create_buffer(pCurrentFile);
yy_switch_to_buffer(CurrentFlexHandle);
//Dirty hack to give the INCLUDE directive a linefeed
/* Dirty hack to give the INCLUDE directive a linefeed */
yyunput('\n');
nLineNo -= 1;
@@ -290,35 +289,35 @@ fstk_RunInclude(char *tzFileName)
/*
* Set up a macro for parsing
*/
uint32_t
fstk_RunMacro(char *s)
uint32_t fstk_RunMacro(char *s)
{
struct sSymbol *sym;
struct sSymbol *sym = sym_FindMacro(s);
if ((sym = sym_FindMacro(s)) != NULL) {
pushcontext();
sym_SetMacroArgID(nMacroCount++);
nLineNo = -1;
sym_UseNewMacroArgs();
nCurrentStatus = STAT_isMacro;
strcpy(tzCurrentFileName, s);
if (sym->pMacro == NULL)
return 0;
pCurrentMacro = sym;
CurrentFlexHandle =
yy_scan_bytes(pCurrentMacro->pMacro,
strlen(pCurrentMacro->pMacro));
yy_switch_to_buffer(CurrentFlexHandle);
return (1);
} else
return (0);
if (sym == NULL)
return 0;
pushcontext();
sym_SetMacroArgID(nMacroCount++);
nLineNo = -1;
sym_UseNewMacroArgs();
nCurrentStatus = STAT_isMacro;
strcpy(tzCurrentFileName, s);
if (sym->pMacro == NULL)
return 0;
pCurrentMacro = sym;
CurrentFlexHandle = yy_scan_bytes(pCurrentMacro->pMacro,
strlen(pCurrentMacro->pMacro));
yy_switch_to_buffer(CurrentFlexHandle);
return 1;
}
/*
* Set up a macroargument for parsing
*/
void
fstk_RunMacroArg(int32_t s)
void fstk_RunMacroArg(int32_t s)
{
char *sym;
@@ -327,40 +326,41 @@ fstk_RunMacroArg(int32_t s)
else
s -= '0';
if ((sym = sym_FindMacroArg(s)) != NULL) {
pushcontext();
nCurrentStatus = STAT_isMacroArg;
sprintf(tzCurrentFileName, "%c", (uint8_t) s);
CurrentFlexHandle = yy_scan_bytes(sym, strlen(sym));
yy_switch_to_buffer(CurrentFlexHandle);
} else
sym = sym_FindMacroArg(s);
if (sym == NULL)
fatalerror("No such macroargument");
pushcontext();
nCurrentStatus = STAT_isMacroArg;
sprintf(tzCurrentFileName, "%c", (uint8_t)s);
CurrentFlexHandle = yy_scan_bytes(sym, strlen(sym));
yy_switch_to_buffer(CurrentFlexHandle);
}
/*
* Set up a stringequate for parsing
*/
void
fstk_RunString(char *s)
void fstk_RunString(char *s)
{
struct sSymbol *pSym;
const struct sSymbol *pSym = sym_FindSymbol(s);
if ((pSym = sym_FindSymbol(s)) != NULL) {
if (pSym != NULL) {
pushcontext();
nCurrentStatus = STAT_isMacroArg;
strcpy(tzCurrentFileName, s);
CurrentFlexHandle =
yy_scan_bytes(pSym->pMacro, strlen(pSym->pMacro));
yy_scan_bytes(pSym->pMacro, strlen(pSym->pMacro));
yy_switch_to_buffer(CurrentFlexHandle);
} else
} else {
yyerror("No such string symbol '%s'", s);
}
}
/*
* Set up a repeat block for parsing
*/
void
fstk_RunRept(uint32_t count)
void fstk_RunRept(uint32_t count)
{
if (count) {
pushcontext();
@@ -372,7 +372,7 @@ fstk_RunRept(uint32_t count)
nCurrentREPTBlockSize = ulNewMacroSize;
pCurrentREPTBlock = tzNewMacro;
CurrentFlexHandle =
yy_scan_bytes(pCurrentREPTBlock, nCurrentREPTBlockSize);
yy_scan_bytes(pCurrentREPTBlock, nCurrentREPTBlockSize);
yy_switch_to_buffer(CurrentFlexHandle);
}
}
@@ -380,21 +380,19 @@ fstk_RunRept(uint32_t count)
/*
* Initialize the filestack routines
*/
void
fstk_Init(char *s)
void fstk_Init(char *s)
{
char tzFileName[_MAX_PATH + 1];
char tzSymFileName[_MAX_PATH + 1 + 2];
snprintf(tzSymFileName, sizeof(tzSymFileName), "\"%s\"", s);
sym_AddString("__FILE__", tzSymFileName);
strcpy(tzFileName, s);
pFileStack = NULL;
pCurrentFile = fopen(tzFileName, "rb");
if (pCurrentFile == NULL) {
if (pCurrentFile == NULL)
err(1, "Unable to open file '%s'", tzFileName);
}
nMacroCount = 0;
nCurrentStatus = STAT_isInclude;