You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ucl_parser.c 69KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184
  1. /* Copyright (c) 2013, 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. #include <math.h>
  24. #include "ucl.h"
  25. #include "ucl_internal.h"
  26. #include "ucl_chartable.h"
  27. /**
  28. * @file ucl_parser.c
  29. * The implementation of ucl parser
  30. */
  31. struct ucl_parser_saved_state {
  32. unsigned int line;
  33. unsigned int column;
  34. size_t remain;
  35. const unsigned char *pos;
  36. };
  37. /**
  38. * Move up to len characters
  39. * @param parser
  40. * @param begin
  41. * @param len
  42. * @return new position in chunk
  43. */
  44. #define ucl_chunk_skipc(chunk, p) \
  45. do { \
  46. if (p == chunk->end) { \
  47. break; \
  48. } \
  49. if (*(p) == '\n') { \
  50. (chunk)->line ++; \
  51. (chunk)->column = 0; \
  52. } \
  53. else (chunk)->column ++; \
  54. (p++); \
  55. (chunk)->pos ++; \
  56. (chunk)->remain --; \
  57. } while (0)
  58. static inline void
  59. ucl_set_err (struct ucl_parser *parser, int code, const char *str, UT_string **err)
  60. {
  61. const char *fmt_string, *filename;
  62. struct ucl_chunk *chunk = parser->chunks;
  63. if (parser->cur_file) {
  64. filename = parser->cur_file;
  65. }
  66. else {
  67. filename = "<unknown>";
  68. }
  69. if (chunk->pos < chunk->end) {
  70. if (isgraph (*chunk->pos)) {
  71. fmt_string = "error while parsing %s: "
  72. "line: %d, column: %d - '%s', character: '%c'";
  73. }
  74. else {
  75. fmt_string = "error while parsing %s: "
  76. "line: %d, column: %d - '%s', character: '0x%02x'";
  77. }
  78. ucl_create_err (err, fmt_string,
  79. filename, chunk->line, chunk->column,
  80. str, *chunk->pos);
  81. }
  82. else {
  83. ucl_create_err (err, "error while parsing %s: at the end of chunk: %s",
  84. filename, str);
  85. }
  86. parser->err_code = code;
  87. parser->state = UCL_STATE_ERROR;
  88. }
  89. static void
  90. ucl_save_comment (struct ucl_parser *parser, const char *begin, size_t len)
  91. {
  92. ucl_object_t *nobj;
  93. if (len > 0 && begin != NULL) {
  94. nobj = ucl_object_fromstring_common (begin, len, 0);
  95. if (parser->last_comment) {
  96. /* We need to append data to an existing object */
  97. DL_APPEND (parser->last_comment, nobj);
  98. }
  99. else {
  100. parser->last_comment = nobj;
  101. }
  102. }
  103. }
  104. static void
  105. ucl_attach_comment (struct ucl_parser *parser, ucl_object_t *obj, bool before)
  106. {
  107. if (parser->last_comment) {
  108. ucl_object_insert_key (parser->comments, parser->last_comment,
  109. (const char *)&obj, sizeof (void *), true);
  110. if (before) {
  111. parser->last_comment->flags |= UCL_OBJECT_INHERITED;
  112. }
  113. parser->last_comment = NULL;
  114. }
  115. }
  116. /**
  117. * Skip all comments from the current pos resolving nested and multiline comments
  118. * @param parser
  119. * @return
  120. */
  121. static bool
  122. ucl_skip_comments (struct ucl_parser *parser)
  123. {
  124. struct ucl_chunk *chunk = parser->chunks;
  125. const unsigned char *p, *beg = NULL;
  126. int comments_nested = 0;
  127. bool quoted = false;
  128. p = chunk->pos;
  129. start:
  130. if (chunk->remain > 0 && *p == '#') {
  131. if (parser->state != UCL_STATE_SCOMMENT &&
  132. parser->state != UCL_STATE_MCOMMENT) {
  133. beg = p;
  134. while (p < chunk->end) {
  135. if (*p == '\n') {
  136. if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
  137. ucl_save_comment (parser, beg, p - beg);
  138. beg = NULL;
  139. }
  140. ucl_chunk_skipc (chunk, p);
  141. goto start;
  142. }
  143. ucl_chunk_skipc (chunk, p);
  144. }
  145. }
  146. }
  147. else if (chunk->remain >= 2 && *p == '/') {
  148. if (p[1] == '*') {
  149. beg = p;
  150. ucl_chunk_skipc (chunk, p);
  151. comments_nested ++;
  152. ucl_chunk_skipc (chunk, p);
  153. while (p < chunk->end) {
  154. if (*p == '"' && *(p - 1) != '\\') {
  155. quoted = !quoted;
  156. }
  157. if (!quoted) {
  158. if (*p == '*') {
  159. ucl_chunk_skipc (chunk, p);
  160. if (*p == '/') {
  161. comments_nested --;
  162. if (comments_nested == 0) {
  163. if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
  164. ucl_save_comment (parser, beg, p - beg + 1);
  165. beg = NULL;
  166. }
  167. ucl_chunk_skipc (chunk, p);
  168. goto start;
  169. }
  170. }
  171. ucl_chunk_skipc (chunk, p);
  172. }
  173. else if (p[0] == '/' && chunk->remain >= 2 && p[1] == '*') {
  174. comments_nested ++;
  175. ucl_chunk_skipc (chunk, p);
  176. ucl_chunk_skipc (chunk, p);
  177. continue;
  178. }
  179. }
  180. ucl_chunk_skipc (chunk, p);
  181. }
  182. if (comments_nested != 0) {
  183. ucl_set_err (parser, UCL_ENESTED,
  184. "unfinished multiline comment", &parser->err);
  185. return false;
  186. }
  187. }
  188. }
  189. if (beg && p > beg && (parser->flags & UCL_PARSER_SAVE_COMMENTS)) {
  190. ucl_save_comment (parser, beg, p - beg);
  191. }
  192. return true;
  193. }
  194. /**
  195. * Return multiplier for a character
  196. * @param c multiplier character
  197. * @param is_bytes if true use 1024 multiplier
  198. * @return multiplier
  199. */
  200. static inline unsigned long
  201. ucl_lex_num_multiplier (const unsigned char c, bool is_bytes) {
  202. const struct {
  203. char c;
  204. long mult_normal;
  205. long mult_bytes;
  206. } multipliers[] = {
  207. {'m', 1000 * 1000, 1024 * 1024},
  208. {'k', 1000, 1024},
  209. {'g', 1000 * 1000 * 1000, 1024 * 1024 * 1024}
  210. };
  211. int i;
  212. for (i = 0; i < 3; i ++) {
  213. if (tolower (c) == multipliers[i].c) {
  214. if (is_bytes) {
  215. return multipliers[i].mult_bytes;
  216. }
  217. return multipliers[i].mult_normal;
  218. }
  219. }
  220. return 1;
  221. }
  222. /**
  223. * Return multiplier for time scaling
  224. * @param c
  225. * @return
  226. */
  227. static inline double
  228. ucl_lex_time_multiplier (const unsigned char c) {
  229. const struct {
  230. char c;
  231. double mult;
  232. } multipliers[] = {
  233. {'m', 60},
  234. {'h', 60 * 60},
  235. {'d', 60 * 60 * 24},
  236. {'w', 60 * 60 * 24 * 7},
  237. {'y', 60 * 60 * 24 * 365}
  238. };
  239. int i;
  240. for (i = 0; i < 5; i ++) {
  241. if (tolower (c) == multipliers[i].c) {
  242. return multipliers[i].mult;
  243. }
  244. }
  245. return 1;
  246. }
  247. /**
  248. * Return true if a character is a end of an atom
  249. * @param c
  250. * @return
  251. */
  252. static inline bool
  253. ucl_lex_is_atom_end (const unsigned char c)
  254. {
  255. return ucl_test_character (c, UCL_CHARACTER_VALUE_END);
  256. }
  257. static inline bool
  258. ucl_lex_is_comment (const unsigned char c1, const unsigned char c2)
  259. {
  260. if (c1 == '/') {
  261. if (c2 == '*') {
  262. return true;
  263. }
  264. }
  265. else if (c1 == '#') {
  266. return true;
  267. }
  268. return false;
  269. }
  270. /**
  271. * Check variable found
  272. * @param parser
  273. * @param ptr
  274. * @param remain
  275. * @param out_len
  276. * @param strict
  277. * @param found
  278. * @return
  279. */
  280. static inline const char *
  281. ucl_check_variable_safe (struct ucl_parser *parser, const char *ptr, size_t remain,
  282. size_t *out_len, bool strict, bool *found)
  283. {
  284. struct ucl_variable *var;
  285. unsigned char *dst;
  286. size_t dstlen;
  287. bool need_free = false;
  288. LL_FOREACH (parser->variables, var) {
  289. if (strict) {
  290. if (remain == var->var_len) {
  291. if (memcmp (ptr, var->var, var->var_len) == 0) {
  292. *out_len += var->value_len;
  293. *found = true;
  294. return (ptr + var->var_len);
  295. }
  296. }
  297. }
  298. else {
  299. if (remain >= var->var_len) {
  300. if (memcmp (ptr, var->var, var->var_len) == 0) {
  301. *out_len += var->value_len;
  302. *found = true;
  303. return (ptr + var->var_len);
  304. }
  305. }
  306. }
  307. }
  308. /* XXX: can only handle ${VAR} */
  309. if (!(*found) && parser->var_handler != NULL && strict) {
  310. /* Call generic handler */
  311. if (parser->var_handler (ptr, remain, &dst, &dstlen, &need_free,
  312. parser->var_data)) {
  313. *found = true;
  314. if (need_free) {
  315. free (dst);
  316. }
  317. return (ptr + remain);
  318. }
  319. }
  320. return ptr;
  321. }
  322. /**
  323. * Check for a variable in a given string
  324. * @param parser
  325. * @param ptr
  326. * @param remain
  327. * @param out_len
  328. * @param vars_found
  329. * @return
  330. */
  331. static const char *
  332. ucl_check_variable (struct ucl_parser *parser, const char *ptr,
  333. size_t remain, size_t *out_len, bool *vars_found)
  334. {
  335. const char *p, *end, *ret = ptr;
  336. bool found = false;
  337. if (*ptr == '{') {
  338. /* We need to match the variable enclosed in braces */
  339. p = ptr + 1;
  340. end = ptr + remain;
  341. while (p < end) {
  342. if (*p == '}') {
  343. ret = ucl_check_variable_safe (parser, ptr + 1, p - ptr - 1,
  344. out_len, true, &found);
  345. if (found) {
  346. /* {} must be excluded actually */
  347. ret ++;
  348. if (!*vars_found) {
  349. *vars_found = true;
  350. }
  351. }
  352. else {
  353. *out_len += 2;
  354. }
  355. break;
  356. }
  357. p ++;
  358. }
  359. if(p == end) {
  360. (*out_len) ++;
  361. }
  362. }
  363. else if (*ptr != '$') {
  364. /* Not count escaped dollar sign */
  365. ret = ucl_check_variable_safe (parser, ptr, remain, out_len, false, &found);
  366. if (found && !*vars_found) {
  367. *vars_found = true;
  368. }
  369. if (!found) {
  370. (*out_len) ++;
  371. }
  372. }
  373. else {
  374. ret ++;
  375. (*out_len) ++;
  376. }
  377. return ret;
  378. }
  379. /**
  380. * Expand a single variable
  381. * @param parser
  382. * @param ptr
  383. * @param in_len
  384. * @param dest
  385. * @param out_len
  386. * @return
  387. */
  388. static const char *
  389. ucl_expand_single_variable (struct ucl_parser *parser, const char *ptr,
  390. size_t in_len, unsigned char **dest, size_t out_len)
  391. {
  392. unsigned char *d = *dest, *dst;
  393. const char *p = ptr + 1, *ret;
  394. struct ucl_variable *var;
  395. size_t dstlen;
  396. bool need_free = false;
  397. bool found = false;
  398. bool strict = false;
  399. ret = ptr + 1;
  400. /* For the $ sign */
  401. in_len --;
  402. if (*p == '$') {
  403. *d++ = *p++;
  404. *dest = d;
  405. return p;
  406. }
  407. else if (*p == '{') {
  408. p ++;
  409. in_len --;
  410. strict = true;
  411. ret += 2;
  412. }
  413. LL_FOREACH (parser->variables, var) {
  414. if (out_len >= var->value_len && in_len >= (var->var_len + (strict ? 1 : 0))) {
  415. if (memcmp (p, var->var, var->var_len) == 0) {
  416. if (!strict || p[var->var_len] == '}') {
  417. memcpy (d, var->value, var->value_len);
  418. ret += var->var_len;
  419. d += var->value_len;
  420. found = true;
  421. break;
  422. }
  423. }
  424. }
  425. }
  426. if (!found) {
  427. if (strict && parser->var_handler != NULL) {
  428. dstlen = out_len;
  429. if (parser->var_handler (p, in_len, &dst, &dstlen, &need_free,
  430. parser->var_data)) {
  431. if (dstlen > out_len) {
  432. /* We do not have enough space! */
  433. if (need_free) {
  434. free (dst);
  435. }
  436. }
  437. else {
  438. memcpy(d, dst, dstlen);
  439. ret += in_len;
  440. d += dstlen;
  441. found = true;
  442. if (need_free) {
  443. free(dst);
  444. }
  445. }
  446. }
  447. }
  448. /* Leave variable as is, in this case we use dest */
  449. if (!found) {
  450. if (strict && out_len >= 2) {
  451. /* Copy '${' */
  452. memcpy (d, ptr, 2);
  453. d += 2;
  454. ret --;
  455. }
  456. else {
  457. memcpy (d, ptr, 1);
  458. d ++;
  459. }
  460. }
  461. }
  462. *dest = d;
  463. return ret;
  464. }
  465. /**
  466. * Expand variables in string
  467. * @param parser
  468. * @param dst
  469. * @param src
  470. * @param in_len
  471. * @return
  472. */
  473. static ssize_t
  474. ucl_expand_variable (struct ucl_parser *parser, unsigned char **dst,
  475. const char *src, size_t in_len)
  476. {
  477. const char *p, *end = src + in_len;
  478. unsigned char *d, *d_end;
  479. size_t out_len = 0;
  480. bool vars_found = false;
  481. if (parser->flags & UCL_PARSER_DISABLE_MACRO) {
  482. *dst = NULL;
  483. return in_len;
  484. }
  485. p = src;
  486. while (p != end) {
  487. if (*p == '$' && p + 1 != end) {
  488. p = ucl_check_variable (parser, p + 1, end - p - 1, &out_len, &vars_found);
  489. }
  490. else {
  491. p ++;
  492. out_len ++;
  493. }
  494. }
  495. if (!vars_found) {
  496. /* Trivial case */
  497. *dst = NULL;
  498. return in_len;
  499. }
  500. *dst = UCL_ALLOC (out_len + 1);
  501. if (*dst == NULL) {
  502. return in_len;
  503. }
  504. d = *dst;
  505. d_end = d + out_len;
  506. p = src;
  507. while (p != end && d != d_end) {
  508. if (*p == '$' && p + 1 != end) {
  509. p = ucl_expand_single_variable (parser, p, end - p, &d, d_end - d);
  510. }
  511. else {
  512. *d++ = *p++;
  513. }
  514. }
  515. *d = '\0';
  516. return out_len;
  517. }
  518. /**
  519. * Store or copy pointer to the trash stack
  520. * @param parser parser object
  521. * @param src src string
  522. * @param dst destination buffer (trash stack pointer)
  523. * @param dst_const const destination pointer (e.g. value of object)
  524. * @param in_len input length
  525. * @param need_unescape need to unescape source (and copy it)
  526. * @param need_lowercase need to lowercase value (and copy)
  527. * @param need_expand need to expand variables (and copy as well)
  528. * @param unescape_squote unescape single quoted string
  529. * @return output length (excluding \0 symbol)
  530. */
  531. static inline ssize_t
  532. ucl_copy_or_store_ptr (struct ucl_parser *parser,
  533. const unsigned char *src, unsigned char **dst,
  534. const char **dst_const, size_t in_len,
  535. bool need_unescape, bool need_lowercase, bool need_expand,
  536. bool unescape_squote)
  537. {
  538. ssize_t ret = -1, tret;
  539. unsigned char *tmp;
  540. if (need_unescape || need_lowercase ||
  541. (need_expand && parser->variables != NULL) ||
  542. !(parser->flags & UCL_PARSER_ZEROCOPY)) {
  543. /* Copy string */
  544. *dst = UCL_ALLOC (in_len + 1);
  545. if (*dst == NULL) {
  546. ucl_set_err (parser, UCL_EINTERNAL, "cannot allocate memory for a string",
  547. &parser->err);
  548. return false;
  549. }
  550. if (need_lowercase) {
  551. ret = ucl_strlcpy_tolower (*dst, src, in_len + 1);
  552. }
  553. else {
  554. ret = ucl_strlcpy_unsafe (*dst, src, in_len + 1);
  555. }
  556. if (need_unescape) {
  557. if (!unescape_squote) {
  558. ret = ucl_unescape_json_string (*dst, ret);
  559. }
  560. else {
  561. ret = ucl_unescape_squoted_string (*dst, ret);
  562. }
  563. }
  564. if (need_expand) {
  565. tmp = *dst;
  566. tret = ret;
  567. ret = ucl_expand_variable (parser, dst, tmp, ret);
  568. if (*dst == NULL) {
  569. /* Nothing to expand */
  570. *dst = tmp;
  571. ret = tret;
  572. }
  573. else {
  574. /* Free unexpanded value */
  575. UCL_FREE (in_len + 1, tmp);
  576. }
  577. }
  578. *dst_const = *dst;
  579. }
  580. else {
  581. *dst_const = src;
  582. ret = in_len;
  583. }
  584. return ret;
  585. }
  586. /**
  587. * Create and append an object at the specified level
  588. * @param parser
  589. * @param is_array
  590. * @param level
  591. * @return
  592. */
  593. static inline ucl_object_t *
  594. ucl_parser_add_container (ucl_object_t *obj, struct ucl_parser *parser,
  595. bool is_array, uint32_t level, bool has_obrace)
  596. {
  597. struct ucl_stack *st;
  598. ucl_object_t *nobj;
  599. if (obj == NULL) {
  600. nobj = ucl_object_new_full (is_array ? UCL_ARRAY : UCL_OBJECT, parser->chunks->priority);
  601. if (nobj == NULL) {
  602. goto enomem0;
  603. }
  604. } else {
  605. if (obj->type == (is_array ? UCL_OBJECT : UCL_ARRAY)) {
  606. /* Bad combination for merge: array and object */
  607. ucl_set_err (parser, UCL_EMERGE,
  608. "cannot merge an object with an array",
  609. &parser->err);
  610. return NULL;
  611. }
  612. nobj = obj;
  613. nobj->type = is_array ? UCL_ARRAY : UCL_OBJECT;
  614. }
  615. if (!is_array) {
  616. if (nobj->value.ov == NULL) {
  617. nobj->value.ov = ucl_hash_create (parser->flags & UCL_PARSER_KEY_LOWERCASE);
  618. if (nobj->value.ov == NULL) {
  619. goto enomem1;
  620. }
  621. }
  622. parser->state = UCL_STATE_KEY;
  623. } else {
  624. parser->state = UCL_STATE_VALUE;
  625. }
  626. st = UCL_ALLOC (sizeof (struct ucl_stack));
  627. if (st == NULL) {
  628. goto enomem1;
  629. }
  630. st->obj = nobj;
  631. if (level >= UINT16_MAX) {
  632. ucl_set_err (parser, UCL_ENESTED,
  633. "objects are nesting too deep (over 65535 limit)",
  634. &parser->err);
  635. if (nobj != obj) {
  636. ucl_object_unref (obj);
  637. }
  638. UCL_FREE(sizeof (struct ucl_stack), st);
  639. return NULL;
  640. }
  641. st->e.params.level = level;
  642. st->e.params.line = parser->chunks->line;
  643. st->chunk = parser->chunks;
  644. if (has_obrace) {
  645. st->e.params.flags = UCL_STACK_HAS_OBRACE;
  646. }
  647. else {
  648. st->e.params.flags = 0;
  649. }
  650. LL_PREPEND (parser->stack, st);
  651. parser->cur_obj = nobj;
  652. return nobj;
  653. enomem1:
  654. if (nobj != obj)
  655. ucl_object_unref (nobj);
  656. enomem0:
  657. ucl_set_err (parser, UCL_EINTERNAL, "cannot allocate memory for an object",
  658. &parser->err);
  659. return NULL;
  660. }
  661. int
  662. ucl_maybe_parse_number (ucl_object_t *obj,
  663. const char *start, const char *end, const char **pos,
  664. bool allow_double, bool number_bytes, bool allow_time)
  665. {
  666. const char *p = start, *c = start;
  667. char *endptr;
  668. bool got_dot = false, got_exp = false, need_double = false,
  669. is_time = false, valid_start = false, is_hex = false,
  670. is_neg = false;
  671. double dv = 0;
  672. int64_t lv = 0;
  673. if (*p == '-') {
  674. is_neg = true;
  675. c ++;
  676. p ++;
  677. }
  678. while (p < end) {
  679. if (is_hex && isxdigit (*p)) {
  680. p ++;
  681. }
  682. else if (isdigit (*p)) {
  683. valid_start = true;
  684. p ++;
  685. }
  686. else if (!is_hex && (*p == 'x' || *p == 'X')) {
  687. is_hex = true;
  688. allow_double = false;
  689. c = p + 1;
  690. }
  691. else if (allow_double) {
  692. if (p == c) {
  693. /* Empty digits sequence, not a number */
  694. *pos = start;
  695. return EINVAL;
  696. }
  697. else if (*p == '.') {
  698. if (got_dot) {
  699. /* Double dots, not a number */
  700. *pos = start;
  701. return EINVAL;
  702. }
  703. else {
  704. got_dot = true;
  705. need_double = true;
  706. p ++;
  707. }
  708. }
  709. else if (*p == 'e' || *p == 'E') {
  710. if (got_exp) {
  711. /* Double exp, not a number */
  712. *pos = start;
  713. return EINVAL;
  714. }
  715. else {
  716. got_exp = true;
  717. need_double = true;
  718. p ++;
  719. if (p >= end) {
  720. *pos = start;
  721. return EINVAL;
  722. }
  723. if (!isdigit (*p) && *p != '+' && *p != '-') {
  724. /* Wrong exponent sign */
  725. *pos = start;
  726. return EINVAL;
  727. }
  728. else {
  729. p ++;
  730. }
  731. }
  732. }
  733. else {
  734. /* Got the end of the number, need to check */
  735. break;
  736. }
  737. }
  738. else {
  739. break;
  740. }
  741. }
  742. if (!valid_start) {
  743. *pos = start;
  744. return EINVAL;
  745. }
  746. errno = 0;
  747. if (need_double) {
  748. dv = strtod (c, &endptr);
  749. }
  750. else {
  751. if (is_hex) {
  752. lv = strtoimax (c, &endptr, 16);
  753. }
  754. else {
  755. lv = strtoimax (c, &endptr, 10);
  756. }
  757. }
  758. if (errno == ERANGE) {
  759. *pos = start;
  760. return ERANGE;
  761. }
  762. /* Now check endptr */
  763. if (endptr == NULL || ucl_lex_is_atom_end (*endptr) || *endptr == '\0') {
  764. p = endptr;
  765. goto set_obj;
  766. }
  767. if (endptr < end && endptr != start) {
  768. p = endptr;
  769. switch (*p) {
  770. case 'm':
  771. case 'M':
  772. case 'g':
  773. case 'G':
  774. case 'k':
  775. case 'K':
  776. if (end - p >= 2) {
  777. if (p[1] == 's' || p[1] == 'S') {
  778. /* Milliseconds */
  779. if (!need_double) {
  780. need_double = true;
  781. dv = lv;
  782. }
  783. is_time = true;
  784. if (p[0] == 'm' || p[0] == 'M') {
  785. dv /= 1000.;
  786. }
  787. else {
  788. dv *= ucl_lex_num_multiplier (*p, false);
  789. }
  790. p += 2;
  791. if (end - p > 0 && !ucl_lex_is_atom_end (*p)) {
  792. *pos = start;
  793. return EINVAL;
  794. }
  795. goto set_obj;
  796. }
  797. else if (number_bytes || (p[1] == 'b' || p[1] == 'B')) {
  798. /* Bytes */
  799. if (need_double) {
  800. need_double = false;
  801. lv = dv;
  802. }
  803. lv *= ucl_lex_num_multiplier (*p, true);
  804. p += 2;
  805. if (end - p > 0 && !ucl_lex_is_atom_end (*p)) {
  806. *pos = start;
  807. return EINVAL;
  808. }
  809. goto set_obj;
  810. }
  811. else if (ucl_lex_is_atom_end (p[1])) {
  812. if (need_double) {
  813. dv *= ucl_lex_num_multiplier (*p, false);
  814. }
  815. else {
  816. lv *= ucl_lex_num_multiplier (*p, number_bytes);
  817. }
  818. p ++;
  819. goto set_obj;
  820. }
  821. else if (allow_time && end - p >= 3) {
  822. if (tolower (p[0]) == 'm' &&
  823. tolower (p[1]) == 'i' &&
  824. tolower (p[2]) == 'n') {
  825. /* Minutes */
  826. if (!need_double) {
  827. need_double = true;
  828. dv = lv;
  829. }
  830. is_time = true;
  831. dv *= 60.;
  832. p += 3;
  833. if (end - p > 0 && !ucl_lex_is_atom_end (*p)) {
  834. *pos = start;
  835. return EINVAL;
  836. }
  837. goto set_obj;
  838. }
  839. }
  840. }
  841. else {
  842. if (need_double) {
  843. dv *= ucl_lex_num_multiplier (*p, false);
  844. }
  845. else {
  846. lv *= ucl_lex_num_multiplier (*p, number_bytes);
  847. }
  848. p ++;
  849. if (end - p > 0 && !ucl_lex_is_atom_end (*p)) {
  850. *pos = start;
  851. return EINVAL;
  852. }
  853. goto set_obj;
  854. }
  855. break;
  856. case 'S':
  857. case 's':
  858. if (allow_time &&
  859. (p == end - 1 || ucl_lex_is_atom_end (p[1]))) {
  860. if (!need_double) {
  861. need_double = true;
  862. dv = lv;
  863. }
  864. p ++;
  865. is_time = true;
  866. goto set_obj;
  867. }
  868. break;
  869. case 'h':
  870. case 'H':
  871. case 'd':
  872. case 'D':
  873. case 'w':
  874. case 'W':
  875. case 'Y':
  876. case 'y':
  877. if (allow_time &&
  878. (p == end - 1 || ucl_lex_is_atom_end (p[1]))) {
  879. if (!need_double) {
  880. need_double = true;
  881. dv = lv;
  882. }
  883. is_time = true;
  884. dv *= ucl_lex_time_multiplier (*p);
  885. p ++;
  886. goto set_obj;
  887. }
  888. break;
  889. case '\t':
  890. case ' ':
  891. while (p < end && ucl_test_character(*p, UCL_CHARACTER_WHITESPACE)) {
  892. p++;
  893. }
  894. if (ucl_lex_is_atom_end(*p))
  895. goto set_obj;
  896. break;
  897. }
  898. }
  899. else if (endptr == end) {
  900. /* Just a number at the end of chunk */
  901. p = endptr;
  902. goto set_obj;
  903. }
  904. *pos = c;
  905. return EINVAL;
  906. set_obj:
  907. if (obj != NULL) {
  908. if (allow_double && (need_double || is_time)) {
  909. if (!is_time) {
  910. obj->type = UCL_FLOAT;
  911. }
  912. else {
  913. obj->type = UCL_TIME;
  914. }
  915. obj->value.dv = is_neg ? (-dv) : dv;
  916. }
  917. else {
  918. obj->type = UCL_INT;
  919. obj->value.iv = is_neg ? (-lv) : lv;
  920. }
  921. }
  922. *pos = p;
  923. return 0;
  924. }
  925. /**
  926. * Parse possible number
  927. * @param parser
  928. * @param chunk
  929. * @param obj
  930. * @return true if a number has been parsed
  931. */
  932. static bool
  933. ucl_lex_number (struct ucl_parser *parser,
  934. struct ucl_chunk *chunk, ucl_object_t *obj)
  935. {
  936. const unsigned char *pos;
  937. int ret;
  938. ret = ucl_maybe_parse_number (obj, chunk->pos, chunk->end, (const char **)&pos,
  939. true, false, ((parser->flags & UCL_PARSER_NO_TIME) == 0));
  940. if (ret == 0) {
  941. chunk->remain -= pos - chunk->pos;
  942. chunk->column += pos - chunk->pos;
  943. chunk->pos = pos;
  944. return true;
  945. }
  946. else if (ret == ERANGE) {
  947. ucl_set_err (parser, UCL_ESYNTAX, "numeric value out of range",
  948. &parser->err);
  949. }
  950. return false;
  951. }
  952. /**
  953. * Parse quoted string with possible escapes
  954. * @param parser
  955. * @param chunk
  956. * @param need_unescape
  957. * @param ucl_escape
  958. * @param var_expand
  959. * @return true if a string has been parsed
  960. */
  961. static bool
  962. ucl_lex_json_string (struct ucl_parser *parser,
  963. struct ucl_chunk *chunk,
  964. bool *need_unescape,
  965. bool *ucl_escape,
  966. bool *var_expand)
  967. {
  968. const unsigned char *p = chunk->pos;
  969. unsigned char c;
  970. int i;
  971. while (p < chunk->end) {
  972. c = *p;
  973. if (c < 0x1F) {
  974. /* Unmasked control character */
  975. if (c == '\n') {
  976. ucl_set_err (parser, UCL_ESYNTAX, "unexpected newline",
  977. &parser->err);
  978. }
  979. else {
  980. ucl_set_err (parser, UCL_ESYNTAX, "unexpected control character",
  981. &parser->err);
  982. }
  983. return false;
  984. }
  985. else if (c == '\\') {
  986. ucl_chunk_skipc (chunk, p);
  987. if (p >= chunk->end) {
  988. ucl_set_err (parser, UCL_ESYNTAX, "unfinished escape character",
  989. &parser->err);
  990. return false;
  991. }
  992. c = *p;
  993. if (ucl_test_character (c, UCL_CHARACTER_ESCAPE)) {
  994. if (c == 'u') {
  995. ucl_chunk_skipc (chunk, p);
  996. for (i = 0; i < 4 && p < chunk->end; i ++) {
  997. if (!isxdigit (*p)) {
  998. ucl_set_err (parser, UCL_ESYNTAX, "invalid utf escape",
  999. &parser->err);
  1000. return false;
  1001. }
  1002. ucl_chunk_skipc (chunk, p);
  1003. }
  1004. if (p >= chunk->end) {
  1005. ucl_set_err (parser, UCL_ESYNTAX,
  1006. "unfinished escape character",
  1007. &parser->err);
  1008. return false;
  1009. }
  1010. }
  1011. else {
  1012. ucl_chunk_skipc (chunk, p);
  1013. }
  1014. }
  1015. *need_unescape = true;
  1016. *ucl_escape = true;
  1017. continue;
  1018. }
  1019. else if (c == '"') {
  1020. ucl_chunk_skipc (chunk, p);
  1021. return true;
  1022. }
  1023. else if (ucl_test_character (c, UCL_CHARACTER_UCL_UNSAFE)) {
  1024. *ucl_escape = true;
  1025. }
  1026. else if (c == '$') {
  1027. *var_expand = true;
  1028. }
  1029. ucl_chunk_skipc (chunk, p);
  1030. }
  1031. ucl_set_err (parser, UCL_ESYNTAX,
  1032. "no quote at the end of json string",
  1033. &parser->err);
  1034. return false;
  1035. }
  1036. /**
  1037. * Process single quoted string
  1038. * @param parser
  1039. * @param chunk
  1040. * @param need_unescape
  1041. * @return
  1042. */
  1043. static bool
  1044. ucl_lex_squoted_string (struct ucl_parser *parser,
  1045. struct ucl_chunk *chunk, bool *need_unescape)
  1046. {
  1047. const unsigned char *p = chunk->pos;
  1048. unsigned char c;
  1049. while (p < chunk->end) {
  1050. c = *p;
  1051. if (c == '\\') {
  1052. ucl_chunk_skipc (chunk, p);
  1053. if (p >= chunk->end) {
  1054. ucl_set_err (parser, UCL_ESYNTAX,
  1055. "unfinished escape character",
  1056. &parser->err);
  1057. return false;
  1058. }
  1059. else {
  1060. ucl_chunk_skipc (chunk, p);
  1061. }
  1062. *need_unescape = true;
  1063. continue;
  1064. }
  1065. else if (c == '\'') {
  1066. ucl_chunk_skipc (chunk, p);
  1067. return true;
  1068. }
  1069. ucl_chunk_skipc (chunk, p);
  1070. }
  1071. ucl_set_err (parser, UCL_ESYNTAX,
  1072. "no quote at the end of single quoted string",
  1073. &parser->err);
  1074. return false;
  1075. }
  1076. static void
  1077. ucl_parser_append_elt (struct ucl_parser *parser, ucl_hash_t *cont,
  1078. ucl_object_t *top,
  1079. ucl_object_t *elt)
  1080. {
  1081. ucl_object_t *nobj;
  1082. if ((parser->flags & UCL_PARSER_NO_IMPLICIT_ARRAYS) == 0) {
  1083. /* Implicit array */
  1084. top->flags |= UCL_OBJECT_MULTIVALUE;
  1085. DL_APPEND (top, elt);
  1086. parser->stack->obj->len ++;
  1087. }
  1088. else {
  1089. if ((top->flags & UCL_OBJECT_MULTIVALUE) != 0) {
  1090. /* Just add to the explicit array */
  1091. ucl_array_append (top, elt);
  1092. }
  1093. else {
  1094. /* Convert to an array */
  1095. nobj = ucl_object_typed_new (UCL_ARRAY);
  1096. nobj->key = top->key;
  1097. nobj->keylen = top->keylen;
  1098. nobj->flags |= UCL_OBJECT_MULTIVALUE;
  1099. ucl_array_append (nobj, top);
  1100. ucl_array_append (nobj, elt);
  1101. ucl_hash_replace (cont, top, nobj);
  1102. }
  1103. }
  1104. }
  1105. bool
  1106. ucl_parser_process_object_element (struct ucl_parser *parser, ucl_object_t *nobj)
  1107. {
  1108. ucl_hash_t *container;
  1109. ucl_object_t *tobj = NULL, *cur;
  1110. char errmsg[256];
  1111. container = parser->stack->obj->value.ov;
  1112. DL_FOREACH (parser->stack->obj, cur) {
  1113. tobj = __DECONST (ucl_object_t *, ucl_hash_search_obj (cur->value.ov, nobj));
  1114. if (tobj != NULL) {
  1115. break;
  1116. }
  1117. }
  1118. if (tobj == NULL) {
  1119. container = ucl_hash_insert_object (container, nobj,
  1120. parser->flags & UCL_PARSER_KEY_LOWERCASE);
  1121. if (container == NULL) {
  1122. return false;
  1123. }
  1124. nobj->prev = nobj;
  1125. nobj->next = NULL;
  1126. parser->stack->obj->len ++;
  1127. }
  1128. else {
  1129. unsigned priold = ucl_object_get_priority (tobj),
  1130. prinew = ucl_object_get_priority (nobj);
  1131. switch (parser->chunks->strategy) {
  1132. case UCL_DUPLICATE_APPEND:
  1133. /*
  1134. * The logic here is the following:
  1135. *
  1136. * - if we have two objects with the same priority, then we form an
  1137. * implicit or explicit array
  1138. * - if a new object has bigger priority, then we overwrite an old one
  1139. * - if a new object has lower priority, then we ignore it
  1140. */
  1141. /* Special case for inherited objects */
  1142. if (tobj->flags & UCL_OBJECT_INHERITED) {
  1143. prinew = priold + 1;
  1144. }
  1145. if (priold == prinew) {
  1146. ucl_parser_append_elt (parser, container, tobj, nobj);
  1147. }
  1148. else if (priold > prinew) {
  1149. /*
  1150. * We add this new object to a list of trash objects just to ensure
  1151. * that it won't come to any real object
  1152. * XXX: rather inefficient approach
  1153. */
  1154. DL_APPEND (parser->trash_objs, nobj);
  1155. }
  1156. else {
  1157. ucl_hash_replace (container, tobj, nobj);
  1158. ucl_object_unref (tobj);
  1159. }
  1160. break;
  1161. case UCL_DUPLICATE_REWRITE:
  1162. /* We just rewrite old values regardless of priority */
  1163. ucl_hash_replace (container, tobj, nobj);
  1164. ucl_object_unref (tobj);
  1165. break;
  1166. case UCL_DUPLICATE_ERROR:
  1167. snprintf(errmsg, sizeof(errmsg),
  1168. "duplicate element for key '%s' found",
  1169. nobj->key);
  1170. ucl_set_err (parser, UCL_EMERGE, errmsg, &parser->err);
  1171. return false;
  1172. case UCL_DUPLICATE_MERGE:
  1173. /*
  1174. * Here we do have some old object so we just push it on top of objects stack
  1175. * Check priority and then perform the merge on the remaining objects
  1176. */
  1177. if (tobj->type == UCL_OBJECT || tobj->type == UCL_ARRAY) {
  1178. ucl_object_unref (nobj);
  1179. nobj = tobj;
  1180. }
  1181. else if (priold == prinew) {
  1182. ucl_parser_append_elt (parser, container, tobj, nobj);
  1183. }
  1184. else if (priold > prinew) {
  1185. /*
  1186. * We add this new object to a list of trash objects just to ensure
  1187. * that it won't come to any real object
  1188. * XXX: rather inefficient approach
  1189. */
  1190. DL_APPEND (parser->trash_objs, nobj);
  1191. }
  1192. else {
  1193. ucl_hash_replace (container, tobj, nobj);
  1194. ucl_object_unref (tobj);
  1195. }
  1196. break;
  1197. }
  1198. }
  1199. parser->stack->obj->value.ov = container;
  1200. parser->cur_obj = nobj;
  1201. ucl_attach_comment (parser, nobj, false);
  1202. return true;
  1203. }
  1204. /**
  1205. * Parse a key in an object
  1206. * @param parser
  1207. * @param chunk
  1208. * @param next_key
  1209. * @param end_of_object
  1210. * @return true if a key has been parsed
  1211. */
  1212. static bool
  1213. ucl_parse_key (struct ucl_parser *parser, struct ucl_chunk *chunk,
  1214. bool *next_key, bool *end_of_object)
  1215. {
  1216. const unsigned char *p, *c = NULL, *end, *t;
  1217. const char *key = NULL;
  1218. bool got_quote = false, got_eq = false, got_semicolon = false,
  1219. need_unescape = false, ucl_escape = false, var_expand = false,
  1220. got_content = false, got_sep = false;
  1221. ucl_object_t *nobj;
  1222. ssize_t keylen;
  1223. p = chunk->pos;
  1224. if (*p == '.') {
  1225. /* It is macro actually */
  1226. if (!(parser->flags & UCL_PARSER_DISABLE_MACRO)) {
  1227. ucl_chunk_skipc (chunk, p);
  1228. }
  1229. parser->prev_state = parser->state;
  1230. parser->state = UCL_STATE_MACRO_NAME;
  1231. *end_of_object = false;
  1232. return true;
  1233. }
  1234. while (p < chunk->end) {
  1235. /*
  1236. * A key must start with alpha, number, '/' or '_' and end with space character
  1237. */
  1238. if (c == NULL) {
  1239. if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
  1240. if (!ucl_skip_comments (parser)) {
  1241. return false;
  1242. }
  1243. p = chunk->pos;
  1244. }
  1245. else if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  1246. ucl_chunk_skipc (chunk, p);
  1247. }
  1248. else if (ucl_test_character (*p, UCL_CHARACTER_KEY_START)) {
  1249. /* The first symbol */
  1250. c = p;
  1251. ucl_chunk_skipc (chunk, p);
  1252. got_content = true;
  1253. }
  1254. else if (*p == '"') {
  1255. /* JSON style key */
  1256. c = p + 1;
  1257. got_quote = true;
  1258. got_content = true;
  1259. ucl_chunk_skipc (chunk, p);
  1260. }
  1261. else if (*p == '}') {
  1262. /* We have actually end of an object */
  1263. *end_of_object = true;
  1264. return true;
  1265. }
  1266. else if (*p == '.') {
  1267. ucl_chunk_skipc (chunk, p);
  1268. parser->prev_state = parser->state;
  1269. parser->state = UCL_STATE_MACRO_NAME;
  1270. return true;
  1271. }
  1272. else {
  1273. /* Invalid identifier */
  1274. ucl_set_err (parser, UCL_ESYNTAX, "key must begin with a letter",
  1275. &parser->err);
  1276. return false;
  1277. }
  1278. }
  1279. else {
  1280. /* Parse the body of a key */
  1281. if (!got_quote) {
  1282. if (ucl_test_character (*p, UCL_CHARACTER_KEY)) {
  1283. got_content = true;
  1284. ucl_chunk_skipc (chunk, p);
  1285. }
  1286. else if (ucl_test_character (*p, UCL_CHARACTER_KEY_SEP)) {
  1287. end = p;
  1288. break;
  1289. }
  1290. else {
  1291. ucl_set_err (parser, UCL_ESYNTAX, "invalid character in a key",
  1292. &parser->err);
  1293. return false;
  1294. }
  1295. }
  1296. else {
  1297. /* We need to parse json like quoted string */
  1298. if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
  1299. return false;
  1300. }
  1301. /* Always escape keys obtained via json */
  1302. end = chunk->pos - 1;
  1303. p = chunk->pos;
  1304. break;
  1305. }
  1306. }
  1307. }
  1308. if (p >= chunk->end && got_content) {
  1309. ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err);
  1310. return false;
  1311. }
  1312. else if (!got_content) {
  1313. return true;
  1314. }
  1315. *end_of_object = false;
  1316. /* We are now at the end of the key, need to parse the rest */
  1317. while (p < chunk->end) {
  1318. if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) {
  1319. ucl_chunk_skipc (chunk, p);
  1320. }
  1321. else if (*p == '=') {
  1322. if (!got_eq && !got_semicolon) {
  1323. ucl_chunk_skipc (chunk, p);
  1324. got_eq = true;
  1325. }
  1326. else {
  1327. ucl_set_err (parser, UCL_ESYNTAX, "unexpected '=' character",
  1328. &parser->err);
  1329. return false;
  1330. }
  1331. }
  1332. else if (*p == ':') {
  1333. if (!got_eq && !got_semicolon) {
  1334. ucl_chunk_skipc (chunk, p);
  1335. got_semicolon = true;
  1336. }
  1337. else {
  1338. ucl_set_err (parser, UCL_ESYNTAX, "unexpected ':' character",
  1339. &parser->err);
  1340. return false;
  1341. }
  1342. }
  1343. else if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
  1344. /* Check for comment */
  1345. if (!ucl_skip_comments (parser)) {
  1346. return false;
  1347. }
  1348. p = chunk->pos;
  1349. }
  1350. else {
  1351. /* Start value */
  1352. break;
  1353. }
  1354. }
  1355. if (p >= chunk->end && got_content) {
  1356. ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err);
  1357. return false;
  1358. }
  1359. got_sep = got_semicolon || got_eq;
  1360. if (!got_sep) {
  1361. /*
  1362. * Maybe we have more keys nested, so search for termination character.
  1363. * Possible choices:
  1364. * 1) key1 key2 ... keyN [:=] value <- we treat that as error
  1365. * 2) key1 ... keyN {} or [] <- we treat that as nested objects
  1366. * 3) key1 value[;,\n] <- we treat that as linear object
  1367. */
  1368. t = p;
  1369. *next_key = false;
  1370. while (ucl_test_character (*t, UCL_CHARACTER_WHITESPACE)) {
  1371. t ++;
  1372. }
  1373. /* Check first non-space character after a key */
  1374. if (*t != '{' && *t != '[') {
  1375. while (t < chunk->end) {
  1376. if (*t == ',' || *t == ';' || *t == '\n' || *t == '\r') {
  1377. break;
  1378. }
  1379. else if (*t == '{' || *t == '[') {
  1380. *next_key = true;
  1381. break;
  1382. }
  1383. t ++;
  1384. }
  1385. }
  1386. }
  1387. /* Create a new object */
  1388. nobj = ucl_object_new_full (UCL_NULL, parser->chunks->priority);
  1389. if (nobj == NULL) {
  1390. return false;
  1391. }
  1392. keylen = ucl_copy_or_store_ptr (parser, c, &nobj->trash_stack[UCL_TRASH_KEY],
  1393. &key, end - c, need_unescape, parser->flags & UCL_PARSER_KEY_LOWERCASE,
  1394. false, false);
  1395. if (keylen == -1) {
  1396. ucl_object_unref (nobj);
  1397. return false;
  1398. }
  1399. else if (keylen == 0) {
  1400. ucl_set_err (parser, UCL_ESYNTAX, "empty keys are not allowed", &parser->err);
  1401. ucl_object_unref (nobj);
  1402. return false;
  1403. }
  1404. nobj->key = key;
  1405. nobj->keylen = keylen;
  1406. if (!ucl_parser_process_object_element (parser, nobj)) {
  1407. return false;
  1408. }
  1409. if (ucl_escape) {
  1410. nobj->flags |= UCL_OBJECT_NEED_KEY_ESCAPE;
  1411. }
  1412. return true;
  1413. }
  1414. /**
  1415. * Parse a cl string
  1416. * @param parser
  1417. * @param chunk
  1418. * @param var_expand
  1419. * @param need_unescape
  1420. * @return true if a key has been parsed
  1421. */
  1422. static bool
  1423. ucl_parse_string_value (struct ucl_parser *parser,
  1424. struct ucl_chunk *chunk, bool *var_expand, bool *need_unescape)
  1425. {
  1426. const unsigned char *p;
  1427. enum {
  1428. UCL_BRACE_ROUND = 0,
  1429. UCL_BRACE_SQUARE,
  1430. UCL_BRACE_FIGURE
  1431. };
  1432. int braces[3][2] = {{0, 0}, {0, 0}, {0, 0}};
  1433. p = chunk->pos;
  1434. while (p < chunk->end) {
  1435. /* Skip pairs of figure braces */
  1436. if (*p == '{') {
  1437. braces[UCL_BRACE_FIGURE][0] ++;
  1438. }
  1439. else if (*p == '}') {
  1440. braces[UCL_BRACE_FIGURE][1] ++;
  1441. if (braces[UCL_BRACE_FIGURE][1] <= braces[UCL_BRACE_FIGURE][0]) {
  1442. /* This is not a termination symbol, continue */
  1443. ucl_chunk_skipc (chunk, p);
  1444. continue;
  1445. }
  1446. }
  1447. /* Skip pairs of square braces */
  1448. else if (*p == '[') {
  1449. braces[UCL_BRACE_SQUARE][0] ++;
  1450. }
  1451. else if (*p == ']') {
  1452. braces[UCL_BRACE_SQUARE][1] ++;
  1453. if (braces[UCL_BRACE_SQUARE][1] <= braces[UCL_BRACE_SQUARE][0]) {
  1454. /* This is not a termination symbol, continue */
  1455. ucl_chunk_skipc (chunk, p);
  1456. continue;
  1457. }
  1458. }
  1459. else if (*p == '$') {
  1460. *var_expand = true;
  1461. }
  1462. else if (*p == '\\') {
  1463. *need_unescape = true;
  1464. ucl_chunk_skipc (chunk, p);
  1465. if (p < chunk->end) {
  1466. ucl_chunk_skipc (chunk, p);
  1467. }
  1468. continue;
  1469. }
  1470. if (ucl_lex_is_atom_end (*p) || (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1]))) {
  1471. break;
  1472. }
  1473. ucl_chunk_skipc (chunk, p);
  1474. }
  1475. return true;
  1476. }
  1477. /**
  1478. * Parse multiline string ending with \n{term}\n
  1479. * @param parser
  1480. * @param chunk
  1481. * @param term
  1482. * @param term_len
  1483. * @param beg
  1484. * @param var_expand
  1485. * @return size of multiline string or 0 in case of error
  1486. */
  1487. static int
  1488. ucl_parse_multiline_string (struct ucl_parser *parser,
  1489. struct ucl_chunk *chunk, const unsigned char *term,
  1490. int term_len, unsigned char const **beg,
  1491. bool *var_expand)
  1492. {
  1493. const unsigned char *p, *c, *tend;
  1494. bool newline = false;
  1495. int len = 0;
  1496. p = chunk->pos;
  1497. c = p;
  1498. while (p < chunk->end) {
  1499. if (newline) {
  1500. if (chunk->end - p < term_len) {
  1501. return 0;
  1502. }
  1503. else if (memcmp (p, term, term_len) == 0) {
  1504. tend = p + term_len;
  1505. if (*tend != '\n' && *tend != ';' && *tend != ',') {
  1506. /* Incomplete terminator */
  1507. ucl_chunk_skipc (chunk, p);
  1508. continue;
  1509. }
  1510. len = p - c;
  1511. chunk->remain -= term_len;
  1512. chunk->pos = p + term_len;
  1513. chunk->column = term_len;
  1514. *beg = c;
  1515. break;
  1516. }
  1517. }
  1518. if (*p == '\n') {
  1519. newline = true;
  1520. }
  1521. else {
  1522. if (*p == '$') {
  1523. *var_expand = true;
  1524. }
  1525. newline = false;
  1526. }
  1527. ucl_chunk_skipc (chunk, p);
  1528. }
  1529. return len;
  1530. }
  1531. static inline ucl_object_t*
  1532. ucl_parser_get_container (struct ucl_parser *parser)
  1533. {
  1534. ucl_object_t *t, *obj = NULL;
  1535. if (parser == NULL || parser->stack == NULL || parser->stack->obj == NULL) {
  1536. return NULL;
  1537. }
  1538. if (parser->stack->obj->type == UCL_ARRAY) {
  1539. /* Object must be allocated */
  1540. obj = ucl_object_new_full (UCL_NULL, parser->chunks->priority);
  1541. t = parser->stack->obj;
  1542. if (!ucl_array_append (t, obj)) {
  1543. ucl_object_unref (obj);
  1544. return NULL;
  1545. }
  1546. parser->cur_obj = obj;
  1547. ucl_attach_comment (parser, obj, false);
  1548. }
  1549. else {
  1550. /* Object has been already allocated */
  1551. obj = parser->cur_obj;
  1552. }
  1553. return obj;
  1554. }
  1555. /**
  1556. * Handle value data
  1557. * @param parser
  1558. * @param chunk
  1559. * @return
  1560. */
  1561. static bool
  1562. ucl_parse_value (struct ucl_parser *parser, struct ucl_chunk *chunk)
  1563. {
  1564. const unsigned char *p, *c;
  1565. ucl_object_t *obj = NULL;
  1566. unsigned int stripped_spaces;
  1567. ssize_t str_len;
  1568. bool need_unescape = false, ucl_escape = false, var_expand = false;
  1569. p = chunk->pos;
  1570. /* Skip any spaces and comments */
  1571. if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) ||
  1572. (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1]))) {
  1573. while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  1574. ucl_chunk_skipc (chunk, p);
  1575. }
  1576. if (!ucl_skip_comments (parser)) {
  1577. return false;
  1578. }
  1579. p = chunk->pos;
  1580. }
  1581. while (p < chunk->end) {
  1582. c = p;
  1583. switch (*p) {
  1584. case '"':
  1585. ucl_chunk_skipc (chunk, p);
  1586. if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape,
  1587. &var_expand)) {
  1588. return false;
  1589. }
  1590. obj = ucl_parser_get_container (parser);
  1591. if (!obj) {
  1592. return false;
  1593. }
  1594. str_len = chunk->pos - c - 2;
  1595. obj->type = UCL_STRING;
  1596. if ((str_len = ucl_copy_or_store_ptr (parser, c + 1,
  1597. &obj->trash_stack[UCL_TRASH_VALUE],
  1598. &obj->value.sv, str_len, need_unescape, false,
  1599. var_expand, false)) == -1) {
  1600. return false;
  1601. }
  1602. obj->len = str_len;
  1603. parser->state = UCL_STATE_AFTER_VALUE;
  1604. return true;
  1605. break;
  1606. case '\'':
  1607. ucl_chunk_skipc (chunk, p);
  1608. if (!ucl_lex_squoted_string (parser, chunk, &need_unescape)) {
  1609. return false;
  1610. }
  1611. obj = ucl_parser_get_container (parser);
  1612. if (!obj) {
  1613. return false;
  1614. }
  1615. str_len = chunk->pos - c - 2;
  1616. obj->type = UCL_STRING;
  1617. obj->flags |= UCL_OBJECT_SQUOTED;
  1618. if ((str_len = ucl_copy_or_store_ptr (parser, c + 1,
  1619. &obj->trash_stack[UCL_TRASH_VALUE],
  1620. &obj->value.sv, str_len, need_unescape, false,
  1621. var_expand, true)) == -1) {
  1622. return false;
  1623. }
  1624. obj->len = str_len;
  1625. parser->state = UCL_STATE_AFTER_VALUE;
  1626. return true;
  1627. break;
  1628. case '{':
  1629. obj = ucl_parser_get_container (parser);
  1630. if (obj == NULL) {
  1631. return false;
  1632. }
  1633. /* We have a new object */
  1634. if (parser->stack) {
  1635. obj = ucl_parser_add_container (obj, parser, false,
  1636. parser->stack->e.params.level, true);
  1637. }
  1638. else {
  1639. return false;
  1640. }
  1641. if (obj == NULL) {
  1642. return false;
  1643. }
  1644. ucl_chunk_skipc (chunk, p);
  1645. return true;
  1646. break;
  1647. case '[':
  1648. obj = ucl_parser_get_container (parser);
  1649. if (obj == NULL) {
  1650. return false;
  1651. }
  1652. /* We have a new array */
  1653. if (parser->stack) {
  1654. obj = ucl_parser_add_container (obj, parser, true,
  1655. parser->stack->e.params.level, true);
  1656. }
  1657. else {
  1658. return false;
  1659. }
  1660. if (obj == NULL) {
  1661. return false;
  1662. }
  1663. ucl_chunk_skipc (chunk, p);
  1664. return true;
  1665. break;
  1666. case ']':
  1667. /* We have the array ending */
  1668. if (parser->stack && parser->stack->obj->type == UCL_ARRAY) {
  1669. parser->state = UCL_STATE_AFTER_VALUE;
  1670. return true;
  1671. }
  1672. else {
  1673. goto parse_string;
  1674. }
  1675. break;
  1676. case '<':
  1677. obj = ucl_parser_get_container (parser);
  1678. /* We have something like multiline value, which must be <<[A-Z]+\n */
  1679. if (chunk->end - p > 3) {
  1680. if (memcmp (p, "<<", 2) == 0) {
  1681. p += 2;
  1682. /* We allow only uppercase characters in multiline definitions */
  1683. while (p < chunk->end && *p >= 'A' && *p <= 'Z') {
  1684. p ++;
  1685. }
  1686. if(p == chunk->end) {
  1687. ucl_set_err (parser, UCL_ESYNTAX,
  1688. "unterminated multiline value", &parser->err);
  1689. return false;
  1690. }
  1691. if (*p =='\n') {
  1692. /* Set chunk positions and start multiline parsing */
  1693. chunk->remain -= p - c + 1;
  1694. c += 2;
  1695. chunk->pos = p + 1;
  1696. chunk->column = 0;
  1697. chunk->line ++;
  1698. if ((str_len = ucl_parse_multiline_string (parser, chunk, c,
  1699. p - c, &c, &var_expand)) == 0) {
  1700. ucl_set_err (parser, UCL_ESYNTAX,
  1701. "unterminated multiline value", &parser->err);
  1702. return false;
  1703. }
  1704. obj->type = UCL_STRING;
  1705. obj->flags |= UCL_OBJECT_MULTILINE;
  1706. if ((str_len = ucl_copy_or_store_ptr (parser, c,
  1707. &obj->trash_stack[UCL_TRASH_VALUE],
  1708. &obj->value.sv, str_len - 1, false,
  1709. false, var_expand, false)) == -1) {
  1710. return false;
  1711. }
  1712. obj->len = str_len;
  1713. parser->state = UCL_STATE_AFTER_VALUE;
  1714. return true;
  1715. }
  1716. }
  1717. }
  1718. /* Fallback to ordinary strings */
  1719. /* FALLTHRU */
  1720. default:
  1721. parse_string:
  1722. if (obj == NULL) {
  1723. obj = ucl_parser_get_container (parser);
  1724. }
  1725. /* Parse atom */
  1726. if (ucl_test_character (*p, UCL_CHARACTER_VALUE_DIGIT_START)) {
  1727. if (!ucl_lex_number (parser, chunk, obj)) {
  1728. if (parser->state == UCL_STATE_ERROR) {
  1729. return false;
  1730. }
  1731. }
  1732. else {
  1733. parser->state = UCL_STATE_AFTER_VALUE;
  1734. return true;
  1735. }
  1736. /* Fallback to normal string */
  1737. }
  1738. if (!ucl_parse_string_value (parser, chunk, &var_expand,
  1739. &need_unescape)) {
  1740. return false;
  1741. }
  1742. /* Cut trailing spaces */
  1743. stripped_spaces = 0;
  1744. while (ucl_test_character (*(chunk->pos - 1 - stripped_spaces),
  1745. UCL_CHARACTER_WHITESPACE)) {
  1746. stripped_spaces ++;
  1747. }
  1748. str_len = chunk->pos - c - stripped_spaces;
  1749. if (str_len <= 0) {
  1750. ucl_set_err (parser, UCL_ESYNTAX, "string value must not be empty",
  1751. &parser->err);
  1752. return false;
  1753. }
  1754. else if (str_len == 4 && memcmp (c, "null", 4) == 0) {
  1755. obj->len = 0;
  1756. obj->type = UCL_NULL;
  1757. }
  1758. else if (str_len == 3 && memcmp (c, "nan", 3) == 0) {
  1759. obj->len = 0;
  1760. obj->type = UCL_FLOAT;
  1761. obj->value.dv = NAN;
  1762. }
  1763. else if (str_len == 3 && memcmp (c, "inf", 3) == 0) {
  1764. obj->len = 0;
  1765. obj->type = UCL_FLOAT;
  1766. obj->value.dv = INFINITY;
  1767. }
  1768. else if (!ucl_maybe_parse_boolean (obj, c, str_len)) {
  1769. obj->type = UCL_STRING;
  1770. if ((str_len = ucl_copy_or_store_ptr (parser, c,
  1771. &obj->trash_stack[UCL_TRASH_VALUE],
  1772. &obj->value.sv, str_len, need_unescape,
  1773. false, var_expand, false)) == -1) {
  1774. return false;
  1775. }
  1776. obj->len = str_len;
  1777. }
  1778. parser->state = UCL_STATE_AFTER_VALUE;
  1779. return true;
  1780. break;
  1781. }
  1782. }
  1783. return true;
  1784. }
  1785. /**
  1786. * Handle after value data
  1787. * @param parser
  1788. * @param chunk
  1789. * @return
  1790. */
  1791. static bool
  1792. ucl_parse_after_value (struct ucl_parser *parser, struct ucl_chunk *chunk)
  1793. {
  1794. const unsigned char *p;
  1795. bool got_sep = false;
  1796. struct ucl_stack *st;
  1797. p = chunk->pos;
  1798. while (p < chunk->end) {
  1799. if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) {
  1800. /* Skip whitespaces */
  1801. ucl_chunk_skipc (chunk, p);
  1802. }
  1803. else if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
  1804. /* Skip comment */
  1805. if (!ucl_skip_comments (parser)) {
  1806. return false;
  1807. }
  1808. /* Treat comment as a separator */
  1809. got_sep = true;
  1810. p = chunk->pos;
  1811. }
  1812. else if (ucl_test_character (*p, UCL_CHARACTER_VALUE_END)) {
  1813. if (*p == '}' || *p == ']') {
  1814. if (parser->stack == NULL) {
  1815. ucl_set_err (parser, UCL_ESYNTAX,
  1816. "end of array or object detected without corresponding start",
  1817. &parser->err);
  1818. return false;
  1819. }
  1820. if ((*p == '}' && parser->stack->obj->type == UCL_OBJECT) ||
  1821. (*p == ']' && parser->stack->obj->type == UCL_ARRAY)) {
  1822. /* Pop all nested objects from a stack */
  1823. st = parser->stack;
  1824. if (!(st->e.params.flags & UCL_STACK_HAS_OBRACE)) {
  1825. parser->err_code = UCL_EUNPAIRED;
  1826. ucl_create_err (&parser->err,
  1827. "%s:%d object closed with } is not opened with { at line %d",
  1828. chunk->fname ? chunk->fname : "memory",
  1829. parser->chunks->line, st->e.params.line);
  1830. return false;
  1831. }
  1832. parser->stack = st->next;
  1833. UCL_FREE (sizeof (struct ucl_stack), st);
  1834. if (parser->cur_obj) {
  1835. ucl_attach_comment (parser, parser->cur_obj, true);
  1836. }
  1837. while (parser->stack != NULL) {
  1838. st = parser->stack;
  1839. if (st->next == NULL) {
  1840. break;
  1841. }
  1842. else if (st->next->e.params.level == st->e.params.level) {
  1843. break;
  1844. }
  1845. parser->stack = st->next;
  1846. parser->cur_obj = st->obj;
  1847. UCL_FREE (sizeof (struct ucl_stack), st);
  1848. }
  1849. }
  1850. else {
  1851. ucl_set_err (parser, UCL_ESYNTAX,
  1852. "unexpected terminating symbol detected",
  1853. &parser->err);
  1854. return false;
  1855. }
  1856. if (parser->stack == NULL) {
  1857. /* Ignore everything after a top object */
  1858. return true;
  1859. }
  1860. else {
  1861. ucl_chunk_skipc (chunk, p);
  1862. }
  1863. got_sep = true;
  1864. }
  1865. else {
  1866. /* Got a separator */
  1867. got_sep = true;
  1868. ucl_chunk_skipc (chunk, p);
  1869. }
  1870. }
  1871. else {
  1872. /* Anything else */
  1873. if (!got_sep) {
  1874. ucl_set_err (parser, UCL_ESYNTAX, "delimiter is missing",
  1875. &parser->err);
  1876. return false;
  1877. }
  1878. return true;
  1879. }
  1880. }
  1881. return true;
  1882. }
  1883. static bool
  1884. ucl_skip_macro_as_comment (struct ucl_parser *parser,
  1885. struct ucl_chunk *chunk)
  1886. {
  1887. const unsigned char *p, *c;
  1888. enum {
  1889. macro_skip_start = 0,
  1890. macro_has_symbols,
  1891. macro_has_obrace,
  1892. macro_has_quote,
  1893. macro_has_backslash,
  1894. macro_has_sqbrace,
  1895. macro_save
  1896. } state = macro_skip_start, prev_state = macro_skip_start;
  1897. p = chunk->pos;
  1898. c = chunk->pos;
  1899. while (p < chunk->end) {
  1900. switch (state) {
  1901. case macro_skip_start:
  1902. if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) {
  1903. state = macro_has_symbols;
  1904. }
  1905. else if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  1906. state = macro_save;
  1907. continue;
  1908. }
  1909. ucl_chunk_skipc (chunk, p);
  1910. break;
  1911. case macro_has_symbols:
  1912. if (*p == '{') {
  1913. state = macro_has_sqbrace;
  1914. }
  1915. else if (*p == '(') {
  1916. state = macro_has_obrace;
  1917. }
  1918. else if (*p == '"') {
  1919. state = macro_has_quote;
  1920. }
  1921. else if (*p == '\n') {
  1922. state = macro_save;
  1923. continue;
  1924. }
  1925. ucl_chunk_skipc (chunk, p);
  1926. break;
  1927. case macro_has_obrace:
  1928. if (*p == '\\') {
  1929. prev_state = state;
  1930. state = macro_has_backslash;
  1931. }
  1932. else if (*p == ')') {
  1933. state = macro_has_symbols;
  1934. }
  1935. ucl_chunk_skipc (chunk, p);
  1936. break;
  1937. case macro_has_sqbrace:
  1938. if (*p == '\\') {
  1939. prev_state = state;
  1940. state = macro_has_backslash;
  1941. }
  1942. else if (*p == '}') {
  1943. state = macro_save;
  1944. }
  1945. ucl_chunk_skipc (chunk, p);
  1946. break;
  1947. case macro_has_quote:
  1948. if (*p == '\\') {
  1949. prev_state = state;
  1950. state = macro_has_backslash;
  1951. }
  1952. else if (*p == '"') {
  1953. state = macro_save;
  1954. }
  1955. ucl_chunk_skipc (chunk, p);
  1956. break;
  1957. case macro_has_backslash:
  1958. state = prev_state;
  1959. ucl_chunk_skipc (chunk, p);
  1960. break;
  1961. case macro_save:
  1962. if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
  1963. ucl_save_comment (parser, c, p - c);
  1964. }
  1965. return true;
  1966. }
  1967. }
  1968. return false;
  1969. }
  1970. /**
  1971. * Handle macro data
  1972. * @param parser
  1973. * @param chunk
  1974. * @param marco
  1975. * @param macro_start
  1976. * @param macro_len
  1977. * @return
  1978. */
  1979. static bool
  1980. ucl_parse_macro_value (struct ucl_parser *parser,
  1981. struct ucl_chunk *chunk, struct ucl_macro *macro,
  1982. unsigned char const **macro_start, size_t *macro_len)
  1983. {
  1984. const unsigned char *p, *c;
  1985. bool need_unescape = false, ucl_escape = false, var_expand = false;
  1986. p = chunk->pos;
  1987. switch (*p) {
  1988. case '"':
  1989. /* We have macro value encoded in quotes */
  1990. c = p;
  1991. ucl_chunk_skipc (chunk, p);
  1992. if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
  1993. return false;
  1994. }
  1995. *macro_start = c + 1;
  1996. *macro_len = chunk->pos - c - 2;
  1997. p = chunk->pos;
  1998. break;
  1999. case '{':
  2000. /* We got a multiline macro body */
  2001. ucl_chunk_skipc (chunk, p);
  2002. /* Skip spaces at the beginning */
  2003. while (p < chunk->end) {
  2004. if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  2005. ucl_chunk_skipc (chunk, p);
  2006. }
  2007. else {
  2008. break;
  2009. }
  2010. }
  2011. c = p;
  2012. while (p < chunk->end) {
  2013. if (*p == '}') {
  2014. break;
  2015. }
  2016. ucl_chunk_skipc (chunk, p);
  2017. }
  2018. *macro_start = c;
  2019. *macro_len = p - c;
  2020. ucl_chunk_skipc (chunk, p);
  2021. break;
  2022. default:
  2023. /* Macro is not enclosed in quotes or braces */
  2024. c = p;
  2025. while (p < chunk->end) {
  2026. if (ucl_lex_is_atom_end (*p)) {
  2027. break;
  2028. }
  2029. ucl_chunk_skipc (chunk, p);
  2030. }
  2031. *macro_start = c;
  2032. *macro_len = p - c;
  2033. break;
  2034. }
  2035. /* We are at the end of a macro */
  2036. /* Skip ';' and space characters and return to previous state */
  2037. while (p < chunk->end) {
  2038. if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) && *p != ';') {
  2039. break;
  2040. }
  2041. ucl_chunk_skipc (chunk, p);
  2042. }
  2043. return true;
  2044. }
  2045. /**
  2046. * Parse macro arguments as UCL object
  2047. * @param parser parser structure
  2048. * @param chunk the current data chunk
  2049. * @return
  2050. */
  2051. static ucl_object_t *
  2052. ucl_parse_macro_arguments (struct ucl_parser *parser,
  2053. struct ucl_chunk *chunk)
  2054. {
  2055. ucl_object_t *res = NULL;
  2056. struct ucl_parser *params_parser;
  2057. int obraces = 1, ebraces = 0, state = 0;
  2058. const unsigned char *p, *c;
  2059. size_t args_len = 0;
  2060. struct ucl_parser_saved_state saved;
  2061. saved.column = chunk->column;
  2062. saved.line = chunk->line;
  2063. saved.pos = chunk->pos;
  2064. saved.remain = chunk->remain;
  2065. p = chunk->pos;
  2066. if (*p != '(' || chunk->remain < 2) {
  2067. return NULL;
  2068. }
  2069. /* Set begin and start */
  2070. ucl_chunk_skipc (chunk, p);
  2071. c = p;
  2072. while ((p) < (chunk)->end) {
  2073. switch (state) {
  2074. case 0:
  2075. /* Parse symbols and check for '(', ')' and '"' */
  2076. if (*p == '(') {
  2077. obraces ++;
  2078. }
  2079. else if (*p == ')') {
  2080. ebraces ++;
  2081. }
  2082. else if (*p == '"') {
  2083. state = 1;
  2084. }
  2085. /* Check pairing */
  2086. if (obraces == ebraces) {
  2087. state = 99;
  2088. }
  2089. else {
  2090. args_len ++;
  2091. }
  2092. /* Check overflow */
  2093. if (chunk->remain == 0) {
  2094. goto restore_chunk;
  2095. }
  2096. ucl_chunk_skipc (chunk, p);
  2097. break;
  2098. case 1:
  2099. /* We have quote character, so skip all but quotes */
  2100. if (*p == '"' && *(p - 1) != '\\') {
  2101. state = 0;
  2102. }
  2103. if (chunk->remain == 0) {
  2104. goto restore_chunk;
  2105. }
  2106. args_len ++;
  2107. ucl_chunk_skipc (chunk, p);
  2108. break;
  2109. case 99:
  2110. /*
  2111. * We have read the full body of arguments, so we need to parse and set
  2112. * object from that
  2113. */
  2114. params_parser = ucl_parser_new (parser->flags);
  2115. if (!ucl_parser_add_chunk (params_parser, c, args_len)) {
  2116. ucl_set_err (parser, UCL_ESYNTAX, "macro arguments parsing error",
  2117. &parser->err);
  2118. }
  2119. else {
  2120. res = ucl_parser_get_object (params_parser);
  2121. }
  2122. ucl_parser_free (params_parser);
  2123. return res;
  2124. break;
  2125. }
  2126. }
  2127. return res;
  2128. restore_chunk:
  2129. chunk->column = saved.column;
  2130. chunk->line = saved.line;
  2131. chunk->pos = saved.pos;
  2132. chunk->remain = saved.remain;
  2133. return NULL;
  2134. }
  2135. #define SKIP_SPACES_COMMENTS(parser, chunk, p) do { \
  2136. while ((p) < (chunk)->end) { \
  2137. if (!ucl_test_character (*(p), UCL_CHARACTER_WHITESPACE_UNSAFE)) { \
  2138. if ((chunk)->remain >= 2 && ucl_lex_is_comment ((p)[0], (p)[1])) { \
  2139. if (!ucl_skip_comments (parser)) { \
  2140. return false; \
  2141. } \
  2142. p = (chunk)->pos; \
  2143. } \
  2144. break; \
  2145. } \
  2146. ucl_chunk_skipc (chunk, p); \
  2147. } \
  2148. } while(0)
  2149. /**
  2150. * Handle the main states of rcl parser
  2151. * @param parser parser structure
  2152. * @return true if chunk has been parsed and false in case of error
  2153. */
  2154. static bool
  2155. ucl_state_machine (struct ucl_parser *parser)
  2156. {
  2157. ucl_object_t *obj, *macro_args;
  2158. struct ucl_chunk *chunk = parser->chunks;
  2159. const unsigned char *p, *c = NULL, *macro_start = NULL;
  2160. unsigned char *macro_escaped;
  2161. size_t macro_len = 0;
  2162. struct ucl_macro *macro = NULL;
  2163. bool next_key = false, end_of_object = false, ret;
  2164. if (parser->top_obj == NULL) {
  2165. parser->state = UCL_STATE_INIT;
  2166. }
  2167. p = chunk->pos;
  2168. while (chunk->pos < chunk->end) {
  2169. switch (parser->state) {
  2170. case UCL_STATE_INIT:
  2171. /*
  2172. * At the init state we can either go to the parse array or object
  2173. * if we got [ or { correspondingly or can just treat new data as
  2174. * a key of newly created object
  2175. */
  2176. if (!ucl_skip_comments (parser)) {
  2177. parser->prev_state = parser->state;
  2178. parser->state = UCL_STATE_ERROR;
  2179. return false;
  2180. }
  2181. else {
  2182. bool seen_obrace = false;
  2183. /* Skip any spaces */
  2184. while (p < chunk->end && ucl_test_character (*p,
  2185. UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  2186. ucl_chunk_skipc (chunk, p);
  2187. }
  2188. p = chunk->pos;
  2189. if (p < chunk->end) {
  2190. if (*p == '[') {
  2191. parser->state = UCL_STATE_VALUE;
  2192. ucl_chunk_skipc (chunk, p);
  2193. seen_obrace = true;
  2194. }
  2195. else {
  2196. if (*p == '{') {
  2197. ucl_chunk_skipc (chunk, p);
  2198. parser->state = UCL_STATE_KEY_OBRACE;
  2199. seen_obrace = true;
  2200. }
  2201. else {
  2202. parser->state = UCL_STATE_KEY;
  2203. }
  2204. }
  2205. }
  2206. if (parser->top_obj == NULL) {
  2207. if (parser->state == UCL_STATE_VALUE) {
  2208. obj = ucl_parser_add_container (NULL, parser, true, 0,
  2209. seen_obrace);
  2210. }
  2211. else {
  2212. obj = ucl_parser_add_container (NULL, parser, false, 0,
  2213. seen_obrace);
  2214. }
  2215. if (obj == NULL) {
  2216. return false;
  2217. }
  2218. parser->top_obj = obj;
  2219. parser->cur_obj = obj;
  2220. }
  2221. }
  2222. break;
  2223. case UCL_STATE_KEY:
  2224. case UCL_STATE_KEY_OBRACE:
  2225. /* Skip any spaces */
  2226. while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  2227. ucl_chunk_skipc (chunk, p);
  2228. }
  2229. if (p == chunk->end || *p == '}') {
  2230. /* We have the end of an object */
  2231. parser->state = UCL_STATE_AFTER_VALUE;
  2232. continue;
  2233. }
  2234. if (parser->stack == NULL) {
  2235. /* No objects are on stack, but we want to parse a key */
  2236. ucl_set_err (parser, UCL_ESYNTAX, "top object is finished but the parser "
  2237. "expects a key", &parser->err);
  2238. parser->prev_state = parser->state;
  2239. parser->state = UCL_STATE_ERROR;
  2240. return false;
  2241. }
  2242. if (!ucl_parse_key (parser, chunk, &next_key, &end_of_object)) {
  2243. parser->prev_state = parser->state;
  2244. parser->state = UCL_STATE_ERROR;
  2245. return false;
  2246. }
  2247. if (end_of_object) {
  2248. p = chunk->pos;
  2249. parser->state = UCL_STATE_AFTER_VALUE;
  2250. continue;
  2251. }
  2252. else if (parser->state != UCL_STATE_MACRO_NAME) {
  2253. if (next_key && parser->stack->obj->type == UCL_OBJECT) {
  2254. /* Parse more keys and nest objects accordingly */
  2255. obj = ucl_parser_add_container (parser->cur_obj,
  2256. parser,
  2257. false,
  2258. parser->stack->e.params.level + 1,
  2259. parser->state == UCL_STATE_KEY_OBRACE);
  2260. if (obj == NULL) {
  2261. return false;
  2262. }
  2263. }
  2264. else {
  2265. parser->state = UCL_STATE_VALUE;
  2266. }
  2267. }
  2268. else {
  2269. c = chunk->pos;
  2270. }
  2271. p = chunk->pos;
  2272. break;
  2273. case UCL_STATE_VALUE:
  2274. /* We need to check what we do have */
  2275. if (!parser->cur_obj || !ucl_parse_value (parser, chunk)) {
  2276. parser->prev_state = parser->state;
  2277. parser->state = UCL_STATE_ERROR;
  2278. return false;
  2279. }
  2280. /* State is set in ucl_parse_value call */
  2281. p = chunk->pos;
  2282. break;
  2283. case UCL_STATE_AFTER_VALUE:
  2284. if (!ucl_parse_after_value (parser, chunk)) {
  2285. parser->prev_state = parser->state;
  2286. parser->state = UCL_STATE_ERROR;
  2287. return false;
  2288. }
  2289. if (parser->stack != NULL) {
  2290. if (parser->stack->obj->type == UCL_OBJECT) {
  2291. parser->state = UCL_STATE_KEY;
  2292. }
  2293. else {
  2294. /* Array */
  2295. parser->state = UCL_STATE_VALUE;
  2296. }
  2297. }
  2298. else {
  2299. /* Skip everything at the end */
  2300. return true;
  2301. }
  2302. p = chunk->pos;
  2303. break;
  2304. case UCL_STATE_MACRO_NAME:
  2305. if (parser->flags & UCL_PARSER_DISABLE_MACRO) {
  2306. if (!ucl_skip_macro_as_comment (parser, chunk)) {
  2307. /* We have invalid macro */
  2308. ucl_create_err (&parser->err,
  2309. "error at %s:%d at column %d: invalid macro",
  2310. chunk->fname ? chunk->fname : "memory",
  2311. chunk->line,
  2312. chunk->column);
  2313. parser->state = UCL_STATE_ERROR;
  2314. return false;
  2315. }
  2316. else {
  2317. p = chunk->pos;
  2318. parser->state = parser->prev_state;
  2319. }
  2320. }
  2321. else {
  2322. if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) &&
  2323. *p != '(') {
  2324. ucl_chunk_skipc (chunk, p);
  2325. }
  2326. else {
  2327. if (c != NULL && p - c > 0) {
  2328. /* We got macro name */
  2329. macro_len = (size_t) (p - c);
  2330. HASH_FIND (hh, parser->macroes, c, macro_len, macro);
  2331. if (macro == NULL) {
  2332. ucl_create_err (&parser->err,
  2333. "error at %s:%d at column %d: "
  2334. "unknown macro: '%.*s', character: '%c'",
  2335. chunk->fname ? chunk->fname : "memory",
  2336. chunk->line,
  2337. chunk->column,
  2338. (int) (p - c),
  2339. c,
  2340. *chunk->pos);
  2341. parser->state = UCL_STATE_ERROR;
  2342. return false;
  2343. }
  2344. /* Now we need to skip all spaces */
  2345. SKIP_SPACES_COMMENTS(parser, chunk, p);
  2346. parser->state = UCL_STATE_MACRO;
  2347. }
  2348. else {
  2349. /* We have invalid macro name */
  2350. ucl_create_err (&parser->err,
  2351. "error at %s:%d at column %d: invalid macro name",
  2352. chunk->fname ? chunk->fname : "memory",
  2353. chunk->line,
  2354. chunk->column);
  2355. parser->state = UCL_STATE_ERROR;
  2356. return false;
  2357. }
  2358. }
  2359. }
  2360. break;
  2361. case UCL_STATE_MACRO:
  2362. if (*chunk->pos == '(') {
  2363. macro_args = ucl_parse_macro_arguments (parser, chunk);
  2364. p = chunk->pos;
  2365. if (macro_args) {
  2366. SKIP_SPACES_COMMENTS(parser, chunk, p);
  2367. }
  2368. }
  2369. else {
  2370. macro_args = NULL;
  2371. }
  2372. if (!ucl_parse_macro_value (parser, chunk, macro,
  2373. &macro_start, &macro_len)) {
  2374. parser->prev_state = parser->state;
  2375. parser->state = UCL_STATE_ERROR;
  2376. return false;
  2377. }
  2378. macro_len = ucl_expand_variable (parser, &macro_escaped,
  2379. macro_start, macro_len);
  2380. parser->state = parser->prev_state;
  2381. if (macro_escaped == NULL && macro != NULL) {
  2382. if (macro->is_context) {
  2383. ret = macro->h.context_handler (macro_start, macro_len,
  2384. macro_args,
  2385. parser->top_obj,
  2386. macro->ud);
  2387. }
  2388. else {
  2389. ret = macro->h.handler (macro_start, macro_len, macro_args,
  2390. macro->ud);
  2391. }
  2392. }
  2393. else if (macro != NULL) {
  2394. if (macro->is_context) {
  2395. ret = macro->h.context_handler (macro_escaped, macro_len,
  2396. macro_args,
  2397. parser->top_obj,
  2398. macro->ud);
  2399. }
  2400. else {
  2401. ret = macro->h.handler (macro_escaped, macro_len, macro_args,
  2402. macro->ud);
  2403. }
  2404. UCL_FREE (macro_len + 1, macro_escaped);
  2405. }
  2406. else {
  2407. ret = false;
  2408. ucl_set_err (parser, UCL_EINTERNAL,
  2409. "internal error: parser has macro undefined", &parser->err);
  2410. }
  2411. /*
  2412. * Chunk can be modified within macro handler
  2413. */
  2414. chunk = parser->chunks;
  2415. p = chunk->pos;
  2416. if (macro_args) {
  2417. ucl_object_unref (macro_args);
  2418. }
  2419. if (!ret) {
  2420. return false;
  2421. }
  2422. break;
  2423. default:
  2424. ucl_set_err (parser, UCL_EINTERNAL,
  2425. "internal error: parser is in an unknown state", &parser->err);
  2426. parser->state = UCL_STATE_ERROR;
  2427. return false;
  2428. }
  2429. }
  2430. if (parser->last_comment) {
  2431. if (parser->cur_obj) {
  2432. ucl_attach_comment (parser, parser->cur_obj, true);
  2433. }
  2434. else if (parser->stack && parser->stack->obj) {
  2435. ucl_attach_comment (parser, parser->stack->obj, true);
  2436. }
  2437. else if (parser->top_obj) {
  2438. ucl_attach_comment (parser, parser->top_obj, true);
  2439. }
  2440. else {
  2441. ucl_object_unref (parser->last_comment);
  2442. }
  2443. }
  2444. if (parser->stack != NULL && parser->state != UCL_STATE_ERROR) {
  2445. struct ucl_stack *st;
  2446. bool has_error = false;
  2447. LL_FOREACH (parser->stack, st) {
  2448. if (st->chunk != parser->chunks) {
  2449. break; /* Not our chunk, give up */
  2450. }
  2451. if (st->e.params.flags & UCL_STACK_HAS_OBRACE) {
  2452. if (parser->err == NULL) {
  2453. utstring_new (parser->err);
  2454. }
  2455. utstring_printf (parser->err, "%s:%d unmatched open brace at %d; ",
  2456. chunk->fname ? chunk->fname : "memory",
  2457. parser->chunks->line,
  2458. st->e.params.line);
  2459. has_error = true;
  2460. }
  2461. }
  2462. if (has_error) {
  2463. parser->err_code = UCL_EUNPAIRED;
  2464. return false;
  2465. }
  2466. }
  2467. return true;
  2468. }
  2469. #define UPRM_SAFE(fn, a, b, c, el) do { \
  2470. if (!fn(a, b, c, a)) \
  2471. goto el; \
  2472. } while (0)
  2473. struct ucl_parser*
  2474. ucl_parser_new (int flags)
  2475. {
  2476. struct ucl_parser *parser;
  2477. parser = UCL_ALLOC (sizeof (struct ucl_parser));
  2478. if (parser == NULL) {
  2479. return NULL;
  2480. }
  2481. memset (parser, 0, sizeof (struct ucl_parser));
  2482. UPRM_SAFE(ucl_parser_register_macro, parser, "include", ucl_include_handler, e0);
  2483. UPRM_SAFE(ucl_parser_register_macro, parser, "try_include", ucl_try_include_handler, e0);
  2484. UPRM_SAFE(ucl_parser_register_macro, parser, "includes", ucl_includes_handler, e0);
  2485. UPRM_SAFE(ucl_parser_register_macro, parser, "priority", ucl_priority_handler, e0);
  2486. UPRM_SAFE(ucl_parser_register_macro, parser, "load", ucl_load_handler, e0);
  2487. UPRM_SAFE(ucl_parser_register_context_macro, parser, "inherit", ucl_inherit_handler, e0);
  2488. parser->flags = flags;
  2489. parser->includepaths = NULL;
  2490. if (flags & UCL_PARSER_SAVE_COMMENTS) {
  2491. parser->comments = ucl_object_typed_new (UCL_OBJECT);
  2492. }
  2493. if (!(flags & UCL_PARSER_NO_FILEVARS)) {
  2494. /* Initial assumption about filevars */
  2495. ucl_parser_set_filevars (parser, NULL, false);
  2496. }
  2497. return parser;
  2498. e0:
  2499. ucl_parser_free(parser);
  2500. return NULL;
  2501. }
  2502. bool
  2503. ucl_parser_set_default_priority (struct ucl_parser *parser, unsigned prio)
  2504. {
  2505. if (parser == NULL) {
  2506. return false;
  2507. }
  2508. parser->default_priority = prio;
  2509. return true;
  2510. }
  2511. int
  2512. ucl_parser_get_default_priority (struct ucl_parser *parser)
  2513. {
  2514. if (parser == NULL) {
  2515. return -1;
  2516. }
  2517. return parser->default_priority;
  2518. }
  2519. bool
  2520. ucl_parser_register_macro (struct ucl_parser *parser, const char *macro,
  2521. ucl_macro_handler handler, void* ud)
  2522. {
  2523. struct ucl_macro *new;
  2524. if (macro == NULL || handler == NULL) {
  2525. return false;
  2526. }
  2527. new = UCL_ALLOC (sizeof (struct ucl_macro));
  2528. if (new == NULL) {
  2529. return false;
  2530. }
  2531. memset (new, 0, sizeof (struct ucl_macro));
  2532. new->h.handler = handler;
  2533. new->name = strdup (macro);
  2534. if (new->name == NULL) {
  2535. UCL_FREE (sizeof (struct ucl_macro), new);
  2536. return false;
  2537. }
  2538. new->ud = ud;
  2539. HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new);
  2540. return true;
  2541. }
  2542. bool
  2543. ucl_parser_register_context_macro (struct ucl_parser *parser, const char *macro,
  2544. ucl_context_macro_handler handler, void* ud)
  2545. {
  2546. struct ucl_macro *new;
  2547. if (macro == NULL || handler == NULL) {
  2548. return false;
  2549. }
  2550. new = UCL_ALLOC (sizeof (struct ucl_macro));
  2551. if (new == NULL) {
  2552. return false;
  2553. }
  2554. memset (new, 0, sizeof (struct ucl_macro));
  2555. new->h.context_handler = handler;
  2556. new->name = strdup (macro);
  2557. if (new->name == NULL) {
  2558. UCL_FREE (sizeof (struct ucl_macro), new);
  2559. return false;
  2560. }
  2561. new->ud = ud;
  2562. new->is_context = true;
  2563. HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new);
  2564. return true;
  2565. }
  2566. void
  2567. ucl_parser_register_variable (struct ucl_parser *parser, const char *var,
  2568. const char *value)
  2569. {
  2570. struct ucl_variable *new = NULL, *cur;
  2571. if (var == NULL) {
  2572. return;
  2573. }
  2574. /* Find whether a variable already exists */
  2575. LL_FOREACH (parser->variables, cur) {
  2576. if (strcmp (cur->var, var) == 0) {
  2577. new = cur;
  2578. break;
  2579. }
  2580. }
  2581. if (value == NULL) {
  2582. if (new != NULL) {
  2583. /* Remove variable */
  2584. DL_DELETE (parser->variables, new);
  2585. free (new->var);
  2586. free (new->value);
  2587. UCL_FREE (sizeof (struct ucl_variable), new);
  2588. }
  2589. else {
  2590. /* Do nothing */
  2591. return;
  2592. }
  2593. }
  2594. else {
  2595. if (new == NULL) {
  2596. new = UCL_ALLOC (sizeof (struct ucl_variable));
  2597. if (new == NULL) {
  2598. return;
  2599. }
  2600. memset (new, 0, sizeof (struct ucl_variable));
  2601. new->var = strdup (var);
  2602. new->var_len = strlen (var);
  2603. new->value = strdup (value);
  2604. new->value_len = strlen (value);
  2605. DL_APPEND (parser->variables, new);
  2606. }
  2607. else {
  2608. free (new->value);
  2609. new->value = strdup (value);
  2610. new->value_len = strlen (value);
  2611. }
  2612. }
  2613. }
  2614. void
  2615. ucl_parser_set_variables_handler (struct ucl_parser *parser,
  2616. ucl_variable_handler handler, void *ud)
  2617. {
  2618. parser->var_handler = handler;
  2619. parser->var_data = ud;
  2620. }
  2621. bool
  2622. ucl_parser_add_chunk_full (struct ucl_parser *parser, const unsigned char *data,
  2623. size_t len, unsigned priority, enum ucl_duplicate_strategy strat,
  2624. enum ucl_parse_type parse_type)
  2625. {
  2626. struct ucl_chunk *chunk;
  2627. struct ucl_parser_special_handler *special_handler;
  2628. if (parser == NULL) {
  2629. return false;
  2630. }
  2631. if (data == NULL && len != 0) {
  2632. ucl_create_err (&parser->err, "invalid chunk added");
  2633. return false;
  2634. }
  2635. if (parser->state != UCL_STATE_ERROR) {
  2636. chunk = UCL_ALLOC (sizeof (struct ucl_chunk));
  2637. if (chunk == NULL) {
  2638. ucl_create_err (&parser->err, "cannot allocate chunk structure");
  2639. return false;
  2640. }
  2641. memset (chunk, 0, sizeof (*chunk));
  2642. /* Apply all matching handlers from the first to the last */
  2643. LL_FOREACH (parser->special_handlers, special_handler) {
  2644. if ((special_handler->flags & UCL_SPECIAL_HANDLER_PREPROCESS_ALL) ||
  2645. (len >= special_handler->magic_len &&
  2646. memcmp (data, special_handler->magic, special_handler->magic_len) == 0)) {
  2647. unsigned char *ndata = NULL;
  2648. size_t nlen = 0;
  2649. if (!special_handler->handler (parser, data, len, &ndata, &nlen,
  2650. special_handler->user_data)) {
  2651. UCL_FREE(sizeof (struct ucl_chunk), chunk);
  2652. ucl_create_err (&parser->err, "call for external handler failed");
  2653. return false;
  2654. }
  2655. struct ucl_parser_special_handler_chain *nchain;
  2656. nchain = UCL_ALLOC (sizeof (*nchain));
  2657. nchain->begin = ndata;
  2658. nchain->len = nlen;
  2659. nchain->special_handler = special_handler;
  2660. /* Free order is reversed */
  2661. LL_PREPEND (chunk->special_handlers, nchain);
  2662. data = ndata;
  2663. len = nlen;
  2664. }
  2665. }
  2666. if (parse_type == UCL_PARSE_AUTO && len > 0) {
  2667. /* We need to detect parse type by the first symbol */
  2668. if ((*data & 0x80) == 0x80 && (*data >= 0xdc && *data <= 0xdf)) {
  2669. parse_type = UCL_PARSE_MSGPACK;
  2670. }
  2671. else if (*data == '(') {
  2672. parse_type = UCL_PARSE_CSEXP;
  2673. }
  2674. else {
  2675. parse_type = UCL_PARSE_UCL;
  2676. }
  2677. }
  2678. chunk->begin = data;
  2679. chunk->remain = len;
  2680. chunk->pos = chunk->begin;
  2681. chunk->end = chunk->begin + len;
  2682. chunk->line = 1;
  2683. chunk->column = 0;
  2684. chunk->priority = priority;
  2685. chunk->strategy = strat;
  2686. chunk->parse_type = parse_type;
  2687. if (parser->cur_file) {
  2688. chunk->fname = strdup (parser->cur_file);
  2689. }
  2690. LL_PREPEND (parser->chunks, chunk);
  2691. parser->recursion ++;
  2692. if (parser->recursion > UCL_MAX_RECURSION) {
  2693. ucl_create_err (&parser->err, "maximum include nesting limit is reached: %d",
  2694. parser->recursion);
  2695. return false;
  2696. }
  2697. if (len > 0) {
  2698. /* Need to parse something */
  2699. switch (parse_type) {
  2700. default:
  2701. case UCL_PARSE_UCL:
  2702. return ucl_state_machine (parser);
  2703. case UCL_PARSE_MSGPACK:
  2704. return ucl_parse_msgpack (parser);
  2705. case UCL_PARSE_CSEXP:
  2706. return ucl_parse_csexp (parser);
  2707. }
  2708. }
  2709. else {
  2710. /* Just add empty chunk and go forward */
  2711. if (parser->top_obj == NULL) {
  2712. /*
  2713. * In case of empty object, create one to indicate that we've
  2714. * read something
  2715. */
  2716. parser->top_obj = ucl_object_new_full (UCL_OBJECT, priority);
  2717. }
  2718. return true;
  2719. }
  2720. }
  2721. ucl_create_err (&parser->err, "a parser is in an invalid state");
  2722. return false;
  2723. }
  2724. bool
  2725. ucl_parser_add_chunk_priority (struct ucl_parser *parser,
  2726. const unsigned char *data, size_t len, unsigned priority)
  2727. {
  2728. /* We dereference parser, so this check is essential */
  2729. if (parser == NULL) {
  2730. return false;
  2731. }
  2732. return ucl_parser_add_chunk_full (parser, data, len,
  2733. priority, UCL_DUPLICATE_APPEND, UCL_PARSE_UCL);
  2734. }
  2735. bool
  2736. ucl_parser_add_chunk (struct ucl_parser *parser, const unsigned char *data,
  2737. size_t len)
  2738. {
  2739. if (parser == NULL) {
  2740. return false;
  2741. }
  2742. return ucl_parser_add_chunk_full (parser, data, len,
  2743. parser->default_priority, UCL_DUPLICATE_APPEND, UCL_PARSE_UCL);
  2744. }
  2745. bool
  2746. ucl_parser_insert_chunk (struct ucl_parser *parser, const unsigned char *data,
  2747. size_t len)
  2748. {
  2749. if (parser == NULL || parser->top_obj == NULL) {
  2750. return false;
  2751. }
  2752. bool res;
  2753. struct ucl_chunk *chunk;
  2754. int state = parser->state;
  2755. parser->state = UCL_STATE_INIT;
  2756. /* Prevent inserted chunks from unintentionally closing the current object */
  2757. if (parser->stack != NULL && parser->stack->next != NULL) {
  2758. parser->stack->e.params.level = parser->stack->next->e.params.level;
  2759. }
  2760. res = ucl_parser_add_chunk_full (parser, data, len, parser->chunks->priority,
  2761. parser->chunks->strategy, parser->chunks->parse_type);
  2762. /* Remove chunk from the stack */
  2763. chunk = parser->chunks;
  2764. if (chunk != NULL) {
  2765. parser->chunks = chunk->next;
  2766. ucl_chunk_free (chunk);
  2767. parser->recursion --;
  2768. }
  2769. parser->state = state;
  2770. return res;
  2771. }
  2772. bool
  2773. ucl_parser_add_string_priority (struct ucl_parser *parser, const char *data,
  2774. size_t len, unsigned priority)
  2775. {
  2776. if (data == NULL) {
  2777. ucl_create_err (&parser->err, "invalid string added");
  2778. return false;
  2779. }
  2780. if (len == 0) {
  2781. len = strlen (data);
  2782. }
  2783. return ucl_parser_add_chunk_priority (parser,
  2784. (const unsigned char *)data, len, priority);
  2785. }
  2786. bool
  2787. ucl_parser_add_string (struct ucl_parser *parser, const char *data,
  2788. size_t len)
  2789. {
  2790. if (parser == NULL) {
  2791. return false;
  2792. }
  2793. return ucl_parser_add_string_priority (parser,
  2794. (const unsigned char *)data, len, parser->default_priority);
  2795. }
  2796. bool
  2797. ucl_set_include_path (struct ucl_parser *parser, ucl_object_t *paths)
  2798. {
  2799. if (parser == NULL || paths == NULL) {
  2800. return false;
  2801. }
  2802. if (parser->includepaths == NULL) {
  2803. parser->includepaths = ucl_object_copy (paths);
  2804. }
  2805. else {
  2806. ucl_object_unref (parser->includepaths);
  2807. parser->includepaths = ucl_object_copy (paths);
  2808. }
  2809. if (parser->includepaths == NULL) {
  2810. return false;
  2811. }
  2812. return true;
  2813. }
  2814. unsigned char ucl_parser_chunk_peek (struct ucl_parser *parser)
  2815. {
  2816. if (parser == NULL || parser->chunks == NULL || parser->chunks->pos == NULL || parser->chunks->end == NULL ||
  2817. parser->chunks->pos == parser->chunks->end) {
  2818. return 0;
  2819. }
  2820. return( *parser->chunks->pos );
  2821. }
  2822. bool ucl_parser_chunk_skip (struct ucl_parser *parser)
  2823. {
  2824. if (parser == NULL || parser->chunks == NULL || parser->chunks->pos == NULL || parser->chunks->end == NULL ||
  2825. parser->chunks->pos == parser->chunks->end) {
  2826. return false;
  2827. }
  2828. const unsigned char *p = parser->chunks->pos;
  2829. ucl_chunk_skipc( parser->chunks, p );
  2830. if( parser->chunks->pos != NULL ) return true;
  2831. return false;
  2832. }
  2833. ucl_object_t*
  2834. ucl_parser_get_current_stack_object (struct ucl_parser *parser, unsigned int depth)
  2835. {
  2836. ucl_object_t *obj;
  2837. if (parser == NULL || parser->stack == NULL) {
  2838. return NULL;
  2839. }
  2840. struct ucl_stack *stack = parser->stack;
  2841. if(stack == NULL || stack->obj == NULL || ucl_object_type (stack->obj) != UCL_OBJECT)
  2842. {
  2843. return NULL;
  2844. }
  2845. for( unsigned int i = 0; i < depth; ++i )
  2846. {
  2847. stack = stack->next;
  2848. if(stack == NULL || stack->obj == NULL || ucl_object_type (stack->obj) != UCL_OBJECT)
  2849. {
  2850. return NULL;
  2851. }
  2852. }
  2853. obj = ucl_object_ref (stack->obj);
  2854. return obj;
  2855. }