コード例 #1
0
ファイル: lua_interface.c プロジェクト: ruinned/swupdate
int run_lua_script(char *script, char *function, char *parms)
{
    int ret;

    lua_State *L = luaL_newstate(); /* opens Lua */
    luaL_openlibs(L); /* opens the standard libraries */

    if (luaL_loadfile(L, script)) {
        ERROR("ERROR loading %s", script);
        return 1;
    }

    ret = lua_pcall(L, 0, 0, 0);
    if (ret) {
        LUAstackDump(L);
        ERROR("ERROR preparing %s script %d", script, ret);
        return 1;
    }

    lua_getglobal(L, function);
    /* passing arguments */
    lua_pushstring(L, parms);

    if (lua_pcall(L, 1, 1, 0)) {
        LUAstackDump(L);
        ERROR("ERROR running script");
        return 1;
    }

    if (lua_type(L, 1) != LUA_TNUMBER) {
        ERROR("LUA script returns wrong type");
        lua_close(L);
        return 1;
    }

    ret = lua_tonumber(L, 1);
    lua_close(L);

    return ret;

}
コード例 #2
0
ファイル: lua_scripthandler.c プロジェクト: Chadizzm/swupdate
static int start_lua_script(struct img_type *img, void *data)
{
	int ret;
	const char *fnname;
	const char *output;
	char filename[64];
	script_fn scriptfn;
	lua_State *L = luaL_newstate(); /* opens Lua */

	if (!data)
		return -1;

	scriptfn = *(script_fn *)data;

	switch (scriptfn) {
	case PREINSTALL:
		fnname="preinst";
		break;
	case POSTINSTALL:
		fnname="postinst";
		break;
	default:
		/* no error, simply no call */
		return 0;
	}

	snprintf(filename, sizeof(filename), "%s%s", TMPDIR, img->fname);
	TRACE("Calling LUA %s", filename);

	luaL_openlibs(L); /* opens the standard libraries */

	if (luaL_loadfile(L, filename)) {
		ERROR("ERROR loading %s", filename);
		lua_close(L);
		return -1;
	}

	ret = lua_pcall(L, 0, 0, 0);
	if (ret) {
		LUAstackDump(L);
		ERROR("ERROR preparing LUA script %s %d",
			filename, ret);
		lua_close(L);
		return -1;
	}

	lua_getglobal(L, fnname);

	if(!lua_isfunction(L,lua_gettop(L))) {
		lua_close(L);
		TRACE("Script : no %s in %s script, exiting", fnname, filename);
		return 0;
	}

	/* passing arguments */
	lua_pushstring(L, filename);

	if (lua_pcall(L, 1, 2, 0)) {
		LUAstackDump(L);
		ERROR("ERROR Calling LUA script %s", filename);
		lua_close(L);
		return -1;
	}

	if (lua_type(L, 1) == LUA_TBOOLEAN)
		ret = lua_toboolean(L, 1) ? 0 : 1;

	if (lua_type(L, 2) == LUA_TSTRING) {
		output = lua_tostring(L, 2);
		TRACE("Script output: %s script end", output);
	}

	lua_close(L);

	return ret;

}