uci.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*
  2. * libuci plugin for Lua
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <sys/types.h>
  15. #include <sys/time.h>
  16. #include <stdbool.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <lauxlib.h>
  23. #include <uci.h>
  24. #define MODNAME "uci"
  25. #define METANAME MODNAME ".meta"
  26. //#define DEBUG 1
  27. #ifdef DEBUG
  28. #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
  29. #else
  30. #define DPRINTF(...) do {} while (0)
  31. #endif
  32. #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
  33. /*
  34. * ** Adapted from Lua 5.2.0
  35. * */
  36. static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
  37. luaL_checkstack(L, nup+1, "too many upvalues");
  38. for (; l->name != NULL; l++) { /* fill the table with given functions */
  39. int i;
  40. lua_pushstring(L, l->name);
  41. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  42. lua_pushvalue(L, -(nup+1));
  43. lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
  44. lua_settable(L, -(nup + 3));
  45. }
  46. lua_pop(L, nup); /* remove upvalues */
  47. }
  48. #define lua_rawlen(L, i) lua_objlen(L, i)
  49. #endif
  50. static struct uci_context *global_ctx = NULL;
  51. static struct uci_context *
  52. find_context(lua_State *L, int *offset)
  53. {
  54. struct uci_context **ctx;
  55. if (!lua_isuserdata(L, 1)) {
  56. if (!global_ctx) {
  57. global_ctx = uci_alloc_context();
  58. if (!global_ctx) {
  59. luaL_error(L, "failed to allocate UCI context");
  60. return NULL;
  61. }
  62. }
  63. if (offset)
  64. *offset = 0;
  65. return global_ctx;
  66. }
  67. if (offset)
  68. *offset = 1;
  69. ctx = luaL_checkudata(L, 1, METANAME);
  70. if (!ctx || !*ctx) {
  71. luaL_error(L, "failed to get UCI context");
  72. return NULL;
  73. }
  74. return *ctx;
  75. }
  76. static struct uci_package *
  77. find_package(lua_State *L, struct uci_context *ctx, const char *str, bool al)
  78. {
  79. struct uci_package *p = NULL;
  80. struct uci_element *e;
  81. char *sep;
  82. char *name;
  83. sep = strchr(str, '.');
  84. if (sep) {
  85. name = malloc(1 + sep - str);
  86. if (!name) {
  87. luaL_error(L, "out of memory");
  88. return NULL;
  89. }
  90. strncpy(name, str, sep - str);
  91. name[sep - str] = 0;
  92. } else
  93. name = (char *) str;
  94. uci_foreach_element(&ctx->root, e) {
  95. if (strcmp(e->name, name) != 0)
  96. continue;
  97. p = uci_to_package(e);
  98. goto done;
  99. }
  100. if (al)
  101. uci_load(ctx, name, &p);
  102. done:
  103. if (name != str)
  104. free(name);
  105. return p;
  106. }
  107. static int
  108. lookup_extended(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
  109. {
  110. int rv;
  111. struct uci_ptr lookup;
  112. /* use a copy of the passed ptr since failing lookups will
  113. * clobber the state */
  114. lookup = *ptr;
  115. lookup.flags |= UCI_LOOKUP_EXTENDED;
  116. rv = uci_lookup_ptr(ctx, &lookup, str, extended);
  117. /* copy to passed ptr on success */
  118. if (!rv)
  119. *ptr = lookup;
  120. return rv;
  121. }
  122. static int
  123. lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
  124. {
  125. if (ptr && !ptr->s && ptr->section && *ptr->section == '@')
  126. return lookup_extended(ctx, ptr, str, extended);
  127. return uci_lookup_ptr(ctx, ptr, str, extended);
  128. }
  129. static int
  130. lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *ptr, char **buf)
  131. {
  132. char *s = NULL;
  133. int n;
  134. n = lua_gettop(L);
  135. luaL_checkstring(L, 1 + offset);
  136. s = strdup(lua_tostring(L, 1 + offset));
  137. if (!s)
  138. goto error;
  139. memset(ptr, 0, sizeof(struct uci_ptr));
  140. if (!find_package(L, ctx, s, true))
  141. goto error;
  142. switch (n - offset) {
  143. case 4:
  144. case 3:
  145. ptr->option = luaL_checkstring(L, 3 + offset);
  146. /* fall through */
  147. case 2:
  148. ptr->section = luaL_checkstring(L, 2 + offset);
  149. ptr->package = luaL_checkstring(L, 1 + offset);
  150. if (lookup_ptr(ctx, ptr, NULL, true) != UCI_OK)
  151. goto error;
  152. break;
  153. case 1:
  154. if (lookup_ptr(ctx, ptr, s, true) != UCI_OK)
  155. goto error;
  156. break;
  157. default:
  158. luaL_error(L, "invalid argument count");
  159. goto error;
  160. }
  161. *buf = s;
  162. return 0;
  163. error:
  164. if (s)
  165. free(s);
  166. return 1;
  167. }
  168. static int
  169. uci_push_status(lua_State *L, struct uci_context *ctx, bool hasarg)
  170. {
  171. char *str = NULL;
  172. if (!hasarg)
  173. lua_pushboolean(L, (ctx->err == UCI_OK));
  174. if (ctx->err) {
  175. uci_get_errorstr(ctx, &str, MODNAME);
  176. if (str) {
  177. lua_pushstring(L, str);
  178. free(str);
  179. return 2;
  180. }
  181. }
  182. return 1;
  183. }
  184. static void
  185. uci_push_option(lua_State *L, struct uci_option *o)
  186. {
  187. struct uci_element *e;
  188. int i = 0;
  189. switch(o->type) {
  190. case UCI_TYPE_STRING:
  191. lua_pushstring(L, o->v.string);
  192. break;
  193. case UCI_TYPE_LIST:
  194. lua_newtable(L);
  195. uci_foreach_element(&o->v.list, e) {
  196. i++;
  197. lua_pushstring(L, e->name);
  198. lua_rawseti(L, -2, i);
  199. }
  200. break;
  201. default:
  202. lua_pushnil(L);
  203. break;
  204. }
  205. }
  206. static void
  207. uci_push_section(lua_State *L, struct uci_section *s, int index)
  208. {
  209. struct uci_element *e;
  210. lua_newtable(L);
  211. lua_pushboolean(L, s->anonymous);
  212. lua_setfield(L, -2, ".anonymous");
  213. lua_pushstring(L, s->type);
  214. lua_setfield(L, -2, ".type");
  215. lua_pushstring(L, s->e.name);
  216. lua_setfield(L, -2, ".name");
  217. if (index >= 0) {
  218. lua_pushinteger(L, index);
  219. lua_setfield(L, -2, ".index");
  220. }
  221. uci_foreach_element(&s->options, e) {
  222. struct uci_option *o = uci_to_option(e);
  223. uci_push_option(L, o);
  224. lua_setfield(L, -2, o->e.name);
  225. }
  226. }
  227. static void
  228. uci_push_package(lua_State *L, struct uci_package *p)
  229. {
  230. struct uci_element *e;
  231. int i = 0;
  232. lua_newtable(L);
  233. uci_foreach_element(&p->sections, e) {
  234. uci_push_section(L, uci_to_section(e), i);
  235. lua_setfield(L, -2, e->name);
  236. i++;
  237. }
  238. }
  239. static int
  240. uci_lua_unload(lua_State *L)
  241. {
  242. struct uci_context *ctx;
  243. struct uci_package *p;
  244. const char *s;
  245. int offset = 0;
  246. ctx = find_context(L, &offset);
  247. luaL_checkstring(L, 1 + offset);
  248. s = lua_tostring(L, 1 + offset);
  249. p = find_package(L, ctx, s, false);
  250. if (p) {
  251. uci_unload(ctx, p);
  252. return uci_push_status(L, ctx, false);
  253. } else {
  254. lua_pushboolean(L, 0);
  255. }
  256. return 1;
  257. }
  258. static int
  259. uci_lua_load(lua_State *L)
  260. {
  261. struct uci_context *ctx;
  262. struct uci_package *p = NULL;
  263. const char *s;
  264. int offset = 0;
  265. ctx = find_context(L, &offset);
  266. uci_lua_unload(L);
  267. lua_pop(L, 1); /* bool ret value of unload */
  268. s = lua_tostring(L, -1);
  269. uci_load(ctx, s, &p);
  270. return uci_push_status(L, ctx, false);
  271. }
  272. static int
  273. uci_lua_foreach(lua_State *L)
  274. {
  275. struct uci_context *ctx;
  276. struct uci_package *p;
  277. struct uci_element *e, *tmp;
  278. const char *package, *type;
  279. bool ret = false;
  280. int offset = 0;
  281. int i = 0;
  282. ctx = find_context(L, &offset);
  283. package = luaL_checkstring(L, 1 + offset);
  284. if (lua_isnil(L, 2 + offset))
  285. type = NULL;
  286. else
  287. type = luaL_checkstring(L, 2 + offset);
  288. if (!lua_isfunction(L, 3 + offset) || !package)
  289. return luaL_error(L, "Invalid argument");
  290. p = find_package(L, ctx, package, true);
  291. if (!p)
  292. goto done;
  293. uci_foreach_element_safe(&p->sections, tmp, e) {
  294. struct uci_section *s = uci_to_section(e);
  295. i++;
  296. if (type && (strcmp(s->type, type) != 0))
  297. continue;
  298. lua_pushvalue(L, 3 + offset); /* iterator function */
  299. uci_push_section(L, s, i - 1);
  300. if (lua_pcall(L, 1, 1, 0) == 0) {
  301. ret = true;
  302. if (lua_isboolean(L, -1) && !lua_toboolean(L, -1))
  303. break;
  304. }
  305. else
  306. {
  307. lua_error(L);
  308. break;
  309. }
  310. }
  311. done:
  312. lua_pushboolean(L, ret);
  313. return 1;
  314. }
  315. static int
  316. uci_lua_get_any(lua_State *L, bool all)
  317. {
  318. struct uci_context *ctx;
  319. struct uci_element *e = NULL;
  320. struct uci_ptr ptr;
  321. int offset = 0;
  322. int nret = 1;
  323. char *s = NULL;
  324. int err = UCI_ERR_NOTFOUND;
  325. ctx = find_context(L, &offset);
  326. if (lookup_args(L, ctx, offset, &ptr, &s))
  327. goto error;
  328. lookup_ptr(ctx, &ptr, NULL, true);
  329. if (!all && !ptr.s) {
  330. err = UCI_ERR_INVAL;
  331. goto error;
  332. }
  333. if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
  334. err = UCI_ERR_NOTFOUND;
  335. goto error;
  336. }
  337. err = UCI_OK;
  338. e = ptr.last;
  339. switch(e->type) {
  340. case UCI_TYPE_PACKAGE:
  341. uci_push_package(L, ptr.p);
  342. break;
  343. case UCI_TYPE_SECTION:
  344. if (all) {
  345. uci_push_section(L, ptr.s, -1);
  346. }
  347. else {
  348. lua_pushstring(L, ptr.s->type);
  349. lua_pushstring(L, ptr.s->e.name);
  350. nret++;
  351. }
  352. break;
  353. case UCI_TYPE_OPTION:
  354. uci_push_option(L, ptr.o);
  355. break;
  356. default:
  357. err = UCI_ERR_INVAL;
  358. goto error;
  359. }
  360. if (s)
  361. free(s);
  362. if (!err)
  363. return nret;
  364. error:
  365. if (s)
  366. free(s);
  367. lua_pushnil(L);
  368. return uci_push_status(L, ctx, true);
  369. }
  370. static int
  371. uci_lua_get(lua_State *L)
  372. {
  373. return uci_lua_get_any(L, false);
  374. }
  375. static int
  376. uci_lua_get_all(lua_State *L)
  377. {
  378. return uci_lua_get_any(L, true);
  379. }
  380. static int
  381. uci_lua_add(lua_State *L)
  382. {
  383. struct uci_context *ctx;
  384. struct uci_section *s = NULL;
  385. struct uci_package *p;
  386. const char *package;
  387. const char *type;
  388. const char *name = NULL;
  389. int offset = 0;
  390. ctx = find_context(L, &offset);
  391. package = luaL_checkstring(L, 1 + offset);
  392. type = luaL_checkstring(L, 2 + offset);
  393. p = find_package(L, ctx, package, true);
  394. if (!p)
  395. goto fail;
  396. if (uci_add_section(ctx, p, type, &s) || !s)
  397. goto fail;
  398. name = s->e.name;
  399. lua_pushstring(L, name);
  400. return 1;
  401. fail:
  402. lua_pushnil(L);
  403. return uci_push_status(L, ctx, true);
  404. }
  405. static int
  406. uci_lua_delete(lua_State *L)
  407. {
  408. struct uci_context *ctx;
  409. struct uci_ptr ptr;
  410. int offset = 0;
  411. char *s = NULL;
  412. ctx = find_context(L, &offset);
  413. if (lookup_args(L, ctx, offset, &ptr, &s))
  414. goto error;
  415. uci_delete(ctx, &ptr);
  416. error:
  417. if (s)
  418. free(s);
  419. return uci_push_status(L, ctx, false);
  420. }
  421. static int
  422. uci_lua_rename(lua_State *L)
  423. {
  424. struct uci_context *ctx;
  425. struct uci_ptr ptr;
  426. int err = UCI_ERR_MEM;
  427. char *s = NULL;
  428. int nargs, offset = 0;
  429. ctx = find_context(L, &offset);
  430. nargs = lua_gettop(L);
  431. if (lookup_args(L, ctx, offset, &ptr, &s))
  432. goto error;
  433. switch(nargs - offset) {
  434. case 1:
  435. /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
  436. break;
  437. case 4:
  438. /* Format: uci.set("p", "s", "o", "v") */
  439. ptr.value = luaL_checkstring(L, nargs);
  440. break;
  441. case 3:
  442. /* Format: uci.set("p", "s", "v") */
  443. ptr.value = ptr.option;
  444. ptr.option = NULL;
  445. break;
  446. default:
  447. err = UCI_ERR_INVAL;
  448. goto error;
  449. }
  450. err = lookup_ptr(ctx, &ptr, NULL, true);
  451. if (err)
  452. goto error;
  453. if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
  454. err = UCI_ERR_INVAL;
  455. goto error;
  456. }
  457. err = uci_rename(ctx, &ptr);
  458. if (err)
  459. goto error;
  460. error:
  461. if (s)
  462. free(s);
  463. return uci_push_status(L, ctx, false);
  464. }
  465. static int
  466. uci_lua_reorder(lua_State *L)
  467. {
  468. struct uci_context *ctx;
  469. struct uci_ptr ptr;
  470. int err = UCI_ERR_MEM;
  471. char *s = NULL;
  472. int nargs, offset = 0;
  473. ctx = find_context(L, &offset);
  474. nargs = lua_gettop(L);
  475. if (lookup_args(L, ctx, offset, &ptr, &s))
  476. goto error;
  477. switch(nargs - offset) {
  478. case 1:
  479. /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
  480. if (ptr.option) {
  481. err = UCI_ERR_INVAL;
  482. goto error;
  483. }
  484. break;
  485. case 3:
  486. /* Format: uci.set("p", "s", "v") */
  487. ptr.value = ptr.option;
  488. ptr.option = NULL;
  489. break;
  490. default:
  491. err = UCI_ERR_INVAL;
  492. goto error;
  493. }
  494. err = lookup_ptr(ctx, &ptr, NULL, true);
  495. if (err)
  496. goto error;
  497. if ((ptr.s == NULL) || (ptr.value == NULL)) {
  498. err = UCI_ERR_INVAL;
  499. goto error;
  500. }
  501. err = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
  502. if (err)
  503. goto error;
  504. error:
  505. if (s)
  506. free(s);
  507. return uci_push_status(L, ctx, false);
  508. }
  509. static int
  510. uci_lua_set(lua_State *L)
  511. {
  512. struct uci_context *ctx;
  513. struct uci_ptr ptr;
  514. bool istable = false;
  515. int err = UCI_ERR_MEM;
  516. char *s = NULL;
  517. const char *v;
  518. unsigned int i;
  519. int nargs, offset = 0;
  520. ctx = find_context(L, &offset);
  521. nargs = lua_gettop(L);
  522. if (lookup_args(L, ctx, offset, &ptr, &s))
  523. goto error;
  524. switch(nargs - offset) {
  525. case 1:
  526. /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
  527. break;
  528. case 4:
  529. /* Format: uci.set("p", "s", "o", "v") */
  530. if (lua_istable(L, nargs)) {
  531. if (lua_rawlen(L, nargs) < 1) {
  532. free(s);
  533. return luaL_error(L, "Cannot set an uci option to an empty table value");
  534. }
  535. lua_rawgeti(L, nargs, 1);
  536. ptr.value = luaL_checkstring(L, -1);
  537. lua_pop(L, 1);
  538. istable = true;
  539. } else {
  540. ptr.value = luaL_checkstring(L, nargs);
  541. }
  542. break;
  543. case 3:
  544. /* Format: uci.set("p", "s", "v") */
  545. ptr.value = ptr.option;
  546. ptr.option = NULL;
  547. break;
  548. default:
  549. err = UCI_ERR_INVAL;
  550. goto error;
  551. }
  552. err = lookup_ptr(ctx, &ptr, NULL, true);
  553. if (err)
  554. goto error;
  555. if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
  556. err = UCI_ERR_INVAL;
  557. goto error;
  558. }
  559. if (istable) {
  560. if (lua_rawlen(L, nargs) == 1) {
  561. i = 1;
  562. if (ptr.o) {
  563. v = ptr.value;
  564. ptr.value = NULL;
  565. err = uci_delete(ctx, &ptr);
  566. if (err)
  567. goto error;
  568. ptr.value = v;
  569. }
  570. } else {
  571. i = 2;
  572. err = uci_set(ctx, &ptr);
  573. if (err)
  574. goto error;
  575. }
  576. for (; i <= lua_rawlen(L, nargs); i++) {
  577. lua_rawgeti(L, nargs, i);
  578. ptr.value = luaL_checkstring(L, -1);
  579. err = uci_add_list(ctx, &ptr);
  580. lua_pop(L, 1);
  581. if (err)
  582. goto error;
  583. }
  584. } else {
  585. err = uci_set(ctx, &ptr);
  586. if (err)
  587. goto error;
  588. }
  589. error:
  590. if (s)
  591. free(s);
  592. return uci_push_status(L, ctx, false);
  593. }
  594. enum pkg_cmd {
  595. CMD_SAVE,
  596. CMD_COMMIT,
  597. CMD_REVERT
  598. };
  599. static int
  600. uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
  601. {
  602. struct uci_context *ctx;
  603. struct uci_element *e, *tmp;
  604. struct uci_ptr ptr;
  605. char *s = NULL;
  606. int nargs, offset = 0;
  607. ctx = find_context(L, &offset);
  608. nargs = lua_gettop(L);
  609. if ((cmd != CMD_REVERT) && (nargs - offset > 1))
  610. goto err;
  611. if (lookup_args(L, ctx, offset, &ptr, &s))
  612. goto err;
  613. lookup_ptr(ctx, &ptr, NULL, true);
  614. uci_foreach_element_safe(&ctx->root, tmp, e) {
  615. struct uci_package *p = uci_to_package(e);
  616. if (ptr.p && (ptr.p != p))
  617. continue;
  618. ptr.p = p;
  619. switch(cmd) {
  620. case CMD_COMMIT:
  621. uci_commit(ctx, &p, false);
  622. break;
  623. case CMD_SAVE:
  624. uci_save(ctx, p);
  625. break;
  626. case CMD_REVERT:
  627. uci_revert(ctx, &ptr);
  628. break;
  629. }
  630. }
  631. err:
  632. if (s)
  633. free(s);
  634. return uci_push_status(L, ctx, false);
  635. }
  636. static int
  637. uci_lua_save(lua_State *L)
  638. {
  639. return uci_lua_package_cmd(L, CMD_SAVE);
  640. }
  641. static int
  642. uci_lua_commit(lua_State *L)
  643. {
  644. return uci_lua_package_cmd(L, CMD_COMMIT);
  645. }
  646. static int
  647. uci_lua_revert(lua_State *L)
  648. {
  649. return uci_lua_package_cmd(L, CMD_REVERT);
  650. }
  651. static void
  652. uci_lua_add_change(lua_State *L, struct uci_element *e)
  653. {
  654. struct uci_delta *h;
  655. const char *name;
  656. const char *value;
  657. h = uci_to_delta(e);
  658. if (!h->section)
  659. return;
  660. lua_getfield(L, -1, h->section);
  661. if (lua_isnil(L, -1)) {
  662. lua_pop(L, 1);
  663. lua_newtable(L);
  664. lua_pushvalue(L, -1); /* copy for setfield */
  665. lua_setfield(L, -3, h->section);
  666. }
  667. name = h->e.name;
  668. value = h->value ? h->value : "";
  669. if (name) {
  670. lua_getfield(L, -1, name);
  671. /* this delta is a list add operation */
  672. if (h->cmd == UCI_CMD_LIST_ADD) {
  673. /* there seems to be no table yet */
  674. if (!lua_istable(L, -1)) {
  675. lua_newtable(L);
  676. /* if there is a value on the stack already, add */
  677. if (!lua_isnil(L, -2)) {
  678. lua_pushvalue(L, -2);
  679. lua_rawseti(L, -2, 1);
  680. lua_pushstring(L, value);
  681. lua_rawseti(L, -2, 2);
  682. /* this is the first table item */
  683. } else {
  684. lua_pushstring(L, value);
  685. lua_rawseti(L, -2, 1);
  686. }
  687. lua_setfield(L, -3, name);
  688. /* a table is on the top of the stack and this is a subsequent,
  689. * list_add, append this value to table */
  690. } else {
  691. lua_pushstring(L, value);
  692. lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
  693. }
  694. /* non-list change, simply set/replace field */
  695. } else {
  696. lua_pushstring(L, value);
  697. lua_setfield(L, -3, name);
  698. }
  699. lua_pop(L, 1);
  700. } else {
  701. lua_pushstring(L, value);
  702. lua_setfield(L, -2, ".type");
  703. }
  704. lua_pop(L, 1);
  705. }
  706. static void
  707. uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
  708. {
  709. struct uci_package *p = NULL;
  710. struct uci_element *e;
  711. bool autoload = false;
  712. p = find_package(L, ctx, package, false);
  713. if (!p) {
  714. autoload = true;
  715. p = find_package(L, ctx, package, true);
  716. if (!p)
  717. return;
  718. }
  719. if (uci_list_empty(&p->delta) && uci_list_empty(&p->saved_delta))
  720. goto done;
  721. lua_newtable(L);
  722. uci_foreach_element(&p->saved_delta, e) {
  723. uci_lua_add_change(L, e);
  724. }
  725. uci_foreach_element(&p->delta, e) {
  726. uci_lua_add_change(L, e);
  727. }
  728. lua_setfield(L, -2, p->e.name);
  729. done:
  730. if (autoload)
  731. uci_unload(ctx, p);
  732. }
  733. static int
  734. uci_lua_changes(lua_State *L)
  735. {
  736. struct uci_context *ctx;
  737. const char *package = NULL;
  738. char **config = NULL;
  739. int nargs;
  740. int i, offset = 0;
  741. ctx = find_context(L, &offset);
  742. nargs = lua_gettop(L);
  743. switch(nargs - offset) {
  744. case 1:
  745. package = luaL_checkstring(L, 1 + offset);
  746. case 0:
  747. break;
  748. default:
  749. return luaL_error(L, "invalid argument count");
  750. }
  751. lua_newtable(L);
  752. if (package) {
  753. uci_lua_changes_pkg(L, ctx, package);
  754. return 1;
  755. }
  756. if ((uci_list_configs(ctx, &config) != UCI_OK) || !config)
  757. return 1;
  758. for (i = 0; config[i] != NULL; i++) {
  759. uci_lua_changes_pkg(L, ctx, config[i]);
  760. }
  761. free(config);
  762. return 1;
  763. }
  764. static int
  765. uci_lua_get_confdir(lua_State *L)
  766. {
  767. struct uci_context *ctx = find_context(L, NULL);
  768. lua_pushstring(L, ctx->confdir);
  769. return 1;
  770. }
  771. static int
  772. uci_lua_set_confdir(lua_State *L)
  773. {
  774. struct uci_context *ctx;
  775. int offset = 0;
  776. ctx = find_context(L, &offset);
  777. luaL_checkstring(L, 1 + offset);
  778. uci_set_confdir(ctx, lua_tostring(L, -1));
  779. return uci_push_status(L, ctx, false);
  780. }
  781. static int
  782. uci_lua_get_savedir(lua_State *L)
  783. {
  784. struct uci_context *ctx = find_context(L, NULL);
  785. lua_pushstring(L, ctx->savedir);
  786. return 1;
  787. }
  788. static int
  789. uci_lua_add_delta(lua_State *L)
  790. {
  791. struct uci_context *ctx;
  792. int offset = 0;
  793. ctx = find_context(L, &offset);
  794. luaL_checkstring(L, 1 + offset);
  795. uci_add_delta_path(ctx, lua_tostring(L, -1));
  796. return uci_push_status(L, ctx, false);
  797. }
  798. static int
  799. uci_lua_set_savedir(lua_State *L)
  800. {
  801. struct uci_context *ctx;
  802. int offset = 0;
  803. ctx = find_context(L, &offset);
  804. luaL_checkstring(L, 1 + offset);
  805. uci_set_savedir(ctx, lua_tostring(L, -1));
  806. return uci_push_status(L, ctx, false);
  807. }
  808. static int
  809. uci_lua_list_configs(lua_State *L)
  810. {
  811. struct uci_context *ctx;
  812. char **configs = NULL;
  813. char **ptr;
  814. int i = 1;
  815. ctx = find_context(L, NULL);
  816. if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs)
  817. return uci_push_status(L, ctx, false);
  818. lua_newtable(L);
  819. for (ptr = configs; *ptr; ptr++) {
  820. lua_pushstring(L, *ptr);
  821. lua_rawseti(L, -2, i++);
  822. }
  823. free(configs);
  824. return 1;
  825. }
  826. static int
  827. uci_lua_gc(lua_State *L)
  828. {
  829. struct uci_context **ctx;
  830. if (!lua_isuserdata(L, 1)) {
  831. if (!global_ctx)
  832. return 0;
  833. ctx = &global_ctx;
  834. } else {
  835. ctx = luaL_checkudata(L, 1, METANAME);
  836. if (!*ctx)
  837. return 0;
  838. }
  839. uci_free_context(*ctx);
  840. *ctx = NULL;
  841. return 0;
  842. }
  843. static int
  844. uci_lua_cursor(lua_State *L)
  845. {
  846. struct uci_context **u;
  847. int argc = lua_gettop(L);
  848. u = lua_newuserdata(L, sizeof(struct uci_context *));
  849. luaL_getmetatable(L, METANAME);
  850. lua_setmetatable(L, -2);
  851. *u = uci_alloc_context();
  852. if (!*u)
  853. return luaL_error(L, "Cannot allocate UCI context");
  854. switch (argc) {
  855. case 2:
  856. if (lua_isstring(L, 2) &&
  857. (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
  858. return luaL_error(L, "Unable to set savedir");
  859. /* fall through */
  860. case 1:
  861. if (lua_isstring(L, 1) &&
  862. (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
  863. return luaL_error(L, "Unable to set confdir");
  864. break;
  865. default:
  866. break;
  867. }
  868. return 1;
  869. }
  870. static const luaL_Reg uci[] = {
  871. { "__gc", uci_lua_gc },
  872. { "close", uci_lua_gc },
  873. { "cursor", uci_lua_cursor },
  874. { "load", uci_lua_load },
  875. { "unload", uci_lua_unload },
  876. { "get", uci_lua_get },
  877. { "get_all", uci_lua_get_all },
  878. { "add", uci_lua_add },
  879. { "set", uci_lua_set },
  880. { "rename", uci_lua_rename },
  881. { "save", uci_lua_save },
  882. { "delete", uci_lua_delete },
  883. { "commit", uci_lua_commit },
  884. { "revert", uci_lua_revert },
  885. { "reorder", uci_lua_reorder },
  886. { "changes", uci_lua_changes },
  887. { "foreach", uci_lua_foreach },
  888. { "add_history", uci_lua_add_delta },
  889. { "add_delta", uci_lua_add_delta },
  890. { "get_confdir", uci_lua_get_confdir },
  891. { "set_confdir", uci_lua_set_confdir },
  892. { "get_savedir", uci_lua_get_savedir },
  893. { "set_savedir", uci_lua_set_savedir },
  894. { "list_configs", uci_lua_list_configs },
  895. { NULL, NULL },
  896. };
  897. int
  898. luaopen_uci(lua_State *L)
  899. {
  900. /* create metatable */
  901. luaL_newmetatable(L, METANAME);
  902. /* metatable.__index = metatable */
  903. lua_pushvalue(L, -1);
  904. lua_setfield(L, -2, "__index");
  905. /* fill metatable */
  906. luaL_setfuncs(L, uci, 0);
  907. lua_pop(L, 1);
  908. /* create module */
  909. lua_newtable(L);
  910. lua_pushvalue(L, -1);
  911. luaL_setfuncs(L, uci, 0);
  912. lua_setglobal(L, MODNAME);
  913. return 1;
  914. }