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

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