/* ** Convert a string mask (for 'sethook') into a bit mask */ static int makemask (const char *smask, int count) { int mask = 0; if (c_strchr(smask, 'c')) mask |= LUA_MASKCALL; if (c_strchr(smask, 'r')) mask |= LUA_MASKRET; if (c_strchr(smask, 'l')) mask |= LUA_MASKLINE; if (count > 0) mask |= LUA_MASKCOUNT; return mask; }
sint32_t vfs_chdir( const char *path ) { vfs_fs_fns *fs_fns; const char *normpath = normalize_path( path ); const char *level; char *outname; int ok = VFS_RES_ERR; #if LDRV_TRAVERSAL // track dir level if (normpath[0] == '/') { dir_level = 0; level = &(normpath[1]); } else { level = normpath; } while (c_strlen( level ) > 0) { dir_level++; if (level = c_strchr( level, '/' )) { level++; } else { break; } } #endif #ifdef BUILD_SPIFFS if (fs_fns = myspiffs_realm( normpath, &outname, TRUE )) { // our SPIFFS integration doesn't support directories if (c_strlen( outname ) == 0) { ok = VFS_RES_OK; } } #endif #ifdef BUILD_FATFS if (fs_fns = myfatfs_realm( normpath, &outname, TRUE )) { if (c_strchr( outname, ':' )) { // need to set FatFS' default drive fs_fns->chdrive( outname ); // and force chdir to root in case path points only to root fs_fns->chdir( "/" ); } if (fs_fns->chdir( outname ) == VFS_RES_OK) { ok = VFS_RES_OK; } c_free( outname ); } #endif return ok == VFS_RES_OK ? VFS_RES_OK : VFS_RES_ERR; }
LUALIB_API const char *luaL_findtable (lua_State *L, int idx, const char *fname, int szhint) { const char *e; lua_pushvalue(L, idx); do { e = c_strchr(fname, '.'); if (e == NULL) e = fname + c_strlen(fname); lua_pushlstring(L, fname, e - fname); lua_rawget(L, -2); if (lua_isnil(L, -1)) { /* If looking for a global variable, check the rotables too */ void *ptable = luaR_findglobal(fname, e - fname); if (ptable) { lua_pop(L, 1); lua_pushrotable(L, ptable); } } if (lua_isnil(L, -1)) { /* no such field? */ lua_pop(L, 1); /* remove this nil */ lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ lua_pushlstring(L, fname, e - fname); lua_pushvalue(L, -2); lua_settable(L, -4); /* set new table into field */ } else if (!lua_istable(L, -1) && !lua_isrotable(L, -1)) { /* field has a non-table value? */ lua_pop(L, 2); /* remove table and value */ return fname; /* return problematic part of the name */ } lua_remove(L, -2); /* remove previous table */ fname = e + 1; } while (*e == '.'); return NULL; }
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { int n = 1; pushstr(L, ""); for (;;) { const char *e = c_strchr(fmt, '%'); if (e == NULL) break; setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); incr_top(L); switch (*(e+1)) { case 's': { const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; pushstr(L, s); break; } case 'c': { char buff[2]; buff[0] = cast(char, va_arg(argp, int)); buff[1] = '\0'; pushstr(L, buff); break; } case 'd': { setnvalue(L->top, cast_num(va_arg(argp, int))); incr_top(L); break; } case 'f': { setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); incr_top(L); break; } case 'p': { char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ c_sprintf(buff, "%p", va_arg(argp, void *)); pushstr(L, buff); break; } case '%': { pushstr(L, "%"); break; } default: { char buff[3]; buff[0] = '%'; buff[1] = *(e+1); buff[2] = '\0'; pushstr(L, buff); break; } } n += 2; fmt = e+2; } pushstr(L, fmt); luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); L->top -= n; return svalue(L->top - 1); }
static int traversetable (global_State *g, Table *h) { int i; int weakkey = 0; int weakvalue = 0; const TValue *mode; if (h->metatable && !luaR_isrotable(h->metatable)) markobject(g, h->metatable); mode = gfasttm(g, h->metatable, TM_MODE); if (mode && ttisstring(mode)) { /* is there a weak mode? */ weakkey = (c_strchr(svalue(mode), 'k') != NULL); weakvalue = (c_strchr(svalue(mode), 'v') != NULL); if (weakkey || weakvalue) { /* is really weak? */ h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ h->marked |= cast_byte((weakkey << KEYWEAKBIT) | (weakvalue << VALUEWEAKBIT)); h->gclist = g->weak; /* must be cleared after GC, ... */ g->weak = obj2gco(h); /* ... so put in the appropriate list */ } } if (weakkey && weakvalue) return 1; if (!weakvalue) { i = h->sizearray; while (i--) markvalue(g, &h->array[i]); } i = sizenode(h); while (i--) { Node *n = gnode(h, i); lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n))); if (ttisnil(gval(n))) removeentry(n); /* remove empty entries */ else { lua_assert(!ttisnil(gkey(n))); if (!weakkey) markvalue(g, gkey(n)); if (!weakvalue) markvalue(g, gval(n)); } } return weakkey || weakvalue; }
static const char *checkoption (lua_State *L, const char *conv, char *buff) { static const char *const options[] = LUA_STRFTIMEOPTIONS; unsigned int i; for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) { if (*conv != '\0' && c_strchr(options[i], *conv) != NULL) { buff[1] = *conv; if (*options[i + 1] == '\0') { /* one-char conversion specifier? */ buff[2] = '\0'; /* end buffer */ return conv + 1; } else if (*(conv + 1) != '\0' && c_strchr(options[i + 1], *(conv + 1)) != NULL) { buff[2] = *(conv + 1); /* valid two-char conversion specifier */ buff[3] = '\0'; /* end buffer */ return conv + 2; } } } luaL_argerror(L, 1, lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv)); return conv; /* to avoid warnings */ }
LUA_API int ICACHE_FLASH_ATTR lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { int status; Closure *f = NULL; CallInfo *ci = NULL; void *plight = NULL; lua_lock(L); if (*what == '>') { StkId func = L->top - 1; luai_apicheck(L, ttisfunction(func) || ttislightfunction(func)); what++; /* skip the '>' */ if (ttisfunction(func)) f = clvalue(func); else plight = fvalue(func); L->top--; /* pop function */ } else if (ar->i_ci != 0) { /* no tail call? */ ci = L->base_ci + ar->i_ci; lua_assert(ttisfunction(ci->func) || ttislightfunction(ci->func)); if (ttisfunction(ci->func)) f = clvalue(ci->func); else plight = fvalue(ci->func); } status = auxgetinfo(L, what, ar, f, plight, ci); if (c_strchr(what, 'f')) { if (f != NULL) setclvalue(L, L->top, f) else if (plight != NULL) setfvalue(L->top, plight) else setnilvalue(L->top); incr_top(L); } if (c_strchr(what, 'L')) collectvalidlines(L, f); lua_unlock(L); return status; }
/* ** Calls 'lua_getinfo' and collects all results in a new table. */ static int db_getinfo (lua_State *L) { lua_Debug ar; int arg; lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnStu"); if (lua_isfunction(L, arg + 1)) { /* info about a function? */ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ lua_xmove(L, L1, 1); } else { /* stack level */ if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { lua_pushnil(L); /* level out of range */ return 1; } } if (!lua_getinfo(L1, options, &ar)) return luaL_argerror(L, arg+2, "invalid option"); lua_newtable(L); /* table to collect results */ if (c_strchr(options, 'S')) { settabss(L, "source", ar.source); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); settabss(L, "what", ar.what); } if (c_strchr(options, 'l')) settabsi(L, "currentline", ar.currentline); if (c_strchr(options, 'u')) { settabsi(L, "nups", ar.nups); settabsi(L, "nparams", ar.nparams); settabsb(L, "isvararg", ar.isvararg); } if (c_strchr(options, 'n')) { settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); } if (c_strchr(options, 't')) settabsb(L, "istailcall", ar.istailcall); if (c_strchr(options, 'L')) treatstackoption(L, L1, "activelines"); if (c_strchr(options, 'f')) treatstackoption(L, L1, "func"); return 1; /* return table */ }
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { const char *p = strfrmt; while (*p != '\0' && c_strchr(FLAGS, *p) != NULL) p++; /* skip flags */ if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) luaL_error(L, "invalid format (repeated flags)"); if (isdigit(uchar(*p))) p++; /* skip width */ if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ if (*p == '.') { p++; if (isdigit(uchar(*p))) p++; /* skip precision */ if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ } if (isdigit(uchar(*p))) luaL_error(L, "invalid format (width or precision too long)"); *(form++) = '%'; c_strncpy(form, strfrmt, p - strfrmt + 1); form += p - strfrmt + 1; *form = '\0'; return p; }
void scanName(xsMachine* the, const char *name, int scanDefault) { char *colon = c_strchr(name, ':'); if (colon) { *colon = 0; scanNamespace(the, name); xsVar(NAME) = xsString(colon + 1); xsVar(PREFIX) = xsString((xsStringValue)name); *colon = ':'; } else if (scanDefault) { scanNamespace(the, "xmlns"); xsVar(NAME) = xsString((xsStringValue)name); xsVar(PREFIX) = xsNull; } else { xsVar(NAME) = xsString((xsStringValue)name); xsVar(PREFIX) = xsNull; xsVar(NAMESPACE) = xsNull; } }
static int str_format (lua_State *L) { int top = lua_gettop(L); int arg = 1; size_t sfl; const char *strfrmt = luaL_checklstring(L, arg, &sfl); const char *strfrmt_end = strfrmt+sfl; luaL_Buffer b; luaL_buffinit(L, &b); while (strfrmt < strfrmt_end) { if (*strfrmt != L_ESC) luaL_addchar(&b, *strfrmt++); else if (*++strfrmt == L_ESC) luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format (`%...') */ char buff[MAX_ITEM]; /* to store the formatted item */ if (++arg > top) luaL_argerror(L, arg, "no value"); strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { c_sprintf(buff, form, (int)luaL_checknumber(L, arg)); break; } case 'd': case 'i': { addintlen(form); c_sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); break; } case 'o': case 'u': case 'x': case 'X': { addintlen(form); c_sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); break; } #if !defined LUA_NUMBER_INTEGRAL case 'e': case 'E': case 'f': case 'g': case 'G': { c_sprintf(buff, form, (double)luaL_checknumber(L, arg)); break; } #endif case 'q': { addquoted(L, &b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { size_t l; const char *s = luaL_checklstring(L, arg, &l); if (!c_strchr(form, '.') && l >= 100) { /* no precision and string is too long to be formatted; keep original string */ lua_pushvalue(L, arg); luaL_addvalue(&b); continue; /* skip the `addsize' at the end */ } else { c_sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " LUA_QL("format"), *(strfrmt - 1)); } } luaL_addlstring(&b, buff, c_strlen(buff)); } } luaL_pushresult(&b); return 1; }
static int str_format (lua_State *L) { int top = lua_gettop(L); int arg = 1; size_t sfl; const char *strfrmt = luaL_checklstring(L, arg, &sfl); const char *strfrmt_end = strfrmt+sfl; luaL_Buffer b; luaL_buffinit(L, &b); while (strfrmt < strfrmt_end) { if (*strfrmt != L_ESC) luaL_addchar(&b, *strfrmt++); else if (*++strfrmt == L_ESC) luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format ('%...') */ char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) luaL_argerror(L, arg, "no value"); strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { nb = c_sprintf(buff, form, (int)luaL_checkinteger(L, arg)); break; } case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { lua_Integer n = luaL_checkinteger(L, arg); addlenmod(form, LUA_INTEGER_FRMLEN); nb = c_sprintf(buff, form, n); break; } #if defined(LUA_USE_AFORMAT) case 'a': case 'A': #endif case 'e': case 'E': case 'f': case 'g': case 'G': { addlenmod(form, LUA_NUMBER_FRMLEN); nb = c_sprintf(buff, form, luaL_checknumber(L, arg)); break; } case 'q': { addquoted(L, &b, arg); break; } case 's': { size_t l; const char *s = luaL_tolstring(L, arg, &l); if (!c_strchr(form, '.') && l >= 100) { /* no precision and string is too long to be formatted; keep original string */ luaL_addvalue(&b); break; } else { nb = c_sprintf(buff, form, s); lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ break; } } default: { /* also treat cases 'pnLlh' */ return luaL_error(L, "invalid option '%%%c' to 'format'", *(strfrmt - 1)); } } luaL_addsize(&b, nb); } } luaL_pushresult(&b); return 1; }