void luaT_setfallback (void) { static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL}; TObject oldfunc; lua_CFunction replace; char *name = luaL_check_string(1); lua_Object func = lua_getparam(2); luaL_arg_check(lua_isfunction(func), 2, "function expected"); switch (luaL_findstring(name, oldnames)) { case 0: /* old error fallback */ oldfunc = L->errorim; L->errorim = *luaA_Address(func); replace = errorFB; break; case 1: /* old getglobal fallback */ oldfunc = *luaT_getim(LUA_T_NIL, IM_GETGLOBAL); *luaT_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaA_Address(func); replace = nilFB; break; case 2: { /* old arith fallback */ int i; oldfunc = *luaT_getim(LUA_T_NUMBER, IM_POW); for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ fillvalids(i, luaA_Address(func)); replace = typeFB; break; } case 3: { /* old order fallback */ int i; oldfunc = *luaT_getim(LUA_T_NIL, IM_LT); for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */ fillvalids(i, luaA_Address(func)); replace = typeFB; break; } default: { int e; if ((e = luaL_findstring(name, luaT_eventname)) >= 0) { oldfunc = *luaT_getim(LUA_T_NIL, e); fillvalids(e, luaA_Address(func)); replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB; } else { luaL_verror("`%.50s' is not a valid fallback name", name); replace = NULL; /* to avoid warnings */ } } } if (oldfunc.ttype != LUA_T_NIL) luaA_pushobject(&oldfunc); else lua_pushcfunction(replace); }
static int luaI_checkevent (char *name, char *list[]) { int e = luaL_findstring(name, list); if (e < 0) luaL_verror("`%.50s' is not a valid event name", name); return e; }
static int doselection(lua_State *L, int i, const char *const S[], Selector F, const void *data) { if (lua_isnone(L, i)) { lua_newtable(L); for (i=0; S[i]!=NULL; i++) { lua_pushstring(L, S[i]); F(L, i, data); lua_settable(L, -3); } return 1; } else { #if LUA_VERSION_NUM < 501 int j=luaL_findstring(luaL_checkstring(L, i), S); #else int j=luaL_checkoption(L, i, NULL, S); #endif if (j==-1) luaL_argerror(L, i, "unknown selector"); return F(L, j, data); } }
static void inclinenumber (LexState *LS) { static char *pragmas [] = {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL}; next(LS); /* skip '\n' */ ++LS->linenumber; if (LS->current == '$') { /* is a pragma? */ char buff[PRAGMASIZE+1]; int ifnot = 0; int skip = LS->ifstate[LS->iflevel].skip; next(LS); /* skip $ */ readname(LS, buff); switch (luaL_findstring(buff, pragmas)) { case 0: /* debug */ if (!skip) L->debug = 1; break; case 1: /* nodebug */ if (!skip) L->debug = 0; break; case 2: /* endinput */ if (!skip) { LS->current = EOZ; LS->iflevel = 0; /* to allow $endinput inside a $if */ } break; case 3: /* end */ if (LS->iflevel-- == 0) luaX_syntaxerror(LS, "unmatched $end", "$end"); break; case 4: /* ifnot */ ifnot = 1; /* go through */ case 5: /* if */ if (LS->iflevel == MAX_IFS-1) luaX_syntaxerror(LS, "too many nested $ifs", "$if"); readname(LS, buff); LS->iflevel++; LS->ifstate[LS->iflevel].elsepart = 0; LS->ifstate[LS->iflevel].condition = checkcond(LS, buff) ? !ifnot : ifnot; LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition; break; case 6: /* else */ if (LS->ifstate[LS->iflevel].elsepart) luaX_syntaxerror(LS, "unmatched $else", "$else"); LS->ifstate[LS->iflevel].elsepart = 1; LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip || LS->ifstate[LS->iflevel].condition; break; default: luaX_syntaxerror(LS, "unknown pragma", buff); } skipspace(LS); if (LS->current == '\n') /* pragma must end with a '\n' ... */ inclinenumber(LS); else if (LS->current != EOZ) /* or eof */ luaX_syntaxerror(LS, "invalid pragma format", buff); ifskip(LS); } }
static int setloc (lua_State *L) { static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME}; static const char *const catnames[] = {"all", "collate", "ctype", "monetary", "numeric", "time", NULL}; int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames); luaL_arg_check(L, op != -1, 2, "invalid option"); lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1))); return 1; }
static int luaI_checkevent (lua_State *L, const char *name, int t) { int e = luaL_findstring(name, luaT_eventname); if (e >= IM_N) luaL_verror(L, "event `%.50s' is deprecated", name); if (e == IM_GC && t == TAG_TABLE) luaL_verror(L, "event `gc' for tables is deprecated"); if (e < 0) luaL_verror(L, "`%.50s' is not a valid event name", name); return e; }
static int checkcond (LexState *LS, char *buff) { static char *opts[] = {"nil", "1", NULL}; int i = luaL_findstring(buff, opts); if (i >= 0) return i; else if (isalpha((unsigned char)buff[0]) || buff[0] == '_') return luaS_globaldefined(buff); else { luaX_syntaxerror(LS, "invalid $if condition", buff); return 0; /* to avoid warnings */ } }
static int io_setloc (lua_State *L) { static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME}; static const char *const catnames[] = {"all", "collate", "ctype", "monetary", "numeric", "time", NULL}; const char *l = lua_tostring(L, 1); int op = luaL_findstring(luaL_optstring(L, 2, "all"), catnames); luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected"); luaL_argcheck(L, op != -1, 2, "invalid option"); lua_pushstring(L, setlocale(cat[op], l)); return 1; }
static int f_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; FILE *f = tofile(L, 1); int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames); long offset = luaL_optlong(L, 3, 0); luaL_argcheck(L, op != -1, 2, "invalid mode"); op = fseek(f, offset, mode[op]); if (op) return pushresult(L, 0, NULL); /* error */ else { lua_pushnumber(L, ftell(f)); return 1; } }
static void checkcallbacks (lua_State *L) { static const char *const validkeys[] = { "StartCdataSection", "EndCdataSection", "CharacterData", "Comment", "Default", "DefaultExpand", "StartElement", "EndElement", "ExternalEntityRef", "StartNamespaceDecl", "EndNamespaceDecl", "NotationDecl", "NotStandalone", "ProcessingInstruction", "UnparsedEntityDecl", NULL}; if (hasfield(L, "_nonstrict")) return; lua_pushnil(L); while (lua_next(L, 1)) { lua_pop(L, 1); /* remove value */ if (lua_type(L, -1) != LUA_TSTRING || luaL_findstring(lua_tostring(L, -1), validkeys) < 0) luaL_error(L, "invalid key `%s' in callback table", lua_tostring(L, -1)); } }
static int ff_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; ZZIP_FILE *f = tointernalfile(L, 1); long offset = luaL_optlong(L, 3, 0); #if LUA_VERSION_NUM < 501 int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames); luaL_argcheck(L, op != -1, 2, "invalid mode"); #else int op = luaL_checkoption(L, 2, "cur", modenames); #endif op = zzip_seek(f, offset, mode[op]); if (op < 0) return pushresult(L, 0, NULL); /* error */ else { lua_pushnumber(L, zzip_tell(f)); return 1; } }
static int io_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *f; int op; long offset; lua_pop(L, 1); /* remove upvalue */ f = getnonullfile(L, ctrl, 1); op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames); offset = luaL_opt_long(L, 3, 0); luaL_arg_check(L, op != -1, 2, "invalid mode"); op = fseek(f, offset, mode[op]); if (op) return pushresult(L, 0); /* error */ else { lua_pushnumber(L, ftell(f)); return 1; } }