Author: tkreuzer Date: Sat Aug 30 17:18:48 2014 New Revision: 63993
URL: http://svn.reactos.org/svn/reactos?rev=63993&view=rev Log: [HPP] Implement support for variable substitution
Added: trunk/reactos/tools/hpp/CMakeLists.txt (with props) Modified: trunk/reactos/tools/hpp/hpp.c
Added: trunk/reactos/tools/hpp/CMakeLists.txt URL: http://svn.reactos.org/svn/reactos/trunk/reactos/tools/hpp/CMakeLists.txt?re... ============================================================================== --- trunk/reactos/tools/hpp/CMakeLists.txt (added) +++ trunk/reactos/tools/hpp/CMakeLists.txt [iso-8859-1] Sat Aug 30 17:18:48 2014 @@ -0,0 +1 @@ +add_executable(hpp hpp.c)
Propchange: trunk/reactos/tools/hpp/CMakeLists.txt ------------------------------------------------------------------------------ svn:eol-style = native
Modified: trunk/reactos/tools/hpp/hpp.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/tools/hpp/hpp.c?rev=63993&a... ============================================================================== --- trunk/reactos/tools/hpp/hpp.c [iso-8859-1] (original) +++ trunk/reactos/tools/hpp/hpp.c [iso-8859-1] Sat Aug 30 17:18:48 2014 @@ -23,9 +23,12 @@ typedef struct _DEFINE { struct _DEFINE *pNext; - int len; int val; - char szName[1]; + char *pszName; + unsigned int cchName; + char *pszValue; + unsigned int cchValue; + char achBuffer[1]; } DEFINE, *PDEFINE;
DEFINE *gpDefines = 0; @@ -58,7 +61,7 @@ #endif i++; } - return(newpath); + return newpath; }
char* @@ -160,45 +163,87 @@ return len; }
- -void -WriteLine(char *pszLine, FILE *fileOut) -{ - char * pszEnd; - - pszEnd = strchr(pszLine, '\n'); - if (pszEnd) - { - int len = pszEnd - pszLine + 1; - fwrite(pszLine, 1, len, fileOut); - } -} - -int -EvaluateConstant(const char *p, char **pNext) +PDEFINE +FindDefine(const char *p, char **pNext) { PDEFINE pDefine; - int len; - - len = strxlen(p); + int cchName; + + cchName = strxlen(p); if (pNext) - *pNext = (char*)p + len; + *pNext = (char*)p + cchName;
/* search for the define in the global list */ pDefine = gpDefines; while (pDefine != 0) { - trace("found a define: %s\n", pDefine->szName); - if (pDefine->len == len) - { - if (strncmp(p, pDefine->szName, len) == 0) - { - return pDefine->val; + trace("found a define: %s\n", pDefine->pszName); + if (pDefine->cchName == cchName) + { + if (strncmp(p, pDefine->pszName, cchName) == 0) + { + return pDefine; } } pDefine = pDefine->pNext; } return 0; +} + +void +WriteLine(char *pchLine, FILE *fileOut) +{ + char *pch, *pchLineEnd, *pchVariable; + int len; + PDEFINE pDefine; + + pchLineEnd = strchr(pchLine, '\n'); + if (pchLineEnd == 0) + return; + + len = pchLineEnd - pchLine + 1; + + pch = pchLine; + while (len > 0) + { + /* Check if there is a $ variable in the line */ + pchVariable = strchr(pch, '$'); + if (pchVariable && (pchVariable < pchLineEnd)) + { + fwrite(pch, 1, pchVariable - pch, fileOut); + + pDefine = FindDefine(pchVariable + 1, &pch); + if (pDefine != 0) + { + fwrite(pDefine->pszValue, 1, pDefine->cchValue, fileOut); + } + else + { + len = strxlen(pchVariable + 1) + 1; + error("Could not find variable '%.*s'\n", len, pchVariable); + fwrite(pchVariable, 1, pch - pchVariable, fileOut); + } + + len = pchLineEnd - pch; + } + else + { + fwrite(pch, 1, len, fileOut); + break; + } + } +} + +int +EvaluateConstant(const char *p, char **pNext) +{ + PDEFINE pDefine; + + pDefine = FindDefine(p, pNext); + if (!pDefine) + return 0; + + return pDefine->val; }
int @@ -289,7 +334,7 @@ } else { - error("+Parse error: expected '(' or operator in Line %d, got %c\n", + error("+Parse error: expected '(' or operator in Line %d, got %c\n", iLine, pstart[0]); return -1; } @@ -317,7 +362,7 @@ ParseInputFile(const char *pszInFile, FILE *fileOut) { char* pInputData, *pCurrentLine, *p1, *p2; - size_t cbInFileLenth, len; + size_t cbInFileLenth; int iIfLevel, iCopyLevel;
trace("parsing input file: %s\n", pszInFile); @@ -393,33 +438,75 @@ if (strncmp(pCurrentLine, "$define", 7) == 0) { PDEFINE pDefine; + char *pchName, *pchValue; + size_t cchName, cchValue;
trace("found $define\n"); p1 = GetNextChar(pCurrentLine + 7); if (*p1 != '(') { - error("Parse error: expected '(' at %s:%d\n", + error("Parse error: expected '(' at %s:%d\n", pszInFile, iLine); return -1; } - p1 = GetNextChar(p1 + 1); - len = strxlen(p1); - p2 = p1 + len; - if (*p2 != ')') + + pchName = GetNextChar(p1 + 1); + cchName = strxlen(pchName); + p1 = GetNextChar(pchName + cchName); + + /* Check for assignment */ + if (*p1 == '=') + { + trace("found $define with assignment\n"); + pchValue = GetNextChar(p1 + 1); + cchValue = strxlen(pchValue); + p1 = GetNextChar(pchValue + cchValue); + } + else + { + pchValue = 0; + cchValue = 0; + } + + /* Allocate a DEFINE structure */ + pDefine = malloc(sizeof(DEFINE) + cchName + cchValue + 2); + if (pDefine == 0) + { + error("Failed to allocate %u bytes\n", + sizeof(DEFINE) + cchName + cchValue + 2); + return -1; + } + + pDefine->pszName = pDefine->achBuffer; + strncpy(pDefine->pszName, pchName, cchName); + pDefine->pszName[cchName] = 0; + pDefine->cchName = cchName; + pDefine->val = 1; + + if (pchValue != 0) + { + pDefine->pszValue = &pDefine->achBuffer[cchName + 1]; + strncpy(pDefine->pszValue, pchValue, cchValue); + pDefine->pszValue[cchValue] = 0; + pDefine->cchValue = cchValue; + } + else + { + pDefine->pszValue = 0; + pDefine->cchValue = 0; + } + + /* Insert the new define into the global list */ + pDefine->pNext = gpDefines; + gpDefines = pDefine; + + /* Check for closing ')' */ + if (*p1 != ')') { error("Parse error: expected ')' at %s:%d\n", pszInFile, iLine); return -1; } - - /* Insert the new define into the global list */ - pDefine = malloc(sizeof(DEFINE) + len); - strncpy(pDefine->szName, p1, len); - pDefine->szName[len] = 0; - pDefine->len = len; - pDefine->val = 1; - pDefine->pNext = gpDefines; - gpDefines = pDefine; }
/* Check for $if */ @@ -457,7 +544,7 @@ p1 = GetNextChar(pCurrentLine + 8); if (*p1 != '(') { - error("Parse error: expected '(' at %s:%d, found '%c'\n", + error("Parse error: expected '(' at %s:%d, found '%c'\n", pszInFile, iLine, *p1); return -1; } @@ -470,7 +557,7 @@
/* Restore the global file name */ gpszCurFile = pszInFile; - + /* Restore the zeroed character */ *p2 = ')';