static int io_flush (lua_State *L) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *f; lua_pop(L, 1); /* remove upvalue */ f = gethandle(L, ctrl, 1); luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle"); return pushresult(L, fflush(f) == 0); }
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) { if (lua_isnull(L, narg)) { if (len) *len = (def ? strlen(def) : 0); return def; } else return luaL_check_lstr(L, narg, len); }
static int luaB_globals (lua_State *L) { lua_getglobals(L); /* value to be returned */ if (!lua_isnull(L, 1)) { luaL_checktype(L, 1, LUA_TTABLE); lua_pushvalue(L, 1); /* new table of globals */ lua_setglobals(L); } return 1; }
static int luaB_sort (lua_State *L) { int n; luaL_checktype(L, 1, LUA_TTABLE); n = lua_getn(L, 1); if (!lua_isnull(L, 2)) /* is there a 2nd argument? */ luaL_checktype(L, 2, LUA_TFUNCTION); lua_settop(L, 2); /* make sure there is two arguments */ auxsort(L, 1, n); return 0; }
static int io_fromto (lua_State *L, int inout, const char *mode) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *current; lua_pop(L, 1); /* remove upvalue */ if (lua_isnull(L, 1)) { closefile(L, ctrl, getfilebyref(L, ctrl, inout)); current = (inout == 0) ? stdin : stdout; } else if (lua_tag(L, 1) == ctrl->iotag) /* deprecated option */ current = (FILE *)lua_touserdata(L, 1); else { const char *s = luaL_check_string(L, 1); current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode); } return setreturn(L, ctrl, current, inout); }
static int luaB_call (lua_State *L) { int oldtop; const char *options = luaL_opt_string(L, 3, ""); int err = 0; /* index of old error method */ int i, status; int n; luaL_checktype(L, 2, LUA_TTABLE); n = lua_getn(L, 2); if (!lua_isnull(L, 4)) /* set new error method */ { lua_getglobal(L, LUA_ERRORMESSAGE); err = lua_gettop(L); /* get index */ lua_pushvalue(L, 4); lua_setglobal(L, LUA_ERRORMESSAGE); } oldtop = lua_gettop(L); /* top before function-call preparation */ /* push function */ lua_pushvalue(L, 1); luaL_checkstack(L, n, "too many arguments"); for (i=0; i<n; i++) /* push arg[1...n] */ lua_rawgeti(L, 2, i+1); status = lua_call(L, n, LUA_MULTRET); if (err != 0) /* restore old error method */ { lua_pushvalue(L, err); lua_setglobal(L, LUA_ERRORMESSAGE); } if (status != 0) /* error in call? */ { if (strchr(options, 'x')) lua_pushnil(L); /* return nil to signal the error */ else lua_error(L, NULL); /* propagate error without additional messages */ return 1; } if (strchr(options, 'p')) /* pack results? */ lua_error(L, "deprecated option `p' in `call'"); return lua_gettop(L) - oldtop; /* results are already on the stack */ }
LUALIB_API long luaL_opt_number (lua_State *L, int narg, long def) { if (lua_isnull(L, narg)) return def; else return luaL_check_number(L, narg); }
static void checkseed (lua_State *L) { if (lua_isnull(L, 3)) { /* no seed? */ time_t tm = time(NULL); /* for `random' seed */ lua_pushlstring(L, (char *)&tm, sizeof(tm)); } }