void _pushFileStack(char *fileName) { int i; if (fileStackLevel >= 0) { for (i = 0; i < g_tlang.numOfOutFiles; i++) { g_tlang.outputCurrLine(i, 0); if (CURR_OUTPUT(i) != NULL) { wicFclose(CURR_OUTPUT(i)); } } } fileStackLevel++; if (fileStackLevel >= MAX_INCLUDE_NEST) { fileStackLevel--; reportError(ERR_NEST_INCLUDE); return; } else { FDReadInd entry; pFDReadInd incFile; initFDReadInd(&entry, fileName, 0); incFile = findHTableElem(g_fileTable, &entry); assert(incFile != NULL); assert(fileName != NULL); for (i = 0; i < g_tlang.numOfOutFiles; i++) { setNewFileExt(CURR_FILE_NAME(i), fileName, g_tlang.extension[i]); if (incFile->readOnly) { CURR_OUTPUT(i) = NULL; } else { CURR_OUTPUT(i) = wicFopen(CURR_FILE_NAME(i), "wt"); if (CURR_OUTPUT(i) == NULL) { int j; for (j = i-1; j >= 0; j--) { wicFclose(CURR_OUTPUT(j)); CURR_OUTPUT(j) = NULL; } // fileStackLevel--; return; } } CURR_LAST_CHAR(i) = 0; CURR_LINE_LEN(i) = 0; CURR_LINE(i)[0] = 0; CURR_LINE_FLAGS(i) = g_tlang.lfInitFlags; } } }
int popTokFile(void) { assert(currTokF >= 0); wicFclose(TOK_FILE); if (currTokF == 0) { g_currFileName = NULL; return 0; } else { debugOut("Pop file: %s\n", TOK_FILE_NAME); restoreTokFile(); return 1; } }
static FILE *_openIncludeFile(char *fname, char **newAllocName) { FILE *file = NULL; char fullName[_MAX_PATH]; pFDReadInd dir = NULL; char *fullNameCopy; strcpy(fullName, fname); rewindCurrSLListPos(g_opt.incPathList); do { setDefaultExt(fullName, "h"); if (fileReadable(fullName)) { file = wicFopen(fullName, "r"); if (file != NULL) { pFDReadInd newElem; fullNameCopy = wicStrdup(fullName); newElem = createFDReadInd(fullNameCopy, dir == NULL ? 0 : dir->readOnly); if (!addHTableElem(g_fileTable, newElem)) { reportError(ERR_FILE_WAS_INCLUDED, fullNameCopy); wicFclose(file); zapFDReadInd(newElem); file = NULL; } break; } } if (!getCurrSLListPosElem(g_opt.incPathList, &dir)) { strcpy(fullName, fname); setDefaultExt(fullName, "h"); reportError(ERR_INC_FILE_NOT_FOUND, fullName); break; } sprintf(fullName, "%s\\%s", dir->name, fname); incCurrSLListPos(g_opt.incPathList); } while (1); if (file == NULL) { *newAllocName = NULL; } else { *newAllocName = fullNameCopy; } return file; }
void _popFileStack(void) { int i; assert(fileStackLevel >= 0); for (i = 0; i < g_tlang.numOfOutFiles; i++) { g_tlang.outputCurrLine(i, 0); if (CURR_OUTPUT(i) != NULL) { wicFclose(CURR_OUTPUT(i)); } } fileStackLevel--; if (fileStackLevel >= 0) { for (i = 0; i < g_tlang.numOfOutFiles; i++) { CURR_OUTPUT(i) = wicFopen(CURR_FILE_NAME(i), "at"); if (CURR_OUTPUT(i) == NULL) { reportError(ERR_OPEN_FILE, CURR_FILE_NAME(i), strerror(errno)); reportError(FATAL_DIE); } } } }
char* pushTokFile(char *fname) { if (currTokF >= 0) { if (TOK_FILE != NULL) { CURR_FILE_POS = ftell(TOK_FILE); wicFclose(TOK_FILE); } } currTokF++; assert(currLineIsInclude == 0); currLineIsInclude = 0; if (currTokF >= MAX_INCLUDE_NEST) { currTokF--; reportError(ERR_NEST_INCLUDE); return NULL; } else { assert(fname != NULL); TOK_FILE = _openIncludeFile(fname, &TOK_FILE_NAME); if (TOK_FILE == NULL) { restoreTokFile(); return NULL; } else { debugOut("Push file: %s\n", fname); g_currFileName = TOK_FILE_NAME; STATE = TS_START; LINE_NUM = 1; PREV_TOK_LINE_NUM = 1; COL_NUM = 0; g_currLineNum = LINE_NUM; g_currColNum = COL_NUM; NEXT_CHAR = 0; _NEXT_NEXT_CHAR = fgetc(TOK_FILE); TOK_NUM_AFTER_NEW_LINE = 0; getNextChar(); } } return TOK_FILE_NAME; }