static void setprogdir(lua_State *L) { char buff[MAX_PATH + 1]; char *lb; DWORD nsize = sizeof(buff); DWORD n = GetModuleFileNameA(NULL, buff, nsize); if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) { luaL_error(L, "unable to get ModuleFileName"); } else { *lb = '\0'; luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); lua_remove(L, -2); /* remove original string */ } }
JNIEXPORT jbyteArray JNICALL Java_m_lua_Lua_Lgsub (JNIEnv* env, jobject thiz, jlong nativeObj, jstring s, jstring p, jstring r) { pushJNIEnv(env, nativeObj); const char* cs = (*env)->GetStringUTFChars(env, s, JNI_FALSE); const char* cp = (*env)->GetStringUTFChars(env, p, JNI_FALSE); const char* cr = (*env)->GetStringUTFChars(env, r, JNI_FALSE); const char* ret = luaL_gsub((lua_State*) nativeObj, cs, cp, cr); jsize len = (jsize) lua_strlen((lua_State*) nativeObj, -1); jbyteArray arr = (*env)->NewByteArray(env, len); (*env)->SetByteArrayRegion(env, arr, 0, len, ret); (*env)->ReleaseStringUTFChars(env, r, cr); (*env)->ReleaseStringUTFChars(env, p, cp); (*env)->ReleaseStringUTFChars(env, s, cs); return arr; }
int main(int argc, const char * argv[]) { if (argc < 2) { printf("no input path\n"); exit(1); } const char* apppath = argv[0]; char* path = strrchr(argv[0], '/'); char buff[1024] = {0}; strncat(buff, apppath, path ? (path - apppath + 1) : 0); strcat(buff, "converter.lua"); lua_State* L = lua_open(); luaL_openlibs(L); luaopen_cjson(L); luaopen_spinebinary(L); luaL_dofile(L, buff); FILE* file = fopen(argv[1], "r"); if (!file) { printf("can not open file: %s\n", argv[1]); exit(1); } fseek(file, 0, SEEK_END); size_t len = ftell(file); char* data = (char*)malloc(len); fseek(file, 0, SEEK_SET); fread(data, 1, len, file); fclose(file); lua_pushcfunction(L, _traceback); lua_getglobal(L, "convert"); luaL_checktype(L, -1, LUA_TFUNCTION); lua_pushlstring(L, data, len); luaL_gsub(L, argv[1], ".json", ".skel"); lua_pcall(L, 2, 0, -4); lua_close(L); free(data); return 0; }
static int loadfunc (lua_State *L, const char *filename, const char *modname) { const char *funcname; const char *mark; modname = luaL_gsub(L, modname, ".", LUA_OFSEP); mark = strchr(modname, *LUA_IGMARK); if (mark) { int stat; funcname = lua_pushlstring(L, modname, mark - modname); funcname = lua_pushfstring(L, LUA_POF"%s", funcname); stat = ll_loadfunc(L, filename, funcname); if (stat != ERRFUNC) return stat; modname = mark + 1; /* else go ahead and try old-style name */ } funcname = lua_pushfstring(L, LUA_POF"%s", modname); return ll_loadfunc(L, filename, funcname); }
static int loadfunc(lua_State *L, const char *filename, const char *modname) { const char *openfunc; const char *mark; modname = luaL_gsub(L, modname, ".", "_"); mark = strchr(modname, *LUA_IGMARK); if (mark) { int stat; openfunc = lua_pushlstring(L, modname, mark - modname); openfunc = lua_pushfstring(L, "luaopen_%s", openfunc); stat = lookforfunc(L, filename, openfunc); if (stat != 2) return stat; modname = mark + 1; } openfunc = lua_pushfstring(L, "luaopen_%s", modname); return lookforfunc(L, filename, openfunc); }
static void setprogdir (lua_State *L) { char buff[MAX_PATH + 1]; char *lb; DWORD nsize = sizeof(buff)/sizeof(char); #if LUAPLUS_EXTENSIONS #ifndef NDEBUG const char* luaplusdllName = "lua52_debug.dll"; #else // _DEBUG const char* luaplusdllName = "lua52.dll"; #endif // _DEBUG DWORD n = GetModuleFileNameA(GetModuleHandle(luaplusdllName), buff, nsize); if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) luaL_error(L, "unable to get ModuleFileName"); else { const char* envOverride = getenv("LUA52_ROOT_PATH"); if (envOverride) { strcpy(buff, envOverride); if ((lb = strrchr(buff, '\\')) == NULL && (lb = strrchr(buff, '/')) == NULL) { luaL_error(L, "LUA52_ROOT_PATH is missing a closing backslash"); } if (lb[1] != 0) { lb = buff + strlen(buff); } } #else DWORD n = GetModuleFileNameA(NULL, buff, nsize); if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) luaL_error(L, "unable to get ModuleFileName"); else { #endif /* LUAPLUS_EXTENSIONS */ *lb = '\0'; luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); lua_remove(L, -2); /* remove original string */ } } static void pusherror (lua_State *L) { int error = GetLastError(); char buffer[128]; if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL)) lua_pushstring(L, buffer); else lua_pushfstring(L, "system error %d\n", error); }
static void setprogdir (lua_State *L) { char buff[MAX_PATH + 1]; char *lb; DWORD nsize = sizeof(buff)/sizeof(char); //DWORD n = GetModuleFileName(NULL, buff, nsize); //Robert Buehlmann because of unicode, load dll's from lua DWORD n = GetModuleFileNameA(NULL, buff, nsize); //printf("rlmurx: GetModuelFilename n=%d buff=%s\n", n, buff); //strcpy(buff,pvarg0); //strcpy(buff,"z:\\cc\\priv\\cvs\\pvb\\language_bindings\\lua\\pvslua\\pvslua"); if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) luaL_error(L, "unable to get ModuleFileName"); else { *lb = '\0'; luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); lua_remove(L, -2); /* remove original string */ } }
static void setprogdir (lua_State *L) { wchar_t buff[MAX_PATH + 1]; wchar_t *lb; DWORD nsize = sizeof(buff)/sizeof(wchar_t); DWORD n = GetModuleFileNameW(NULL, buff, nsize); if (n == 0 || n == nsize || (lb = wcsrchr(buff, L'\\')) == NULL) luaL_error(L, "unable to get ModuleFileName"); else { char *lbuff; size_t lbuffn; *lb = '\0'; lbuffn = WideCharToMultiByte(CP_UTF8, 0, buff, n, 0, 0, 0, 0); lbuff = (char*)malloc(lbuffn+1); WideCharToMultiByte(CP_UTF8, 0, buff, n, lbuff, (int)lbuffn+1, 0, 0); luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, lbuff); free(lbuff); lua_remove(L, -2); /* remove original string */ } }
static void setprogdir (lua_State *L) { char* buff; char *lb; unsigned int path_len = sizeof(buff)/sizeof(char); #ifdef _DEBUG const char* luaplusdllName = "lua52_debug.so"; #else // _DEBUG const char* luaplusdllName = "lua52.so"; #endif // _DEBUG Dl_info info; dladdr(setprogdir, &info); buff = malloc(MAXPATHLEN + 1); strcpy(buff, info.dli_fname); lb = strrchr(buff, '/'); *lb = '\0'; luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); lua_remove(L, -2); /* remove original string */ free(buff); }
/** * field -> "path" or "cpath" * sub_pat -> "?.lua" * rep_pat -> "./?.lua" * pool -> lifecycle pool for allocations * paths -> things to add * file -> ??? */ static void munge_path(lua_State *L, const char *field, const char *sub_pat, const char *rep_pat, apr_pool_t *pool, apr_array_header_t *paths, const char *file) { const char *current; const char *parent_dir; const char *pattern; const char *modified; char *part; lua_getglobal(L, "package"); lua_getfield(L, -1, field); current = lua_tostring(L, -1); parent_dir = ap_make_dirstr_parent(pool, file); pattern = apr_pstrcat(pool, parent_dir, sub_pat, NULL); luaL_gsub(L, current, rep_pat, pattern); lua_setfield(L, -3, field); lua_getfield(L, -2, field); modified = lua_tostring(L, -1); lua_pop(L, 2); part = apr_pstrcat(pool, modified, ";", apr_array_pstrcat(pool, paths, ';'), NULL); lua_pushstring(L, part); lua_setfield(L, -2, field); lua_pop(L, 1); /* pop "package" off the stack */ }
static void Xml_pushDecode(lua_State* L, const char* s, size_t s_size) { luaL_Buffer b; const char* found = strstr(s, "&#"); size_t start=0, pos, i; if(!s_size) s_size=strlen(s); luaL_buffinit(L, &b); found = strstr(s, "&#"); pos = found ? found-s : s_size; while(found) { char ch = 0; size_t i=0; for(found += 2; i<3; ++i, ++found) if(isdigit(*found)) ch = ch * 10 + (*found - 48); else break; if(*found == ';') { if(pos>start) luaL_addlstring(&b, s+start, pos-start); luaL_addchar(&b, ch); start = pos + 3 + i; } found = strstr(found+1, "&#"); pos = found ? found-s : s_size; } if(pos>start) luaL_addlstring(&b,s+start, pos-start); luaL_pushresult(&b); for(i=sv_code_size-1; i<sv_code_size; i-=2) { luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i-1]); lua_remove(L,-2); } }
int Xml_encode(lua_State *L) { if(lua_gettop(L)!=1) return 0; luaL_checkstring(L,-1); size_t i; for(i=0; i<sv_code_size; i+=2) { luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i+1]); lua_remove(L,-2); } char buf[8]; const char* s=lua_tostring(L,1); size_t start, pos; luaL_Buffer b; luaL_buffinit(L, &b); for(start=pos=0; s[pos]!=0; ++pos) if(s[pos]<0) { if(pos>start) luaL_addlstring(&b,s+start, pos-start); luaL_addstring(&b,char2code((unsigned char)(s[pos]),buf)); start=pos+1; } if(pos>start) luaL_addlstring(&b,s+start, pos-start); luaL_pushresult(&b); lua_remove(L,-2); return 1; }
static void setprogdir (lua_State *L) { char buff[MAX_PATH + 1]; char *lb; DWORD nsize = sizeof(buff)/sizeof(char); // BEGIN PATCH: Lua Unicode // the following lines are a unicode patch for "incompatible types" error // http://lua-users.org/lists/lua-l/2006-09/msg00033.html (error report) // http://lua-users.org/lists/lua-l/2006-06/msg00427.html (patch solution) // DWORD n = GetModuleFileName(NULL, buff, nsize); #ifdef UNICODE DWORD n = GetModuleFileNameA(NULL, buff, nsize); #else DWORD n = GetModuleFileName(NULL, buff, nsize); #endif // END PATCH: Lua Unicode if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) luaL_error(L, "unable to get ModuleFileName"); else { *lb = '\0'; luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); lua_remove(L, -2); /* remove original string */ } }
static void Xml_pushDecode(lua_State* L, const char* s, size_t s_size) { size_t start=0, pos; if(!s_size) s_size=strlen(s); luaL_Buffer b; luaL_buffinit(L, &b); const char* found = strstr(s, "&#"); if(!found) pos = s_size; else pos = found-s; while(found&&(pos+5<s_size)&&(*(found+5)==';')&&isdigit(*(found+2))&&isdigit(*(found+3))&&isdigit(*(found+4)) ) { if(pos>start) luaL_addlstring(&b,s+start, pos-start); luaL_addchar(&b, 100*(s[pos+2]-48)+10*(s[pos+3]-48)+(s[pos+4]-48)); start=pos+6; found = strstr(found+6, "&#"); if(!found) pos = s_size; else pos = found-s; } if(pos>start) luaL_addlstring(&b,s+start, pos-start); luaL_pushresult(&b); size_t i; for(i=sv_code_size-1; i<sv_code_size; i-=2) { luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i-1]); lua_remove(L,-2); } }
const char* LuaState::GSub(const char *s, const char *p, const char *r) { return luaL_gsub(m_state, s, p, r); }
static void setprogdir(lua_State *L) { char progdir[_PATH_MAX + 1]; char *lb; int nsize = sizeof(progdir)/sizeof(char); int n = 0; #if defined(__CYGWIN__) char win_buff[_PATH_MAX + 1]; GetModuleFileNameA(NULL, win_buff, nsize); cygwin_conv_to_posix_path(win_buff, progdir); n = strlen(progdir); #elif defined(_WIN32) n = GetModuleFileNameA(NULL, progdir, nsize); #elif defined(__linux__) n = readlink("/proc/self/exe", progdir, nsize); if (n > 0) progdir[n] = 0; #elif defined(__sun) pid_t pid = getpid(); char linkname[256]; sprintf(linkname, "/proc/%d/path/a.out", pid); n = readlink(linkname, progdir, nsize); if (n > 0) progdir[n] = 0; #elif defined(__FreeBSD__) int mib[4]; mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PATHNAME; mib[3] = -1; size_t cb = nsize; sysctl(mib, 4, progdir, &cb, NULL, 0); n = cb; #elif defined(__BSD__) n = readlink("/proc/curproc/file", progdir, nsize); if (n > 0) progdir[n] = 0; #elif defined(__APPLE__) uint32_t nsize_apple = nsize; if (_NSGetExecutablePath(progdir, &nsize_apple) == 0) n = strlen(progdir); #else // FALLBACK // Use 'lsof' ... should work on most UNIX systems (incl. OSX) // lsof will list open files, this captures the 1st file listed (usually the executable) int pid; FILE* fd; char cmd[80]; pid = getpid(); sprintf(cmd, "lsof -p %d | awk '{if ($5==\"REG\") { print $9 ; exit}}' 2> /dev/null", pid); fd = popen(cmd, "r"); n = fread(progdir, 1, nsize, fd); pclose(fd); // remove newline if (n > 1) progdir[--n] = '\0'; #endif if (n == 0 || n == nsize || (lb = strrchr(progdir, (int)LUA_DIRSEP[0])) == NULL) luaL_error(L, "unable to get process executable path"); else { *lb = '\0'; // Replace the relative path placeholder luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, progdir); lua_remove(L, -2); } }