Ejemplo n.º 1
0
static int luaB_loadfile (lua_State *L) {
  const char *fname = luaL_optstring(L, 1, NULL);
  const char *mode = luaL_optstring(L, 2, NULL);
  int env = (!lua_isnone(L, 3) ? 3 : 0);  /* 'env' index or 0 if no 'env' */
  int status = luaL_loadfilex(L, fname, mode);
  return load_aux(L, status, env);
}
Ejemplo n.º 2
0
void lluas_load_file(const char* filename, unsigned instr_limit) {
  lua_State* L = interpreter;

  if (les_fatal == error_status) return;

  /* TODO: The error reporting could be better; in some cases, it'll be hard to
   * tell which file caused the problem.
   */

  lua_setinstrlimit(L, instr_limit);
  switch (luaL_loadfilex(L, filename, "t" /* no bytecode allowed */)) {
  case LUA_OK: break;
  case LUA_ERRSYNTAX:
    lluas_error("Syntax error", les_problematic);
    return;

  case LUA_ERRFILE:
    lluas_error("Missing file?", les_fatal);
    return;

  case LUA_ERRGCMM:
  case LUA_ERRMEM:
    lluas_error("Script memory exhausted", les_fatal);
    return;

  default:
    lluas_error("Unexpected error loading script", les_fatal);
    return;
  }

  lluas_invoke_top_of_stack();
}
Ejemplo n.º 3
0
static int luaB_loadfile (lua_State *L) {
  const char *fname = luaL_optstring(L, 1, NULL);
  const char *mode = luaL_optstring(L, 2, NULL);
  int env = !lua_isnone(L, 3);  /* 'env' parameter? */
  int status = luaL_loadfilex(L, fname, mode);
  if (status == LUA_OK && env) {  /* 'env' parameter? */
    lua_pushvalue(L, 3);
    lua_setupvalue(L, -2, 1);  /* set it as 1st upvalue of loaded chunk */
  }
  return load_aux(L, status);
}
Ejemplo n.º 4
0
static int luaB_loadfile (lua_State *L) {
  const char *fname = luaL_optstring(L, 1, NULL);
  const char *mode = luaL_optstring(L, 2, NULL);
  int env = (!lua_isnone(L, 3) ? 3 : 0);  /* 'env' index or 0 if no 'env' */
#if defined( LUA_USES_LOADF )
  int status = luaL_loadfilex(L, fname, mode);
#else
  int status = luaL_loadfsfilex(L, fname, mode);
#endif
  return load_aux(L, status, env);
}
Ejemplo n.º 5
0
int pg_cop_get_config_number(const char *conf_key, int *num)
{
	const char *filename = pg_cop_lua_config_file;

	if (!filename)
		filename = "/etc/pgcop_conf.lua";

	lua_State *L = (lua_State *) luaL_newstate();
	if (!L)
		goto new_state;

	if (luaL_loadfilex(L, filename, NULL) ||
	        lua_pcall(L, 0, 0, 0)) {
		DEBUG_ERROR("Cannot load configs from lua. error=%s", lua_tostring(L, -1));
		goto load_lua_file;
	}

	lua_getglobal(L, "pgcop");
	if (!lua_istable(L, -1))
		goto check_global_type;

	char *tmp = strdup(conf_key);
	char *np = tmp;
	char *cp;
	while ((cp = strsep(&np, "."))) {
		if (np) {
			lua_getfield(L, -1, cp);
			if (!lua_istable(L, -1))
				goto check_type;
		} else {
			lua_getfield(L, -1, cp);
			if (!lua_isnumber(L, -1))
				goto check_type;
			*num = lua_tonumber(L, -1);
			break;
		}
	}

	free(tmp);
	tmp = NULL;
	return 0;

check_type:
	DEBUG_ERROR("Config key %s not be found.", conf_key);
	free(tmp);
	tmp = NULL;
check_global_type:
load_lua_file:
	lua_close(L);
	L = NULL;
new_state:
	return -1;
}
Ejemplo n.º 6
0
int pg_cop_read_config()
{
	lua_State *L = (lua_State *) luaL_newstate();
	if (!L)
		goto new_state;

	const char *filename = pg_cop_lua_config_file;
	if (!filename)
		filename = "/etc/pgcop_conf.lua";

	if (luaL_loadfilex(L, filename, NULL) ||
	        lua_pcall(L, 0, 0, 0)) {
		DEBUG_CRITICAL("Cannot load configs from lua. error=%s", lua_tostring(L, -1));
		goto load_lua_file;
	}

	lua_getglobal(L, "pgcop");
	if (lua_istable(L, -1)) {
		lua_getfield(L, -1, "modules_path");
		if (lua_isstring(L, -1))
			pg_cop_modules_path = strdup(lua_tostring(L, -1));
	}
	lua_getglobal(L, "pgcop");
	if (lua_istable(L, -1)) {
		lua_getfield(L, -1, "seeds_path");
		if (lua_isstring(L, -1))
			pg_cop_seeds_path = strdup(lua_tostring(L, -1));
	}
	lua_getglobal(L, "pgcop");
	if (lua_istable(L, -1)) {
		lua_getfield(L, -1, "incoming_port");
		if (lua_isnumber(L, -1))
			tracker_incoming_port = lua_tonumber(L, -1);
	}

	if (pg_cop_modules_path == NULL)
		pg_cop_modules_path = "/usr/local/pgcop/share/pgcop/modules";
	if (pg_cop_seeds_path == NULL)
		pg_cop_seeds_path = "/usr/local/pgcop/share/pgcop/seeds";

	lua_close(L);
	return 0;

load_lua_file:
	lua_close(L);
	L = NULL;
new_state:
	return -1;
}
Ejemplo n.º 7
0
LUALIB_API int lnode_load_script(lua_State* L, char* filename, int argc, char* argv[], int offset) {
  // Load the init.lua script
  if (luaL_loadfilex(L, filename, NULL)) {
    fprintf(stderr, "%s\n", lua_tostring(L, -1));
    return -1;
  }

  // args
  lnode_create_arg_table(L, argv, argc, offset);

  // Start the main script.
  if (lua_pcall(L, 0, 1, 0)) {
    fprintf(stderr, "%s\n", lua_tostring(L, -1));
    return -1;
  }

  // Use the return value from the script as process exit code.
  int res = 0;
  if (lua_type(L, -1) == LUA_TNUMBER) {
    res = lua_tointeger(L, -1);
  }

  return res;
}
Ejemplo n.º 8
0
LUALIB_API int luaL_loadfile(lua_State *L, const char *filename)
{
  return luaL_loadfilex(L, filename, NULL);
}