Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <lua.h>
- #include <xbps.h>
- /* Converts an XBPS object to a Lua value. */
- int
- xbps_obj_to_lua(lua_State *L, xbps_object_t obj)
- {
- lua_Number n;
- xbps_object_t obj2;
- xbps_object_t allkeys, keysym;
- const char *ksymname;
- int tbl;
- switch (xbps_object_type(obj)) {
- case XBPS_TYPE_STRING:
- lua_pushstring(L, xbps_string_cstring_nocopy(obj));
- break;
- case XBPS_TYPE_NUMBER:
- lua_pushnumber(L, xbps_number_unsigned_integer_value(obj));
- break;
- case XBPS_TYPE_BOOL:
- lua_pushboolean(L, xbps_bool_true(obj));
- break;
- case XBPS_TYPE_ARRAY:
- lua_newtable(L);
- tbl = lua_gettop(L);
- for (unsigned int i = 0; i < xbps_array_count(obj); i++) {
- xbps_obj_to_lua(L, xbps_array_get(obj, i));
- lua_rawseti(L, tbl, i + 1);
- }
- break;
- case XBPS_TYPE_DICTIONARY:
- lua_newtable(L);
- tbl = lua_gettop(L);
- allkeys = xbps_dictionary_all_keys(obj);
- for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
- keysym = xbps_array_get(allkeys, i);
- ksymname = xbps_dictionary_keysym_cstring_nocopy(keysym);
- obj2 = xbps_dictionary_get_keysym(obj, keysym);
- lua_pushstring(L, ksymname);
- if (xbps_obj_to_lua(L, obj2))
- lua_rawset(L, tbl);
- else
- lua_pop(L, 1);
- }
- xbps_object_release(allkeys);
- break;
- case XBPS_TYPE_DATA:
- /* install-script and remove-script - could just use string for these */
- default:
- return 0;
- }
- return 1;
- }
- struct luaxbps {
- struct xbps_handle xh;
- };
- int
- luaxbps_new(lua_State *L)
- {
- /* local self = userdata(sizeof(struct luaxbps)) */
- lua_newuserdata(L, sizeof(struct luaxbps));
- int self = lua_gettop(L);
- struct luaxbps *selfptr = lua_touserdata(L, self);
- memset(&selfptr->xh, 0, sizeof(struct xbps_handle));
- int err;
- if ((err = xbps_init(&selfptr->xh)) != 0) {
- lua_pushfstring(L, "Failed to initialize libxbps: %s", strerror(err));
- lua_error(L);
- /* not reached */
- }
- /* setmetatable(self, registry.luaxbps) */
- lua_pushstring(L, "luaxbps");
- lua_rawget(L, LUA_REGISTRYINDEX);
- lua_setmetatable(L, self);
- return 1;
- }
- struct xbps_list_state {
- lua_State *L;
- int list;
- int i;
- };
- int
- xbps_list_callback(struct xbps_handle *xhp,
- xbps_object_t obj,
- const char *key,
- void *arg,
- bool *loop_done)
- {
- struct xbps_list_state *state = arg;
- xbps_obj_to_lua(state->L, obj);
- /* state->list[state->i++] = obj */
- lua_rawseti(state->L, state->list, state->i++);
- return 0;
- }
- /* Gets a list of all installed packages. */
- int
- luaxbps_list_pkgs(lua_State *L)
- {
- struct luaxbps *selfptr = lua_touserdata(L, lua_gettop(L));
- lua_newtable(L);
- struct xbps_list_state state;
- state.L = L;
- state.list = lua_gettop(L);
- state.i = 1;
- xbps_pkgdb_foreach_cb(&selfptr->xh, xbps_list_callback, &state);
- return 1;
- }
- int
- luaxbps_gc(lua_State *L)
- {
- struct luaxbps *selfptr = lua_touserdata(L, lua_gettop(L));
- if (selfptr)
- xbps_end(&selfptr->xh);
- return 0;
- }
- int
- luaopen_luaxbps(lua_State *L)
- {
- /* local luaxbps = {} */
- lua_newtable(L);
- int luaxbps = lua_gettop(L);
- /* luaxbps.__index = module */
- lua_pushstring(L, "__index");
- lua_pushvalue(L, luaxbps);
- lua_rawset(L, luaxbps);
- /* luaxbps.__gc = luaxbps_gc */
- lua_pushstring(L, "__gc");
- lua_pushcfunction(L, luaxbps_gc);
- lua_rawset(L, luaxbps);
- /* luaxbps.new = luaxbps_new */
- lua_pushstring(L, "new");
- lua_pushcfunction(L, luaxbps_new);
- lua_rawset(L, luaxbps);
- /* luaxbps.list_pkgs = luaxbps_list_pkgs */
- lua_pushstring(L, "list_pkgs");
- lua_pushcfunction(L, luaxbps_list_pkgs);
- lua_rawset(L, luaxbps);
- /* registry.luaxbps = luaxbps */
- lua_pushstring(L, "luaxbps");
- lua_pushvalue(L, luaxbps);
- lua_rawset(L, LUA_REGISTRYINDEX);
- return 1;
- }
Add Comment
Please, Sign In to add comment