Exemplo n.º 1
0
bool module_set_default_path()
{
	static const char *HAKA_CORE_PATH = "/share/haka/core/*";
	static const char *HAKA_MODULE_PATH = "/share/haka/modules/*";
	static const char *HAKA_MODULE_CPATH = "/lib/haka/modules/*";

	size_t path_len;
	char *path;
	const char *haka_path_s = haka_path();

	/* Lua module path */
	{
		/* format <haka_path_s><HAKA_CORE_PATH>;<haka_path_s><HAKA_MODULE_PATH>\0
		 * in a string.
		 */

		path_len = 2*strlen(haka_path_s) + strlen(HAKA_CORE_PATH) + 1 +
				strlen(HAKA_MODULE_PATH) + 1;

		path = malloc(path_len);
		if (!path) {
			error(L"memory error");
			return false;
		}

		snprintf(path, path_len, "%s%s;%s%s", haka_path_s, HAKA_CORE_PATH,
				haka_path_s, HAKA_MODULE_PATH);

		module_set_path(path, false);

		free(path);
	}

	/* C module path */
	{
		/* format <haka_path_s><HAKA_MODULE_CPATH>\0
		 * in a string.
		 */

		path_len = strlen(haka_path_s) + strlen(HAKA_MODULE_CPATH) + 1;

		path = malloc(path_len);
		if (!path) {
			error(L"memory error");
			return false;
		}

		snprintf(path, path_len, "%s%s", haka_path_s, HAKA_MODULE_CPATH);

		module_set_path(path, true);

		free(path);
	}

	return true;
}
Exemplo n.º 2
0
bool initialize_console(struct lua_state *state)
{
	const char *haka_path_s = haka_path();
	char *console_path;
	size_t size;
	DIR *dir;
	struct dirent entry, *result = NULL;

	/* Load all console utilities */
	lua_newtable(state->L);
	lua_setglobal(state->L, "console");
	lua_getglobal(state->L, "console");

	/* <haka_path_s><HAKA_CONSOLE/\0 */
	size = strlen(haka_path_s) + strlen(HAKA_CONSOLE) + 1;

	console_path = malloc(size);
	if (!console_path) {
		error("memory error");
		return false;
	}

	snprintf(console_path, size, "%s%s", haka_path_s, HAKA_CONSOLE);

	dir = opendir(console_path);
	if (!dir) {
		error("cannot open console script folder: %s", console_path);
		return false;
	}
	else {
		while (!readdir_r(dir, &entry, &result) && result) {
			const size_t len = strlen(entry.d_name);
			char fullfilename[PATH_MAX];
			const int level = lua_gettop(state->L);

			snprintf(fullfilename, sizeof(fullfilename), "%s/%s",
					console_path, entry.d_name);

			if (len > 4 && strcmp(entry.d_name + (len - 4), ".lua") == 0) {
				entry.d_name[len - 4] = 0;
			}
			else if (len > 3
			         && strcmp(entry.d_name + (len - 3), ".bc") == 0) {
				entry.d_name[len - 3] = 0;
			}
			else {
				continue;
			}

			messagef(HAKA_LOG_DEBUG, "hakactl", "loading console script '%s'", entry.d_name);

			if (luaL_dofile(state->L, fullfilename)) {
				const char *msg = lua_tostring(state->L, -1);
				messagef(HAKA_LOG_ERROR, "hakactl", "cannot open console script '%s': %s",
				         entry.d_name, msg);
				lua_pop(state->L, 1);
			}
			else {
				const int res = lua_gettop(state->L) - level;
				if (res == 1) {
					lua_setfield(state->L, -2, entry.d_name);
				}
				else {
					lua_pop(state->L, res);
				}
			}
		}
	}

	return true;
}