您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /* Copyright (c) 2014, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /**
  24. * @file lua ucl bindings
  25. */
  26. #include "ucl.h"
  27. #include "ucl_internal.h"
  28. #include "lua_ucl.h"
  29. #include <strings.h>
  30. /***
  31. * @module ucl
  32. * This lua module allows to parse objects from strings and to store data into
  33. * ucl objects. It uses `libucl` C library to parse and manipulate with ucl objects.
  34. * @example
  35. local ucl = require("ucl")
  36. local parser = ucl.parser()
  37. local res,err = parser:parse_string('{key=value}')
  38. if not res then
  39. print('parser error: ' .. err)
  40. else
  41. local obj = parser:get_object()
  42. local got = ucl.to_format(obj, 'json')
  43. endif
  44. local table = {
  45. str = 'value',
  46. num = 100500,
  47. null = ucl.null,
  48. func = function ()
  49. return 'huh'
  50. end
  51. }
  52. print(ucl.to_format(table, 'ucl'))
  53. -- Output:
  54. --[[
  55. num = 100500;
  56. str = "value";
  57. null = null;
  58. func = "huh";
  59. --]]
  60. */
  61. #define PARSER_META "ucl.parser.meta"
  62. #define EMITTER_META "ucl.emitter.meta"
  63. #define NULL_META "ucl.null.meta"
  64. #define OBJECT_META "ucl.object.meta"
  65. #define UCL_OBJECT_TYPE_META "ucl.type.object"
  66. #define UCL_ARRAY_TYPE_META "ucl.type.array"
  67. #define UCL_IMPL_ARRAY_TYPE_META "ucl.type.impl_array"
  68. static int ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj);
  69. static int ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj, bool allow_array);
  70. static ucl_object_t* ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags);
  71. static ucl_object_t* ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags);
  72. static void *ucl_null;
  73. /**
  74. * Push a single element of an object to lua
  75. * @param L
  76. * @param key
  77. * @param obj
  78. */
  79. static void
  80. ucl_object_lua_push_element (lua_State *L, const char *key,
  81. const ucl_object_t *obj)
  82. {
  83. lua_pushstring (L, key);
  84. ucl_object_push_lua (L, obj, true);
  85. lua_settable (L, -3);
  86. }
  87. static void
  88. lua_ucl_userdata_dtor (void *ud)
  89. {
  90. struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud;
  91. luaL_unref (fd->L, LUA_REGISTRYINDEX, fd->idx);
  92. if (fd->ret != NULL) {
  93. free (fd->ret);
  94. }
  95. free (fd);
  96. }
  97. static const char *
  98. lua_ucl_userdata_emitter (void *ud)
  99. {
  100. struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud;
  101. const char *out = "";
  102. lua_rawgeti (fd->L, LUA_REGISTRYINDEX, fd->idx);
  103. lua_pcall (fd->L, 0, 1, 0);
  104. out = lua_tostring (fd->L, -1);
  105. if (out != NULL) {
  106. /* We need to store temporary string in a more appropriate place */
  107. if (fd->ret) {
  108. free (fd->ret);
  109. }
  110. fd->ret = strdup (out);
  111. }
  112. lua_settop (fd->L, 0);
  113. return fd->ret;
  114. }
  115. /**
  116. * Push a single object to lua
  117. * @param L
  118. * @param obj
  119. * @return
  120. */
  121. static int
  122. ucl_object_lua_push_object (lua_State *L, const ucl_object_t *obj,
  123. bool allow_array)
  124. {
  125. const ucl_object_t *cur;
  126. ucl_object_iter_t it = NULL;
  127. if (allow_array && obj->next != NULL) {
  128. /* Actually we need to push this as an array */
  129. return ucl_object_lua_push_array (L, obj);
  130. }
  131. lua_createtable (L, 0, obj->len);
  132. it = NULL;
  133. while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) {
  134. ucl_object_lua_push_element (L, ucl_object_key (cur), cur);
  135. }
  136. luaL_getmetatable (L, UCL_OBJECT_TYPE_META);
  137. lua_setmetatable (L, -2);
  138. return 1;
  139. }
  140. /**
  141. * Push an array to lua as table indexed by integers
  142. * @param L
  143. * @param obj
  144. * @return
  145. */
  146. static int
  147. ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj)
  148. {
  149. const ucl_object_t *cur;
  150. ucl_object_iter_t it;
  151. int i = 1, nelt = 0;
  152. if (obj->type == UCL_ARRAY) {
  153. nelt = obj->len;
  154. it = ucl_object_iterate_new (obj);
  155. lua_createtable (L, nelt, 0);
  156. while ((cur = ucl_object_iterate_safe (it, true))) {
  157. ucl_object_push_lua (L, cur, false);
  158. lua_rawseti (L, -2, i);
  159. i ++;
  160. }
  161. luaL_getmetatable (L, UCL_ARRAY_TYPE_META);
  162. lua_setmetatable (L, -2);
  163. ucl_object_iterate_free (it);
  164. }
  165. else {
  166. /* Optimize allocation by preallocation of table */
  167. LL_FOREACH (obj, cur) {
  168. nelt ++;
  169. }
  170. lua_createtable (L, nelt, 0);
  171. LL_FOREACH (obj, cur) {
  172. ucl_object_push_lua (L, cur, false);
  173. lua_rawseti (L, -2, i);
  174. i ++;
  175. }
  176. luaL_getmetatable (L, UCL_IMPL_ARRAY_TYPE_META);
  177. lua_setmetatable (L, -2);
  178. }
  179. return 1;
  180. }
  181. /**
  182. * Push a simple object to lua depending on its actual type
  183. */
  184. static int
  185. ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj,
  186. bool allow_array)
  187. {
  188. struct ucl_lua_funcdata *fd;
  189. if (allow_array && obj->next != NULL) {
  190. /* Actually we need to push this as an array */
  191. return ucl_object_lua_push_array (L, obj);
  192. }
  193. switch (obj->type) {
  194. case UCL_BOOLEAN:
  195. lua_pushboolean (L, ucl_obj_toboolean (obj));
  196. break;
  197. case UCL_STRING:
  198. lua_pushstring (L, ucl_obj_tostring (obj));
  199. break;
  200. case UCL_INT:
  201. #if LUA_VERSION_NUM >= 501
  202. lua_pushinteger (L, ucl_obj_toint (obj));
  203. #else
  204. lua_pushnumber (L, ucl_obj_toint (obj));
  205. #endif
  206. break;
  207. case UCL_FLOAT:
  208. case UCL_TIME:
  209. lua_pushnumber (L, ucl_obj_todouble (obj));
  210. break;
  211. case UCL_NULL:
  212. lua_getfield (L, LUA_REGISTRYINDEX, "ucl.null");
  213. break;
  214. case UCL_USERDATA:
  215. fd = (struct ucl_lua_funcdata *)obj->value.ud;
  216. lua_rawgeti (L, LUA_REGISTRYINDEX, fd->idx);
  217. break;
  218. default:
  219. lua_pushnil (L);
  220. break;
  221. }
  222. return 1;
  223. }
  224. /***
  225. * @function ucl_object_push_lua(L, obj, allow_array)
  226. * This is a `C` function to push `UCL` object as lua variable. This function
  227. * converts `obj` to lua representation using the following conversions:
  228. *
  229. * - *scalar* values are directly presented by lua objects
  230. * - *userdata* values are converted to lua function objects using `LUA_REGISTRYINDEX`,
  231. * this can be used to pass functions from lua to c and vice-versa
  232. * - *arrays* are converted to lua tables with numeric indicies suitable for `ipairs` iterations
  233. * - *objects* are converted to lua tables with string indicies
  234. * @param {lua_State} L lua state pointer
  235. * @param {ucl_object_t} obj object to push
  236. * @param {bool} allow_array expand implicit arrays (should be true for all but partial arrays)
  237. * @return {int} `1` if an object is pushed to lua
  238. */
  239. int
  240. ucl_object_push_lua (lua_State *L, const ucl_object_t *obj, bool allow_array)
  241. {
  242. switch (obj->type) {
  243. case UCL_OBJECT:
  244. return ucl_object_lua_push_object (L, obj, allow_array);
  245. case UCL_ARRAY:
  246. return ucl_object_lua_push_array (L, obj);
  247. default:
  248. return ucl_object_lua_push_scalar (L, obj, allow_array);
  249. }
  250. }
  251. /**
  252. * Parse lua table into object top
  253. * @param L
  254. * @param top
  255. * @param idx
  256. */
  257. static ucl_object_t *
  258. ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags)
  259. {
  260. ucl_object_t *obj, *top = NULL, *cur;
  261. size_t keylen;
  262. const char *k;
  263. bool is_array = true, is_implicit = false, found_mt = false;
  264. size_t max = 0, nelts = 0;
  265. if (idx < 0) {
  266. /* For negative indicies we want to invert them */
  267. idx = lua_gettop (L) + idx + 1;
  268. }
  269. /* First, we check from metatable */
  270. if (luaL_getmetafield (L, idx, "class") != 0) {
  271. if (lua_type (L, -1) == LUA_TSTRING) {
  272. const char *classname = lua_tostring (L, -1);
  273. if (strcmp (classname, UCL_OBJECT_TYPE_META) == 0) {
  274. is_array = false;
  275. found_mt = true;
  276. } else if (strcmp (classname, UCL_ARRAY_TYPE_META) == 0) {
  277. is_array = true;
  278. found_mt = true;
  279. #if LUA_VERSION_NUM >= 502
  280. max = lua_rawlen (L, idx);
  281. #else
  282. max = lua_objlen (L, idx);
  283. #endif
  284. nelts = max;
  285. } else if (strcmp (classname, UCL_IMPL_ARRAY_TYPE_META) == 0) {
  286. is_array = true;
  287. is_implicit = true;
  288. found_mt = true;
  289. #if LUA_VERSION_NUM >= 502
  290. max = lua_rawlen (L, idx);
  291. #else
  292. max = lua_objlen (L, idx);
  293. #endif
  294. nelts = max;
  295. }
  296. }
  297. lua_pop (L, 1);
  298. }
  299. if (!found_mt) {
  300. /* Check for array */
  301. lua_pushnil (L);
  302. while (lua_next (L, idx) != 0) {
  303. if (lua_type (L, -2) == LUA_TNUMBER) {
  304. double num = lua_tonumber (L, -2);
  305. if (num == (int) num) {
  306. if (num > max) {
  307. max = num;
  308. }
  309. } else {
  310. /* Keys are not integer */
  311. is_array = false;
  312. }
  313. } else {
  314. /* Keys are not numeric */
  315. is_array = false;
  316. }
  317. lua_pop (L, 1);
  318. nelts ++;
  319. }
  320. }
  321. /* Table iterate */
  322. if (is_array) {
  323. int i;
  324. if (!is_implicit) {
  325. top = ucl_object_typed_new (UCL_ARRAY);
  326. ucl_object_reserve (top, nelts);
  327. }
  328. else {
  329. top = NULL;
  330. }
  331. for (i = 1; i <= max; i ++) {
  332. lua_pushinteger (L, i);
  333. lua_gettable (L, idx);
  334. obj = ucl_object_lua_fromelt (L, lua_gettop (L), flags);
  335. if (obj != NULL) {
  336. if (is_implicit) {
  337. DL_APPEND (top, obj);
  338. }
  339. else {
  340. ucl_array_append (top, obj);
  341. }
  342. }
  343. lua_pop (L, 1);
  344. }
  345. }
  346. else {
  347. lua_pushnil (L);
  348. top = ucl_object_typed_new (UCL_OBJECT);
  349. ucl_object_reserve (top, nelts);
  350. while (lua_next (L, idx) != 0) {
  351. /* copy key to avoid modifications */
  352. lua_pushvalue (L, -2);
  353. k = lua_tolstring (L, -1, &keylen);
  354. obj = ucl_object_lua_fromelt (L, lua_gettop (L) - 1, flags);
  355. if (obj != NULL) {
  356. ucl_object_insert_key (top, obj, k, keylen, true);
  357. DL_FOREACH (obj, cur) {
  358. if (cur->keylen == 0) {
  359. cur->keylen = obj->keylen;
  360. cur->key = obj->key;
  361. }
  362. }
  363. }
  364. lua_pop (L, 2);
  365. }
  366. }
  367. return top;
  368. }
  369. /**
  370. * Get a single element from lua to object obj
  371. * @param L
  372. * @param obj
  373. * @param idx
  374. */
  375. static ucl_object_t *
  376. ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags)
  377. {
  378. int type;
  379. double num;
  380. ucl_object_t *obj = NULL;
  381. struct ucl_lua_funcdata *fd;
  382. const char *str;
  383. size_t sz;
  384. type = lua_type (L, idx);
  385. switch (type) {
  386. case LUA_TSTRING:
  387. str = lua_tolstring (L, idx, &sz);
  388. if (str) {
  389. obj = ucl_object_fromstring_common (str, sz, flags);
  390. }
  391. else {
  392. obj = ucl_object_typed_new (UCL_NULL);
  393. }
  394. break;
  395. case LUA_TNUMBER:
  396. num = lua_tonumber (L, idx);
  397. if (num == (int64_t)num) {
  398. obj = ucl_object_fromint (num);
  399. }
  400. else {
  401. obj = ucl_object_fromdouble (num);
  402. }
  403. break;
  404. case LUA_TBOOLEAN:
  405. obj = ucl_object_frombool (lua_toboolean (L, idx));
  406. break;
  407. case LUA_TUSERDATA:
  408. if (lua_topointer (L, idx) == ucl_null) {
  409. obj = ucl_object_typed_new (UCL_NULL);
  410. }
  411. break;
  412. case LUA_TTABLE:
  413. case LUA_TFUNCTION:
  414. case LUA_TTHREAD:
  415. if (luaL_getmetafield (L, idx, "__gen_ucl")) {
  416. if (lua_isfunction (L, -1)) {
  417. lua_settop (L, 3); /* gen, obj, func */
  418. lua_insert (L, 1); /* func, gen, obj */
  419. lua_insert (L, 2); /* func, obj, gen */
  420. lua_call(L, 2, 1);
  421. obj = ucl_object_lua_fromelt (L, 1, flags);
  422. }
  423. lua_pop (L, 2);
  424. }
  425. else {
  426. if (type == LUA_TTABLE) {
  427. obj = ucl_object_lua_fromtable (L, idx, flags);
  428. }
  429. else if (type == LUA_TFUNCTION) {
  430. fd = malloc (sizeof (*fd));
  431. if (fd != NULL) {
  432. lua_pushvalue (L, idx);
  433. fd->L = L;
  434. fd->ret = NULL;
  435. fd->idx = luaL_ref (L, LUA_REGISTRYINDEX);
  436. obj = ucl_object_new_userdata (lua_ucl_userdata_dtor,
  437. lua_ucl_userdata_emitter, (void *)fd);
  438. }
  439. }
  440. }
  441. break;
  442. }
  443. return obj;
  444. }
  445. /**
  446. * @function ucl_object_lua_import(L, idx)
  447. * Extracts ucl object from lua variable at `idx` position,
  448. * @see ucl_object_push_lua for conversion definitions
  449. * @param {lua_state} L lua state machine pointer
  450. * @param {int} idx index where the source variable is placed
  451. * @return {ucl_object_t} new ucl object extracted from lua variable. Reference count of this object is 1,
  452. * this object thus needs to be unref'ed after usage.
  453. */
  454. ucl_object_t *
  455. ucl_object_lua_import (lua_State *L, int idx)
  456. {
  457. ucl_object_t *obj;
  458. int t;
  459. t = lua_type (L, idx);
  460. switch (t) {
  461. case LUA_TTABLE:
  462. obj = ucl_object_lua_fromtable (L, idx, 0);
  463. break;
  464. default:
  465. obj = ucl_object_lua_fromelt (L, idx, 0);
  466. break;
  467. }
  468. return obj;
  469. }
  470. /**
  471. * @function ucl_object_lua_import_escape(L, idx)
  472. * Extracts ucl object from lua variable at `idx` position escaping JSON strings
  473. * @see ucl_object_push_lua for conversion definitions
  474. * @param {lua_state} L lua state machine pointer
  475. * @param {int} idx index where the source variable is placed
  476. * @return {ucl_object_t} new ucl object extracted from lua variable. Reference count of this object is 1,
  477. * this object thus needs to be unref'ed after usage.
  478. */
  479. ucl_object_t *
  480. ucl_object_lua_import_escape (lua_State *L, int idx)
  481. {
  482. ucl_object_t *obj;
  483. int t;
  484. t = lua_type (L, idx);
  485. switch (t) {
  486. case LUA_TTABLE:
  487. obj = ucl_object_lua_fromtable (L, idx, UCL_STRING_RAW);
  488. break;
  489. default:
  490. obj = ucl_object_lua_fromelt (L, idx, UCL_STRING_RAW);
  491. break;
  492. }
  493. return obj;
  494. }
  495. static int
  496. lua_ucl_to_string (lua_State *L, const ucl_object_t *obj, enum ucl_emitter type)
  497. {
  498. unsigned char *result;
  499. result = ucl_object_emit (obj, type);
  500. if (result != NULL) {
  501. lua_pushstring (L, (const char *)result);
  502. free (result);
  503. }
  504. else {
  505. lua_pushnil (L);
  506. }
  507. return 1;
  508. }
  509. static int
  510. lua_ucl_parser_init (lua_State *L)
  511. {
  512. struct ucl_parser *parser, **pparser;
  513. int flags = UCL_PARSER_NO_FILEVARS;
  514. if (lua_gettop (L) >= 1) {
  515. flags = lua_tonumber (L, 1);
  516. }
  517. parser = ucl_parser_new (flags);
  518. if (parser == NULL) {
  519. lua_pushnil (L);
  520. }
  521. pparser = lua_newuserdata (L, sizeof (parser));
  522. *pparser = parser;
  523. luaL_getmetatable (L, PARSER_META);
  524. lua_setmetatable (L, -2);
  525. return 1;
  526. }
  527. static struct ucl_parser *
  528. lua_ucl_parser_get (lua_State *L, int index)
  529. {
  530. return *((struct ucl_parser **) luaL_checkudata(L, index, PARSER_META));
  531. }
  532. static ucl_object_t *
  533. lua_ucl_object_get (lua_State *L, int index)
  534. {
  535. return *((ucl_object_t **) luaL_checkudata(L, index, OBJECT_META));
  536. }
  537. static void
  538. lua_ucl_push_opaque (lua_State *L, ucl_object_t *obj)
  539. {
  540. ucl_object_t **pobj;
  541. pobj = lua_newuserdata (L, sizeof (*pobj));
  542. *pobj = obj;
  543. luaL_getmetatable (L, OBJECT_META);
  544. lua_setmetatable (L, -2);
  545. }
  546. static inline enum ucl_parse_type
  547. lua_ucl_str_to_parse_type (const char *str)
  548. {
  549. enum ucl_parse_type type = UCL_PARSE_UCL;
  550. if (str != NULL) {
  551. if (strcasecmp (str, "msgpack") == 0) {
  552. type = UCL_PARSE_MSGPACK;
  553. }
  554. else if (strcasecmp (str, "sexp") == 0 ||
  555. strcasecmp (str, "csexp") == 0) {
  556. type = UCL_PARSE_CSEXP;
  557. }
  558. else if (strcasecmp (str, "auto") == 0) {
  559. type = UCL_PARSE_AUTO;
  560. }
  561. }
  562. return type;
  563. }
  564. /***
  565. * @method parser:parse_file(name)
  566. * Parse UCL object from file.
  567. * @param {string} name filename to parse
  568. * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
  569. @example
  570. local parser = ucl.parser()
  571. local res,err = parser:parse_file('/some/file.conf')
  572. if not res then
  573. print('parser error: ' .. err)
  574. else
  575. -- Do something with object
  576. end
  577. */
  578. static int
  579. lua_ucl_parser_parse_file (lua_State *L)
  580. {
  581. struct ucl_parser *parser;
  582. const char *file;
  583. int ret = 2;
  584. parser = lua_ucl_parser_get (L, 1);
  585. file = luaL_checkstring (L, 2);
  586. if (parser != NULL && file != NULL) {
  587. if (ucl_parser_add_file (parser, file)) {
  588. lua_pushboolean (L, true);
  589. ret = 1;
  590. }
  591. else {
  592. lua_pushboolean (L, false);
  593. lua_pushstring (L, ucl_parser_get_error (parser));
  594. }
  595. }
  596. else {
  597. lua_pushboolean (L, false);
  598. lua_pushstring (L, "invalid arguments");
  599. }
  600. return ret;
  601. }
  602. /***
  603. * @method parser:register_variable(name, value)
  604. * Register parser variable
  605. * @param {string} name name of variable
  606. * @param {string} value value of variable
  607. * @return {bool} success
  608. @example
  609. local parser = ucl.parser()
  610. local res = parser:register_variable('CONFDIR', '/etc/foo')
  611. */
  612. static int
  613. lua_ucl_parser_register_variable (lua_State *L)
  614. {
  615. struct ucl_parser *parser;
  616. const char *name, *value;
  617. int ret = 2;
  618. parser = lua_ucl_parser_get (L, 1);
  619. name = luaL_checkstring (L, 2);
  620. value = luaL_checkstring (L, 3);
  621. if (parser != NULL && name != NULL && value != NULL) {
  622. ucl_parser_register_variable (parser, name, value);
  623. lua_pushboolean (L, true);
  624. ret = 1;
  625. }
  626. else {
  627. return luaL_error (L, "invalid arguments");
  628. }
  629. return ret;
  630. }
  631. /***
  632. * @method parser:register_variables(vars)
  633. * Register parser variables
  634. * @param {table} vars names/values of variables
  635. * @return {bool} success
  636. @example
  637. local parser = ucl.parser()
  638. local res = parser:register_variables({CONFDIR = '/etc/foo', VARDIR = '/var'})
  639. */
  640. static int
  641. lua_ucl_parser_register_variables (lua_State *L)
  642. {
  643. struct ucl_parser *parser;
  644. const char *name, *value;
  645. int ret = 2;
  646. parser = lua_ucl_parser_get (L, 1);
  647. if (parser != NULL && lua_type (L, 2) == LUA_TTABLE) {
  648. for (lua_pushnil (L); lua_next (L, 2); lua_pop (L, 1)) {
  649. lua_pushvalue (L, -2);
  650. name = luaL_checkstring (L, -1);
  651. value = luaL_checkstring (L, -2);
  652. ucl_parser_register_variable (parser, name, value);
  653. lua_pop (L, 1);
  654. }
  655. lua_pushboolean (L, true);
  656. ret = 1;
  657. }
  658. else {
  659. return luaL_error (L, "invalid arguments");
  660. }
  661. return ret;
  662. }
  663. /***
  664. * @method parser:parse_string(input)
  665. * Parse UCL object from file.
  666. * @param {string} input string to parse
  667. * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
  668. */
  669. static int
  670. lua_ucl_parser_parse_string (lua_State *L)
  671. {
  672. struct ucl_parser *parser;
  673. const char *string;
  674. size_t llen;
  675. enum ucl_parse_type type = UCL_PARSE_UCL;
  676. int ret = 2;
  677. parser = lua_ucl_parser_get (L, 1);
  678. string = luaL_checklstring (L, 2, &llen);
  679. if (lua_type (L, 3) == LUA_TSTRING) {
  680. type = lua_ucl_str_to_parse_type (lua_tostring (L, 3));
  681. }
  682. if (parser != NULL && string != NULL) {
  683. if (ucl_parser_add_chunk_full (parser, (const unsigned char *)string,
  684. llen, 0, UCL_DUPLICATE_APPEND, type)) {
  685. lua_pushboolean (L, true);
  686. ret = 1;
  687. }
  688. else {
  689. lua_pushboolean (L, false);
  690. lua_pushstring (L, ucl_parser_get_error (parser));
  691. }
  692. }
  693. else {
  694. lua_pushboolean (L, false);
  695. lua_pushstring (L, "invalid arguments");
  696. }
  697. return ret;
  698. }
  699. struct _rspamd_lua_text {
  700. const char *start;
  701. unsigned int len;
  702. unsigned int flags;
  703. };
  704. /***
  705. * @method parser:parse_text(input)
  706. * Parse UCL object from file.
  707. * @param {string} input string to parse
  708. * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
  709. */
  710. static int
  711. lua_ucl_parser_parse_text (lua_State *L)
  712. {
  713. struct ucl_parser *parser;
  714. struct _rspamd_lua_text *t;
  715. enum ucl_parse_type type = UCL_PARSE_UCL;
  716. int ret = 2;
  717. parser = lua_ucl_parser_get (L, 1);
  718. t = luaL_checkudata (L, 2, "rspamd{text}");
  719. if (lua_type (L, 3) == LUA_TSTRING) {
  720. type = lua_ucl_str_to_parse_type (lua_tostring (L, 3));
  721. }
  722. if (parser != NULL && t != NULL) {
  723. if (ucl_parser_add_chunk_full (parser, (const unsigned char *)t->start,
  724. t->len, 0, UCL_DUPLICATE_APPEND, type)) {
  725. lua_pushboolean (L, true);
  726. ret = 1;
  727. }
  728. else {
  729. lua_pushboolean (L, false);
  730. lua_pushstring (L, ucl_parser_get_error (parser));
  731. }
  732. }
  733. else {
  734. lua_pushboolean (L, false);
  735. lua_pushstring (L, "invalid arguments");
  736. }
  737. return ret;
  738. }
  739. /***
  740. * @method parser:get_object()
  741. * Get top object from parser and export it to lua representation.
  742. * @return {variant or nil} ucl object as lua native variable
  743. */
  744. static int
  745. lua_ucl_parser_get_object (lua_State *L)
  746. {
  747. struct ucl_parser *parser;
  748. ucl_object_t *obj;
  749. int ret = 1;
  750. parser = lua_ucl_parser_get (L, 1);
  751. obj = ucl_parser_get_object (parser);
  752. if (obj != NULL) {
  753. ret = ucl_object_push_lua (L, obj, false);
  754. /* no need to keep reference */
  755. ucl_object_unref (obj);
  756. }
  757. else {
  758. lua_pushnil (L);
  759. }
  760. return ret;
  761. }
  762. /***
  763. * @method parser:get_object_wrapped()
  764. * Get top object from parser and export it to userdata object without
  765. * unwrapping to lua.
  766. * @return {ucl.object or nil} ucl object wrapped variable
  767. */
  768. static int
  769. lua_ucl_parser_get_object_wrapped (lua_State *L)
  770. {
  771. struct ucl_parser *parser;
  772. ucl_object_t *obj;
  773. int ret = 1;
  774. parser = lua_ucl_parser_get (L, 1);
  775. obj = ucl_parser_get_object (parser);
  776. if (obj != NULL) {
  777. lua_ucl_push_opaque (L, obj);
  778. }
  779. else {
  780. lua_pushnil (L);
  781. }
  782. return ret;
  783. }
  784. /***
  785. * @method parser:validate(schema)
  786. * Validates the top object in the parser against schema. Schema might be
  787. * another object or a string that represents file to load schema from.
  788. *
  789. * @param {string/table} schema input schema
  790. * @return {result,err} two values: boolean result and the corresponding error
  791. *
  792. */
  793. static int
  794. lua_ucl_parser_validate (lua_State *L)
  795. {
  796. struct ucl_parser *parser, *schema_parser;
  797. ucl_object_t *schema;
  798. const char *schema_file;
  799. struct ucl_schema_error err;
  800. parser = lua_ucl_parser_get (L, 1);
  801. if (parser && parser->top_obj) {
  802. if (lua_type (L, 2) == LUA_TTABLE) {
  803. schema = ucl_object_lua_import (L, 2);
  804. if (schema == NULL) {
  805. lua_pushboolean (L, false);
  806. lua_pushstring (L, "cannot load schema from lua table");
  807. return 2;
  808. }
  809. }
  810. else if (lua_type (L, 2) == LUA_TSTRING) {
  811. schema_parser = ucl_parser_new (0);
  812. schema_file = luaL_checkstring (L, 2);
  813. if (!ucl_parser_add_file (schema_parser, schema_file)) {
  814. lua_pushboolean (L, false);
  815. lua_pushfstring (L, "cannot parse schema file \"%s\": "
  816. "%s", schema_file, ucl_parser_get_error (parser));
  817. ucl_parser_free (schema_parser);
  818. return 2;
  819. }
  820. schema = ucl_parser_get_object (schema_parser);
  821. ucl_parser_free (schema_parser);
  822. }
  823. else {
  824. lua_pushboolean (L, false);
  825. lua_pushstring (L, "invalid schema argument");
  826. return 2;
  827. }
  828. if (!ucl_object_validate (schema, parser->top_obj, &err)) {
  829. lua_pushboolean (L, false);
  830. lua_pushfstring (L, "validation error: "
  831. "%s", err.msg);
  832. }
  833. else {
  834. lua_pushboolean (L, true);
  835. lua_pushnil (L);
  836. }
  837. ucl_object_unref (schema);
  838. }
  839. else {
  840. lua_pushboolean (L, false);
  841. lua_pushstring (L, "invalid parser or empty top object");
  842. }
  843. return 2;
  844. }
  845. static int
  846. lua_ucl_parser_gc (lua_State *L)
  847. {
  848. struct ucl_parser *parser;
  849. parser = lua_ucl_parser_get (L, 1);
  850. ucl_parser_free (parser);
  851. return 0;
  852. }
  853. /***
  854. * @method object:unwrap()
  855. * Unwraps opaque ucl object to the native lua object (performing copying)
  856. * @return {variant} any lua object
  857. */
  858. static int
  859. lua_ucl_object_unwrap (lua_State *L)
  860. {
  861. ucl_object_t *obj;
  862. obj = lua_ucl_object_get (L, 1);
  863. if (obj) {
  864. ucl_object_push_lua (L, obj, true);
  865. }
  866. else {
  867. lua_pushnil (L);
  868. }
  869. return 1;
  870. }
  871. static inline enum ucl_emitter
  872. lua_ucl_str_to_emit_type (const char *strtype)
  873. {
  874. enum ucl_emitter format = UCL_EMIT_JSON_COMPACT;
  875. if (strcasecmp (strtype, "json") == 0) {
  876. format = UCL_EMIT_JSON;
  877. }
  878. else if (strcasecmp (strtype, "json-compact") == 0) {
  879. format = UCL_EMIT_JSON_COMPACT;
  880. }
  881. else if (strcasecmp (strtype, "yaml") == 0) {
  882. format = UCL_EMIT_YAML;
  883. }
  884. else if (strcasecmp (strtype, "config") == 0 ||
  885. strcasecmp (strtype, "ucl") == 0) {
  886. format = UCL_EMIT_CONFIG;
  887. }
  888. return format;
  889. }
  890. /***
  891. * @method object:tostring(type)
  892. * Unwraps opaque ucl object to string (json by default). Optionally you can
  893. * specify output format:
  894. *
  895. * - `json` - fine printed json
  896. * - `json-compact` - compacted json
  897. * - `config` - fine printed configuration
  898. * - `ucl` - same as `config`
  899. * - `yaml` - embedded yaml
  900. * @param {string} type optional
  901. * @return {string} string representation of the opaque ucl object
  902. */
  903. static int
  904. lua_ucl_object_tostring (lua_State *L)
  905. {
  906. ucl_object_t *obj;
  907. enum ucl_emitter format = UCL_EMIT_JSON_COMPACT;
  908. obj = lua_ucl_object_get (L, 1);
  909. if (obj) {
  910. if (lua_gettop (L) > 1) {
  911. if (lua_type (L, 2) == LUA_TSTRING) {
  912. const char *strtype = lua_tostring (L, 2);
  913. format = lua_ucl_str_to_emit_type (strtype);
  914. }
  915. }
  916. return lua_ucl_to_string (L, obj, format);
  917. }
  918. else {
  919. lua_pushnil (L);
  920. }
  921. return 1;
  922. }
  923. /***
  924. * @method object:validate(schema[, path[, ext_refs]])
  925. * Validates the given ucl object using schema object represented as another
  926. * opaque ucl object. You can also specify path in the form `#/path/def` to
  927. * specify the specific schema element to perform validation.
  928. *
  929. * @param {ucl.object} schema schema object
  930. * @param {string} path optional path for validation procedure
  931. * @return {result,err} two values: boolean result and the corresponding
  932. * error, if `ext_refs` are also specified, then they are returned as opaque
  933. * ucl object as {result,err,ext_refs}
  934. */
  935. static int
  936. lua_ucl_object_validate (lua_State *L)
  937. {
  938. ucl_object_t *obj, *schema, *ext_refs = NULL;
  939. const ucl_object_t *schema_elt;
  940. bool res = false;
  941. struct ucl_schema_error err;
  942. const char *path = NULL;
  943. obj = lua_ucl_object_get (L, 1);
  944. schema = lua_ucl_object_get (L, 2);
  945. if (schema && obj && ucl_object_type (schema) == UCL_OBJECT) {
  946. if (lua_gettop (L) > 2) {
  947. if (lua_type (L, 3) == LUA_TSTRING) {
  948. path = lua_tostring (L, 3);
  949. if (path[0] == '#') {
  950. path++;
  951. }
  952. }
  953. else if (lua_type (L, 3) == LUA_TUSERDATA || lua_type (L, 3) ==
  954. LUA_TTABLE) {
  955. /* External refs */
  956. ext_refs = lua_ucl_object_get (L, 3);
  957. }
  958. if (lua_gettop (L) > 3) {
  959. if (lua_type (L, 4) == LUA_TUSERDATA || lua_type (L, 4) ==
  960. LUA_TTABLE) {
  961. /* External refs */
  962. ext_refs = lua_ucl_object_get (L, 4);
  963. }
  964. }
  965. }
  966. if (path) {
  967. schema_elt = ucl_object_lookup_path_char (schema, path, '/');
  968. }
  969. else {
  970. /* Use the top object */
  971. schema_elt = schema;
  972. }
  973. if (schema_elt) {
  974. res = ucl_object_validate_root_ext (schema_elt, obj, schema,
  975. ext_refs, &err);
  976. if (res) {
  977. lua_pushboolean (L, res);
  978. lua_pushnil (L);
  979. if (ext_refs) {
  980. lua_ucl_push_opaque (L, ext_refs);
  981. }
  982. }
  983. else {
  984. lua_pushboolean (L, res);
  985. lua_pushfstring (L, "validation error: %s", err.msg);
  986. if (ext_refs) {
  987. lua_ucl_push_opaque (L, ext_refs);
  988. }
  989. }
  990. }
  991. else {
  992. lua_pushboolean (L, res);
  993. lua_pushfstring (L, "cannot find the requested path: %s", path);
  994. if (ext_refs) {
  995. lua_ucl_push_opaque (L, ext_refs);
  996. }
  997. }
  998. }
  999. else {
  1000. lua_pushboolean (L, res);
  1001. lua_pushstring (L, "invalid object or schema");
  1002. }
  1003. if (ext_refs) {
  1004. return 3;
  1005. }
  1006. return 2;
  1007. }
  1008. static int
  1009. lua_ucl_object_gc (lua_State *L)
  1010. {
  1011. ucl_object_t *obj;
  1012. obj = lua_ucl_object_get (L, 1);
  1013. ucl_object_unref (obj);
  1014. return 0;
  1015. }
  1016. static void
  1017. lua_ucl_parser_mt (lua_State *L)
  1018. {
  1019. luaL_newmetatable (L, PARSER_META);
  1020. lua_pushvalue(L, -1);
  1021. lua_setfield(L, -2, "__index");
  1022. lua_pushcfunction (L, lua_ucl_parser_gc);
  1023. lua_setfield (L, -2, "__gc");
  1024. lua_pushcfunction (L, lua_ucl_parser_parse_file);
  1025. lua_setfield (L, -2, "parse_file");
  1026. lua_pushcfunction (L, lua_ucl_parser_parse_string);
  1027. lua_setfield (L, -2, "parse_string");
  1028. lua_pushcfunction (L, lua_ucl_parser_parse_text);
  1029. lua_setfield (L, -2, "parse_text");
  1030. lua_pushcfunction (L, lua_ucl_parser_register_variable);
  1031. lua_setfield (L, -2, "register_variable");
  1032. lua_pushcfunction (L, lua_ucl_parser_register_variables);
  1033. lua_setfield (L, -2, "register_variables");
  1034. lua_pushcfunction (L, lua_ucl_parser_get_object);
  1035. lua_setfield (L, -2, "get_object");
  1036. lua_pushcfunction (L, lua_ucl_parser_get_object_wrapped);
  1037. lua_setfield (L, -2, "get_object_wrapped");
  1038. lua_pushcfunction (L, lua_ucl_parser_validate);
  1039. lua_setfield (L, -2, "validate");
  1040. lua_pop (L, 1);
  1041. }
  1042. static void
  1043. lua_ucl_object_mt (lua_State *L)
  1044. {
  1045. luaL_newmetatable (L, OBJECT_META);
  1046. lua_pushvalue(L, -1);
  1047. lua_setfield(L, -2, "__index");
  1048. lua_pushcfunction (L, lua_ucl_object_gc);
  1049. lua_setfield (L, -2, "__gc");
  1050. lua_pushcfunction (L, lua_ucl_object_tostring);
  1051. lua_setfield (L, -2, "__tostring");
  1052. lua_pushcfunction (L, lua_ucl_object_tostring);
  1053. lua_setfield (L, -2, "tostring");
  1054. lua_pushcfunction (L, lua_ucl_object_unwrap);
  1055. lua_setfield (L, -2, "unwrap");
  1056. lua_pushcfunction (L, lua_ucl_object_unwrap);
  1057. lua_setfield (L, -2, "tolua");
  1058. lua_pushcfunction (L, lua_ucl_object_validate);
  1059. lua_setfield (L, -2, "validate");
  1060. lua_pushstring (L, OBJECT_META);
  1061. lua_setfield (L, -2, "class");
  1062. lua_pop (L, 1);
  1063. }
  1064. static void
  1065. lua_ucl_types_mt (lua_State *L)
  1066. {
  1067. luaL_newmetatable (L, UCL_OBJECT_TYPE_META);
  1068. lua_pushcfunction (L, lua_ucl_object_tostring);
  1069. lua_setfield (L, -2, "__tostring");
  1070. lua_pushcfunction (L, lua_ucl_object_tostring);
  1071. lua_setfield (L, -2, "tostring");
  1072. lua_pushstring (L, UCL_OBJECT_TYPE_META);
  1073. lua_setfield (L, -2, "class");
  1074. lua_pop (L, 1);
  1075. luaL_newmetatable (L, UCL_ARRAY_TYPE_META);
  1076. lua_pushcfunction (L, lua_ucl_object_tostring);
  1077. lua_setfield (L, -2, "__tostring");
  1078. lua_pushcfunction (L, lua_ucl_object_tostring);
  1079. lua_setfield (L, -2, "tostring");
  1080. lua_pushstring (L, UCL_ARRAY_TYPE_META);
  1081. lua_setfield (L, -2, "class");
  1082. lua_pop (L, 1);
  1083. luaL_newmetatable (L, UCL_IMPL_ARRAY_TYPE_META);
  1084. lua_pushcfunction (L, lua_ucl_object_tostring);
  1085. lua_setfield (L, -2, "__tostring");
  1086. lua_pushcfunction (L, lua_ucl_object_tostring);
  1087. lua_setfield (L, -2, "tostring");
  1088. lua_pushstring (L, UCL_IMPL_ARRAY_TYPE_META);
  1089. lua_setfield (L, -2, "class");
  1090. lua_pop (L, 1);
  1091. }
  1092. static int
  1093. lua_ucl_to_json (lua_State *L)
  1094. {
  1095. ucl_object_t *obj;
  1096. int format = UCL_EMIT_JSON;
  1097. if (lua_gettop (L) > 1) {
  1098. if (lua_toboolean (L, 2)) {
  1099. format = UCL_EMIT_JSON_COMPACT;
  1100. }
  1101. }
  1102. obj = ucl_object_lua_import (L, 1);
  1103. if (obj != NULL) {
  1104. lua_ucl_to_string (L, obj, format);
  1105. ucl_object_unref (obj);
  1106. }
  1107. else {
  1108. lua_pushnil (L);
  1109. }
  1110. return 1;
  1111. }
  1112. static int
  1113. lua_ucl_to_config (lua_State *L)
  1114. {
  1115. ucl_object_t *obj;
  1116. obj = ucl_object_lua_import (L, 1);
  1117. if (obj != NULL) {
  1118. lua_ucl_to_string (L, obj, UCL_EMIT_CONFIG);
  1119. ucl_object_unref (obj);
  1120. }
  1121. else {
  1122. lua_pushnil (L);
  1123. }
  1124. return 1;
  1125. }
  1126. /***
  1127. * @function ucl.to_format(var, format)
  1128. * Converts lua variable `var` to the specified `format`. Formats supported are:
  1129. *
  1130. * - `json` - fine printed json
  1131. * - `json-compact` - compacted json
  1132. * - `config` - fine printed configuration
  1133. * - `ucl` - same as `config`
  1134. * - `yaml` - embedded yaml
  1135. *
  1136. * If `var` contains function, they are called during output formatting and if
  1137. * they return string value, then this value is used for output.
  1138. * @param {variant} var any sort of lua variable (if userdata then metafield `__to_ucl` is searched for output)
  1139. * @param {string} format any available format
  1140. * @return {string} string representation of `var` in the specific `format`.
  1141. * @example
  1142. local table = {
  1143. str = 'value',
  1144. num = 100500,
  1145. null = ucl.null,
  1146. func = function ()
  1147. return 'huh'
  1148. end
  1149. }
  1150. print(ucl.to_format(table, 'ucl'))
  1151. -- Output:
  1152. --[[
  1153. num = 100500;
  1154. str = "value";
  1155. null = null;
  1156. func = "huh";
  1157. --]]
  1158. */
  1159. static int
  1160. lua_ucl_to_format (lua_State *L)
  1161. {
  1162. ucl_object_t *obj;
  1163. int format = UCL_EMIT_JSON;
  1164. bool sort = false;
  1165. if (lua_gettop (L) > 1) {
  1166. if (lua_type (L, 2) == LUA_TNUMBER) {
  1167. format = lua_tonumber (L, 2);
  1168. if (format < 0 || format >= UCL_EMIT_YAML) {
  1169. lua_pushnil (L);
  1170. return 1;
  1171. }
  1172. }
  1173. else if (lua_type (L, 2) == LUA_TSTRING) {
  1174. const char *strtype = lua_tostring (L, 2);
  1175. if (strcasecmp (strtype, "json") == 0) {
  1176. format = UCL_EMIT_JSON;
  1177. }
  1178. else if (strcasecmp (strtype, "json-compact") == 0) {
  1179. format = UCL_EMIT_JSON_COMPACT;
  1180. }
  1181. else if (strcasecmp (strtype, "yaml") == 0) {
  1182. format = UCL_EMIT_YAML;
  1183. }
  1184. else if (strcasecmp (strtype, "config") == 0 ||
  1185. strcasecmp (strtype, "ucl") == 0) {
  1186. format = UCL_EMIT_CONFIG;
  1187. }
  1188. else if (strcasecmp (strtype, "msgpack") == 0) {
  1189. format = UCL_EMIT_MSGPACK;
  1190. }
  1191. }
  1192. if (lua_isboolean (L, 3)) {
  1193. sort = lua_toboolean (L, 3);
  1194. }
  1195. }
  1196. obj = ucl_object_lua_import (L, 1);
  1197. if (obj != NULL) {
  1198. if (sort) {
  1199. if (ucl_object_type (obj) == UCL_OBJECT) {
  1200. ucl_object_sort_keys (obj, UCL_SORT_KEYS_RECURSIVE);
  1201. }
  1202. }
  1203. lua_ucl_to_string (L, obj, format);
  1204. ucl_object_unref (obj);
  1205. }
  1206. else {
  1207. lua_pushnil (L);
  1208. }
  1209. return 1;
  1210. }
  1211. static int
  1212. lua_ucl_null_tostring (lua_State* L)
  1213. {
  1214. lua_pushstring (L, "null");
  1215. return 1;
  1216. }
  1217. static void
  1218. lua_ucl_null_mt (lua_State *L)
  1219. {
  1220. luaL_newmetatable (L, NULL_META);
  1221. lua_pushcfunction (L, lua_ucl_null_tostring);
  1222. lua_setfield (L, -2, "__tostring");
  1223. lua_pop (L, 1);
  1224. }
  1225. int
  1226. luaopen_ucl (lua_State *L)
  1227. {
  1228. lua_ucl_parser_mt (L);
  1229. lua_ucl_null_mt (L);
  1230. lua_ucl_object_mt (L);
  1231. lua_ucl_types_mt (L);
  1232. /* Create the refs weak table: */
  1233. lua_createtable (L, 0, 2);
  1234. lua_pushliteral (L, "v"); /* tbl, "v" */
  1235. lua_setfield (L, -2, "__mode");
  1236. lua_pushvalue (L, -1); /* tbl, tbl */
  1237. lua_setmetatable (L, -2); /* tbl */
  1238. lua_setfield (L, LUA_REGISTRYINDEX, "ucl.refs");
  1239. lua_newtable (L);
  1240. lua_pushcfunction (L, lua_ucl_parser_init);
  1241. lua_setfield (L, -2, "parser");
  1242. lua_pushcfunction (L, lua_ucl_to_json);
  1243. lua_setfield (L, -2, "to_json");
  1244. lua_pushcfunction (L, lua_ucl_to_config);
  1245. lua_setfield (L, -2, "to_config");
  1246. lua_pushcfunction (L, lua_ucl_to_format);
  1247. lua_setfield (L, -2, "to_format");
  1248. ucl_null = lua_newuserdata (L, 0);
  1249. luaL_getmetatable (L, NULL_META);
  1250. lua_setmetatable (L, -2);
  1251. lua_pushvalue (L, -1);
  1252. lua_setfield (L, LUA_REGISTRYINDEX, "ucl.null");
  1253. lua_setfield (L, -2, "null");
  1254. return 1;
  1255. }
  1256. struct ucl_lua_funcdata*
  1257. ucl_object_toclosure (const ucl_object_t *obj)
  1258. {
  1259. if (obj == NULL || obj->type != UCL_USERDATA) {
  1260. return NULL;
  1261. }
  1262. return (struct ucl_lua_funcdata*)obj->value.ud;
  1263. }