static int dopackage(lua_State* L) { const char* oldScript; char oldcwd[8192]; char filename[8192]; int result; /* Clear the current global so included script can create a new one */ lua_pushnil(L); lua_setglobal(L, "package"); /* Remember the current state of things so I can restore after script runs */ oldScript = currentScript; strcpy(oldcwd, io_getcwd()); /* Try to locate the script file */ strcpy(filename, lua_tostring(L, 1)); if (!io_fileexists(filename)) { strcpy(filename, path_join("", lua_tostring(L, 1), "lua")); } if (!io_fileexists(filename)) { strcpy(filename, path_join(lua_tostring(L, 1), "premake.lua", "")); } if (!io_fileexists(filename)) { lua_pushstring(L, "Unable to open package '"); lua_pushvalue(L, 1); lua_pushstring(L, "'"); lua_concat(L, 3); lua_error(L); } currentScript = filename; io_chdir(path_getdir(filename)); result = lua_dofile(L, path_getname(filename)); /* Restore the previous state */ currentScript = oldScript; io_chdir(oldcwd); return 0; }
static int chdir_lua(lua_State* L) { const char* path = luaL_checkstring(L, 2); if (io_chdir(path)) lua_pushnumber(L, 1); else lua_pushnil(L); return 1; }
int main(int argc, char** argv) { /* If no args are specified... */ if (argc == 1) { puts(HELP_MSG); return 1; } /* Set defaults */ os_detect(); g_filename = DEFAULT; g_cc = NULL; g_dotnet = NULL; g_verbose = 0; /* Process any options that will effect script processing */ arg_set(argc, argv); if (!preprocess()) return 1; /* chdir() to the directory containing the project script, so that * relative paths may be used in the script */ io_chdir(path_getdir(g_filename)); /* Now run the script */ g_hasScript = script_run(g_filename); if (g_hasScript < 0) { puts("** Script failed to run, ending."); return 1; } /* Process any options that depend on the script output */ arg_reset(); if (!postprocess()) return 1; /* All done */ if (g_hasScript) script_close(); prj_close(); return 0; }