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

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