static int loadline(LuaState L) { int status; Lua.LuaSetTop(L, 0); if (pushline(L, 1) == 0) { return(-1); /* no input */ } for (; ;) { /* repeat until gets a complete line */ status = Lua.LuaLLoadBuffer(L, Lua.LuaToString(L, 1), Lua.LuaStrLen(L, 1), "=stdin"); if (incomplete(L, status) == 0) { break; /* cannot try to add lines? */ } if (pushline(L, 0) == 0) /* no more input? */ { return(-1); } Lua.LuaPushLiteral(L, "\n"); /* add a new line... */ Lua.LuaInsert(L, -2); /* ...between the two lines */ Lua.LuaConcat(L, 3); /* join them */ } Lua.lua_saveline(L, 1); Lua.LuaRemove(L, 1); /* remove line */ return(status); }
static void dotty(LuaState L) { int status; CharPtr oldprogname = progname; progname = null; while ((status = loadline(L)) != -1) { if (status == 0) { status = docall(L, 0, 0); } report(L, status); if (status == 0 && Lua.LuaGetTop(L) > 0) { /* any result to print? */ Lua.LuaGetGlobal(L, "print"); Lua.LuaInsert(L, 1); if (Lua.LuaPCall(L, Lua.LuaGetTop(L) - 1, 0, 0) != 0) { l_message(progname, Lua.LuaPushFString(L, "error calling " + Lua.LUA_QL("print").ToString() + " (%s)", Lua.LuaToString(L, -1))); } } } Lua.LuaSetTop(L, 0); /* clear stack */ Lua.fputs("\n", Lua.stdout); Lua.fflush(Lua.stdout); progname = oldprogname; }
static int docall(LuaState L, int narg, int clear) { int status; int base_ = Lua.LuaGetTop(L) - narg; /* function index */ Lua.LuaPushCFunction(L, traceback); /* push traceback function */ Lua.LuaInsert(L, base_); /* put it under chunk and args */ //signal(SIGINT, laction); status = Lua.LuaPCall(L, narg, ((clear != 0) ? 0 : Lua.LUA_MULTRET), base_); //signal(SIGINT, SIG_DFL); Lua.LuaRemove(L, base_); /* remove traceback function */ /* force a complete garbage collection in case of errors */ if (status != 0) { Lua.LuaGC(L, Lua.LUA_GCCOLLECT, 0); } return(status); }
static int handle_script(LuaState L, string[] argv, int n) { int status; CharPtr fname; int narg = getargs(L, argv, n); /* collect arguments */ Lua.LuaSetGlobal(L, "arg"); fname = argv[n]; if (Lua.strcmp(fname, "-") == 0 && Lua.strcmp(argv[n - 1], "--") != 0) { fname = null; /* stdin */ } status = Lua.LuaLLoadFile(L, fname); Lua.LuaInsert(L, -(narg + 1)); if (status == 0) { status = docall(L, narg, 0); } else { Lua.LuaPop(L, narg); } return(report(L, status)); }