bool BatchStar( Environment *theEnv, const char *fileName) { int inchar; bool done = false; FILE *theFile; char *theString = NULL; size_t position = 0; size_t maxChars = 0; #if (! RUN_TIME) && (! BLOAD_ONLY) char *oldParsingFileName; long oldLineCountValue; #endif /*======================*/ /* Open the batch file. */ /*======================*/ theFile = GenOpen(theEnv,fileName,"r"); if (theFile == NULL) { OpenErrorMessage(theEnv,"batch",fileName); return false; } /*======================================*/ /* Setup for capturing errors/warnings. */ /*======================================*/ #if (! RUN_TIME) && (! BLOAD_ONLY) oldParsingFileName = CopyString(theEnv,GetParsingFileName(theEnv)); SetParsingFileName(theEnv,fileName); CreateErrorCaptureRouter(theEnv); oldLineCountValue = SetLineCount(theEnv,1); #endif /*=====================================*/ /* If embedded, clear the error flags. */ /*=====================================*/ if (EvaluationData(theEnv)->CurrentExpression == NULL) { ResetErrorFlags(theEnv); } /*=============================================*/ /* Evaluate commands from the file one by one. */ /*=============================================*/ while (! done) { inchar = getc(theFile); if (inchar == EOF) { inchar = '\n'; done = true; } theString = ExpandStringWithChar(theEnv,inchar,theString,&position, &maxChars,maxChars+80); if (CompleteCommand(theString) != 0) { FlushPPBuffer(theEnv); SetPPBufferStatus(theEnv,false); RouteCommand(theEnv,theString,false); FlushPPBuffer(theEnv); SetHaltExecution(theEnv,false); SetEvaluationError(theEnv,false); FlushBindList(theEnv,NULL); genfree(theEnv,theString,maxChars); theString = NULL; maxChars = 0; position = 0; #if (! RUN_TIME) && (! BLOAD_ONLY) FlushParsingMessages(theEnv); #endif } if ((inchar == '\r') || (inchar == '\n')) { IncrementLineCount(theEnv); } } if (theString != NULL) { genfree(theEnv,theString,maxChars); } /*=======================*/ /* Close the batch file. */ /*=======================*/ GenClose(theEnv,theFile); /*========================================*/ /* Cleanup for capturing errors/warnings. */ /*========================================*/ #if (! RUN_TIME) && (! BLOAD_ONLY) FlushParsingMessages(theEnv); DeleteErrorCaptureRouter(theEnv); SetLineCount(theEnv,oldLineCountValue); SetParsingFileName(theEnv,oldParsingFileName); DeleteString(theEnv,oldParsingFileName); #endif return true; }
globle int EnvGetcRouter( void *theEnv, char *logicalName) { struct router *currentPtr; int inchar; /*===================================================*/ /* If the "fast load" option is being used, then the */ /* logical name is actually a pointer to a file and */ /* getc can be called directly to bypass querying */ /* all of the routers. */ /*===================================================*/ if (((char *) RouterData(theEnv)->FastLoadFilePtr) == logicalName) { inchar = getc(RouterData(theEnv)->FastLoadFilePtr); if ((inchar == '\r') || (inchar == '\n')) { if (((char *) RouterData(theEnv)->FastLoadFilePtr) == RouterData(theEnv)->LineCountRouter) { IncrementLineCount(theEnv); } } if (inchar == '\r') return('\n'); return(inchar); } /*===============================================*/ /* If the "fast string get" option is being used */ /* for the specified logical name, then bypass */ /* the router system and extract the character */ /* directly from the fast get string. */ /*===============================================*/ if (RouterData(theEnv)->FastCharGetRouter == logicalName) { inchar = (unsigned char) RouterData(theEnv)->FastCharGetString[RouterData(theEnv)->FastCharGetIndex]; RouterData(theEnv)->FastCharGetIndex++; if (inchar == '\0') return(EOF); if ((inchar == '\r') || (inchar == '\n')) { if (RouterData(theEnv)->FastCharGetRouter == RouterData(theEnv)->LineCountRouter) { IncrementLineCount(theEnv); } } if (inchar == '\r') return('\n'); return(inchar); } /*==============================================*/ /* Search through the list of routers until one */ /* is found that will handle the getc request. */ /*==============================================*/ currentPtr = RouterData(theEnv)->ListOfRouters; while (currentPtr != NULL) { if ((currentPtr->charget != NULL) ? QueryRouter(theEnv,logicalName,currentPtr) : FALSE) { if (currentPtr->environmentAware) { inchar = (*currentPtr->charget)(theEnv,logicalName); } else { inchar = ((int (*)(char *)) (*currentPtr->charget))(logicalName); } if ((inchar == '\r') || (inchar == '\n')) { if ((RouterData(theEnv)->LineCountRouter != NULL) && (strcmp(logicalName,RouterData(theEnv)->LineCountRouter) == 0)) { IncrementLineCount(theEnv); } } if (inchar == '\r') return('\n'); /* if (inchar != '\b') { return(inchar); } */ return(inchar); } currentPtr = currentPtr->next; } /*=====================================================*/ /* The logical name was not recognized by any routers. */ /*=====================================================*/ UnrecognizedRouterMessage(theEnv,logicalName); return(-1); }
int LLGetcBatch( Environment *theEnv, const char *logicalName, bool returnOnEOF) { int rv = EOF, flag = 1; /*=================================================*/ /* Get a character until a valid character appears */ /* or no more batch files are left. */ /*=================================================*/ while ((rv == EOF) && (flag == 1)) { if (FileCommandData(theEnv)->BatchType == FILE_BATCH) { rv = getc(FileCommandData(theEnv)->BatchFileSource); } else { rv = ReadRouter(theEnv,FileCommandData(theEnv)->BatchLogicalSource); } if (rv == EOF) { if (FileCommandData(theEnv)->BatchCurrentPosition > 0) WriteString(theEnv,STDOUT,(char *) FileCommandData(theEnv)->BatchBuffer); flag = RemoveBatch(theEnv); } } /*=========================================================*/ /* If the character retrieved is an end-of-file character, */ /* then there are no batch files with character input */ /* remaining. Remove the batch router. */ /*=========================================================*/ if (rv == EOF) { if (FileCommandData(theEnv)->BatchCurrentPosition > 0) WriteString(theEnv,STDOUT,(char *) FileCommandData(theEnv)->BatchBuffer); DeleteRouter(theEnv,"batch"); RemoveBatch(theEnv); if (returnOnEOF == true) { return (EOF); } else { return ReadRouter(theEnv,logicalName); } } /*========================================*/ /* Add the character to the batch buffer. */ /*========================================*/ if (RouterData(theEnv)->InputUngets == 0) { FileCommandData(theEnv)->BatchBuffer = ExpandStringWithChar(theEnv,(char) rv,FileCommandData(theEnv)->BatchBuffer,&FileCommandData(theEnv)->BatchCurrentPosition, &FileCommandData(theEnv)->BatchMaximumPosition,FileCommandData(theEnv)->BatchMaximumPosition+BUFFER_SIZE); } /*======================================*/ /* If a carriage return is encountered, */ /* then flush the batch buffer. */ /*======================================*/ if ((char) rv == '\n') { WriteString(theEnv,STDOUT,(char *) FileCommandData(theEnv)->BatchBuffer); FileCommandData(theEnv)->BatchCurrentPosition = 0; if ((FileCommandData(theEnv)->BatchBuffer != NULL) && (FileCommandData(theEnv)->BatchMaximumPosition > BUFFER_SIZE)) { rm(theEnv,FileCommandData(theEnv)->BatchBuffer,FileCommandData(theEnv)->BatchMaximumPosition); FileCommandData(theEnv)->BatchMaximumPosition = 0; FileCommandData(theEnv)->BatchBuffer = NULL; } } /*=============================*/ /* Increment the line counter. */ /*=============================*/ if (((char) rv == '\r') || ((char) rv == '\n')) { IncrementLineCount(theEnv); } /*=====================================================*/ /* Return the character retrieved from the batch file. */ /*=====================================================*/ return(rv); }