Guest User

Untitled

a guest
Dec 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <lua.h>
  6. #include <xbps.h>
  7.  
  8. /* Converts an XBPS object to a Lua value. */
  9. int
  10. xbps_obj_to_lua(lua_State *L, xbps_object_t obj)
  11. {
  12. lua_Number n;
  13. xbps_object_t obj2;
  14. xbps_object_t allkeys, keysym;
  15. const char *ksymname;
  16. int tbl;
  17.  
  18. switch (xbps_object_type(obj)) {
  19. case XBPS_TYPE_STRING:
  20. lua_pushstring(L, xbps_string_cstring_nocopy(obj));
  21. break;
  22. case XBPS_TYPE_NUMBER:
  23. lua_pushnumber(L, xbps_number_unsigned_integer_value(obj));
  24. break;
  25. case XBPS_TYPE_BOOL:
  26. lua_pushboolean(L, xbps_bool_true(obj));
  27. break;
  28. case XBPS_TYPE_ARRAY:
  29. lua_newtable(L);
  30. tbl = lua_gettop(L);
  31. for (unsigned int i = 0; i < xbps_array_count(obj); i++) {
  32. xbps_obj_to_lua(L, xbps_array_get(obj, i));
  33. lua_rawseti(L, tbl, i + 1);
  34. }
  35. break;
  36. case XBPS_TYPE_DICTIONARY:
  37. lua_newtable(L);
  38. tbl = lua_gettop(L);
  39. allkeys = xbps_dictionary_all_keys(obj);
  40. for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
  41. keysym = xbps_array_get(allkeys, i);
  42. ksymname = xbps_dictionary_keysym_cstring_nocopy(keysym);
  43. obj2 = xbps_dictionary_get_keysym(obj, keysym);
  44. lua_pushstring(L, ksymname);
  45. if (xbps_obj_to_lua(L, obj2))
  46. lua_rawset(L, tbl);
  47. else
  48. lua_pop(L, 1);
  49. }
  50. xbps_object_release(allkeys);
  51. break;
  52. case XBPS_TYPE_DATA:
  53. /* install-script and remove-script - could just use string for these */
  54. default:
  55. return 0;
  56. }
  57. return 1;
  58. }
  59.  
  60. struct luaxbps {
  61. struct xbps_handle xh;
  62. };
  63.  
  64. int
  65. luaxbps_new(lua_State *L)
  66. {
  67. /* local self = userdata(sizeof(struct luaxbps)) */
  68. lua_newuserdata(L, sizeof(struct luaxbps));
  69. int self = lua_gettop(L);
  70.  
  71. struct luaxbps *selfptr = lua_touserdata(L, self);
  72. memset(&selfptr->xh, 0, sizeof(struct xbps_handle));
  73. int err;
  74. if ((err = xbps_init(&selfptr->xh)) != 0) {
  75. lua_pushfstring(L, "Failed to initialize libxbps: %s", strerror(err));
  76. lua_error(L);
  77. /* not reached */
  78. }
  79.  
  80. /* setmetatable(self, registry.luaxbps) */
  81. lua_pushstring(L, "luaxbps");
  82. lua_rawget(L, LUA_REGISTRYINDEX);
  83. lua_setmetatable(L, self);
  84.  
  85. return 1;
  86. }
  87.  
  88. struct xbps_list_state {
  89. lua_State *L;
  90. int list;
  91. int i;
  92. };
  93.  
  94. int
  95. xbps_list_callback(struct xbps_handle *xhp,
  96. xbps_object_t obj,
  97. const char *key,
  98. void *arg,
  99. bool *loop_done)
  100. {
  101. struct xbps_list_state *state = arg;
  102.  
  103. xbps_obj_to_lua(state->L, obj);
  104.  
  105. /* state->list[state->i++] = obj */
  106. lua_rawseti(state->L, state->list, state->i++);
  107.  
  108. return 0;
  109. }
  110.  
  111. /* Gets a list of all installed packages. */
  112. int
  113. luaxbps_list_pkgs(lua_State *L)
  114. {
  115. struct luaxbps *selfptr = lua_touserdata(L, lua_gettop(L));
  116.  
  117. lua_newtable(L);
  118.  
  119. struct xbps_list_state state;
  120. state.L = L;
  121. state.list = lua_gettop(L);
  122. state.i = 1;
  123.  
  124. xbps_pkgdb_foreach_cb(&selfptr->xh, xbps_list_callback, &state);
  125.  
  126. return 1;
  127. }
  128.  
  129. int
  130. luaxbps_gc(lua_State *L)
  131. {
  132. struct luaxbps *selfptr = lua_touserdata(L, lua_gettop(L));
  133.  
  134. if (selfptr)
  135. xbps_end(&selfptr->xh);
  136.  
  137. return 0;
  138. }
  139.  
  140. int
  141. luaopen_luaxbps(lua_State *L)
  142. {
  143. /* local luaxbps = {} */
  144. lua_newtable(L);
  145. int luaxbps = lua_gettop(L);
  146.  
  147. /* luaxbps.__index = module */
  148. lua_pushstring(L, "__index");
  149. lua_pushvalue(L, luaxbps);
  150. lua_rawset(L, luaxbps);
  151.  
  152. /* luaxbps.__gc = luaxbps_gc */
  153. lua_pushstring(L, "__gc");
  154. lua_pushcfunction(L, luaxbps_gc);
  155. lua_rawset(L, luaxbps);
  156.  
  157. /* luaxbps.new = luaxbps_new */
  158. lua_pushstring(L, "new");
  159. lua_pushcfunction(L, luaxbps_new);
  160. lua_rawset(L, luaxbps);
  161.  
  162. /* luaxbps.list_pkgs = luaxbps_list_pkgs */
  163. lua_pushstring(L, "list_pkgs");
  164. lua_pushcfunction(L, luaxbps_list_pkgs);
  165. lua_rawset(L, luaxbps);
  166.  
  167. /* registry.luaxbps = luaxbps */
  168. lua_pushstring(L, "luaxbps");
  169. lua_pushvalue(L, luaxbps);
  170. lua_rawset(L, LUA_REGISTRYINDEX);
  171.  
  172. return 1;
  173. }
Add Comment
Please, Sign In to add comment