static void add_s (MatchState *ms, luaL_Buffer *b, const lua_WChar *s, const lua_WChar *e) { lua_State *L = ms->L; if (lua_iswstring(L, 3)) { const lua_WChar *news = lua_towstring(L, 3); size_t l = lua_strlen(L, 3); size_t i; for (i=0; i<l; i++) { if (news[i] != ESC) luaL_putwchar(b, news[i]); else { i++; /* skip ESC */ if (!iswdigit(news[i])) luaL_putwchar(b, news[i]); else { int level = check_capture(ms, news[i]); push_onecapture(ms, level); luaL_addvalue(b); /* add capture to accumulated result */ } } } } else { /* is a function */ int n; lua_pushvalue(L, 3); n = push_captures(ms, s, e); lua_call(L, n, 1); if (lua_iswstring(L, -1)) luaL_addvalue(b); /* add return to accumulated result */ else lua_pop(L, 1); /* function result is not a string: pop it */ } }
/**-------------------------------------------------------------------------- * Write a response to the T.Http.Message. * \param L The lua state. * \lparam Http.Message instance. * \lparam string. * \return int # of values pushed onto the stack. * --------------------------------------------------------------------------*/ static int lt_htp_str_write( lua_State *L ) { struct t_htp_str *s = t_htp_str_check_ud( L, 1, 1 ); size_t sz; char *b; size_t c = 0; luaL_Buffer lB; luaL_buffinit( L, &lB ); luaL_checklstring( L, 2, &sz ); // this assumes first ever call is write -> chunked if (T_HTP_STR_SEND != s->state) { luaL_buffinit( L, &lB ); c = t_htp_str_formHeader( L, &lB, s, 200, NULL, 0, 0 ); b = luaL_prepbuffer( &lB ); c = sprintf( b, "%zx\r\n", sz ); luaL_addsize( &lB, c ); lua_pushvalue( L, 2 ); luaL_addvalue( &lB ); luaL_addlstring( &lB, "\r\n", 2 ); luaL_pushresult( &lB ); s->state = T_HTP_STR_SEND; } else { // if the response Content-length is not known when we are sending // the encoding must be chunked if (! s->rsCl) { luaL_buffinit( L, &lB ); b = luaL_prepbuffer( &lB ); c = sprintf( b, "%zx\r\n", sz ); luaL_addsize( &lB, c ); lua_pushvalue( L, 2 ); luaL_addvalue( &lB ); luaL_addlstring( &lB, "\r\n", 2 ); luaL_pushresult( &lB ); } else lua_pushvalue( L, 2 ); } // TODO: t_htp_str_addbuffer( L, s, lB.n, 0 ); return 0; }
static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) { if (lua_isstring(L, 3)) { const char *news = lua_tostring(L, 3); size_t l = lua_strlen(L, 3); size_t i; for (i=0; i<l; i++) { if (news[i] != ESC) luaL_putchar(b, news[i]); else { i++; /* skip ESC */ if (!isdigit((unsigned char)news[i])) luaL_putchar(b, news[i]); else { int level = check_capture(L, news[i], cap); luaL_addlstring(b, cap->capture[level].init, cap->capture[level].len); } } } } else { /* is a function */ int n; lua_pushvalue(L, 3); n = push_captures(L, cap); lua_rawcall(L, n, 1); if (lua_isstring(L, -1)) luaL_addvalue(b); /* add return to accumulated result */ else lua_pop(L, 1); /* function result is not a string: pop it */ } }
static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { lua_State *L = ms->L; switch (lua_type(L, 3)) { case LUA_TNUMBER: case LUA_TSTRING: { add_s(ms, b, s, e); return; } case LUA_TFUNCTION: { int n; lua_pushvalue(L, 3); n = push_captures(ms, s, e); lua_call(L, n, 1); break; } case LUA_TTABLE: { push_onecapture(ms, 0, s, e); lua_gettable(L, 3); break; } default: { luaL_argerror(L, 3, "string/function/table expected"); return; } } if (!lua_toboolean(L, -1)) { /* nil or false? */ lua_pop(L, 1); lua_pushlstring(L, s, e - s); /* keep original text */ } else if (!lua_isstring(L, -1)) luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); luaL_addvalue(b); /* add result to accumulator */ }
/* Concatinate and remove elements from the table at idx starting at * element begin and going to length len. The final concatinated string * is on the top of the stack. */ static int lhp_table_concat_and_clear(lua_State *L, int idx, int begin, int len) { luaL_Buffer buff; int real_len = len-begin+1; /* Empty table? */ if ( !real_len ) { lua_pushliteral(L, ""); return 0; } /* One element? */ if ( 1 == real_len ) { lua_rawgeti(L, idx, begin); /* remove values from buffer. */ lua_pushnil(L); lua_rawseti(L, idx, begin); return 0; } /* do a table concat. */ luaL_buffinit(L, &buff); for(; begin <= len; begin++) { lua_rawgeti(L, idx, begin); luaL_addvalue(&buff); /* remove values from buffer. */ lua_pushnil(L); lua_rawseti(L, idx, begin); } luaL_pushresult(&buff); return 0; }
static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { size_t l, i; lua_State *L = ms->L; const char *news = lua_tolstring(L, 3, &l); for (i = 0; i < l; i++) { if (news[i] != L_ESC) luaL_addchar(b, news[i]); else { i++; /* skip ESC */ if (!isdigit(uchar(news[i]))) { if (news[i] != L_ESC) luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); luaL_addchar(b, news[i]); } else if (news[i] == '0') luaL_addlstring(b, s, e - s); else { push_onecapture(ms, news[i] - '1', s, e); luaL_tolstring(L, -1, NULL); /* if number, convert it to string */ lua_remove(L, -2); /* remove original value */ luaL_addvalue(b); /* add capture to accumulated result */ } } } }
static const wchar_t *searchpath (lua_State *L, const char *name, const wchar_t *path, const wchar_t *sep, const wchar_t *dirsep) { const wchar_t *wname; luaL_Buffer msg; /* to build error message */ luaL_buffinit(L, &msg); lua_pushstring(L, name); wname = utf8_to_utf16(L, -1, NULL); /* `name' is encoded in UTF-8 */ if (*sep != 0) /* non-empty separator? */ wname = LF_Gsub(L, wname, sep, dirsep); /* replace it by 'dirsep' */ while ((path = pushnexttemplate(L, path)) != NULL) { const wchar_t *filename = LF_Gsub(L, (const wchar_t*)lua_tostring(L, -1), LUA_PATH_MARK, wname); lua_remove(L, -2); /* remove path template */ if (readable(filename)) /* does file exist and is readable? */ return filename; /* return that file name */ push_utf8_string(L, filename, -1); lua_pushfstring(L, "\n\tno file " LUA_QS, lua_tostring(L, -1)); lua_remove(L, -2); /* remove UTF-8 file name */ lua_remove(L, -2); /* remove UTF-16 file name */ luaL_addvalue(&msg); /* concatenate error msg. entry */ } luaL_pushresult(&msg); /* create error message */ return NULL; /* not found */ }
static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { switch (lua_type(L, arg)) { case LUA_TSTRING: { size_t len; const char *s = lua_tolstring(L, arg, &len); addquoted(b, s, len); break; } case LUA_TNUMBER: { char *buff = luaL_prepbuffsize(b, MAX_ITEM); int nb; if (!lua_isinteger(L, arg)) /* float? */ nb = quotefloat(L, buff, lua_tonumber(L, arg)); else { /* integers */ lua_Integer n = lua_tointeger(L, arg); const char *format = (n == LUA_MININTEGER) /* corner case? */ ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hex */ : LUA_INTEGER_FMT; /* else use default format */ nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); } luaL_addsize(b, nb); break; } case LUA_TNIL: case LUA_TBOOLEAN: { luaL_tolstring(L, arg, NULL); luaL_addvalue(b); break; } default: { luaL_argerror(L, arg, "value has no literal form"); } } }
static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ luaL_buffinit(L, &msg); lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */ if (!lua_istable(L, 3)) luaL_error(L, LUA_QL("package.searchers") " must be a table"); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { lua_rawgeti(L, 3, i); /* get a searcher */ if (lua_isnil(L, -1)) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ luaL_pushresult(&msg); /* create error message */ luaL_error(L, "module " LUA_QS " not found:%s", name, lua_tostring(L, -1)); } lua_pushstring(L, name); lua_call(L, 1, 2); /* call it */ if (lua_isfunction(L, -2)) /* did it find a loader? */ return; /* module loader found */ else if (lua_isstring(L, -2)) { /* searcher returned error message? */ lua_pop(L, 1); /* remove extra return */ luaL_addvalue(&msg); /* concatenate error message */ } else lua_pop(L, 2); /* remove both returns */ } }
static void addfield (lua_State *L, luaL_Buffer *b, int i) { lua_rawgeti(L, 1, i); if (!lua_isstring(L, -1)) lxs_error(L, "invalid value (%s) at index %d in table for " LUA_QL("concat"), luaL_typename(L, -1), i); luaL_addvalue(b); }
static const char *searchpath (lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep) { luaL_Buffer msg; /* to build error message */ luaL_buffinit(L, &msg); if (*sep != '\0') /* non-empty separator? */ name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ while ((path = pushnexttemplate(L, path)) != NULL) { const char *filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); lua_remove(L, -2); /* remove path template */ // @Voidious: Use lua_State->cwd in place of current dir. const char *absFilename = luaL_gsub(L, filename, "~", lua_getcwd(L)); if (readable(absFilename)) { /* does file exist and is readable? */ lua_pop(L, 1); #if defined(_WIN32) const char *relativeFilename = luaL_gsub(L, filename, "~\\", ""); #else const char *relativeFilename = luaL_gsub(L, filename, "~/", ""); #endif lua_remove(L, -2); /* remove file name */ return relativeFilename; } lua_pop(L, 1); lua_pushfstring(L, "\n\tno file " LUA_QS, filename); lua_remove(L, -2); /* remove file name */ luaL_addvalue(&msg); /* concatenate error msg. entry */ } luaL_pushresult(&msg); /* create error message */ return NULL; /* not found */ }
/* Note this one does not dump values to protect from embedded zeroes. */ static int dump_lua_stack(lua_State * L, int base) { int top = lua_gettop(L); if (top == 0) { lua_pushliteral(L, "-- stack is empty --"); } else { int pos = 0; luaL_Buffer b; luaL_buffinit(L, &b); for (pos = top; pos > 0; --pos) { luaL_addstring(&b, (pos != base) ? "[" : "{"); lua_pushinteger(L, pos); luaL_addvalue(&b); luaL_addstring(&b, (pos != base) ? "] - " : "} -"); luaL_addstring(&b, luaL_typename(L, pos)); luaL_addstring(&b, "\n"); } luaL_pushresult(&b); } if (lua_gettop(L) != top + 1) { return luaL_error(L, "dumpstack not balanced %d %d", top, lua_gettop(L)); } return 1; }
/* EXT - new library function; some parts copied from 'str_gsub_aux' */ static int matchobj_expand(lua_State *L) { size_t lt, i; const char *t; luaL_Buffer b; luaL_checktype(L, 1, LUA_TTABLE); t = luaL_checklstring(L, 2, <); luaL_buffinit(L, &b); for (i = 0; i < lt; i++) { if (t[i] != L_ESC) luaL_addchar(&b, t[i]); else { i++; /* skip ESC */ if (!isdigit(uchar(t[i]))) { if (t[i] != L_ESC) luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); luaL_addchar(&b, t[i]); } else { int idx = t[i] - '0'; #if LUA_VERSION_NUM == 501 int len = lua_objlen(L, 1); #else int len = luaL_len(L, 1); #endif if (idx > len) luaL_error(L, "invalid capture index %%%d", idx); lua_rawgeti(L, 1, idx); lua_tostring(L, -1); /* if number, convert it to string */ luaL_addvalue(&b); /* add capture to accumulated result */ } } } luaL_pushresult(&b); return 1; }
static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, const char *e, int tr, int table) { lua_State *L = ms->L; switch (tr) { case LUA_TFUNCTION: { int n; lua_pushvalue(L, 3); if (table) n = build_result_table(ms, s, e); /* EXT */ else n = push_captures(ms, s, e); lua_call(L, n, 1); break; } case LUA_TTABLE: { push_onecapture(ms, 0, s, e); lua_gettable(L, 3); break; } default: { /* LUA_TNUMBER or LUA_TSTRING */ add_s(ms, b, s, e); return; } } if (!lua_toboolean(L, -1)) { /* nil or false? */ lua_pop(L, 1); lua_pushlstring(L, s, e - s); /* keep original text */ } else if (!lua_isstring(L, -1)) luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); luaL_addvalue(b); /* add result to accumulator */ }
static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { (*ta->geti)(L, 1, i); if (!lua_isstring(L, -1)) luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", luaL_typename(L, -1), i); luaL_addvalue(b); }
/** * @param: the table (at stack 1) * @param: the cycle table (at stack 2) * @return: the table string */ static int l_compact_repr_table(lua_State *L) { lua_pushvalue(L, 1); /* duplicate the table in stack 3 */ lua_rawget(L, 2); /* get from the cycle table 2 */ if (lua_isnil(L, -1)) { /* leave the nil at stack 3 */ lua_pushfstring(L, "te(%d)", ((size_t)lua_topointer(L, 1))&(8192-1)); /* set the value in stack 4 */ lua_pushvalue(L, 1); /* push in stack 5 */ lua_insert(L, -2); /* swap 4/5 to set the key */ lua_rawset(L, 2); /* adjust the cycle table and pop 4,5 */ lua_pushnil(L); /* make a room at stack 4 */ /* luaL_Buffer START */ luaL_Buffer b; luaL_buffinit(L, &b); luaL_addlstring(&b, "{ ", 2); /* iterate the table */ for (lua_pushnil(L); lua_next(L, 1) != 0; lua_pushvalue(L, 4)) { lua_replace(L, 3); /* backups the value in stack 3 */ lua_replace(L, 4); /* backups the key in stack 4 */ /* repr the key */ lua_pushcfunction(L, l_repr_key); lua_pushvalue(L, 4); lua_pushvalue(L, 2); lua_call(L, 2, 1); luaL_addvalue(&b); /* add the return value into buffer */ luaL_addlstring(&b, " = ", 3); /* repr the value */ lua_pushcfunction(L, l_compact_repr_slice); lua_pushvalue(L, 3); lua_pushvalue(L, 2); lua_call(L, 2, 1); luaL_addvalue(&b); /* add the return value into buffer */ luaL_addlstring(&b, ", ", 2); } luaL_addchar(&b, '}'); luaL_pushresult(&b); /* luaL_Buffer END */ } else { /* just use the return value of gettable as the result */ } return 1; }
LB_API const char *lbind_dumpstack(lua_State *L, const char *msg) { int i, top = lua_gettop(L); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addstring(&b, "dump stack: "); luaL_addstring(&b, msg != NULL ? msg : ""); luaL_addstring(&b, "\n---------------------------\n"); for (i = 1; i <= top; ++i) { lua_pushfstring(L, "%d: ", i); luaL_addvalue(&b); lbind_tolstring(L, i, NULL); luaL_addvalue(&b); luaL_addstring(&b, "\n"); } luaL_addstring(&b, "---------------------------\n"); luaL_pushresult(&b); return lua_tostring(L, -1); }
static void inject_string(script_info *info, char const *line) { lua_State *L = info->state; int base, top; char *ret_line; gboolean force_ret = FALSE; if(line[0] == '=') { line++; force_ret = TRUE; } ret_line = g_strconcat("return ", line, NULL); lua_rawgeti(L, LUA_REGISTRYINDEX, info->traceback); base = lua_gettop(L); if(luaL_loadbuffer(L, ret_line, strlen(ret_line), "@interpreter")) { if(!force_ret) lua_pop(L, 1); if(force_ret || luaL_loadbuffer(L, line, strlen(line), "@interpreter")) { hexchat_printf(ph, "Lua syntax error: %s", luaL_optstring(L, -1, "")); lua_pop(L, 2); g_free(ret_line); return; } } g_free(ret_line); info->status |= STATUS_ACTIVE; if(lua_pcall(L, 0, LUA_MULTRET, base)) { char const *error = lua_tostring(L, -1); lua_pop(L, 2); hexchat_printf(ph, "Lua error: %s", error ? error : "(non-string error)"); return; } top = lua_gettop(L); if(top > base) { int i; luaL_Buffer b; luaL_buffinit(L, &b); for(i = base + 1; i <= top; i++) { if(i != base + 1) luaL_addstring(&b, " "); tostring(L, i); luaL_addvalue(&b); } luaL_pushresult(&b); hexchat_print(ph, lua_tostring(L, -1)); lua_pop(L, top - base + 1); } lua_pop(L, 1); check_deferred(info); }
static int concat_table(lua_State *L, int pos, luaL_Buffer* b) { int i, last; last = lua_objlen(L, pos); luaL_buffinit(L, b); for (i = 1; i <= last; i++) { lua_rawgeti(L, pos, i); luaL_addvalue(b); } luaL_pushresult(b); return 1; }
static int _try_load(lua_State *L, const char * path, int pathlen, const char * name) { int namelen = strlen(name); char tmp[pathlen + namelen]; int i; for (i=0;i<pathlen;i++) { if (path[i] == '?') break; tmp[i] = path[i]; } if (path[i] == '?') { memcpy(tmp+i,name,namelen); memcpy(tmp+i+namelen,path+i+1,pathlen - i -1); } else { fprintf(stderr,"snlua : Invalid lua service path\n"); exit(1); } tmp[namelen+pathlen-1] = '\0'; // luacode_loadfile is the same with luaL_loadfile except cache. #ifdef NOCODECACHE int r = luaL_loadfile(L,tmp); #else int r = luacode_loadfile(L,tmp); #endif if (r == LUA_OK) { int i; for (i=namelen+pathlen-2;i>=0;i--) { if (tmp[i] == '/') { lua_pushlstring(L,tmp,i+1); lua_setglobal(L,"SERVICE_PATH"); break; } } if (i<0) { return 0; } lua_getglobal(L,"package"); lua_getfield(L,-1,"path"); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addlstring(&b, tmp, i+1); luaL_addstring(&b, "?.lua;"); luaL_addvalue(&b); luaL_pushresult(&b); lua_setfield(L,-2,"path"); lua_pop(L,1); return 0; } else if (r == LUA_ERRFILE) { lua_pop(L,1); return -1; } return 1; }
static void _add_path(lua_State* L, const char* path, const char* pathfield) { lua_getglobal(L, "package"); lua_getfield(L, -1, pathfield); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addlstring(&b, path, strlen(path)); luaL_addchar(&b, ';'); luaL_addvalue(&b); luaL_pushresult(&b); lua_setfield(L, -2, pathfield); lua_pop(L, 1); }
static void read_object(lua_State *L, luaL_Buffer* b, char** frame, int* index, int call, size_t* len) { // printf("\r\n\tread[%d/call=%d/index=%d/len=%d/%p]", (*index + call - ((int) (*len))), call,*index, *len, *frame); if ((*index + call - ((int) (*len))) > 0) { if (lua_istable(L, 1)) { int last = lua_tointeger(L, 3); // TableMax 3 int indice = lua_tointeger(L, 4); // TableCurrentIndice 4 size_t current = 0; luaL_buffinit(L, b); if (*index < *len){ lua_pushvalue(L, 5); luaL_addvalue(b); current = *len-*index; // printf("\r\n\taddcur[indice=%d,index=%d,current=%d]", indice, *index, current); } else { *index = 0; } while (current < call) { if (indice >= last) { luaL_error(L, "cannot deserialize: end of table"); } indice++; lua_rawgeti(L, 1, indice); current = current + lua_objlen(L, -1); luaL_addvalue(b); // printf("\r\n\tadd[indice=%d,index=%d,current=%d]", indice, *index, current); } luaL_pushresult(b); // StringBuffer 5 lua_replace(L, 5); lua_pushinteger(L, indice); // TableCurrentIndice 4 lua_replace(L, 4); *frame = (char*) luaL_checklstring(L, 5, len); // printf("\r\n\t->[%p]", *frame); } else { luaL_error(L, "cannot deserialize: end of string"); } } }
/* ** Copy at most n bytes to buffer b and remove them from the ** output stream buffer. */ static int lzstream_flush_buffer(lua_State *L, lz_stream *s, size_t n, luaL_Buffer *b) { /* check output */ if (n > s->o_buffer_len) { n = s->o_buffer_len; } if (n > 0) { lua_pushlstring(L, s->o_buffer, n); luaL_addvalue(b); lzstream_remove(s, n); } return n; }
static int api_hexchat_print(lua_State *L) { int i, args = lua_gettop(L); luaL_Buffer b; luaL_buffinit(L, &b); for(i = 1; i <= args; i++) { if(i != 1) luaL_addstring(&b, " "); tostring(L, i); luaL_addvalue(&b); } luaL_pushresult(&b); hexchat_print(ph, lua_tostring(L, -1)); return 0; }
/* ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... */ static int utfchar (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ if (n == 1) /* optimize common case of single char */ pushutfchar(L, 1); else { int i; luaL_Buffer b; luaL_buffinit(L, &b); for (i = 1; i <= n; i++) { pushutfchar(L, i); luaL_addvalue(&b); } luaL_pushresult(&b); } return 1; }
static int ags_print(lua_State *L) { luaL_Buffer b; int NUM_PARAMS = lua_gettop(L); luaL_buffinit(L, &b); lua_getglobal(L, "tostring"); for (int i = 1; i <= NUM_PARAMS; i++) { lua_pushvalue(L, -1); lua_pushvalue(L, i); lua_call(L, 1, 1); if (i != 1) { luaL_addstring(&b, ", "); } luaL_addvalue(&b); } luaL_pushresult(&b); AGS_Display(lua_tostring(L,-1)); return 0; }
static const char *searchpath(lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep, bool is_local) { luaL_Buffer msg; /* to build error message */ luaL_buffinit(L, &msg); if (*sep != '\0') /* non-empty separator? */ name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ while ((path = pushnexttemplate(L, path)) != NULL) { const char *filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); lua_remove(L, -2); /* remove path template */ if (readable(filename, is_local)) /* does file exist and is readable? */ return filename; /* return that file name */ lua_pushfstring(L, "\n\tno file " LUA_QS, filename); lua_remove(L, -2); /* remove file name */ luaL_addvalue(&msg); /* concatenate error msg. entry */ } luaL_pushresult(&msg); /* create error message */ return NULL; /* not found */ }
static int tconcat (lua_State *L) { luaL_Buffer b; size_t lsep; int i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 3, 1); last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1)); luaL_buffinit(L, &b); for (; i <= last; i++) { lua_rawgeti(L, 1, i); luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings"); luaL_addvalue(&b); if (i != last) luaL_addlstring(&b, sep, lsep); } luaL_pushresult(&b); return 1; }
static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { size_t l, i; const char *news = lua_tolstring(ms->L, 3, &l); for (i = 0; i < l; i++) { if (news[i] != L_ESC) luaL_addchar(b, news[i]); else { i++; /* skip ESC */ if (!isdigit(uchar(news[i]))) luaL_addchar(b, news[i]); else if (news[i] == '0') luaL_addlstring(b, s, e - s); else { push_onecapture(ms, news[i] - '1', s, e); luaL_addvalue(b); /* add capture to accumulated result */ } } } }
/** Implement the Lua function print(...). * * Construct a message from all the arguments to print(), passing * each through the global function tostring() make certain they * are strings, and separating them with tab characters. The message * is ultimately passed to the Windows service OutputDebugString() * for display in a debugger or debug message logger. * * \param L Lua state context for the function. * \returns The number of values on the Lua stack to be returned * to the Lua caller. */ static int dbgPrint(lua_State *L) { luaL_Buffer b; int n = lua_gettop(L); /* number of arguments */ int i; lua_getglobal(L, "tostring"); luaL_buffinit(L, &b); for (i=1; i<=n; i++) { lua_pushvalue(L, n+1); /* b tostring */ lua_pushvalue(L, i); /* b tostring argi */ lua_call(L, 1, 1); /* b tostring(argi) */ luaL_addvalue(&b); /* b */ if (i<n) luaL_addchar(&b, '\t'); } luaL_pushresult(&b); OutputDebugStringA(lua_tostring(L, -1)); //fputs(s, stdout); lua_pop(L,1); return 0; }