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

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