/* * Interpret from the console or from a literal command */ static int interpretCommands(EcCompiler *cp, cchar *cmd) { Ejs *ejs; EjsString *result; char *tmpArgv[1]; int err; ejs = cp->ejs; if (ecOpenConsoleStream(cp->lexer, (cmd) ? commandGets: consoleGets) < 0) { mprError(cp, "Can't open input"); return EJS_ERR; } if (cmd) { cp->lexer->input->stream->buf = mprStrdup(cp, cmd); } else { cp->interactive = 1; } cp->input = cp->lexer->input; cp->token = cp->lexer->input->token; ecResetInput(cp); tmpArgv[0] = EC_INPUT_STREAM; while (!cp->lexer->input->stream->eof) { err = 0; cp->uid = 0; if (ecCompile(cp, 1, tmpArgv, 0) < 0) { ejs->result = ejs->undefinedValue; err++; } if (cp->run && !err && cp->errorCount == 0) { if (ejsRun(ejs) < 0) { ejsReportError(ejs, "Error in script"); } } if (!ejs->exception && ejs->result != ejs->undefinedValue) { if (ejs->result->primitive || ejsIsDate(ejs->result)) { if ((result = (EjsString*) ejsToString(ejs, ejs->result)) != 0) { mprPrintf(cp, "%s\n", result->value); } } else { if ((result = (EjsString*) ejsToJson(ejs, ejs->result)) != 0) { mprPrintf(cp, "%s\n", result->value); } } } ecResetInput(cp); cp->errorCount = 0; err = 0; } ecCloseStream(cp->lexer); return 0; }
/* * Interpret from the console or from a literal command */ static int interpretCommands(EcCompiler *cp, cchar *cmd) { Ejs *ejs; EjsString *result; char *tmpArgv[1]; int err; ejs = cp->ejs; if (ecOpenConsoleStream(cp->lexer, (cmd) ? commandGets: consoleGets) < 0) { mprError(cp, "Can't open input"); return EJS_ERR; } if (cmd) { cp->lexer->input->stream->buf = mprStrdup(cp, cmd); } else { cp->interactive = 1; } cp->input = cp->lexer->input; cp->token = cp->lexer->input->token; ecResetInput(cp); tmpArgv[0] = EC_INPUT_STREAM; while (!cp->lexer->input->stream->eof) { ejs->flags |= EJS_FLAG_COMPILER; err = 0; cp->uid = 0; if (ecCompile(cp, 1, tmpArgv, 0) < 0) { ejs->result = ejs->undefinedValue; err++; } if (cp->run && !err && cp->errorCount == 0) { ejs->flags &= ~EJS_FLAG_COMPILER; ejsCollectGarbage(ejs, 1); ejsRun(ejs); } if (ejs->result != ejs->undefinedValue) { result = (EjsString*) ejsCastVar(ejs, ejs->result, ejs->stringType); mprPrintf(cp, "%s\n", result->value); } ecResetInput(cp); err = 0; cp->errorCount = 0; } ecCloseStream(cp->lexer); return 0; }
/* Interpret from the console or from a literal command */ static int interpretCommands(EcCompiler *cp, cchar *cmd) { Ejs *ejs; EjsString *result; char *tmpArgv[1]; int err; ejs = cp->ejs; cp->interactive = 1; if (ecOpenConsoleStream(cp, (cmd) ? commandGets: consoleGets, cmd) < 0) { mprError("Cannot open input"); return EJS_ERR; } tmpArgv[0] = EC_INPUT_STREAM; while (!cp->stream->eof && !mprIsStopping()) { err = 0; cp->uid = 0; ejs->result = ESV(undefined); if (ecCompile(cp, 1, tmpArgv) < 0) { mprRawLog(0, "%s", cp->errorMsg); ejs->result = ESV(undefined); err++; } if (!err && cp->errorCount == 0) { if (ejsRunProgram(ejs, NULL, NULL) < 0) { ejsReportError(ejs, "Error in script"); } } if (!ejs->exception && ejs->result != ESV(undefined)) { if (ejsIs(ejs, ejs->result, Date) /* MOB || ejsIsType(ejs, ejs->result) */) { if ((result = (EjsString*) ejsToString(ejs, ejs->result)) != 0) { mprPrintf("%@\n", result); } } else if (ejs->result != ESV(null)) { if ((result = (EjsString*) ejsSerialize(ejs, ejs->result, EJS_JSON_SHOW_PRETTY)) != 0) { mprPrintf("%@\n", result); } } } ecResetInput(cp); cp->errorCount = 0; cp->fatalError = 0; } ecCloseStream(cp); return 0; }