int premake_execute(lua_State* L, int argc, const char** argv) { int z; /* push the absolute path to the Premake executable */ lua_pushcfunction(L, path_getabsolute); premake_locate(L, argv[0]); lua_call(L, 1, 1); lua_setglobal(L, "_PREMAKE_COMMAND"); /* Parse the command line arguments */ z = process_arguments(L, argc, argv); /* Run the built-in Premake scripts */ if (z == OKAY) z = load_builtin_scripts(L); return z; }
int premake_execute(lua_State* L, int argc, const char** argv) { int iErrFunc; /* push the absolute path to the Premake executable */ lua_pushcfunction(L, path_getabsolute); premake_locate(L, argv[0]); lua_call(L, 1, 1); lua_setglobal(L, "_PREMAKE_COMMAND"); /* Parse the command line arguments */ if (process_arguments(L, argc, argv) != OKAY) { return !OKAY; } /* load the main script */ if (luaL_dofile(L, "src/_premake_main.lua") != OKAY) { printf(ERROR_MESSAGE, lua_tostring(L, -1)); return !OKAY; } /* in debug mode, show full traceback on all errors */ #if defined(NDEBUG) iErrFunc = 0; #else lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); iErrFunc = -2; #endif /* and call the main entry point */ lua_getglobal(L, "_premake_main"); if (lua_pcall(L, 0, 1, iErrFunc) != OKAY) { printf(ERROR_MESSAGE, lua_tostring(L, -1)); return !OKAY; } else { return (int)lua_tonumber(L, -1); } }
int main(int argc, const char** argv) { lua_State* L; int z = OKAY; L = lua_open(); luaL_openlibs(L); z = premake_init(L); /* push the location of the Premake executable */ lua_pushcfunction(L, path_getabsolute); premake_locate(L, argv[0]); lua_call(L, 1, 1); lua_setglobal(L, "_PREMAKE_COMMAND"); if (z == OKAY) { z = premake_execute(L, argc, argv); } lua_close(L); return z; }
int main (int argc, char *argv[]) { char *script_path; lua_State *L; int c; while ((c = getopt(argc, argv, "C:")) != -1) { switch (c) { case 'C': if (chdir(optarg)) { fprintf(stderr, "could not change working directory\n"); return EXIT_FAILURE; } break; case '?': default: return EXIT_FAILURE; } } if (optind >= argc || argv[optind][0] == '-') { fprintf(stderr, "lua script not provided\n"); return EXIT_FAILURE; } script_path = argv[optind]; optind++; L = luaL_newstate(); if (L == NULL) { fprintf(stderr, "cannot create lua state\n"); return EXIT_FAILURE; } luaL_openlibs(L); luaL_requiref(L, "nettle", luaopen_nettle, 1); lua_pop(L, 1); premake_init(L); premake_locate(L, argv[0]); lua_setglobal(L, "_EXE_PATH"); lua_pushstring(L, script_path); lua_setglobal(L, "_GRANGER_SCRIPT"); lua_newtable(L); for (int i = 1; optind < argc; i++, optind++) { lua_pushinteger(L, i); lua_pushstring(L, argv[optind]); lua_settable(L, 1); } lua_setglobal(L, "argv"); if (luaL_dofile(L, script_path)) { fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); lua_close(L); return EXIT_FAILURE; } lua_close(L); return EXIT_SUCCESS; }