PUBLIC int ecOpenFileStream(EcCompiler *cp, cchar *path) { EcFileStream *fs; MprPath info; char *contents; if ((fs = ecCreateStream(cp, sizeof(EcFileStream), path, manageFileStream)) == 0) { return MPR_ERR_MEMORY; } if ((fs->file = mprOpenFile(path, O_RDONLY | O_BINARY, 0666)) == 0) { return MPR_ERR_CANT_OPEN; } if (mprGetPathInfo(path, &info) < 0 || info.size < 0) { mprCloseFile(fs->file); return MPR_ERR_CANT_ACCESS; } if ((contents = mprAlloc((int) info.size + 1)) == 0) { mprCloseFile(fs->file); return MPR_ERR_MEMORY; } if (mprReadFile(fs->file, contents, (int) info.size) != (int) info.size) { mprCloseFile(fs->file); return MPR_ERR_CANT_READ; } contents[info.size] = '\0'; ecSetStreamBuf((EcStream*) fs, contents, (ssize) info.size); mprCloseFile(fs->file); fs->file = 0; return 0; }
PUBLIC int ecOpenMemoryStream(EcCompiler *cp, cchar *contents, ssize len) { EcMemStream *ms; if ((ms = ecCreateStream(cp, sizeof(EcMemStream), "memory", ecManageStream)) == 0) { return MPR_ERR_MEMORY; } ecSetStreamBuf((EcStream*) ms, contents, len); return 0; }
PUBLIC int ecOpenConsoleStream(EcCompiler *cp, EcStreamGet getInput, cchar *contents) { EcConsoleStream *cs; if ((cs = ecCreateStream(cp, sizeof(EcConsoleStream), "console", ecManageStream)) == 0) { return MPR_ERR_MEMORY; } cs->stream.getInput = getInput; ecSetStreamBuf((EcStream*) cs, sclone(contents), contents ? strlen(contents) : 0); return 0; }
/* Read input from the console (stdin) */ static int consoleGets(EcStream *stream) { char prompt[MPR_MAX_STRING], *line, *cp; int level; if (stream->flags & EC_STREAM_EOL) { return 0; } level = stream->compiler->state ? stream->compiler->state->blockNestCount : 0; fmt(prompt, sizeof(prompt), "%s-%d> ", EJS_NAME, level); mprYield(MPR_YIELD_STICKY); line = readline(prompt); mprResetYield(); if (line == NULL) { stream->eof = 1; mprPrintf("\n"); return -1; } cp = strim(line, "\r\n", MPR_TRIM_BOTH); ecSetStreamBuf(stream, cp, slen(cp)); stream->flags |= EC_STREAM_EOL; return (int) slen(cp); }