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 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 int Main(string[] args) { // prepend the exe name to the arg list as it's done in C // so that we don't have to change any of the args indexing // code above List <string> newargs = new List <string>(args); newargs.Insert(0, Assembly.GetExecutingAssembly().Location); args = (string[])newargs.ToArray(); LuaState L; Smain s = new Smain(); int argc = args.Length; int i = doargs(argc, args); newargs.RemoveRange(0, i); argc -= i; args = (string[])newargs.ToArray(); if (argc <= 0) { usage("no input files given"); } L = Lua.LuaOpen(); if (L == null) { fatal("not enough memory for state"); } s.argc = argc; s.argv = args; if (Lua.LuaCPCall(L, pmain, s) != 0) { fatal(Lua.LuaToString(L, -1)); } Lua.LuaClose(L); return(Lua.EXIT_SUCCESS); }
static CharPtr get_prompt(LuaState L, int firstline) { CharPtr p; Lua.LuaGetField(L, Lua.LUA_GLOBALSINDEX, (firstline != 0) ? "_PROMPT" : "_PROMPT2"); p = Lua.LuaToString(L, -1); if (p == null) { p = ((firstline != 0) ? Lua.LUA_PROMPT : Lua.LUA_PROMPT2); } Lua.LuaPop(L, 1); /* remove global */ return(p); }
static int report(LuaState L, int status) { if ((status != 0) && !Lua.LuaIsNil(L, -1)) { CharPtr msg = Lua.LuaToString(L, -1); if (msg == null) { msg = "(error object is not a string)"; } l_message(progname, msg); Lua.LuaPop(L, 1); } return(status); }
static int pmain(LuaState L) { Smain s = (Smain)Lua.LuaToUserData(L, 1); int argc = s.argc; string[] argv = s.argv; Lua.Proto f; int i; if (Lua.LuaCheckStack(L, argc) == 0) { fatal("too many input files"); } for (i = 0; i < argc; i++) { CharPtr filename = (Lua.strcmp(argv[i], "-") == 0) ? null : argv[i]; if (Lua.LuaLLoadFile(L, filename) != 0) { fatal(Lua.LuaToString(L, -1)); } } f = combine(L, argc); if (listing != 0) { Lua.luaU_print(f, (listing > 1)?1:0); } if (dumping != 0) { Stream D = (output == null) ? Lua.stdout : Lua.fopen(output, "wb"); if (D == null) { cannot("open"); } Lua.LuaLock(L); Lua.LuaUDump(L, f, writer, D, stripping); Lua.LuaUnlock(L); if (Lua.ferror(D) != 0) { cannot("write"); } if (Lua.fclose(D) != 0) { cannot("close"); } } return(0); }