uci.c 19 KB

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