/************************************************************************** f i c l E v a l u a t e ** Wrapper for ficlExec() which sets SOURCE-ID to -1. **************************************************************************/ int ficlEvaluate(FICL_VM *pVM, char *pText) { int returnValue; CELL id = pVM->sourceID; pVM->sourceID.i = -1; returnValue = ficlExecC(pVM, pText, -1); pVM->sourceID = id; return returnValue; }
int ficlExecFD(FICL_VM *pVM, int fd) { char cp[nLINEBUF]; int nLine = 0, rval = VM_OUTOFTEXT; char ch; CELL id; id = pVM->sourceID; pVM->sourceID.i = fd; /* feed each line to ficlExec */ while (1) { int status, i; i = 0; while ((status = read(fd, &ch, 1)) > 0 && ch != '\n') cp[i++] = ch; nLine++; if (!i) { if (status < 1) break; continue; } rval = ficlExecC(pVM, cp, i); if(rval != VM_QUIT && rval != VM_USEREXIT && rval != VM_OUTOFTEXT) { pVM->sourceID = id; return rval; } } /* ** Pass an empty line with SOURCE-ID == -1 to flush ** any pending REFILLs (as required by FILE wordset) */ pVM->sourceID.i = -1; ficlExec(pVM, ""); pVM->sourceID = id; return rval; }
static void ficlIncludeFile(FICL_VM *pVM) /* ( i*x fileid -- j*x ) */ { ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack); CELL id = pVM->sourceID; int result = VM_OUTOFTEXT; long currentPosition, totalSize; long size; pVM->sourceID.p = (void *)ff; currentPosition = ftell(ff->f); totalSize = fileSize(ff->f); size = totalSize - currentPosition; if ((totalSize != -1) && (currentPosition != -1) && (size > 0)) { char *buffer = (char *)malloc(size); long got = fread(buffer, 1, size, ff->f); if (got == size) result = ficlExecC(pVM, buffer, size); } #if 0 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack); CELL id = pVM->sourceID; char cp[nLINEBUF]; int nLine = 0; int keepGoing; int result; pVM->sourceID.p = (void *)ff; /* feed each line to ficlExec */ keepGoing = TRUE; while (keepGoing && fgets(cp, nLINEBUF, ff->f)) { int len = strlen(cp) - 1; nLine++; if (len <= 0) continue; if (cp[len] == '\n') cp[len] = '\0'; result = ficlExec(pVM, cp); switch (result) { case VM_OUTOFTEXT: case VM_USEREXIT: break; default: pVM->sourceID = id; keepGoing = FALSE; break; } } #endif /* 0 */ /* ** Pass an empty line with SOURCE-ID == -1 to flush ** any pending REFILLs (as required by FILE wordset) */ pVM->sourceID.i = -1; ficlExec(pVM, ""); pVM->sourceID = id; closeFiclFILE(ff); }
static void ficlLoad(FICL_VM *pVM) { char cp[nLINEBUF]; char filename[nLINEBUF]; FICL_STRING *pFilename = (FICL_STRING *)filename; int nLine = 0; FILE *fp; int result; CELL id; struct stat buf; vmGetString(pVM, pFilename, '\n'); if (pFilename->count <= 0) { vmTextOut(pVM, "Warning (load): nothing happened", 1); return; } /* ** get the file's size and make sure it exists */ result = stat( pFilename->text, &buf ); if (result != 0) { vmTextOut(pVM, "Unable to stat file: ", 0); vmTextOut(pVM, pFilename->text, 1); vmThrow(pVM, VM_QUIT); } fp = fopen(pFilename->text, "r"); if (!fp) { vmTextOut(pVM, "Unable to open file ", 0); vmTextOut(pVM, pFilename->text, 1); vmThrow(pVM, VM_QUIT); } id = pVM->sourceID; pVM->sourceID.p = (void *)fp; /* feed each line to ficlExec */ while (fgets(cp, nLINEBUF, fp)) { int len = strlen(cp) - 1; nLine++; if (len <= 0) continue; result = ficlExecC(pVM, cp, len); if (result != VM_QUIT && result != VM_USEREXIT && result != VM_OUTOFTEXT ) { pVM->sourceID = id; fclose(fp); vmThrowErr(pVM, "Error loading file <%s> line %d", pFilename->text, nLine); break; } } /* ** Pass an empty line with SOURCE-ID == -1 to flush ** any pending REFILLs (as required by FILE wordset) */ pVM->sourceID.i = -1; ficlExec(pVM, ""); pVM->sourceID = id; fclose(fp); /* handle "bye" in loaded files. --lch */ if (result == VM_USEREXIT) vmThrow(pVM, VM_USEREXIT); return; }
/************************************************************************** f i c l E x e c ** Evaluates a block of input text in the context of the ** specified interpreter. Emits any requested output to the ** interpreter's output function. ** ** Contains the "inner interpreter" code in a tight loop ** ** Returns one of the VM_XXXX codes defined in ficl.h: ** VM_OUTOFTEXT is the normal exit condition ** VM_ERREXIT means that the interp encountered a syntax error ** and the vm has been reset to recover (some or all ** of the text block got ignored ** VM_USEREXIT means that the user executed the "bye" command ** to shut down the interpreter. This would be a good ** time to delete the vm, etc -- or you can ignore this ** signal. **************************************************************************/ int ficlExec(FICL_VM *pVM, char *pText) { return ficlExecC(pVM, pText, -1); }