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 70KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212
  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 (chunk->remain > 0 && *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. int is_neg = 0;
  671. double dv = 0;
  672. int64_t lv = 0;
  673. if (*p == '-') {
  674. is_neg = 1;
  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. p ++;
  691. }
  692. else if (allow_double) {
  693. if (p == c) {
  694. /* Empty digits sequence, not a number */
  695. *pos = start;
  696. return EINVAL;
  697. }
  698. else if (*p == '.') {
  699. if (got_dot) {
  700. /* Double dots, not a number */
  701. *pos = start;
  702. return EINVAL;
  703. }
  704. else {
  705. got_dot = true;
  706. need_double = true;
  707. p ++;
  708. }
  709. }
  710. else if (*p == 'e' || *p == 'E') {
  711. if (got_exp) {
  712. /* Double exp, not a number */
  713. *pos = start;
  714. return EINVAL;
  715. }
  716. else {
  717. got_exp = true;
  718. need_double = true;
  719. p ++;
  720. if (p >= end) {
  721. *pos = start;
  722. return EINVAL;
  723. }
  724. if (!isdigit (*p) && *p != '+' && *p != '-') {
  725. /* Wrong exponent sign */
  726. *pos = start;
  727. return EINVAL;
  728. }
  729. else {
  730. p ++;
  731. }
  732. }
  733. }
  734. else {
  735. /* Got the end of the number, need to check */
  736. break;
  737. }
  738. }
  739. else if (!allow_double && *p == '.') {
  740. /* Unexpected dot */
  741. *pos = start;
  742. return EINVAL;
  743. }
  744. else {
  745. break;
  746. }
  747. }
  748. if (!valid_start || p == c) {
  749. *pos = start;
  750. return EINVAL;
  751. }
  752. char numbuf[128];
  753. if ((size_t)(p - c + 1) >= sizeof(numbuf)) {
  754. *pos = start;
  755. return EINVAL;
  756. }
  757. if (is_neg) {
  758. numbuf[0] = '-';
  759. ucl_strlcpy (&numbuf[1], c, p - c + 1);
  760. }
  761. else {
  762. ucl_strlcpy (numbuf, c, p - c + 1);
  763. }
  764. errno = 0;
  765. if (need_double) {
  766. dv = strtod (numbuf, &endptr);
  767. }
  768. else {
  769. if (is_hex) {
  770. lv = strtoimax (numbuf, &endptr, 16);
  771. }
  772. else {
  773. lv = strtoimax (numbuf, &endptr, 10);
  774. }
  775. }
  776. if (errno == ERANGE) {
  777. *pos = start;
  778. return ERANGE;
  779. }
  780. /* Now check endptr and move it from numbuf to the real ending */
  781. if (endptr != NULL) {
  782. long shift = endptr - numbuf - is_neg;
  783. endptr = (char *)c + shift;
  784. }
  785. if (endptr >= end) {
  786. p = end;
  787. goto set_obj;
  788. }
  789. if (endptr == NULL || ucl_lex_is_atom_end (*endptr) || *endptr == '\0') {
  790. p = endptr;
  791. goto set_obj;
  792. }
  793. if (endptr < end && endptr != start) {
  794. switch (*p) {
  795. case 'm':
  796. case 'M':
  797. case 'g':
  798. case 'G':
  799. case 'k':
  800. case 'K':
  801. if (end - p >= 2) {
  802. if (p[1] == 's' || p[1] == 'S') {
  803. /* Milliseconds */
  804. if (!need_double) {
  805. need_double = true;
  806. dv = lv;
  807. }
  808. is_time = true;
  809. if (p[0] == 'm' || p[0] == 'M') {
  810. dv /= 1000.;
  811. }
  812. else {
  813. dv *= ucl_lex_num_multiplier (*p, false);
  814. }
  815. p += 2;
  816. if (end - p > 0 && !ucl_lex_is_atom_end (*p)) {
  817. *pos = start;
  818. return EINVAL;
  819. }
  820. goto set_obj;
  821. }
  822. else if (number_bytes || (p[1] == 'b' || p[1] == 'B')) {
  823. /* Bytes */
  824. if (need_double) {
  825. need_double = false;
  826. lv = dv;
  827. }
  828. lv *= ucl_lex_num_multiplier (*p, true);
  829. p += 2;
  830. if (end - p > 0 && !ucl_lex_is_atom_end (*p)) {
  831. *pos = start;
  832. return EINVAL;
  833. }
  834. goto set_obj;
  835. }
  836. else if (ucl_lex_is_atom_end (p[1])) {
  837. if (need_double) {
  838. dv *= ucl_lex_num_multiplier (*p, false);
  839. }
  840. else {
  841. lv *= ucl_lex_num_multiplier (*p, number_bytes);
  842. }
  843. p ++;
  844. goto set_obj;
  845. }
  846. else if (allow_time && end - p >= 3) {
  847. if (tolower (p[0]) == 'm' &&
  848. tolower (p[1]) == 'i' &&
  849. tolower (p[2]) == 'n') {
  850. /* Minutes */
  851. if (!need_double) {
  852. need_double = true;
  853. dv = lv;
  854. }
  855. is_time = true;
  856. dv *= 60.;
  857. p += 3;
  858. if (end - p > 0 && !ucl_lex_is_atom_end (*p)) {
  859. *pos = start;
  860. return EINVAL;
  861. }
  862. goto set_obj;
  863. }
  864. }
  865. }
  866. else {
  867. if (need_double) {
  868. dv *= ucl_lex_num_multiplier (*p, false);
  869. }
  870. else {
  871. lv *= ucl_lex_num_multiplier (*p, number_bytes);
  872. }
  873. p ++;
  874. if (end - p > 0 && !ucl_lex_is_atom_end (*p)) {
  875. *pos = start;
  876. return EINVAL;
  877. }
  878. goto set_obj;
  879. }
  880. break;
  881. case 'S':
  882. case 's':
  883. if (allow_time &&
  884. (p == end - 1 || ucl_lex_is_atom_end (p[1]))) {
  885. if (!need_double) {
  886. need_double = true;
  887. dv = lv;
  888. }
  889. p ++;
  890. is_time = true;
  891. goto set_obj;
  892. }
  893. break;
  894. case 'h':
  895. case 'H':
  896. case 'd':
  897. case 'D':
  898. case 'w':
  899. case 'W':
  900. case 'Y':
  901. case 'y':
  902. if (allow_time &&
  903. (p == end - 1 || ucl_lex_is_atom_end (p[1]))) {
  904. if (!need_double) {
  905. need_double = true;
  906. dv = lv;
  907. }
  908. is_time = true;
  909. dv *= ucl_lex_time_multiplier (*p);
  910. p ++;
  911. goto set_obj;
  912. }
  913. break;
  914. case '\t':
  915. case ' ':
  916. while (p < end && ucl_test_character(*p, UCL_CHARACTER_WHITESPACE)) {
  917. p++;
  918. }
  919. if (ucl_lex_is_atom_end(*p))
  920. goto set_obj;
  921. break;
  922. }
  923. }
  924. else if (endptr == end) {
  925. /* Just a number at the end of chunk */
  926. p = end;
  927. goto set_obj;
  928. }
  929. *pos = c;
  930. return EINVAL;
  931. set_obj:
  932. if (obj != NULL) {
  933. if (allow_double && (need_double || is_time)) {
  934. if (!is_time) {
  935. obj->type = UCL_FLOAT;
  936. }
  937. else {
  938. obj->type = UCL_TIME;
  939. }
  940. obj->value.dv = dv;
  941. }
  942. else {
  943. obj->type = UCL_INT;
  944. obj->value.iv = lv;
  945. }
  946. }
  947. *pos = p;
  948. return 0;
  949. }
  950. /**
  951. * Parse possible number
  952. * @param parser
  953. * @param chunk
  954. * @param obj
  955. * @return true if a number has been parsed
  956. */
  957. static bool
  958. ucl_lex_number (struct ucl_parser *parser,
  959. struct ucl_chunk *chunk, ucl_object_t *obj)
  960. {
  961. const unsigned char *pos;
  962. int ret;
  963. ret = ucl_maybe_parse_number (obj, chunk->pos, chunk->end, (const char **)&pos,
  964. true, false, ((parser->flags & UCL_PARSER_NO_TIME) == 0));
  965. if (ret == 0) {
  966. chunk->remain -= pos - chunk->pos;
  967. chunk->column += pos - chunk->pos;
  968. chunk->pos = pos;
  969. return true;
  970. }
  971. else if (ret == ERANGE) {
  972. ucl_set_err (parser, UCL_ESYNTAX, "numeric value out of range",
  973. &parser->err);
  974. }
  975. return false;
  976. }
  977. /**
  978. * Parse quoted string with possible escapes
  979. * @param parser
  980. * @param chunk
  981. * @param need_unescape
  982. * @param ucl_escape
  983. * @param var_expand
  984. * @return true if a string has been parsed
  985. */
  986. static bool
  987. ucl_lex_json_string (struct ucl_parser *parser,
  988. struct ucl_chunk *chunk,
  989. bool *need_unescape,
  990. bool *ucl_escape,
  991. bool *var_expand)
  992. {
  993. const unsigned char *p = chunk->pos;
  994. unsigned char c;
  995. int i;
  996. while (p < chunk->end) {
  997. c = *p;
  998. if (c < 0x1F) {
  999. /* Unmasked control character */
  1000. if (c == '\n') {
  1001. ucl_set_err (parser, UCL_ESYNTAX, "unexpected newline",
  1002. &parser->err);
  1003. }
  1004. else {
  1005. ucl_set_err (parser, UCL_ESYNTAX, "unexpected control character",
  1006. &parser->err);
  1007. }
  1008. return false;
  1009. }
  1010. else if (c == '\\') {
  1011. ucl_chunk_skipc (chunk, p);
  1012. if (p >= chunk->end) {
  1013. ucl_set_err (parser, UCL_ESYNTAX, "unfinished escape character",
  1014. &parser->err);
  1015. return false;
  1016. }
  1017. c = *p;
  1018. if (ucl_test_character (c, UCL_CHARACTER_ESCAPE)) {
  1019. if (c == 'u') {
  1020. ucl_chunk_skipc (chunk, p);
  1021. for (i = 0; i < 4 && p < chunk->end; i ++) {
  1022. if (!isxdigit (*p)) {
  1023. ucl_set_err (parser, UCL_ESYNTAX, "invalid utf escape",
  1024. &parser->err);
  1025. return false;
  1026. }
  1027. ucl_chunk_skipc (chunk, p);
  1028. }
  1029. if (p >= chunk->end) {
  1030. ucl_set_err (parser, UCL_ESYNTAX,
  1031. "unfinished escape character",
  1032. &parser->err);
  1033. return false;
  1034. }
  1035. }
  1036. else {
  1037. ucl_chunk_skipc (chunk, p);
  1038. }
  1039. }
  1040. *need_unescape = true;
  1041. *ucl_escape = true;
  1042. continue;
  1043. }
  1044. else if (c == '"') {
  1045. ucl_chunk_skipc (chunk, p);
  1046. return true;
  1047. }
  1048. else if (ucl_test_character (c, UCL_CHARACTER_UCL_UNSAFE)) {
  1049. *ucl_escape = true;
  1050. }
  1051. else if (c == '$') {
  1052. *var_expand = true;
  1053. }
  1054. ucl_chunk_skipc (chunk, p);
  1055. }
  1056. ucl_set_err (parser, UCL_ESYNTAX,
  1057. "no quote at the end of json string",
  1058. &parser->err);
  1059. return false;
  1060. }
  1061. /**
  1062. * Process single quoted string
  1063. * @param parser
  1064. * @param chunk
  1065. * @param need_unescape
  1066. * @return
  1067. */
  1068. static bool
  1069. ucl_lex_squoted_string (struct ucl_parser *parser,
  1070. struct ucl_chunk *chunk, bool *need_unescape)
  1071. {
  1072. const unsigned char *p = chunk->pos;
  1073. unsigned char c;
  1074. while (p < chunk->end) {
  1075. c = *p;
  1076. if (c == '\\') {
  1077. ucl_chunk_skipc (chunk, p);
  1078. if (p >= chunk->end) {
  1079. ucl_set_err (parser, UCL_ESYNTAX,
  1080. "unfinished escape character",
  1081. &parser->err);
  1082. return false;
  1083. }
  1084. else {
  1085. ucl_chunk_skipc (chunk, p);
  1086. }
  1087. *need_unescape = true;
  1088. continue;
  1089. }
  1090. else if (c == '\'') {
  1091. ucl_chunk_skipc (chunk, p);
  1092. return true;
  1093. }
  1094. ucl_chunk_skipc (chunk, p);
  1095. }
  1096. ucl_set_err (parser, UCL_ESYNTAX,
  1097. "no quote at the end of single quoted string",
  1098. &parser->err);
  1099. return false;
  1100. }
  1101. static void
  1102. ucl_parser_append_elt (struct ucl_parser *parser, ucl_hash_t *cont,
  1103. ucl_object_t *top,
  1104. ucl_object_t *elt)
  1105. {
  1106. ucl_object_t *nobj;
  1107. if ((parser->flags & UCL_PARSER_NO_IMPLICIT_ARRAYS) == 0) {
  1108. /* Implicit array */
  1109. top->flags |= UCL_OBJECT_MULTIVALUE;
  1110. DL_APPEND (top, elt);
  1111. parser->stack->obj->len ++;
  1112. }
  1113. else {
  1114. if ((top->flags & UCL_OBJECT_MULTIVALUE) != 0) {
  1115. /* Just add to the explicit array */
  1116. ucl_array_append (top, elt);
  1117. }
  1118. else {
  1119. /* Convert to an array */
  1120. nobj = ucl_object_typed_new (UCL_ARRAY);
  1121. nobj->key = top->key;
  1122. nobj->keylen = top->keylen;
  1123. nobj->flags |= UCL_OBJECT_MULTIVALUE;
  1124. ucl_array_append (nobj, top);
  1125. ucl_array_append (nobj, elt);
  1126. ucl_hash_replace (cont, top, nobj);
  1127. }
  1128. }
  1129. }
  1130. bool
  1131. ucl_parser_process_object_element (struct ucl_parser *parser, ucl_object_t *nobj)
  1132. {
  1133. ucl_hash_t *container;
  1134. ucl_object_t *tobj = NULL, *cur;
  1135. char errmsg[256];
  1136. container = parser->stack->obj->value.ov;
  1137. DL_FOREACH (parser->stack->obj, cur) {
  1138. tobj = __DECONST (ucl_object_t *, ucl_hash_search_obj (cur->value.ov, nobj));
  1139. if (tobj != NULL) {
  1140. break;
  1141. }
  1142. }
  1143. if (tobj == NULL) {
  1144. container = ucl_hash_insert_object (container, nobj,
  1145. parser->flags & UCL_PARSER_KEY_LOWERCASE);
  1146. if (container == NULL) {
  1147. return false;
  1148. }
  1149. nobj->prev = nobj;
  1150. nobj->next = NULL;
  1151. parser->stack->obj->len ++;
  1152. }
  1153. else {
  1154. unsigned priold = ucl_object_get_priority (tobj),
  1155. prinew = ucl_object_get_priority (nobj);
  1156. switch (parser->chunks->strategy) {
  1157. case UCL_DUPLICATE_APPEND:
  1158. /*
  1159. * The logic here is the following:
  1160. *
  1161. * - if we have two objects with the same priority, then we form an
  1162. * implicit or explicit array
  1163. * - if a new object has bigger priority, then we overwrite an old one
  1164. * - if a new object has lower priority, then we ignore it
  1165. */
  1166. /* Special case for inherited objects */
  1167. if (tobj->flags & UCL_OBJECT_INHERITED) {
  1168. prinew = priold + 1;
  1169. }
  1170. if (priold == prinew) {
  1171. ucl_parser_append_elt (parser, container, tobj, nobj);
  1172. }
  1173. else if (priold > prinew) {
  1174. /*
  1175. * We add this new object to a list of trash objects just to ensure
  1176. * that it won't come to any real object
  1177. * XXX: rather inefficient approach
  1178. */
  1179. DL_APPEND (parser->trash_objs, nobj);
  1180. }
  1181. else {
  1182. ucl_hash_replace (container, tobj, nobj);
  1183. ucl_object_unref (tobj);
  1184. }
  1185. break;
  1186. case UCL_DUPLICATE_REWRITE:
  1187. /* We just rewrite old values regardless of priority */
  1188. ucl_hash_replace (container, tobj, nobj);
  1189. ucl_object_unref (tobj);
  1190. break;
  1191. case UCL_DUPLICATE_ERROR:
  1192. snprintf(errmsg, sizeof(errmsg),
  1193. "duplicate element for key '%s' found",
  1194. nobj->key);
  1195. ucl_set_err (parser, UCL_EMERGE, errmsg, &parser->err);
  1196. return false;
  1197. case UCL_DUPLICATE_MERGE:
  1198. /*
  1199. * Here we do have some old object so we just push it on top of objects stack
  1200. * Check priority and then perform the merge on the remaining objects
  1201. */
  1202. if (tobj->type == UCL_OBJECT || tobj->type == UCL_ARRAY) {
  1203. ucl_object_unref (nobj);
  1204. nobj = tobj;
  1205. }
  1206. else if (priold == prinew) {
  1207. ucl_parser_append_elt (parser, container, tobj, nobj);
  1208. }
  1209. else if (priold > prinew) {
  1210. /*
  1211. * We add this new object to a list of trash objects just to ensure
  1212. * that it won't come to any real object
  1213. * XXX: rather inefficient approach
  1214. */
  1215. DL_APPEND (parser->trash_objs, nobj);
  1216. }
  1217. else {
  1218. ucl_hash_replace (container, tobj, nobj);
  1219. ucl_object_unref (tobj);
  1220. }
  1221. break;
  1222. }
  1223. }
  1224. parser->stack->obj->value.ov = container;
  1225. parser->cur_obj = nobj;
  1226. ucl_attach_comment (parser, nobj, false);
  1227. return true;
  1228. }
  1229. /**
  1230. * Parse a key in an object
  1231. * @param parser
  1232. * @param chunk
  1233. * @param next_key
  1234. * @param end_of_object
  1235. * @return true if a key has been parsed
  1236. */
  1237. static bool
  1238. ucl_parse_key (struct ucl_parser *parser, struct ucl_chunk *chunk,
  1239. bool *next_key, bool *end_of_object)
  1240. {
  1241. const unsigned char *p, *c = NULL, *end, *t;
  1242. const char *key = NULL;
  1243. bool got_quote = false, got_eq = false, got_semicolon = false,
  1244. need_unescape = false, ucl_escape = false, var_expand = false,
  1245. got_content = false, got_sep = false;
  1246. ucl_object_t *nobj;
  1247. ssize_t keylen;
  1248. p = chunk->pos;
  1249. if (*p == '.') {
  1250. /* It is macro actually */
  1251. if (!(parser->flags & UCL_PARSER_DISABLE_MACRO)) {
  1252. ucl_chunk_skipc (chunk, p);
  1253. }
  1254. parser->prev_state = parser->state;
  1255. parser->state = UCL_STATE_MACRO_NAME;
  1256. *end_of_object = false;
  1257. return true;
  1258. }
  1259. while (p < chunk->end) {
  1260. /*
  1261. * A key must start with alpha, number, '/' or '_' and end with space character
  1262. */
  1263. if (c == NULL) {
  1264. if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
  1265. if (!ucl_skip_comments (parser)) {
  1266. return false;
  1267. }
  1268. p = chunk->pos;
  1269. }
  1270. else if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  1271. ucl_chunk_skipc (chunk, p);
  1272. }
  1273. else if (ucl_test_character (*p, UCL_CHARACTER_KEY_START)) {
  1274. /* The first symbol */
  1275. c = p;
  1276. ucl_chunk_skipc (chunk, p);
  1277. got_content = true;
  1278. }
  1279. else if (*p == '"') {
  1280. /* JSON style key */
  1281. c = p + 1;
  1282. got_quote = true;
  1283. got_content = true;
  1284. ucl_chunk_skipc (chunk, p);
  1285. }
  1286. else if (*p == '}') {
  1287. /* We have actually end of an object */
  1288. *end_of_object = true;
  1289. return true;
  1290. }
  1291. else if (*p == '.') {
  1292. ucl_chunk_skipc (chunk, p);
  1293. parser->prev_state = parser->state;
  1294. parser->state = UCL_STATE_MACRO_NAME;
  1295. return true;
  1296. }
  1297. else {
  1298. /* Invalid identifier */
  1299. ucl_set_err (parser, UCL_ESYNTAX, "key must begin with a letter",
  1300. &parser->err);
  1301. return false;
  1302. }
  1303. }
  1304. else {
  1305. /* Parse the body of a key */
  1306. if (!got_quote) {
  1307. if (ucl_test_character (*p, UCL_CHARACTER_KEY)) {
  1308. got_content = true;
  1309. ucl_chunk_skipc (chunk, p);
  1310. }
  1311. else if (ucl_test_character (*p, UCL_CHARACTER_KEY_SEP)) {
  1312. end = p;
  1313. break;
  1314. }
  1315. else {
  1316. ucl_set_err (parser, UCL_ESYNTAX, "invalid character in a key",
  1317. &parser->err);
  1318. return false;
  1319. }
  1320. }
  1321. else {
  1322. /* We need to parse json like quoted string */
  1323. if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
  1324. return false;
  1325. }
  1326. /* Always escape keys obtained via json */
  1327. end = chunk->pos - 1;
  1328. p = chunk->pos;
  1329. break;
  1330. }
  1331. }
  1332. }
  1333. if (p >= chunk->end && got_content) {
  1334. ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err);
  1335. return false;
  1336. }
  1337. else if (!got_content) {
  1338. return true;
  1339. }
  1340. *end_of_object = false;
  1341. /* We are now at the end of the key, need to parse the rest */
  1342. while (p < chunk->end) {
  1343. if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) {
  1344. ucl_chunk_skipc (chunk, p);
  1345. }
  1346. else if (*p == '=') {
  1347. if (!got_eq && !got_semicolon) {
  1348. ucl_chunk_skipc (chunk, p);
  1349. got_eq = true;
  1350. }
  1351. else {
  1352. ucl_set_err (parser, UCL_ESYNTAX, "unexpected '=' character",
  1353. &parser->err);
  1354. return false;
  1355. }
  1356. }
  1357. else if (*p == ':') {
  1358. if (!got_eq && !got_semicolon) {
  1359. ucl_chunk_skipc (chunk, p);
  1360. got_semicolon = true;
  1361. }
  1362. else {
  1363. ucl_set_err (parser, UCL_ESYNTAX, "unexpected ':' character",
  1364. &parser->err);
  1365. return false;
  1366. }
  1367. }
  1368. else if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
  1369. /* Check for comment */
  1370. if (!ucl_skip_comments (parser)) {
  1371. return false;
  1372. }
  1373. p = chunk->pos;
  1374. }
  1375. else {
  1376. /* Start value */
  1377. break;
  1378. }
  1379. }
  1380. if (p >= chunk->end && got_content) {
  1381. ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err);
  1382. return false;
  1383. }
  1384. got_sep = got_semicolon || got_eq;
  1385. if (!got_sep) {
  1386. /*
  1387. * Maybe we have more keys nested, so search for termination character.
  1388. * Possible choices:
  1389. * 1) key1 key2 ... keyN [:=] value <- we treat that as error
  1390. * 2) key1 ... keyN {} or [] <- we treat that as nested objects
  1391. * 3) key1 value[;,\n] <- we treat that as linear object
  1392. */
  1393. t = p;
  1394. *next_key = false;
  1395. while (ucl_test_character (*t, UCL_CHARACTER_WHITESPACE)) {
  1396. t ++;
  1397. }
  1398. /* Check first non-space character after a key */
  1399. if (*t != '{' && *t != '[') {
  1400. while (t < chunk->end) {
  1401. if (*t == ',' || *t == ';' || *t == '\n' || *t == '\r') {
  1402. break;
  1403. }
  1404. else if (*t == '{' || *t == '[') {
  1405. *next_key = true;
  1406. break;
  1407. }
  1408. t ++;
  1409. }
  1410. }
  1411. }
  1412. /* Create a new object */
  1413. nobj = ucl_object_new_full (UCL_NULL, parser->chunks->priority);
  1414. if (nobj == NULL) {
  1415. return false;
  1416. }
  1417. keylen = ucl_copy_or_store_ptr (parser, c, &nobj->trash_stack[UCL_TRASH_KEY],
  1418. &key, end - c, need_unescape, parser->flags & UCL_PARSER_KEY_LOWERCASE,
  1419. false, false);
  1420. if (keylen == -1) {
  1421. ucl_object_unref (nobj);
  1422. return false;
  1423. }
  1424. else if (keylen == 0) {
  1425. ucl_set_err (parser, UCL_ESYNTAX, "empty keys are not allowed", &parser->err);
  1426. ucl_object_unref (nobj);
  1427. return false;
  1428. }
  1429. nobj->key = key;
  1430. nobj->keylen = keylen;
  1431. if (!ucl_parser_process_object_element (parser, nobj)) {
  1432. return false;
  1433. }
  1434. if (ucl_escape) {
  1435. nobj->flags |= UCL_OBJECT_NEED_KEY_ESCAPE;
  1436. }
  1437. return true;
  1438. }
  1439. /**
  1440. * Parse a cl string
  1441. * @param parser
  1442. * @param chunk
  1443. * @param var_expand
  1444. * @param need_unescape
  1445. * @return true if a key has been parsed
  1446. */
  1447. static bool
  1448. ucl_parse_string_value (struct ucl_parser *parser,
  1449. struct ucl_chunk *chunk, bool *var_expand, bool *need_unescape)
  1450. {
  1451. const unsigned char *p;
  1452. enum {
  1453. UCL_BRACE_ROUND = 0,
  1454. UCL_BRACE_SQUARE,
  1455. UCL_BRACE_FIGURE
  1456. };
  1457. int braces[3][2] = {{0, 0}, {0, 0}, {0, 0}};
  1458. p = chunk->pos;
  1459. while (p < chunk->end) {
  1460. /* Skip pairs of figure braces */
  1461. if (*p == '{') {
  1462. braces[UCL_BRACE_FIGURE][0] ++;
  1463. }
  1464. else if (*p == '}') {
  1465. braces[UCL_BRACE_FIGURE][1] ++;
  1466. if (braces[UCL_BRACE_FIGURE][1] <= braces[UCL_BRACE_FIGURE][0]) {
  1467. /* This is not a termination symbol, continue */
  1468. ucl_chunk_skipc (chunk, p);
  1469. continue;
  1470. }
  1471. }
  1472. /* Skip pairs of square braces */
  1473. else if (*p == '[') {
  1474. braces[UCL_BRACE_SQUARE][0] ++;
  1475. }
  1476. else if (*p == ']') {
  1477. braces[UCL_BRACE_SQUARE][1] ++;
  1478. if (braces[UCL_BRACE_SQUARE][1] <= braces[UCL_BRACE_SQUARE][0]) {
  1479. /* This is not a termination symbol, continue */
  1480. ucl_chunk_skipc (chunk, p);
  1481. continue;
  1482. }
  1483. }
  1484. else if (*p == '$') {
  1485. *var_expand = true;
  1486. }
  1487. else if (*p == '\\') {
  1488. *need_unescape = true;
  1489. ucl_chunk_skipc (chunk, p);
  1490. if (p < chunk->end) {
  1491. ucl_chunk_skipc (chunk, p);
  1492. }
  1493. continue;
  1494. }
  1495. if (ucl_lex_is_atom_end (*p) || (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1]))) {
  1496. break;
  1497. }
  1498. ucl_chunk_skipc (chunk, p);
  1499. }
  1500. return true;
  1501. }
  1502. /**
  1503. * Parse multiline string ending with \n{term}\n
  1504. * @param parser
  1505. * @param chunk
  1506. * @param term
  1507. * @param term_len
  1508. * @param beg
  1509. * @param var_expand
  1510. * @return size of multiline string or 0 in case of error
  1511. */
  1512. static int
  1513. ucl_parse_multiline_string (struct ucl_parser *parser,
  1514. struct ucl_chunk *chunk, const unsigned char *term,
  1515. int term_len, unsigned char const **beg,
  1516. bool *var_expand)
  1517. {
  1518. const unsigned char *p, *c, *tend;
  1519. bool newline = false;
  1520. int len = 0;
  1521. p = chunk->pos;
  1522. c = p;
  1523. while (p < chunk->end) {
  1524. if (newline) {
  1525. if (chunk->end - p < term_len) {
  1526. return 0;
  1527. }
  1528. else if (memcmp (p, term, term_len) == 0) {
  1529. tend = p + term_len;
  1530. if (*tend != '\n' && *tend != ';' && *tend != ',') {
  1531. /* Incomplete terminator */
  1532. ucl_chunk_skipc (chunk, p);
  1533. continue;
  1534. }
  1535. len = p - c;
  1536. chunk->remain -= term_len;
  1537. chunk->pos = p + term_len;
  1538. chunk->column = term_len;
  1539. *beg = c;
  1540. break;
  1541. }
  1542. }
  1543. if (*p == '\n') {
  1544. newline = true;
  1545. }
  1546. else {
  1547. if (*p == '$') {
  1548. *var_expand = true;
  1549. }
  1550. newline = false;
  1551. }
  1552. ucl_chunk_skipc (chunk, p);
  1553. }
  1554. return len;
  1555. }
  1556. static inline ucl_object_t*
  1557. ucl_parser_get_container (struct ucl_parser *parser)
  1558. {
  1559. ucl_object_t *t, *obj = NULL;
  1560. if (parser == NULL || parser->stack == NULL || parser->stack->obj == NULL) {
  1561. return NULL;
  1562. }
  1563. if (parser->stack->obj->type == UCL_ARRAY) {
  1564. /* Object must be allocated */
  1565. obj = ucl_object_new_full (UCL_NULL, parser->chunks->priority);
  1566. t = parser->stack->obj;
  1567. if (!ucl_array_append (t, obj)) {
  1568. ucl_object_unref (obj);
  1569. return NULL;
  1570. }
  1571. parser->cur_obj = obj;
  1572. ucl_attach_comment (parser, obj, false);
  1573. }
  1574. else {
  1575. /* Object has been already allocated */
  1576. obj = parser->cur_obj;
  1577. }
  1578. return obj;
  1579. }
  1580. /**
  1581. * Handle value data
  1582. * @param parser
  1583. * @param chunk
  1584. * @return
  1585. */
  1586. static bool
  1587. ucl_parse_value (struct ucl_parser *parser, struct ucl_chunk *chunk)
  1588. {
  1589. const unsigned char *p, *c;
  1590. ucl_object_t *obj = NULL;
  1591. unsigned int stripped_spaces;
  1592. ssize_t str_len;
  1593. bool need_unescape = false, ucl_escape = false, var_expand = false;
  1594. p = chunk->pos;
  1595. /* Skip any spaces and comments */
  1596. if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) ||
  1597. (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1]))) {
  1598. while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  1599. ucl_chunk_skipc (chunk, p);
  1600. }
  1601. if (!ucl_skip_comments (parser)) {
  1602. return false;
  1603. }
  1604. p = chunk->pos;
  1605. }
  1606. while (p < chunk->end) {
  1607. c = p;
  1608. switch (*p) {
  1609. case '"':
  1610. ucl_chunk_skipc (chunk, p);
  1611. if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape,
  1612. &var_expand)) {
  1613. return false;
  1614. }
  1615. obj = ucl_parser_get_container (parser);
  1616. if (!obj) {
  1617. return false;
  1618. }
  1619. str_len = chunk->pos - c - 2;
  1620. obj->type = UCL_STRING;
  1621. if ((str_len = ucl_copy_or_store_ptr (parser, c + 1,
  1622. &obj->trash_stack[UCL_TRASH_VALUE],
  1623. &obj->value.sv, str_len, need_unescape, false,
  1624. var_expand, false)) == -1) {
  1625. return false;
  1626. }
  1627. obj->len = str_len;
  1628. parser->state = UCL_STATE_AFTER_VALUE;
  1629. return true;
  1630. break;
  1631. case '\'':
  1632. ucl_chunk_skipc (chunk, p);
  1633. if (!ucl_lex_squoted_string (parser, chunk, &need_unescape)) {
  1634. return false;
  1635. }
  1636. obj = ucl_parser_get_container (parser);
  1637. if (!obj) {
  1638. return false;
  1639. }
  1640. str_len = chunk->pos - c - 2;
  1641. obj->type = UCL_STRING;
  1642. obj->flags |= UCL_OBJECT_SQUOTED;
  1643. if ((str_len = ucl_copy_or_store_ptr (parser, c + 1,
  1644. &obj->trash_stack[UCL_TRASH_VALUE],
  1645. &obj->value.sv, str_len, need_unescape, false,
  1646. var_expand, true)) == -1) {
  1647. return false;
  1648. }
  1649. obj->len = str_len;
  1650. parser->state = UCL_STATE_AFTER_VALUE;
  1651. return true;
  1652. break;
  1653. case '{':
  1654. obj = ucl_parser_get_container (parser);
  1655. if (obj == NULL) {
  1656. return false;
  1657. }
  1658. /* We have a new object */
  1659. if (parser->stack) {
  1660. obj = ucl_parser_add_container (obj, parser, false,
  1661. parser->stack->e.params.level, true);
  1662. }
  1663. else {
  1664. return false;
  1665. }
  1666. if (obj == NULL) {
  1667. return false;
  1668. }
  1669. ucl_chunk_skipc (chunk, p);
  1670. return true;
  1671. break;
  1672. case '[':
  1673. obj = ucl_parser_get_container (parser);
  1674. if (obj == NULL) {
  1675. return false;
  1676. }
  1677. /* We have a new array */
  1678. if (parser->stack) {
  1679. obj = ucl_parser_add_container (obj, parser, true,
  1680. parser->stack->e.params.level, true);
  1681. }
  1682. else {
  1683. return false;
  1684. }
  1685. if (obj == NULL) {
  1686. return false;
  1687. }
  1688. ucl_chunk_skipc (chunk, p);
  1689. return true;
  1690. break;
  1691. case ']':
  1692. /* We have the array ending */
  1693. if (parser->stack && parser->stack->obj->type == UCL_ARRAY) {
  1694. parser->state = UCL_STATE_AFTER_VALUE;
  1695. return true;
  1696. }
  1697. else {
  1698. goto parse_string;
  1699. }
  1700. break;
  1701. case '<':
  1702. obj = ucl_parser_get_container (parser);
  1703. /* We have something like multiline value, which must be <<[A-Z]+\n */
  1704. if (chunk->end - p > 3) {
  1705. if (memcmp (p, "<<", 2) == 0) {
  1706. p += 2;
  1707. /* We allow only uppercase characters in multiline definitions */
  1708. while (p < chunk->end && *p >= 'A' && *p <= 'Z') {
  1709. p ++;
  1710. }
  1711. if(p == chunk->end) {
  1712. ucl_set_err (parser, UCL_ESYNTAX,
  1713. "unterminated multiline value", &parser->err);
  1714. return false;
  1715. }
  1716. if (*p =='\n') {
  1717. /* Set chunk positions and start multiline parsing */
  1718. chunk->remain -= p - c + 1;
  1719. c += 2;
  1720. chunk->pos = p + 1;
  1721. chunk->column = 0;
  1722. chunk->line ++;
  1723. if ((str_len = ucl_parse_multiline_string (parser, chunk, c,
  1724. p - c, &c, &var_expand)) == 0) {
  1725. ucl_set_err (parser, UCL_ESYNTAX,
  1726. "unterminated multiline value", &parser->err);
  1727. return false;
  1728. }
  1729. obj->type = UCL_STRING;
  1730. obj->flags |= UCL_OBJECT_MULTILINE;
  1731. if ((str_len = ucl_copy_or_store_ptr (parser, c,
  1732. &obj->trash_stack[UCL_TRASH_VALUE],
  1733. &obj->value.sv, str_len - 1, false,
  1734. false, var_expand, false)) == -1) {
  1735. return false;
  1736. }
  1737. obj->len = str_len;
  1738. parser->state = UCL_STATE_AFTER_VALUE;
  1739. return true;
  1740. }
  1741. }
  1742. }
  1743. /* Fallback to ordinary strings */
  1744. /* FALLTHRU */
  1745. default:
  1746. parse_string:
  1747. if (obj == NULL) {
  1748. obj = ucl_parser_get_container (parser);
  1749. }
  1750. /* Parse atom */
  1751. if (ucl_test_character (*p, UCL_CHARACTER_VALUE_DIGIT_START)) {
  1752. if (!ucl_lex_number (parser, chunk, obj)) {
  1753. if (parser->state == UCL_STATE_ERROR) {
  1754. return false;
  1755. }
  1756. }
  1757. else {
  1758. parser->state = UCL_STATE_AFTER_VALUE;
  1759. return true;
  1760. }
  1761. /* Fallback to normal string */
  1762. }
  1763. if (!ucl_parse_string_value (parser, chunk, &var_expand,
  1764. &need_unescape)) {
  1765. return false;
  1766. }
  1767. /* Cut trailing spaces */
  1768. stripped_spaces = 0;
  1769. while (ucl_test_character (*(chunk->pos - 1 - stripped_spaces),
  1770. UCL_CHARACTER_WHITESPACE)) {
  1771. stripped_spaces ++;
  1772. }
  1773. str_len = chunk->pos - c - stripped_spaces;
  1774. if (str_len <= 0) {
  1775. ucl_set_err (parser, UCL_ESYNTAX, "string value must not be empty",
  1776. &parser->err);
  1777. return false;
  1778. }
  1779. else if (str_len == 4 && memcmp (c, "null", 4) == 0) {
  1780. obj->len = 0;
  1781. obj->type = UCL_NULL;
  1782. }
  1783. else if (str_len == 3 && memcmp (c, "nan", 3) == 0) {
  1784. obj->len = 0;
  1785. obj->type = UCL_FLOAT;
  1786. obj->value.dv = NAN;
  1787. }
  1788. else if (str_len == 3 && memcmp (c, "inf", 3) == 0) {
  1789. obj->len = 0;
  1790. obj->type = UCL_FLOAT;
  1791. obj->value.dv = INFINITY;
  1792. }
  1793. else if (!ucl_maybe_parse_boolean (obj, c, str_len)) {
  1794. obj->type = UCL_STRING;
  1795. if ((str_len = ucl_copy_or_store_ptr (parser, c,
  1796. &obj->trash_stack[UCL_TRASH_VALUE],
  1797. &obj->value.sv, str_len, need_unescape,
  1798. false, var_expand, false)) == -1) {
  1799. return false;
  1800. }
  1801. obj->len = str_len;
  1802. }
  1803. parser->state = UCL_STATE_AFTER_VALUE;
  1804. return true;
  1805. break;
  1806. }
  1807. }
  1808. return true;
  1809. }
  1810. /**
  1811. * Handle after value data
  1812. * @param parser
  1813. * @param chunk
  1814. * @return
  1815. */
  1816. static bool
  1817. ucl_parse_after_value (struct ucl_parser *parser, struct ucl_chunk *chunk)
  1818. {
  1819. const unsigned char *p;
  1820. bool got_sep = false;
  1821. struct ucl_stack *st;
  1822. p = chunk->pos;
  1823. while (p < chunk->end) {
  1824. if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) {
  1825. /* Skip whitespaces */
  1826. ucl_chunk_skipc (chunk, p);
  1827. }
  1828. else if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
  1829. /* Skip comment */
  1830. if (!ucl_skip_comments (parser)) {
  1831. return false;
  1832. }
  1833. /* Treat comment as a separator */
  1834. got_sep = true;
  1835. p = chunk->pos;
  1836. }
  1837. else if (ucl_test_character (*p, UCL_CHARACTER_VALUE_END)) {
  1838. if (*p == '}' || *p == ']') {
  1839. if (parser->stack == NULL) {
  1840. ucl_set_err (parser, UCL_ESYNTAX,
  1841. "end of array or object detected without corresponding start",
  1842. &parser->err);
  1843. return false;
  1844. }
  1845. if ((*p == '}' && parser->stack->obj->type == UCL_OBJECT) ||
  1846. (*p == ']' && parser->stack->obj->type == UCL_ARRAY)) {
  1847. /* Pop all nested objects from a stack */
  1848. st = parser->stack;
  1849. if (!(st->e.params.flags & UCL_STACK_HAS_OBRACE)) {
  1850. parser->err_code = UCL_EUNPAIRED;
  1851. ucl_create_err (&parser->err,
  1852. "%s:%d object closed with } is not opened with { at line %d",
  1853. chunk->fname ? chunk->fname : "memory",
  1854. parser->chunks->line, st->e.params.line);
  1855. return false;
  1856. }
  1857. parser->stack = st->next;
  1858. UCL_FREE (sizeof (struct ucl_stack), st);
  1859. if (parser->cur_obj) {
  1860. ucl_attach_comment (parser, parser->cur_obj, true);
  1861. }
  1862. while (parser->stack != NULL) {
  1863. st = parser->stack;
  1864. if (st->next == NULL) {
  1865. break;
  1866. }
  1867. else if (st->next->e.params.level == st->e.params.level) {
  1868. break;
  1869. }
  1870. parser->stack = st->next;
  1871. parser->cur_obj = st->obj;
  1872. UCL_FREE (sizeof (struct ucl_stack), st);
  1873. }
  1874. }
  1875. else {
  1876. ucl_set_err (parser, UCL_ESYNTAX,
  1877. "unexpected terminating symbol detected",
  1878. &parser->err);
  1879. return false;
  1880. }
  1881. if (parser->stack == NULL) {
  1882. /* Ignore everything after a top object */
  1883. return true;
  1884. }
  1885. else {
  1886. ucl_chunk_skipc (chunk, p);
  1887. }
  1888. got_sep = true;
  1889. }
  1890. else {
  1891. /* Got a separator */
  1892. got_sep = true;
  1893. ucl_chunk_skipc (chunk, p);
  1894. }
  1895. }
  1896. else {
  1897. /* Anything else */
  1898. if (!got_sep) {
  1899. ucl_set_err (parser, UCL_ESYNTAX, "delimiter is missing",
  1900. &parser->err);
  1901. return false;
  1902. }
  1903. return true;
  1904. }
  1905. }
  1906. return true;
  1907. }
  1908. static bool
  1909. ucl_skip_macro_as_comment (struct ucl_parser *parser,
  1910. struct ucl_chunk *chunk)
  1911. {
  1912. const unsigned char *p, *c;
  1913. enum {
  1914. macro_skip_start = 0,
  1915. macro_has_symbols,
  1916. macro_has_obrace,
  1917. macro_has_quote,
  1918. macro_has_backslash,
  1919. macro_has_sqbrace,
  1920. macro_save
  1921. } state = macro_skip_start, prev_state = macro_skip_start;
  1922. p = chunk->pos;
  1923. c = chunk->pos;
  1924. while (p < chunk->end) {
  1925. switch (state) {
  1926. case macro_skip_start:
  1927. if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) {
  1928. state = macro_has_symbols;
  1929. }
  1930. else if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  1931. state = macro_save;
  1932. continue;
  1933. }
  1934. ucl_chunk_skipc (chunk, p);
  1935. break;
  1936. case macro_has_symbols:
  1937. if (*p == '{') {
  1938. state = macro_has_sqbrace;
  1939. }
  1940. else if (*p == '(') {
  1941. state = macro_has_obrace;
  1942. }
  1943. else if (*p == '"') {
  1944. state = macro_has_quote;
  1945. }
  1946. else if (*p == '\n') {
  1947. state = macro_save;
  1948. continue;
  1949. }
  1950. ucl_chunk_skipc (chunk, p);
  1951. break;
  1952. case macro_has_obrace:
  1953. if (*p == '\\') {
  1954. prev_state = state;
  1955. state = macro_has_backslash;
  1956. }
  1957. else if (*p == ')') {
  1958. state = macro_has_symbols;
  1959. }
  1960. ucl_chunk_skipc (chunk, p);
  1961. break;
  1962. case macro_has_sqbrace:
  1963. if (*p == '\\') {
  1964. prev_state = state;
  1965. state = macro_has_backslash;
  1966. }
  1967. else if (*p == '}') {
  1968. state = macro_save;
  1969. }
  1970. ucl_chunk_skipc (chunk, p);
  1971. break;
  1972. case macro_has_quote:
  1973. if (*p == '\\') {
  1974. prev_state = state;
  1975. state = macro_has_backslash;
  1976. }
  1977. else if (*p == '"') {
  1978. state = macro_save;
  1979. }
  1980. ucl_chunk_skipc (chunk, p);
  1981. break;
  1982. case macro_has_backslash:
  1983. state = prev_state;
  1984. ucl_chunk_skipc (chunk, p);
  1985. break;
  1986. case macro_save:
  1987. if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
  1988. ucl_save_comment (parser, c, p - c);
  1989. }
  1990. return true;
  1991. }
  1992. }
  1993. return false;
  1994. }
  1995. /**
  1996. * Handle macro data
  1997. * @param parser
  1998. * @param chunk
  1999. * @param marco
  2000. * @param macro_start
  2001. * @param macro_len
  2002. * @return
  2003. */
  2004. static bool
  2005. ucl_parse_macro_value (struct ucl_parser *parser,
  2006. struct ucl_chunk *chunk, struct ucl_macro *macro,
  2007. unsigned char const **macro_start, size_t *macro_len)
  2008. {
  2009. const unsigned char *p, *c;
  2010. bool need_unescape = false, ucl_escape = false, var_expand = false;
  2011. p = chunk->pos;
  2012. switch (*p) {
  2013. case '"':
  2014. /* We have macro value encoded in quotes */
  2015. c = p;
  2016. ucl_chunk_skipc (chunk, p);
  2017. if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
  2018. return false;
  2019. }
  2020. *macro_start = c + 1;
  2021. *macro_len = chunk->pos - c - 2;
  2022. p = chunk->pos;
  2023. break;
  2024. case '{':
  2025. /* We got a multiline macro body */
  2026. ucl_chunk_skipc (chunk, p);
  2027. /* Skip spaces at the beginning */
  2028. while (p < chunk->end) {
  2029. if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  2030. ucl_chunk_skipc (chunk, p);
  2031. }
  2032. else {
  2033. break;
  2034. }
  2035. }
  2036. c = p;
  2037. while (p < chunk->end) {
  2038. if (*p == '}') {
  2039. break;
  2040. }
  2041. ucl_chunk_skipc (chunk, p);
  2042. }
  2043. *macro_start = c;
  2044. *macro_len = p - c;
  2045. ucl_chunk_skipc (chunk, p);
  2046. break;
  2047. default:
  2048. /* Macro is not enclosed in quotes or braces */
  2049. c = p;
  2050. while (p < chunk->end) {
  2051. if (ucl_lex_is_atom_end (*p)) {
  2052. break;
  2053. }
  2054. ucl_chunk_skipc (chunk, p);
  2055. }
  2056. *macro_start = c;
  2057. *macro_len = p - c;
  2058. break;
  2059. }
  2060. /* We are at the end of a macro */
  2061. /* Skip ';' and space characters and return to previous state */
  2062. while (p < chunk->end) {
  2063. if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) && *p != ';') {
  2064. break;
  2065. }
  2066. ucl_chunk_skipc (chunk, p);
  2067. }
  2068. return true;
  2069. }
  2070. /**
  2071. * Parse macro arguments as UCL object
  2072. * @param parser parser structure
  2073. * @param chunk the current data chunk
  2074. * @return
  2075. */
  2076. static ucl_object_t *
  2077. ucl_parse_macro_arguments (struct ucl_parser *parser,
  2078. struct ucl_chunk *chunk)
  2079. {
  2080. ucl_object_t *res = NULL;
  2081. struct ucl_parser *params_parser;
  2082. int obraces = 1, ebraces = 0, state = 0;
  2083. const unsigned char *p, *c;
  2084. size_t args_len = 0;
  2085. struct ucl_parser_saved_state saved;
  2086. saved.column = chunk->column;
  2087. saved.line = chunk->line;
  2088. saved.pos = chunk->pos;
  2089. saved.remain = chunk->remain;
  2090. p = chunk->pos;
  2091. if (*p != '(' || chunk->remain < 2) {
  2092. return NULL;
  2093. }
  2094. /* Set begin and start */
  2095. ucl_chunk_skipc (chunk, p);
  2096. c = p;
  2097. while ((p) < (chunk)->end) {
  2098. switch (state) {
  2099. case 0:
  2100. /* Parse symbols and check for '(', ')' and '"' */
  2101. if (*p == '(') {
  2102. obraces ++;
  2103. }
  2104. else if (*p == ')') {
  2105. ebraces ++;
  2106. }
  2107. else if (*p == '"') {
  2108. state = 1;
  2109. }
  2110. /* Check pairing */
  2111. if (obraces == ebraces) {
  2112. state = 99;
  2113. }
  2114. else {
  2115. args_len ++;
  2116. }
  2117. /* Check overflow */
  2118. if (chunk->remain == 0) {
  2119. goto restore_chunk;
  2120. }
  2121. ucl_chunk_skipc (chunk, p);
  2122. break;
  2123. case 1:
  2124. /* We have quote character, so skip all but quotes */
  2125. if (*p == '"' && *(p - 1) != '\\') {
  2126. state = 0;
  2127. }
  2128. if (chunk->remain == 0) {
  2129. goto restore_chunk;
  2130. }
  2131. args_len ++;
  2132. ucl_chunk_skipc (chunk, p);
  2133. break;
  2134. case 99:
  2135. /*
  2136. * We have read the full body of arguments, so we need to parse and set
  2137. * object from that
  2138. */
  2139. params_parser = ucl_parser_new (parser->flags);
  2140. if (!ucl_parser_add_chunk (params_parser, c, args_len)) {
  2141. ucl_set_err (parser, UCL_ESYNTAX, "macro arguments parsing error",
  2142. &parser->err);
  2143. }
  2144. else {
  2145. res = ucl_parser_get_object (params_parser);
  2146. }
  2147. ucl_parser_free (params_parser);
  2148. return res;
  2149. break;
  2150. }
  2151. }
  2152. return res;
  2153. restore_chunk:
  2154. chunk->column = saved.column;
  2155. chunk->line = saved.line;
  2156. chunk->pos = saved.pos;
  2157. chunk->remain = saved.remain;
  2158. return NULL;
  2159. }
  2160. #define SKIP_SPACES_COMMENTS(parser, chunk, p) do { \
  2161. while ((p) < (chunk)->end) { \
  2162. if (!ucl_test_character (*(p), UCL_CHARACTER_WHITESPACE_UNSAFE)) { \
  2163. if ((chunk)->remain >= 2 && ucl_lex_is_comment ((p)[0], (p)[1])) { \
  2164. if (!ucl_skip_comments (parser)) { \
  2165. return false; \
  2166. } \
  2167. p = (chunk)->pos; \
  2168. } \
  2169. break; \
  2170. } \
  2171. ucl_chunk_skipc (chunk, p); \
  2172. } \
  2173. } while(0)
  2174. /**
  2175. * Handle the main states of rcl parser
  2176. * @param parser parser structure
  2177. * @return true if chunk has been parsed and false in case of error
  2178. */
  2179. static bool
  2180. ucl_state_machine (struct ucl_parser *parser)
  2181. {
  2182. ucl_object_t *obj, *macro_args;
  2183. struct ucl_chunk *chunk = parser->chunks;
  2184. const unsigned char *p, *c = NULL, *macro_start = NULL;
  2185. unsigned char *macro_escaped;
  2186. size_t macro_len = 0;
  2187. struct ucl_macro *macro = NULL;
  2188. bool next_key = false, end_of_object = false, ret;
  2189. if (parser->top_obj == NULL) {
  2190. parser->state = UCL_STATE_INIT;
  2191. }
  2192. p = chunk->pos;
  2193. while (chunk->pos < chunk->end) {
  2194. switch (parser->state) {
  2195. case UCL_STATE_INIT:
  2196. /*
  2197. * At the init state we can either go to the parse array or object
  2198. * if we got [ or { correspondingly or can just treat new data as
  2199. * a key of newly created object
  2200. */
  2201. if (!ucl_skip_comments (parser)) {
  2202. parser->prev_state = parser->state;
  2203. parser->state = UCL_STATE_ERROR;
  2204. return false;
  2205. }
  2206. else {
  2207. bool seen_obrace = false;
  2208. /* Skip any spaces */
  2209. while (p < chunk->end && ucl_test_character (*p,
  2210. UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  2211. ucl_chunk_skipc (chunk, p);
  2212. }
  2213. p = chunk->pos;
  2214. if (p < chunk->end) {
  2215. if (*p == '[') {
  2216. parser->state = UCL_STATE_VALUE;
  2217. ucl_chunk_skipc (chunk, p);
  2218. seen_obrace = true;
  2219. }
  2220. else {
  2221. if (*p == '{') {
  2222. ucl_chunk_skipc (chunk, p);
  2223. parser->state = UCL_STATE_KEY_OBRACE;
  2224. seen_obrace = true;
  2225. }
  2226. else {
  2227. parser->state = UCL_STATE_KEY;
  2228. }
  2229. }
  2230. }
  2231. if (parser->top_obj == NULL) {
  2232. if (parser->state == UCL_STATE_VALUE) {
  2233. obj = ucl_parser_add_container (NULL, parser, true, 0,
  2234. seen_obrace);
  2235. }
  2236. else {
  2237. obj = ucl_parser_add_container (NULL, parser, false, 0,
  2238. seen_obrace);
  2239. }
  2240. if (obj == NULL) {
  2241. return false;
  2242. }
  2243. parser->top_obj = obj;
  2244. parser->cur_obj = obj;
  2245. }
  2246. }
  2247. break;
  2248. case UCL_STATE_KEY:
  2249. case UCL_STATE_KEY_OBRACE:
  2250. /* Skip any spaces */
  2251. while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
  2252. ucl_chunk_skipc (chunk, p);
  2253. }
  2254. if (p == chunk->end || *p == '}') {
  2255. /* We have the end of an object */
  2256. parser->state = UCL_STATE_AFTER_VALUE;
  2257. continue;
  2258. }
  2259. if (parser->stack == NULL) {
  2260. /* No objects are on stack, but we want to parse a key */
  2261. ucl_set_err (parser, UCL_ESYNTAX, "top object is finished but the parser "
  2262. "expects a key", &parser->err);
  2263. parser->prev_state = parser->state;
  2264. parser->state = UCL_STATE_ERROR;
  2265. return false;
  2266. }
  2267. if (!ucl_parse_key (parser, chunk, &next_key, &end_of_object)) {
  2268. parser->prev_state = parser->state;
  2269. parser->state = UCL_STATE_ERROR;
  2270. return false;
  2271. }
  2272. if (end_of_object) {
  2273. p = chunk->pos;
  2274. parser->state = UCL_STATE_AFTER_VALUE;
  2275. continue;
  2276. }
  2277. else if (parser->state != UCL_STATE_MACRO_NAME) {
  2278. if (next_key && parser->stack->obj->type == UCL_OBJECT) {
  2279. /* Parse more keys and nest objects accordingly */
  2280. obj = ucl_parser_add_container (parser->cur_obj,
  2281. parser,
  2282. false,
  2283. parser->stack->e.params.level + 1,
  2284. parser->state == UCL_STATE_KEY_OBRACE);
  2285. if (obj == NULL) {
  2286. return false;
  2287. }
  2288. }
  2289. else {
  2290. parser->state = UCL_STATE_VALUE;
  2291. }
  2292. }
  2293. else {
  2294. c = chunk->pos;
  2295. }
  2296. p = chunk->pos;
  2297. break;
  2298. case UCL_STATE_VALUE:
  2299. /* We need to check what we do have */
  2300. if (!parser->cur_obj || !ucl_parse_value (parser, chunk)) {
  2301. parser->prev_state = parser->state;
  2302. parser->state = UCL_STATE_ERROR;
  2303. return false;
  2304. }
  2305. /* State is set in ucl_parse_value call */
  2306. p = chunk->pos;
  2307. break;
  2308. case UCL_STATE_AFTER_VALUE:
  2309. if (!ucl_parse_after_value (parser, chunk)) {
  2310. parser->prev_state = parser->state;
  2311. parser->state = UCL_STATE_ERROR;
  2312. return false;
  2313. }
  2314. if (parser->stack != NULL) {
  2315. if (parser->stack->obj->type == UCL_OBJECT) {
  2316. parser->state = UCL_STATE_KEY;
  2317. }
  2318. else {
  2319. /* Array */
  2320. parser->state = UCL_STATE_VALUE;
  2321. }
  2322. }
  2323. else {
  2324. /* Skip everything at the end */
  2325. return true;
  2326. }
  2327. p = chunk->pos;
  2328. break;
  2329. case UCL_STATE_MACRO_NAME:
  2330. if (parser->flags & UCL_PARSER_DISABLE_MACRO) {
  2331. if (!ucl_skip_macro_as_comment (parser, chunk)) {
  2332. /* We have invalid macro */
  2333. ucl_create_err (&parser->err,
  2334. "error at %s:%d at column %d: invalid macro",
  2335. chunk->fname ? chunk->fname : "memory",
  2336. chunk->line,
  2337. chunk->column);
  2338. parser->state = UCL_STATE_ERROR;
  2339. return false;
  2340. }
  2341. else {
  2342. p = chunk->pos;
  2343. parser->state = parser->prev_state;
  2344. }
  2345. }
  2346. else {
  2347. if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) &&
  2348. *p != '(') {
  2349. ucl_chunk_skipc (chunk, p);
  2350. }
  2351. else {
  2352. if (c != NULL && p - c > 0) {
  2353. /* We got macro name */
  2354. macro_len = (size_t) (p - c);
  2355. HASH_FIND (hh, parser->macroes, c, macro_len, macro);
  2356. if (macro == NULL) {
  2357. ucl_create_err (&parser->err,
  2358. "error at %s:%d at column %d: "
  2359. "unknown macro: '%.*s', character: '%c'",
  2360. chunk->fname ? chunk->fname : "memory",
  2361. chunk->line,
  2362. chunk->column,
  2363. (int) (p - c),
  2364. c,
  2365. *chunk->pos);
  2366. parser->state = UCL_STATE_ERROR;
  2367. return false;
  2368. }
  2369. /* Now we need to skip all spaces */
  2370. SKIP_SPACES_COMMENTS(parser, chunk, p);
  2371. parser->state = UCL_STATE_MACRO;
  2372. }
  2373. else {
  2374. /* We have invalid macro name */
  2375. ucl_create_err (&parser->err,
  2376. "error at %s:%d at column %d: invalid macro name",
  2377. chunk->fname ? chunk->fname : "memory",
  2378. chunk->line,
  2379. chunk->column);
  2380. parser->state = UCL_STATE_ERROR;
  2381. return false;
  2382. }
  2383. }
  2384. }
  2385. break;
  2386. case UCL_STATE_MACRO:
  2387. if (*chunk->pos == '(') {
  2388. macro_args = ucl_parse_macro_arguments (parser, chunk);
  2389. p = chunk->pos;
  2390. if (macro_args) {
  2391. SKIP_SPACES_COMMENTS(parser, chunk, p);
  2392. }
  2393. }
  2394. else {
  2395. macro_args = NULL;
  2396. }
  2397. if (!ucl_parse_macro_value (parser, chunk, macro,
  2398. &macro_start, &macro_len)) {
  2399. parser->prev_state = parser->state;
  2400. parser->state = UCL_STATE_ERROR;
  2401. return false;
  2402. }
  2403. macro_len = ucl_expand_variable (parser, &macro_escaped,
  2404. macro_start, macro_len);
  2405. parser->state = parser->prev_state;
  2406. if (macro_escaped == NULL && macro != NULL) {
  2407. if (macro->is_context) {
  2408. ret = macro->h.context_handler (macro_start, macro_len,
  2409. macro_args,
  2410. parser->top_obj,
  2411. macro->ud);
  2412. }
  2413. else {
  2414. ret = macro->h.handler (macro_start, macro_len, macro_args,
  2415. macro->ud);
  2416. }
  2417. }
  2418. else if (macro != NULL) {
  2419. if (macro->is_context) {
  2420. ret = macro->h.context_handler (macro_escaped, macro_len,
  2421. macro_args,
  2422. parser->top_obj,
  2423. macro->ud);
  2424. }
  2425. else {
  2426. ret = macro->h.handler (macro_escaped, macro_len, macro_args,
  2427. macro->ud);
  2428. }
  2429. UCL_FREE (macro_len + 1, macro_escaped);
  2430. }
  2431. else {
  2432. ret = false;
  2433. ucl_set_err (parser, UCL_EINTERNAL,
  2434. "internal error: parser has macro undefined", &parser->err);
  2435. }
  2436. /*
  2437. * Chunk can be modified within macro handler
  2438. */
  2439. chunk = parser->chunks;
  2440. p = chunk->pos;
  2441. if (macro_args) {
  2442. ucl_object_unref (macro_args);
  2443. }
  2444. if (!ret) {
  2445. return false;
  2446. }
  2447. break;
  2448. default:
  2449. ucl_set_err (parser, UCL_EINTERNAL,
  2450. "internal error: parser is in an unknown state", &parser->err);
  2451. parser->state = UCL_STATE_ERROR;
  2452. return false;
  2453. }
  2454. }
  2455. if (parser->last_comment) {
  2456. if (parser->cur_obj) {
  2457. ucl_attach_comment (parser, parser->cur_obj, true);
  2458. }
  2459. else if (parser->stack && parser->stack->obj) {
  2460. ucl_attach_comment (parser, parser->stack->obj, true);
  2461. }
  2462. else if (parser->top_obj) {
  2463. ucl_attach_comment (parser, parser->top_obj, true);
  2464. }
  2465. else {
  2466. ucl_object_unref (parser->last_comment);
  2467. }
  2468. }
  2469. if (parser->stack != NULL && parser->state != UCL_STATE_ERROR) {
  2470. struct ucl_stack *st;
  2471. bool has_error = false;
  2472. LL_FOREACH (parser->stack, st) {
  2473. if (st->chunk != parser->chunks) {
  2474. break; /* Not our chunk, give up */
  2475. }
  2476. if (st->e.params.flags & UCL_STACK_HAS_OBRACE) {
  2477. if (parser->err == NULL) {
  2478. utstring_new (parser->err);
  2479. }
  2480. utstring_printf (parser->err, "%s:%d unmatched open brace at %d; ",
  2481. chunk->fname ? chunk->fname : "memory",
  2482. parser->chunks->line,
  2483. st->e.params.line);
  2484. has_error = true;
  2485. }
  2486. }
  2487. if (has_error) {
  2488. parser->err_code = UCL_EUNPAIRED;
  2489. return false;
  2490. }
  2491. }
  2492. return true;
  2493. }
  2494. #define UPRM_SAFE(fn, a, b, c, el) do { \
  2495. if (!fn(a, b, c, a)) \
  2496. goto el; \
  2497. } while (0)
  2498. struct ucl_parser*
  2499. ucl_parser_new (int flags)
  2500. {
  2501. struct ucl_parser *parser;
  2502. parser = UCL_ALLOC (sizeof (struct ucl_parser));
  2503. if (parser == NULL) {
  2504. return NULL;
  2505. }
  2506. memset (parser, 0, sizeof (struct ucl_parser));
  2507. UPRM_SAFE(ucl_parser_register_macro, parser, "include", ucl_include_handler, e0);
  2508. UPRM_SAFE(ucl_parser_register_macro, parser, "try_include", ucl_try_include_handler, e0);
  2509. UPRM_SAFE(ucl_parser_register_macro, parser, "includes", ucl_includes_handler, e0);
  2510. UPRM_SAFE(ucl_parser_register_macro, parser, "priority", ucl_priority_handler, e0);
  2511. UPRM_SAFE(ucl_parser_register_macro, parser, "load", ucl_load_handler, e0);
  2512. UPRM_SAFE(ucl_parser_register_context_macro, parser, "inherit", ucl_inherit_handler, e0);
  2513. parser->flags = flags;
  2514. parser->includepaths = NULL;
  2515. if (flags & UCL_PARSER_SAVE_COMMENTS) {
  2516. parser->comments = ucl_object_typed_new (UCL_OBJECT);
  2517. }
  2518. if (!(flags & UCL_PARSER_NO_FILEVARS)) {
  2519. /* Initial assumption about filevars */
  2520. ucl_parser_set_filevars (parser, NULL, false);
  2521. }
  2522. return parser;
  2523. e0:
  2524. ucl_parser_free(parser);
  2525. return NULL;
  2526. }
  2527. bool
  2528. ucl_parser_set_default_priority (struct ucl_parser *parser, unsigned prio)
  2529. {
  2530. if (parser == NULL) {
  2531. return false;
  2532. }
  2533. parser->default_priority = prio;
  2534. return true;
  2535. }
  2536. int
  2537. ucl_parser_get_default_priority (struct ucl_parser *parser)
  2538. {
  2539. if (parser == NULL) {
  2540. return -1;
  2541. }
  2542. return parser->default_priority;
  2543. }
  2544. bool
  2545. ucl_parser_register_macro (struct ucl_parser *parser, const char *macro,
  2546. ucl_macro_handler handler, void* ud)
  2547. {
  2548. struct ucl_macro *new;
  2549. if (macro == NULL || handler == NULL) {
  2550. return false;
  2551. }
  2552. new = UCL_ALLOC (sizeof (struct ucl_macro));
  2553. if (new == NULL) {
  2554. return false;
  2555. }
  2556. memset (new, 0, sizeof (struct ucl_macro));
  2557. new->h.handler = handler;
  2558. new->name = strdup (macro);
  2559. if (new->name == NULL) {
  2560. UCL_FREE (sizeof (struct ucl_macro), new);
  2561. return false;
  2562. }
  2563. new->ud = ud;
  2564. HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new);
  2565. return true;
  2566. }
  2567. bool
  2568. ucl_parser_register_context_macro (struct ucl_parser *parser, const char *macro,
  2569. ucl_context_macro_handler handler, void* ud)
  2570. {
  2571. struct ucl_macro *new;
  2572. if (macro == NULL || handler == NULL) {
  2573. return false;
  2574. }
  2575. new = UCL_ALLOC (sizeof (struct ucl_macro));
  2576. if (new == NULL) {
  2577. return false;
  2578. }
  2579. memset (new, 0, sizeof (struct ucl_macro));
  2580. new->h.context_handler = handler;
  2581. new->name = strdup (macro);
  2582. if (new->name == NULL) {
  2583. UCL_FREE (sizeof (struct ucl_macro), new);
  2584. return false;
  2585. }
  2586. new->ud = ud;
  2587. new->is_context = true;
  2588. HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new);
  2589. return true;
  2590. }
  2591. void
  2592. ucl_parser_register_variable (struct ucl_parser *parser, const char *var,
  2593. const char *value)
  2594. {
  2595. struct ucl_variable *new = NULL, *cur;
  2596. if (var == NULL) {
  2597. return;
  2598. }
  2599. /* Find whether a variable already exists */
  2600. LL_FOREACH (parser->variables, cur) {
  2601. if (strcmp (cur->var, var) == 0) {
  2602. new = cur;
  2603. break;
  2604. }
  2605. }
  2606. if (value == NULL) {
  2607. if (new != NULL) {
  2608. /* Remove variable */
  2609. DL_DELETE (parser->variables, new);
  2610. free (new->var);
  2611. free (new->value);
  2612. UCL_FREE (sizeof (struct ucl_variable), new);
  2613. }
  2614. else {
  2615. /* Do nothing */
  2616. return;
  2617. }
  2618. }
  2619. else {
  2620. if (new == NULL) {
  2621. new = UCL_ALLOC (sizeof (struct ucl_variable));
  2622. if (new == NULL) {
  2623. return;
  2624. }
  2625. memset (new, 0, sizeof (struct ucl_variable));
  2626. new->var = strdup (var);
  2627. new->var_len = strlen (var);
  2628. new->value = strdup (value);
  2629. new->value_len = strlen (value);
  2630. DL_APPEND (parser->variables, new);
  2631. }
  2632. else {
  2633. free (new->value);
  2634. new->value = strdup (value);
  2635. new->value_len = strlen (value);
  2636. }
  2637. }
  2638. }
  2639. void
  2640. ucl_parser_set_variables_handler (struct ucl_parser *parser,
  2641. ucl_variable_handler handler, void *ud)
  2642. {
  2643. parser->var_handler = handler;
  2644. parser->var_data = ud;
  2645. }
  2646. bool
  2647. ucl_parser_add_chunk_full (struct ucl_parser *parser, const unsigned char *data,
  2648. size_t len, unsigned priority, enum ucl_duplicate_strategy strat,
  2649. enum ucl_parse_type parse_type)
  2650. {
  2651. struct ucl_chunk *chunk;
  2652. struct ucl_parser_special_handler *special_handler;
  2653. if (parser == NULL) {
  2654. return false;
  2655. }
  2656. if (data == NULL && len != 0) {
  2657. ucl_create_err (&parser->err, "invalid chunk added");
  2658. return false;
  2659. }
  2660. if (parser->state != UCL_STATE_ERROR) {
  2661. chunk = UCL_ALLOC (sizeof (struct ucl_chunk));
  2662. if (chunk == NULL) {
  2663. ucl_create_err (&parser->err, "cannot allocate chunk structure");
  2664. return false;
  2665. }
  2666. memset (chunk, 0, sizeof (*chunk));
  2667. /* Apply all matching handlers from the first to the last */
  2668. LL_FOREACH (parser->special_handlers, special_handler) {
  2669. if ((special_handler->flags & UCL_SPECIAL_HANDLER_PREPROCESS_ALL) ||
  2670. (len >= special_handler->magic_len &&
  2671. memcmp (data, special_handler->magic, special_handler->magic_len) == 0)) {
  2672. unsigned char *ndata = NULL;
  2673. size_t nlen = 0;
  2674. if (!special_handler->handler (parser, data, len, &ndata, &nlen,
  2675. special_handler->user_data)) {
  2676. UCL_FREE(sizeof (struct ucl_chunk), chunk);
  2677. ucl_create_err (&parser->err, "call for external handler failed");
  2678. return false;
  2679. }
  2680. struct ucl_parser_special_handler_chain *nchain;
  2681. nchain = UCL_ALLOC (sizeof (*nchain));
  2682. nchain->begin = ndata;
  2683. nchain->len = nlen;
  2684. nchain->special_handler = special_handler;
  2685. /* Free order is reversed */
  2686. LL_PREPEND (chunk->special_handlers, nchain);
  2687. data = ndata;
  2688. len = nlen;
  2689. }
  2690. }
  2691. if (parse_type == UCL_PARSE_AUTO && len > 0) {
  2692. /* We need to detect parse type by the first symbol */
  2693. if ((*data & 0x80) == 0x80) {
  2694. parse_type = UCL_PARSE_MSGPACK;
  2695. }
  2696. else if (*data == '(') {
  2697. parse_type = UCL_PARSE_CSEXP;
  2698. }
  2699. else {
  2700. parse_type = UCL_PARSE_UCL;
  2701. }
  2702. }
  2703. chunk->begin = data;
  2704. chunk->remain = len;
  2705. chunk->pos = chunk->begin;
  2706. chunk->end = chunk->begin + len;
  2707. chunk->line = 1;
  2708. chunk->column = 0;
  2709. chunk->priority = priority;
  2710. chunk->strategy = strat;
  2711. chunk->parse_type = parse_type;
  2712. if (parser->cur_file) {
  2713. chunk->fname = strdup (parser->cur_file);
  2714. }
  2715. LL_PREPEND (parser->chunks, chunk);
  2716. parser->recursion ++;
  2717. if (parser->recursion > UCL_MAX_RECURSION) {
  2718. ucl_create_err (&parser->err, "maximum include nesting limit is reached: %d",
  2719. parser->recursion);
  2720. return false;
  2721. }
  2722. if (len > 0) {
  2723. /* Need to parse something */
  2724. switch (parse_type) {
  2725. default:
  2726. case UCL_PARSE_UCL:
  2727. return ucl_state_machine (parser);
  2728. case UCL_PARSE_MSGPACK:
  2729. return ucl_parse_msgpack (parser);
  2730. case UCL_PARSE_CSEXP:
  2731. return ucl_parse_csexp (parser);
  2732. }
  2733. }
  2734. else {
  2735. /* Just add empty chunk and go forward */
  2736. if (parser->top_obj == NULL) {
  2737. /*
  2738. * In case of empty object, create one to indicate that we've
  2739. * read something
  2740. */
  2741. parser->top_obj = ucl_object_new_full (UCL_OBJECT, priority);
  2742. }
  2743. return true;
  2744. }
  2745. }
  2746. ucl_create_err (&parser->err, "a parser is in an invalid state");
  2747. return false;
  2748. }
  2749. bool
  2750. ucl_parser_add_chunk_priority (struct ucl_parser *parser,
  2751. const unsigned char *data, size_t len, unsigned priority)
  2752. {
  2753. /* We dereference parser, so this check is essential */
  2754. if (parser == NULL) {
  2755. return false;
  2756. }
  2757. return ucl_parser_add_chunk_full (parser, data, len,
  2758. priority, UCL_DUPLICATE_APPEND, UCL_PARSE_UCL);
  2759. }
  2760. bool
  2761. ucl_parser_add_chunk (struct ucl_parser *parser, const unsigned char *data,
  2762. size_t len)
  2763. {
  2764. if (parser == NULL) {
  2765. return false;
  2766. }
  2767. return ucl_parser_add_chunk_full (parser, data, len,
  2768. parser->default_priority, UCL_DUPLICATE_APPEND, UCL_PARSE_UCL);
  2769. }
  2770. bool
  2771. ucl_parser_insert_chunk (struct ucl_parser *parser, const unsigned char *data,
  2772. size_t len)
  2773. {
  2774. if (parser == NULL || parser->top_obj == NULL) {
  2775. return false;
  2776. }
  2777. bool res;
  2778. struct ucl_chunk *chunk;
  2779. int state = parser->state;
  2780. parser->state = UCL_STATE_INIT;
  2781. /* Prevent inserted chunks from unintentionally closing the current object */
  2782. if (parser->stack != NULL && parser->stack->next != NULL) {
  2783. parser->stack->e.params.level = parser->stack->next->e.params.level;
  2784. }
  2785. res = ucl_parser_add_chunk_full (parser, data, len, parser->chunks->priority,
  2786. parser->chunks->strategy, parser->chunks->parse_type);
  2787. /* Remove chunk from the stack */
  2788. chunk = parser->chunks;
  2789. if (chunk != NULL) {
  2790. parser->chunks = chunk->next;
  2791. ucl_chunk_free (chunk);
  2792. parser->recursion --;
  2793. }
  2794. parser->state = state;
  2795. return res;
  2796. }
  2797. bool
  2798. ucl_parser_add_string_priority (struct ucl_parser *parser, const char *data,
  2799. size_t len, unsigned priority)
  2800. {
  2801. if (data == NULL) {
  2802. ucl_create_err (&parser->err, "invalid string added");
  2803. return false;
  2804. }
  2805. if (len == 0) {
  2806. len = strlen (data);
  2807. }
  2808. return ucl_parser_add_chunk_priority (parser,
  2809. (const unsigned char *)data, len, priority);
  2810. }
  2811. bool
  2812. ucl_parser_add_string (struct ucl_parser *parser, const char *data,
  2813. size_t len)
  2814. {
  2815. if (parser == NULL) {
  2816. return false;
  2817. }
  2818. return ucl_parser_add_string_priority (parser,
  2819. (const unsigned char *)data, len, parser->default_priority);
  2820. }
  2821. bool
  2822. ucl_set_include_path (struct ucl_parser *parser, ucl_object_t *paths)
  2823. {
  2824. if (parser == NULL || paths == NULL) {
  2825. return false;
  2826. }
  2827. if (parser->includepaths == NULL) {
  2828. parser->includepaths = ucl_object_copy (paths);
  2829. }
  2830. else {
  2831. ucl_object_unref (parser->includepaths);
  2832. parser->includepaths = ucl_object_copy (paths);
  2833. }
  2834. if (parser->includepaths == NULL) {
  2835. return false;
  2836. }
  2837. return true;
  2838. }
  2839. unsigned char ucl_parser_chunk_peek (struct ucl_parser *parser)
  2840. {
  2841. if (parser == NULL || parser->chunks == NULL || parser->chunks->pos == NULL || parser->chunks->end == NULL ||
  2842. parser->chunks->pos == parser->chunks->end) {
  2843. return 0;
  2844. }
  2845. return( *parser->chunks->pos );
  2846. }
  2847. bool ucl_parser_chunk_skip (struct ucl_parser *parser)
  2848. {
  2849. if (parser == NULL || parser->chunks == NULL || parser->chunks->pos == NULL || parser->chunks->end == NULL ||
  2850. parser->chunks->pos == parser->chunks->end) {
  2851. return false;
  2852. }
  2853. const unsigned char *p = parser->chunks->pos;
  2854. ucl_chunk_skipc( parser->chunks, p );
  2855. if( parser->chunks->pos != NULL ) return true;
  2856. return false;
  2857. }
  2858. ucl_object_t*
  2859. ucl_parser_get_current_stack_object (struct ucl_parser *parser, unsigned int depth)
  2860. {
  2861. ucl_object_t *obj;
  2862. if (parser == NULL || parser->stack == NULL) {
  2863. return NULL;
  2864. }
  2865. struct ucl_stack *stack = parser->stack;
  2866. if(stack == NULL || stack->obj == NULL || ucl_object_type (stack->obj) != UCL_OBJECT)
  2867. {
  2868. return NULL;
  2869. }
  2870. for( unsigned int i = 0; i < depth; ++i )
  2871. {
  2872. stack = stack->next;
  2873. if(stack == NULL || stack->obj == NULL || ucl_object_type (stack->obj) != UCL_OBJECT)
  2874. {
  2875. return NULL;
  2876. }
  2877. }
  2878. obj = ucl_object_ref (stack->obj);
  2879. return obj;
  2880. }