-static int run_lua_code(int argc,char **argv) {
- DEBUG1("Run lua code %s",argv[0]);
-// fprintf(stderr,"Run lua code %s\n", (argv ? argv[0] : "(null)"));
- lua_State *L = lua_newthread(simgrid_lua_state);
- int ref = luaL_ref(simgrid_lua_state, LUA_REGISTRYINDEX); // protect the thread from being garbage collected
- int res = 1;
+ /* Get the command line arguments from the lua interpreter */
+ char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
+ int argc = 1;
+ argv[0] = (char *) "/usr/bin/lua"; /* Lie on the argv[0] so that the stack dumping facilities find the right binary. FIXME: what if lua is not in that location? */
+
+ lua_getglobal(L, "arg");
+ /* if arg is a null value, it means we use lua only as a script to init platform
+ * else it should be a table and then take arg in consideration
+ */
+ if (lua_istable(L, -1)) {
+ int done = 0;
+ while (!done) {
+ argc++;
+ lua_pushinteger(L, argc - 2);
+ lua_gettable(L, -2);
+ if (lua_isnil(L, -1)) {
+ done = 1;
+ } else {
+ xbt_assert(lua_isstring(L, -1),
+ "argv[%d] got from lua is no string", argc - 1);
+ xbt_assert(argc < LUA_MAX_ARGS_COUNT,
+ "Too many arguments, please increase LUA_MAX_ARGS_COUNT in %s before recompiling SimGrid if you insist on having more than %d args on command line",
+ __FILE__, LUA_MAX_ARGS_COUNT - 1);
+ argv[argc - 1] = (char *) luaL_checkstring(L, -1);
+ lua_pop(L, 1);
+ XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
+ }
+ }
+ argv[argc--] = NULL;