emitter.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358
  1. #include "yaml_private.h"
  2. /*
  3. * Flush the buffer if needed.
  4. */
  5. #define FLUSH(emitter) \
  6. ((emitter->buffer.pointer+5 < emitter->buffer.end) \
  7. || yaml_emitter_flush(emitter))
  8. /*
  9. * Put a character to the output buffer.
  10. */
  11. #define PUT(emitter,value) \
  12. (FLUSH(emitter) \
  13. && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \
  14. emitter->column++, \
  15. 1))
  16. /*
  17. * Put a line break to the output buffer.
  18. */
  19. #define PUT_BREAK(emitter) \
  20. (FLUSH(emitter) \
  21. && ((emitter->line_break == YAML_CR_BREAK ? \
  22. (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \
  23. emitter->line_break == YAML_LN_BREAK ? \
  24. (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \
  25. emitter->line_break == YAML_CRLN_BREAK ? \
  26. (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \
  27. *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0), \
  28. emitter->column = 0, \
  29. emitter->line ++, \
  30. 1))
  31. /*
  32. * Copy a character from a string into buffer.
  33. */
  34. #define WRITE(emitter,string) \
  35. (FLUSH(emitter) \
  36. && (COPY(emitter->buffer,string), \
  37. emitter->column ++, \
  38. 1))
  39. /*
  40. * Copy a line break character from a string into buffer.
  41. */
  42. #define WRITE_BREAK(emitter,string) \
  43. (FLUSH(emitter) \
  44. && (CHECK(string,'\n') ? \
  45. (PUT_BREAK(emitter), \
  46. string.pointer ++, \
  47. 1) : \
  48. (COPY(emitter->buffer,string), \
  49. emitter->column = 0, \
  50. emitter->line ++, \
  51. 1)))
  52. /*
  53. * API functions.
  54. */
  55. YAML_DECLARE(int)
  56. yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event);
  57. /*
  58. * Utility functions.
  59. */
  60. static int
  61. yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem);
  62. static int
  63. yaml_emitter_need_more_events(yaml_emitter_t *emitter);
  64. static int
  65. yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
  66. yaml_tag_directive_t value, int allow_duplicates);
  67. static int
  68. yaml_emitter_increase_indent(yaml_emitter_t *emitter,
  69. int flow, int indentless);
  70. /*
  71. * State functions.
  72. */
  73. static int
  74. yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event);
  75. static int
  76. yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
  77. yaml_event_t *event);
  78. static int
  79. yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
  80. yaml_event_t *event, int first);
  81. static int
  82. yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
  83. yaml_event_t *event);
  84. static int
  85. yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
  86. yaml_event_t *event);
  87. static int
  88. yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
  89. yaml_event_t *event, int first);
  90. static int
  91. yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
  92. yaml_event_t *event, int first);
  93. static int
  94. yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
  95. yaml_event_t *event, int simple);
  96. static int
  97. yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
  98. yaml_event_t *event, int first);
  99. static int
  100. yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
  101. yaml_event_t *event, int first);
  102. static int
  103. yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
  104. yaml_event_t *event, int simple);
  105. static int
  106. yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
  107. int root, int sequence, int mapping, int simple_key);
  108. static int
  109. yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event);
  110. static int
  111. yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event);
  112. static int
  113. yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event);
  114. static int
  115. yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event);
  116. /*
  117. * Checkers.
  118. */
  119. static int
  120. yaml_emitter_check_empty_document(yaml_emitter_t *emitter);
  121. static int
  122. yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter);
  123. static int
  124. yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter);
  125. static int
  126. yaml_emitter_check_simple_key(yaml_emitter_t *emitter);
  127. static int
  128. yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event);
  129. /*
  130. * Processors.
  131. */
  132. static int
  133. yaml_emitter_process_anchor(yaml_emitter_t *emitter);
  134. static int
  135. yaml_emitter_process_tag(yaml_emitter_t *emitter);
  136. static int
  137. yaml_emitter_process_scalar(yaml_emitter_t *emitter);
  138. /*
  139. * Analyzers.
  140. */
  141. static int
  142. yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
  143. yaml_version_directive_t version_directive);
  144. static int
  145. yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
  146. yaml_tag_directive_t tag_directive);
  147. static int
  148. yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
  149. yaml_char_t *anchor, int alias);
  150. static int
  151. yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
  152. yaml_char_t *tag);
  153. static int
  154. yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
  155. yaml_char_t *value, size_t length);
  156. static int
  157. yaml_emitter_analyze_event(yaml_emitter_t *emitter,
  158. yaml_event_t *event);
  159. /*
  160. * Writers.
  161. */
  162. static int
  163. yaml_emitter_write_bom(yaml_emitter_t *emitter);
  164. static int
  165. yaml_emitter_write_indent(yaml_emitter_t *emitter);
  166. static int
  167. yaml_emitter_write_indicator(yaml_emitter_t *emitter,
  168. const char *indicator, int need_whitespace,
  169. int is_whitespace, int is_indention);
  170. static int
  171. yaml_emitter_write_anchor(yaml_emitter_t *emitter,
  172. yaml_char_t *value, size_t length);
  173. static int
  174. yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
  175. yaml_char_t *value, size_t length);
  176. static int
  177. yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
  178. yaml_char_t *value, size_t length, int need_whitespace);
  179. static int
  180. yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
  181. yaml_char_t *value, size_t length, int allow_breaks);
  182. static int
  183. yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
  184. yaml_char_t *value, size_t length, int allow_breaks);
  185. static int
  186. yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
  187. yaml_char_t *value, size_t length, int allow_breaks);
  188. static int
  189. yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
  190. yaml_string_t string);
  191. static int
  192. yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
  193. yaml_char_t *value, size_t length);
  194. static int
  195. yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
  196. yaml_char_t *value, size_t length);
  197. /*
  198. * Set an emitter error and return 0.
  199. */
  200. static int
  201. yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem)
  202. {
  203. emitter->error = YAML_EMITTER_ERROR;
  204. emitter->problem = problem;
  205. return 0;
  206. }
  207. /*
  208. * Emit an event.
  209. */
  210. YAML_DECLARE(int)
  211. yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event)
  212. {
  213. if (!ENQUEUE(emitter, emitter->events, *event)) {
  214. yaml_event_delete(event);
  215. return 0;
  216. }
  217. while (!yaml_emitter_need_more_events(emitter)) {
  218. if (!yaml_emitter_analyze_event(emitter, emitter->events.head))
  219. return 0;
  220. if (!yaml_emitter_state_machine(emitter, emitter->events.head))
  221. return 0;
  222. yaml_event_delete(&DEQUEUE(emitter, emitter->events));
  223. }
  224. return 1;
  225. }
  226. /*
  227. * Check if we need to accumulate more events before emitting.
  228. *
  229. * We accumulate extra
  230. * - 1 event for DOCUMENT-START
  231. * - 2 events for SEQUENCE-START
  232. * - 3 events for MAPPING-START
  233. */
  234. static int
  235. yaml_emitter_need_more_events(yaml_emitter_t *emitter)
  236. {
  237. int level = 0;
  238. int accumulate = 0;
  239. yaml_event_t *event;
  240. if (QUEUE_EMPTY(emitter, emitter->events))
  241. return 1;
  242. switch (emitter->events.head->type) {
  243. case YAML_DOCUMENT_START_EVENT:
  244. accumulate = 1;
  245. break;
  246. case YAML_SEQUENCE_START_EVENT:
  247. accumulate = 2;
  248. break;
  249. case YAML_MAPPING_START_EVENT:
  250. accumulate = 3;
  251. break;
  252. default:
  253. return 0;
  254. }
  255. if (emitter->events.tail - emitter->events.head > accumulate)
  256. return 0;
  257. for (event = emitter->events.head; event != emitter->events.tail; event ++) {
  258. switch (event->type) {
  259. case YAML_STREAM_START_EVENT:
  260. case YAML_DOCUMENT_START_EVENT:
  261. case YAML_SEQUENCE_START_EVENT:
  262. case YAML_MAPPING_START_EVENT:
  263. level += 1;
  264. break;
  265. case YAML_STREAM_END_EVENT:
  266. case YAML_DOCUMENT_END_EVENT:
  267. case YAML_SEQUENCE_END_EVENT:
  268. case YAML_MAPPING_END_EVENT:
  269. level -= 1;
  270. break;
  271. default:
  272. break;
  273. }
  274. if (!level)
  275. return 0;
  276. }
  277. return 1;
  278. }
  279. /*
  280. * Append a directive to the directives stack.
  281. */
  282. static int
  283. yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
  284. yaml_tag_directive_t value, int allow_duplicates)
  285. {
  286. yaml_tag_directive_t *tag_directive;
  287. yaml_tag_directive_t copy = { NULL, NULL };
  288. for (tag_directive = emitter->tag_directives.start;
  289. tag_directive != emitter->tag_directives.top; tag_directive ++) {
  290. if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) {
  291. if (allow_duplicates)
  292. return 1;
  293. return yaml_emitter_set_emitter_error(emitter,
  294. "duplicate %TAG directive");
  295. }
  296. }
  297. copy.handle = yaml_strdup(value.handle);
  298. copy.prefix = yaml_strdup(value.prefix);
  299. if (!copy.handle || !copy.prefix) {
  300. emitter->error = YAML_MEMORY_ERROR;
  301. goto error;
  302. }
  303. if (!PUSH(emitter, emitter->tag_directives, copy))
  304. goto error;
  305. return 1;
  306. error:
  307. yaml_free(copy.handle);
  308. yaml_free(copy.prefix);
  309. return 0;
  310. }
  311. /*
  312. * Increase the indentation level.
  313. */
  314. static int
  315. yaml_emitter_increase_indent(yaml_emitter_t *emitter,
  316. int flow, int indentless)
  317. {
  318. if (!PUSH(emitter, emitter->indents, emitter->indent))
  319. return 0;
  320. if (emitter->indent < 0) {
  321. emitter->indent = flow ? emitter->best_indent : 0;
  322. }
  323. else if (!indentless) {
  324. emitter->indent += emitter->best_indent;
  325. }
  326. return 1;
  327. }
  328. /*
  329. * State dispatcher.
  330. */
  331. static int
  332. yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event)
  333. {
  334. switch (emitter->state)
  335. {
  336. case YAML_EMIT_STREAM_START_STATE:
  337. return yaml_emitter_emit_stream_start(emitter, event);
  338. case YAML_EMIT_FIRST_DOCUMENT_START_STATE:
  339. return yaml_emitter_emit_document_start(emitter, event, 1);
  340. case YAML_EMIT_DOCUMENT_START_STATE:
  341. return yaml_emitter_emit_document_start(emitter, event, 0);
  342. case YAML_EMIT_DOCUMENT_CONTENT_STATE:
  343. return yaml_emitter_emit_document_content(emitter, event);
  344. case YAML_EMIT_DOCUMENT_END_STATE:
  345. return yaml_emitter_emit_document_end(emitter, event);
  346. case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
  347. return yaml_emitter_emit_flow_sequence_item(emitter, event, 1);
  348. case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE:
  349. return yaml_emitter_emit_flow_sequence_item(emitter, event, 0);
  350. case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
  351. return yaml_emitter_emit_flow_mapping_key(emitter, event, 1);
  352. case YAML_EMIT_FLOW_MAPPING_KEY_STATE:
  353. return yaml_emitter_emit_flow_mapping_key(emitter, event, 0);
  354. case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
  355. return yaml_emitter_emit_flow_mapping_value(emitter, event, 1);
  356. case YAML_EMIT_FLOW_MAPPING_VALUE_STATE:
  357. return yaml_emitter_emit_flow_mapping_value(emitter, event, 0);
  358. case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
  359. return yaml_emitter_emit_block_sequence_item(emitter, event, 1);
  360. case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
  361. return yaml_emitter_emit_block_sequence_item(emitter, event, 0);
  362. case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
  363. return yaml_emitter_emit_block_mapping_key(emitter, event, 1);
  364. case YAML_EMIT_BLOCK_MAPPING_KEY_STATE:
  365. return yaml_emitter_emit_block_mapping_key(emitter, event, 0);
  366. case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
  367. return yaml_emitter_emit_block_mapping_value(emitter, event, 1);
  368. case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE:
  369. return yaml_emitter_emit_block_mapping_value(emitter, event, 0);
  370. case YAML_EMIT_END_STATE:
  371. return yaml_emitter_set_emitter_error(emitter,
  372. "expected nothing after STREAM-END");
  373. default:
  374. assert(1); /* Invalid state. */
  375. }
  376. return 0;
  377. }
  378. /*
  379. * Expect STREAM-START.
  380. */
  381. static int
  382. yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
  383. yaml_event_t *event)
  384. {
  385. emitter->open_ended = 0;
  386. if (event->type == YAML_STREAM_START_EVENT)
  387. {
  388. if (!emitter->encoding) {
  389. emitter->encoding = event->data.stream_start.encoding;
  390. }
  391. if (!emitter->encoding) {
  392. emitter->encoding = YAML_UTF8_ENCODING;
  393. }
  394. if (emitter->best_indent < 2 || emitter->best_indent > 9) {
  395. emitter->best_indent = 2;
  396. }
  397. if (emitter->best_width >= 0
  398. && emitter->best_width <= emitter->best_indent*2) {
  399. emitter->best_width = 80;
  400. }
  401. if (emitter->best_width < 0) {
  402. emitter->best_width = INT_MAX;
  403. }
  404. if (!emitter->line_break) {
  405. emitter->line_break = YAML_LN_BREAK;
  406. }
  407. emitter->indent = -1;
  408. emitter->line = 0;
  409. emitter->column = 0;
  410. emitter->whitespace = 1;
  411. emitter->indention = 1;
  412. if (emitter->encoding != YAML_UTF8_ENCODING) {
  413. if (!yaml_emitter_write_bom(emitter))
  414. return 0;
  415. }
  416. emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE;
  417. return 1;
  418. }
  419. return yaml_emitter_set_emitter_error(emitter,
  420. "expected STREAM-START");
  421. }
  422. /*
  423. * Expect DOCUMENT-START or STREAM-END.
  424. */
  425. static int
  426. yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
  427. yaml_event_t *event, int first)
  428. {
  429. if (event->type == YAML_DOCUMENT_START_EVENT)
  430. {
  431. yaml_tag_directive_t default_tag_directives[] = {
  432. {(yaml_char_t *)"!", (yaml_char_t *)"!"},
  433. {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"},
  434. {NULL, NULL}
  435. };
  436. yaml_tag_directive_t *tag_directive;
  437. int implicit;
  438. if (event->data.document_start.version_directive) {
  439. if (!yaml_emitter_analyze_version_directive(emitter,
  440. *event->data.document_start.version_directive))
  441. return 0;
  442. }
  443. for (tag_directive = event->data.document_start.tag_directives.start;
  444. tag_directive != event->data.document_start.tag_directives.end;
  445. tag_directive ++) {
  446. if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive))
  447. return 0;
  448. if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0))
  449. return 0;
  450. }
  451. for (tag_directive = default_tag_directives;
  452. tag_directive->handle; tag_directive ++) {
  453. if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1))
  454. return 0;
  455. }
  456. implicit = event->data.document_start.implicit;
  457. if (!first || emitter->canonical) {
  458. implicit = 0;
  459. }
  460. if ((event->data.document_start.version_directive ||
  461. (event->data.document_start.tag_directives.start
  462. != event->data.document_start.tag_directives.end)) &&
  463. emitter->open_ended)
  464. {
  465. if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
  466. return 0;
  467. if (!yaml_emitter_write_indent(emitter))
  468. return 0;
  469. }
  470. emitter->open_ended = 0;
  471. if (event->data.document_start.version_directive) {
  472. implicit = 0;
  473. if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
  474. return 0;
  475. if (event->data.document_start.version_directive->minor == 1) {
  476. if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
  477. return 0;
  478. }
  479. else {
  480. if (!yaml_emitter_write_indicator(emitter, "1.2", 1, 0, 0))
  481. return 0;
  482. }
  483. if (!yaml_emitter_write_indent(emitter))
  484. return 0;
  485. }
  486. if (event->data.document_start.tag_directives.start
  487. != event->data.document_start.tag_directives.end) {
  488. implicit = 0;
  489. for (tag_directive = event->data.document_start.tag_directives.start;
  490. tag_directive != event->data.document_start.tag_directives.end;
  491. tag_directive ++) {
  492. if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0))
  493. return 0;
  494. if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle,
  495. strlen((char *)tag_directive->handle)))
  496. return 0;
  497. if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix,
  498. strlen((char *)tag_directive->prefix), 1))
  499. return 0;
  500. if (!yaml_emitter_write_indent(emitter))
  501. return 0;
  502. }
  503. }
  504. if (yaml_emitter_check_empty_document(emitter)) {
  505. implicit = 0;
  506. }
  507. if (!implicit) {
  508. if (!yaml_emitter_write_indent(emitter))
  509. return 0;
  510. if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0))
  511. return 0;
  512. if (emitter->canonical) {
  513. if (!yaml_emitter_write_indent(emitter))
  514. return 0;
  515. }
  516. }
  517. emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE;
  518. emitter->open_ended = 0;
  519. return 1;
  520. }
  521. else if (event->type == YAML_STREAM_END_EVENT)
  522. {
  523. /**
  524. * This can happen if a block scalar with trailing empty lines
  525. * is at the end of the stream
  526. */
  527. if (emitter->open_ended == 2)
  528. {
  529. if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
  530. return 0;
  531. emitter->open_ended = 0;
  532. if (!yaml_emitter_write_indent(emitter))
  533. return 0;
  534. }
  535. if (!yaml_emitter_flush(emitter))
  536. return 0;
  537. emitter->state = YAML_EMIT_END_STATE;
  538. return 1;
  539. }
  540. return yaml_emitter_set_emitter_error(emitter,
  541. "expected DOCUMENT-START or STREAM-END");
  542. }
  543. /*
  544. * Expect the root node.
  545. */
  546. static int
  547. yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
  548. yaml_event_t *event)
  549. {
  550. if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE))
  551. return 0;
  552. return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0);
  553. }
  554. /*
  555. * Expect DOCUMENT-END.
  556. */
  557. static int
  558. yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
  559. yaml_event_t *event)
  560. {
  561. if (event->type == YAML_DOCUMENT_END_EVENT)
  562. {
  563. if (!yaml_emitter_write_indent(emitter))
  564. return 0;
  565. if (!event->data.document_end.implicit) {
  566. if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
  567. return 0;
  568. emitter->open_ended = 0;
  569. if (!yaml_emitter_write_indent(emitter))
  570. return 0;
  571. }
  572. else if (!emitter->open_ended)
  573. emitter->open_ended = 1;
  574. if (!yaml_emitter_flush(emitter))
  575. return 0;
  576. emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
  577. while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
  578. yaml_tag_directive_t tag_directive = POP(emitter,
  579. emitter->tag_directives);
  580. yaml_free(tag_directive.handle);
  581. yaml_free(tag_directive.prefix);
  582. }
  583. return 1;
  584. }
  585. return yaml_emitter_set_emitter_error(emitter,
  586. "expected DOCUMENT-END");
  587. }
  588. /*
  589. *
  590. * Expect a flow item node.
  591. */
  592. static int
  593. yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
  594. yaml_event_t *event, int first)
  595. {
  596. if (first)
  597. {
  598. if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
  599. return 0;
  600. if (!yaml_emitter_increase_indent(emitter, 1, 0))
  601. return 0;
  602. emitter->flow_level ++;
  603. }
  604. if (event->type == YAML_SEQUENCE_END_EVENT)
  605. {
  606. emitter->flow_level --;
  607. emitter->indent = POP(emitter, emitter->indents);
  608. if (emitter->canonical && !first) {
  609. if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
  610. return 0;
  611. if (!yaml_emitter_write_indent(emitter))
  612. return 0;
  613. }
  614. if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
  615. return 0;
  616. emitter->state = POP(emitter, emitter->states);
  617. return 1;
  618. }
  619. if (!first) {
  620. if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
  621. return 0;
  622. }
  623. if (emitter->canonical || emitter->column > emitter->best_width) {
  624. if (!yaml_emitter_write_indent(emitter))
  625. return 0;
  626. }
  627. if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
  628. return 0;
  629. return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
  630. }
  631. /*
  632. * Expect a flow key node.
  633. */
  634. static int
  635. yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
  636. yaml_event_t *event, int first)
  637. {
  638. if (first)
  639. {
  640. if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
  641. return 0;
  642. if (!yaml_emitter_increase_indent(emitter, 1, 0))
  643. return 0;
  644. emitter->flow_level ++;
  645. }
  646. if (event->type == YAML_MAPPING_END_EVENT)
  647. {
  648. emitter->flow_level --;
  649. emitter->indent = POP(emitter, emitter->indents);
  650. if (emitter->canonical && !first) {
  651. if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
  652. return 0;
  653. if (!yaml_emitter_write_indent(emitter))
  654. return 0;
  655. }
  656. if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
  657. return 0;
  658. emitter->state = POP(emitter, emitter->states);
  659. return 1;
  660. }
  661. if (!first) {
  662. if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
  663. return 0;
  664. }
  665. if (emitter->canonical || emitter->column > emitter->best_width) {
  666. if (!yaml_emitter_write_indent(emitter))
  667. return 0;
  668. }
  669. if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
  670. {
  671. if (!PUSH(emitter, emitter->states,
  672. YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
  673. return 0;
  674. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
  675. }
  676. else
  677. {
  678. if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
  679. return 0;
  680. if (!PUSH(emitter, emitter->states,
  681. YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
  682. return 0;
  683. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
  684. }
  685. }
  686. /*
  687. * Expect a flow value node.
  688. */
  689. static int
  690. yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
  691. yaml_event_t *event, int simple)
  692. {
  693. if (simple) {
  694. if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
  695. return 0;
  696. }
  697. else {
  698. if (emitter->canonical || emitter->column > emitter->best_width) {
  699. if (!yaml_emitter_write_indent(emitter))
  700. return 0;
  701. }
  702. if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
  703. return 0;
  704. }
  705. if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
  706. return 0;
  707. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
  708. }
  709. /*
  710. * Expect a block item node.
  711. */
  712. static int
  713. yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
  714. yaml_event_t *event, int first)
  715. {
  716. if (first)
  717. {
  718. if (!yaml_emitter_increase_indent(emitter, 0,
  719. (emitter->mapping_context && !emitter->indention)))
  720. return 0;
  721. }
  722. if (event->type == YAML_SEQUENCE_END_EVENT)
  723. {
  724. emitter->indent = POP(emitter, emitter->indents);
  725. emitter->state = POP(emitter, emitter->states);
  726. return 1;
  727. }
  728. if (!yaml_emitter_write_indent(emitter))
  729. return 0;
  730. if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
  731. return 0;
  732. if (!PUSH(emitter, emitter->states,
  733. YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
  734. return 0;
  735. return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
  736. }
  737. /*
  738. * Expect a block key node.
  739. */
  740. static int
  741. yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
  742. yaml_event_t *event, int first)
  743. {
  744. if (first)
  745. {
  746. if (!yaml_emitter_increase_indent(emitter, 0, 0))
  747. return 0;
  748. }
  749. if (event->type == YAML_MAPPING_END_EVENT)
  750. {
  751. emitter->indent = POP(emitter, emitter->indents);
  752. emitter->state = POP(emitter, emitter->states);
  753. return 1;
  754. }
  755. if (!yaml_emitter_write_indent(emitter))
  756. return 0;
  757. if (yaml_emitter_check_simple_key(emitter))
  758. {
  759. if (!PUSH(emitter, emitter->states,
  760. YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
  761. return 0;
  762. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
  763. }
  764. else
  765. {
  766. if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
  767. return 0;
  768. if (!PUSH(emitter, emitter->states,
  769. YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
  770. return 0;
  771. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
  772. }
  773. }
  774. /*
  775. * Expect a block value node.
  776. */
  777. static int
  778. yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
  779. yaml_event_t *event, int simple)
  780. {
  781. if (simple) {
  782. if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
  783. return 0;
  784. }
  785. else {
  786. if (!yaml_emitter_write_indent(emitter))
  787. return 0;
  788. if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
  789. return 0;
  790. }
  791. if (!PUSH(emitter, emitter->states,
  792. YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
  793. return 0;
  794. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
  795. }
  796. /*
  797. * Expect a node.
  798. */
  799. static int
  800. yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
  801. int root, int sequence, int mapping, int simple_key)
  802. {
  803. emitter->root_context = root;
  804. emitter->sequence_context = sequence;
  805. emitter->mapping_context = mapping;
  806. emitter->simple_key_context = simple_key;
  807. switch (event->type)
  808. {
  809. case YAML_ALIAS_EVENT:
  810. return yaml_emitter_emit_alias(emitter, event);
  811. case YAML_SCALAR_EVENT:
  812. return yaml_emitter_emit_scalar(emitter, event);
  813. case YAML_SEQUENCE_START_EVENT:
  814. return yaml_emitter_emit_sequence_start(emitter, event);
  815. case YAML_MAPPING_START_EVENT:
  816. return yaml_emitter_emit_mapping_start(emitter, event);
  817. default:
  818. return yaml_emitter_set_emitter_error(emitter,
  819. "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
  820. }
  821. return 0;
  822. }
  823. /*
  824. * Expect ALIAS.
  825. */
  826. static int
  827. yaml_emitter_emit_alias(yaml_emitter_t *emitter, SHIM(yaml_event_t *event))
  828. {
  829. if (!yaml_emitter_process_anchor(emitter))
  830. return 0;
  831. if (emitter->simple_key_context)
  832. if (!PUT(emitter, ' ')) return 0;
  833. emitter->state = POP(emitter, emitter->states);
  834. return 1;
  835. }
  836. /*
  837. * Expect SCALAR.
  838. */
  839. static int
  840. yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
  841. {
  842. if (!yaml_emitter_select_scalar_style(emitter, event))
  843. return 0;
  844. if (!yaml_emitter_process_anchor(emitter))
  845. return 0;
  846. if (!yaml_emitter_process_tag(emitter))
  847. return 0;
  848. if (!yaml_emitter_increase_indent(emitter, 1, 0))
  849. return 0;
  850. if (!yaml_emitter_process_scalar(emitter))
  851. return 0;
  852. emitter->indent = POP(emitter, emitter->indents);
  853. emitter->state = POP(emitter, emitter->states);
  854. return 1;
  855. }
  856. /*
  857. * Expect SEQUENCE-START.
  858. */
  859. static int
  860. yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
  861. {
  862. if (!yaml_emitter_process_anchor(emitter))
  863. return 0;
  864. if (!yaml_emitter_process_tag(emitter))
  865. return 0;
  866. if (emitter->flow_level || emitter->canonical
  867. || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
  868. || yaml_emitter_check_empty_sequence(emitter)) {
  869. emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
  870. }
  871. else {
  872. emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
  873. }
  874. return 1;
  875. }
  876. /*
  877. * Expect MAPPING-START.
  878. */
  879. static int
  880. yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
  881. {
  882. if (!yaml_emitter_process_anchor(emitter))
  883. return 0;
  884. if (!yaml_emitter_process_tag(emitter))
  885. return 0;
  886. if (emitter->flow_level || emitter->canonical
  887. || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
  888. || yaml_emitter_check_empty_mapping(emitter)) {
  889. emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
  890. }
  891. else {
  892. emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
  893. }
  894. return 1;
  895. }
  896. /*
  897. * Check if the document content is an empty scalar.
  898. */
  899. static int
  900. yaml_emitter_check_empty_document(SHIM(yaml_emitter_t *emitter))
  901. {
  902. return 0;
  903. }
  904. /*
  905. * Check if the next events represent an empty sequence.
  906. */
  907. static int
  908. yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
  909. {
  910. if (emitter->events.tail - emitter->events.head < 2)
  911. return 0;
  912. return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
  913. && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
  914. }
  915. /*
  916. * Check if the next events represent an empty mapping.
  917. */
  918. static int
  919. yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
  920. {
  921. if (emitter->events.tail - emitter->events.head < 2)
  922. return 0;
  923. return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
  924. && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
  925. }
  926. /*
  927. * Check if the next node can be expressed as a simple key.
  928. */
  929. static int
  930. yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
  931. {
  932. yaml_event_t *event = emitter->events.head;
  933. size_t length = 0;
  934. switch (event->type)
  935. {
  936. case YAML_ALIAS_EVENT:
  937. length += emitter->anchor_data.anchor_length;
  938. break;
  939. case YAML_SCALAR_EVENT:
  940. if (emitter->scalar_data.multiline)
  941. return 0;
  942. length += emitter->anchor_data.anchor_length
  943. + emitter->tag_data.handle_length
  944. + emitter->tag_data.suffix_length
  945. + emitter->scalar_data.length;
  946. break;
  947. case YAML_SEQUENCE_START_EVENT:
  948. if (!yaml_emitter_check_empty_sequence(emitter))
  949. return 0;
  950. length += emitter->anchor_data.anchor_length
  951. + emitter->tag_data.handle_length
  952. + emitter->tag_data.suffix_length;
  953. break;
  954. case YAML_MAPPING_START_EVENT:
  955. if (!yaml_emitter_check_empty_mapping(emitter))
  956. return 0;
  957. length += emitter->anchor_data.anchor_length
  958. + emitter->tag_data.handle_length
  959. + emitter->tag_data.suffix_length;
  960. break;
  961. default:
  962. return 0;
  963. }
  964. if (length > 128)
  965. return 0;
  966. return 1;
  967. }
  968. /*
  969. * Determine an acceptable scalar style.
  970. */
  971. static int
  972. yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
  973. {
  974. yaml_scalar_style_t style = event->data.scalar.style;
  975. int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix);
  976. if (no_tag && !event->data.scalar.plain_implicit
  977. && !event->data.scalar.quoted_implicit) {
  978. return yaml_emitter_set_emitter_error(emitter,
  979. "neither tag nor implicit flags are specified");
  980. }
  981. if (style == YAML_ANY_SCALAR_STYLE)
  982. style = YAML_PLAIN_SCALAR_STYLE;
  983. if (emitter->canonical)
  984. style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
  985. if (emitter->simple_key_context && emitter->scalar_data.multiline)
  986. style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
  987. if (style == YAML_PLAIN_SCALAR_STYLE)
  988. {
  989. if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
  990. || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
  991. style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
  992. if (!emitter->scalar_data.length
  993. && (emitter->flow_level || emitter->simple_key_context))
  994. style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
  995. if (no_tag && !event->data.scalar.plain_implicit)
  996. style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
  997. }
  998. if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
  999. {
  1000. if (!emitter->scalar_data.single_quoted_allowed)
  1001. style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
  1002. }
  1003. if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
  1004. {
  1005. if (!emitter->scalar_data.block_allowed
  1006. || emitter->flow_level || emitter->simple_key_context)
  1007. style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
  1008. }
  1009. if (no_tag && !event->data.scalar.quoted_implicit
  1010. && style != YAML_PLAIN_SCALAR_STYLE)
  1011. {
  1012. emitter->tag_data.handle = (yaml_char_t *)"!";
  1013. emitter->tag_data.handle_length = 1;
  1014. }
  1015. emitter->scalar_data.style = style;
  1016. return 1;
  1017. }
  1018. /*
  1019. * Write an anchor.
  1020. */
  1021. static int
  1022. yaml_emitter_process_anchor(yaml_emitter_t *emitter)
  1023. {
  1024. if (!emitter->anchor_data.anchor)
  1025. return 1;
  1026. if (!yaml_emitter_write_indicator(emitter,
  1027. (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
  1028. return 0;
  1029. return yaml_emitter_write_anchor(emitter,
  1030. emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
  1031. }
  1032. /*
  1033. * Write a tag.
  1034. */
  1035. static int
  1036. yaml_emitter_process_tag(yaml_emitter_t *emitter)
  1037. {
  1038. if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
  1039. return 1;
  1040. if (emitter->tag_data.handle)
  1041. {
  1042. if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
  1043. emitter->tag_data.handle_length))
  1044. return 0;
  1045. if (emitter->tag_data.suffix) {
  1046. if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
  1047. emitter->tag_data.suffix_length, 0))
  1048. return 0;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
  1054. return 0;
  1055. if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
  1056. emitter->tag_data.suffix_length, 0))
  1057. return 0;
  1058. if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
  1059. return 0;
  1060. }
  1061. return 1;
  1062. }
  1063. /*
  1064. * Write a scalar.
  1065. */
  1066. static int
  1067. yaml_emitter_process_scalar(yaml_emitter_t *emitter)
  1068. {
  1069. switch (emitter->scalar_data.style)
  1070. {
  1071. case YAML_PLAIN_SCALAR_STYLE:
  1072. return yaml_emitter_write_plain_scalar(emitter,
  1073. emitter->scalar_data.value, emitter->scalar_data.length,
  1074. !emitter->simple_key_context);
  1075. case YAML_SINGLE_QUOTED_SCALAR_STYLE:
  1076. return yaml_emitter_write_single_quoted_scalar(emitter,
  1077. emitter->scalar_data.value, emitter->scalar_data.length,
  1078. !emitter->simple_key_context);
  1079. case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
  1080. return yaml_emitter_write_double_quoted_scalar(emitter,
  1081. emitter->scalar_data.value, emitter->scalar_data.length,
  1082. !emitter->simple_key_context);
  1083. case YAML_LITERAL_SCALAR_STYLE:
  1084. return yaml_emitter_write_literal_scalar(emitter,
  1085. emitter->scalar_data.value, emitter->scalar_data.length);
  1086. case YAML_FOLDED_SCALAR_STYLE:
  1087. return yaml_emitter_write_folded_scalar(emitter,
  1088. emitter->scalar_data.value, emitter->scalar_data.length);
  1089. default:
  1090. assert(1); /* Impossible. */
  1091. }
  1092. return 0;
  1093. }
  1094. /*
  1095. * Check if a %YAML directive is valid.
  1096. */
  1097. static int
  1098. yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
  1099. yaml_version_directive_t version_directive)
  1100. {
  1101. if (version_directive.major != 1 || (
  1102. version_directive.minor != 1
  1103. && version_directive.minor != 2
  1104. )) {
  1105. return yaml_emitter_set_emitter_error(emitter,
  1106. "incompatible %YAML directive");
  1107. }
  1108. return 1;
  1109. }
  1110. /*
  1111. * Check if a %TAG directive is valid.
  1112. */
  1113. static int
  1114. yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
  1115. yaml_tag_directive_t tag_directive)
  1116. {
  1117. yaml_string_t handle;
  1118. yaml_string_t prefix;
  1119. size_t handle_length;
  1120. size_t prefix_length;
  1121. handle_length = strlen((char *)tag_directive.handle);
  1122. prefix_length = strlen((char *)tag_directive.prefix);
  1123. STRING_ASSIGN(handle, tag_directive.handle, handle_length);
  1124. STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length);
  1125. if (handle.start == handle.end) {
  1126. return yaml_emitter_set_emitter_error(emitter,
  1127. "tag handle must not be empty");
  1128. }
  1129. if (handle.start[0] != '!') {
  1130. return yaml_emitter_set_emitter_error(emitter,
  1131. "tag handle must start with '!'");
  1132. }
  1133. if (handle.end[-1] != '!') {
  1134. return yaml_emitter_set_emitter_error(emitter,
  1135. "tag handle must end with '!'");
  1136. }
  1137. handle.pointer ++;
  1138. while (handle.pointer < handle.end-1) {
  1139. if (!IS_ALPHA(handle)) {
  1140. return yaml_emitter_set_emitter_error(emitter,
  1141. "tag handle must contain alphanumerical characters only");
  1142. }
  1143. MOVE(handle);
  1144. }
  1145. if (prefix.start == prefix.end) {
  1146. return yaml_emitter_set_emitter_error(emitter,
  1147. "tag prefix must not be empty");
  1148. }
  1149. return 1;
  1150. }
  1151. /*
  1152. * Check if an anchor is valid.
  1153. */
  1154. static int
  1155. yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
  1156. yaml_char_t *anchor, int alias)
  1157. {
  1158. size_t anchor_length;
  1159. yaml_string_t string;
  1160. anchor_length = strlen((char *)anchor);
  1161. STRING_ASSIGN(string, anchor, anchor_length);
  1162. if (string.start == string.end) {
  1163. return yaml_emitter_set_emitter_error(emitter, alias ?
  1164. "alias value must not be empty" :
  1165. "anchor value must not be empty");
  1166. }
  1167. while (string.pointer != string.end) {
  1168. if (!IS_ALPHA(string)) {
  1169. return yaml_emitter_set_emitter_error(emitter, alias ?
  1170. "alias value must contain alphanumerical characters only" :
  1171. "anchor value must contain alphanumerical characters only");
  1172. }
  1173. MOVE(string);
  1174. }
  1175. emitter->anchor_data.anchor = string.start;
  1176. emitter->anchor_data.anchor_length = string.end - string.start;
  1177. emitter->anchor_data.alias = alias;
  1178. return 1;
  1179. }
  1180. /*
  1181. * Check if a tag is valid.
  1182. */
  1183. static int
  1184. yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
  1185. yaml_char_t *tag)
  1186. {
  1187. size_t tag_length;
  1188. yaml_string_t string;
  1189. yaml_tag_directive_t *tag_directive;
  1190. tag_length = strlen((char *)tag);
  1191. STRING_ASSIGN(string, tag, tag_length);
  1192. if (string.start == string.end) {
  1193. return yaml_emitter_set_emitter_error(emitter,
  1194. "tag value must not be empty");
  1195. }
  1196. for (tag_directive = emitter->tag_directives.start;
  1197. tag_directive != emitter->tag_directives.top; tag_directive ++) {
  1198. size_t prefix_length = strlen((char *)tag_directive->prefix);
  1199. if (prefix_length < (size_t)(string.end - string.start)
  1200. && strncmp((char *)tag_directive->prefix, (char *)string.start,
  1201. prefix_length) == 0)
  1202. {
  1203. emitter->tag_data.handle = tag_directive->handle;
  1204. emitter->tag_data.handle_length =
  1205. strlen((char *)tag_directive->handle);
  1206. emitter->tag_data.suffix = string.start + prefix_length;
  1207. emitter->tag_data.suffix_length =
  1208. (string.end - string.start) - prefix_length;
  1209. return 1;
  1210. }
  1211. }
  1212. emitter->tag_data.suffix = string.start;
  1213. emitter->tag_data.suffix_length = string.end - string.start;
  1214. return 1;
  1215. }
  1216. /*
  1217. * Check if a scalar is valid.
  1218. */
  1219. static int
  1220. yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
  1221. yaml_char_t *value, size_t length)
  1222. {
  1223. yaml_string_t string;
  1224. int block_indicators = 0;
  1225. int flow_indicators = 0;
  1226. int line_breaks = 0;
  1227. int special_characters = 0;
  1228. int leading_space = 0;
  1229. int leading_break = 0;
  1230. int trailing_space = 0;
  1231. int trailing_break = 0;
  1232. int break_space = 0;
  1233. int space_break = 0;
  1234. int preceded_by_whitespace = 0;
  1235. int followed_by_whitespace = 0;
  1236. int previous_space = 0;
  1237. int previous_break = 0;
  1238. STRING_ASSIGN(string, value, length);
  1239. emitter->scalar_data.value = value;
  1240. emitter->scalar_data.length = length;
  1241. if (string.start == string.end)
  1242. {
  1243. emitter->scalar_data.multiline = 0;
  1244. emitter->scalar_data.flow_plain_allowed = 0;
  1245. emitter->scalar_data.block_plain_allowed = 1;
  1246. emitter->scalar_data.single_quoted_allowed = 1;
  1247. emitter->scalar_data.block_allowed = 0;
  1248. return 1;
  1249. }
  1250. if ((CHECK_AT(string, '-', 0)
  1251. && CHECK_AT(string, '-', 1)
  1252. && CHECK_AT(string, '-', 2))
  1253. || (CHECK_AT(string, '.', 0)
  1254. && CHECK_AT(string, '.', 1)
  1255. && CHECK_AT(string, '.', 2))) {
  1256. block_indicators = 1;
  1257. flow_indicators = 1;
  1258. }
  1259. preceded_by_whitespace = 1;
  1260. followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
  1261. while (string.pointer != string.end)
  1262. {
  1263. if (string.start == string.pointer)
  1264. {
  1265. if (CHECK(string, '#') || CHECK(string, ',')
  1266. || CHECK(string, '[') || CHECK(string, ']')
  1267. || CHECK(string, '{') || CHECK(string, '}')
  1268. || CHECK(string, '&') || CHECK(string, '*')
  1269. || CHECK(string, '!') || CHECK(string, '|')
  1270. || CHECK(string, '>') || CHECK(string, '\'')
  1271. || CHECK(string, '"') || CHECK(string, '%')
  1272. || CHECK(string, '@') || CHECK(string, '`')) {
  1273. flow_indicators = 1;
  1274. block_indicators = 1;
  1275. }
  1276. if (CHECK(string, '?') || CHECK(string, ':')) {
  1277. flow_indicators = 1;
  1278. if (followed_by_whitespace) {
  1279. block_indicators = 1;
  1280. }
  1281. }
  1282. if (CHECK(string, '-') && followed_by_whitespace) {
  1283. flow_indicators = 1;
  1284. block_indicators = 1;
  1285. }
  1286. }
  1287. else
  1288. {
  1289. if (CHECK(string, ',') || CHECK(string, '?')
  1290. || CHECK(string, '[') || CHECK(string, ']')
  1291. || CHECK(string, '{') || CHECK(string, '}')) {
  1292. flow_indicators = 1;
  1293. }
  1294. if (CHECK(string, ':')) {
  1295. flow_indicators = 1;
  1296. if (followed_by_whitespace) {
  1297. block_indicators = 1;
  1298. }
  1299. }
  1300. if (CHECK(string, '#') && preceded_by_whitespace) {
  1301. flow_indicators = 1;
  1302. block_indicators = 1;
  1303. }
  1304. }
  1305. if (!IS_PRINTABLE(string)
  1306. || (!IS_ASCII(string) && !emitter->unicode)) {
  1307. special_characters = 1;
  1308. }
  1309. if (IS_BREAK(string)) {
  1310. line_breaks = 1;
  1311. }
  1312. if (IS_SPACE(string))
  1313. {
  1314. if (string.start == string.pointer) {
  1315. leading_space = 1;
  1316. }
  1317. if (string.pointer+WIDTH(string) == string.end) {
  1318. trailing_space = 1;
  1319. }
  1320. if (previous_break) {
  1321. break_space = 1;
  1322. }
  1323. previous_space = 1;
  1324. previous_break = 0;
  1325. }
  1326. else if (IS_BREAK(string))
  1327. {
  1328. if (string.start == string.pointer) {
  1329. leading_break = 1;
  1330. }
  1331. if (string.pointer+WIDTH(string) == string.end) {
  1332. trailing_break = 1;
  1333. }
  1334. if (previous_space) {
  1335. space_break = 1;
  1336. }
  1337. previous_space = 0;
  1338. previous_break = 1;
  1339. }
  1340. else
  1341. {
  1342. previous_space = 0;
  1343. previous_break = 0;
  1344. }
  1345. preceded_by_whitespace = IS_BLANKZ(string);
  1346. MOVE(string);
  1347. if (string.pointer != string.end) {
  1348. followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
  1349. }
  1350. }
  1351. emitter->scalar_data.multiline = line_breaks;
  1352. emitter->scalar_data.flow_plain_allowed = 1;
  1353. emitter->scalar_data.block_plain_allowed = 1;
  1354. emitter->scalar_data.single_quoted_allowed = 1;
  1355. emitter->scalar_data.block_allowed = 1;
  1356. if (leading_space || leading_break || trailing_space || trailing_break) {
  1357. emitter->scalar_data.flow_plain_allowed = 0;
  1358. emitter->scalar_data.block_plain_allowed = 0;
  1359. }
  1360. if (trailing_space) {
  1361. emitter->scalar_data.block_allowed = 0;
  1362. }
  1363. if (break_space) {
  1364. emitter->scalar_data.flow_plain_allowed = 0;
  1365. emitter->scalar_data.block_plain_allowed = 0;
  1366. emitter->scalar_data.single_quoted_allowed = 0;
  1367. }
  1368. if (space_break || special_characters) {
  1369. emitter->scalar_data.flow_plain_allowed = 0;
  1370. emitter->scalar_data.block_plain_allowed = 0;
  1371. emitter->scalar_data.single_quoted_allowed = 0;
  1372. emitter->scalar_data.block_allowed = 0;
  1373. }
  1374. if (line_breaks) {
  1375. emitter->scalar_data.flow_plain_allowed = 0;
  1376. emitter->scalar_data.block_plain_allowed = 0;
  1377. }
  1378. if (flow_indicators) {
  1379. emitter->scalar_data.flow_plain_allowed = 0;
  1380. }
  1381. if (block_indicators) {
  1382. emitter->scalar_data.block_plain_allowed = 0;
  1383. }
  1384. return 1;
  1385. }
  1386. /*
  1387. * Check if the event data is valid.
  1388. */
  1389. static int
  1390. yaml_emitter_analyze_event(yaml_emitter_t *emitter,
  1391. yaml_event_t *event)
  1392. {
  1393. emitter->anchor_data.anchor = NULL;
  1394. emitter->anchor_data.anchor_length = 0;
  1395. emitter->tag_data.handle = NULL;
  1396. emitter->tag_data.handle_length = 0;
  1397. emitter->tag_data.suffix = NULL;
  1398. emitter->tag_data.suffix_length = 0;
  1399. emitter->scalar_data.value = NULL;
  1400. emitter->scalar_data.length = 0;
  1401. switch (event->type)
  1402. {
  1403. case YAML_ALIAS_EVENT:
  1404. if (!yaml_emitter_analyze_anchor(emitter,
  1405. event->data.alias.anchor, 1))
  1406. return 0;
  1407. return 1;
  1408. case YAML_SCALAR_EVENT:
  1409. if (event->data.scalar.anchor) {
  1410. if (!yaml_emitter_analyze_anchor(emitter,
  1411. event->data.scalar.anchor, 0))
  1412. return 0;
  1413. }
  1414. if (event->data.scalar.tag && (emitter->canonical ||
  1415. (!event->data.scalar.plain_implicit
  1416. && !event->data.scalar.quoted_implicit))) {
  1417. if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
  1418. return 0;
  1419. }
  1420. if (!yaml_emitter_analyze_scalar(emitter,
  1421. event->data.scalar.value, event->data.scalar.length))
  1422. return 0;
  1423. return 1;
  1424. case YAML_SEQUENCE_START_EVENT:
  1425. if (event->data.sequence_start.anchor) {
  1426. if (!yaml_emitter_analyze_anchor(emitter,
  1427. event->data.sequence_start.anchor, 0))
  1428. return 0;
  1429. }
  1430. if (event->data.sequence_start.tag && (emitter->canonical ||
  1431. !event->data.sequence_start.implicit)) {
  1432. if (!yaml_emitter_analyze_tag(emitter,
  1433. event->data.sequence_start.tag))
  1434. return 0;
  1435. }
  1436. return 1;
  1437. case YAML_MAPPING_START_EVENT:
  1438. if (event->data.mapping_start.anchor) {
  1439. if (!yaml_emitter_analyze_anchor(emitter,
  1440. event->data.mapping_start.anchor, 0))
  1441. return 0;
  1442. }
  1443. if (event->data.mapping_start.tag && (emitter->canonical ||
  1444. !event->data.mapping_start.implicit)) {
  1445. if (!yaml_emitter_analyze_tag(emitter,
  1446. event->data.mapping_start.tag))
  1447. return 0;
  1448. }
  1449. return 1;
  1450. default:
  1451. return 1;
  1452. }
  1453. }
  1454. /*
  1455. * Write the BOM character.
  1456. */
  1457. static int
  1458. yaml_emitter_write_bom(yaml_emitter_t *emitter)
  1459. {
  1460. if (!FLUSH(emitter)) return 0;
  1461. *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
  1462. *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
  1463. *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
  1464. return 1;
  1465. }
  1466. static int
  1467. yaml_emitter_write_indent(yaml_emitter_t *emitter)
  1468. {
  1469. int indent = (emitter->indent >= 0) ? emitter->indent : 0;
  1470. if (!emitter->indention || emitter->column > indent
  1471. || (emitter->column == indent && !emitter->whitespace)) {
  1472. if (!PUT_BREAK(emitter)) return 0;
  1473. }
  1474. while (emitter->column < indent) {
  1475. if (!PUT(emitter, ' ')) return 0;
  1476. }
  1477. emitter->whitespace = 1;
  1478. emitter->indention = 1;
  1479. return 1;
  1480. }
  1481. static int
  1482. yaml_emitter_write_indicator(yaml_emitter_t *emitter,
  1483. const char *indicator, int need_whitespace,
  1484. int is_whitespace, int is_indention)
  1485. {
  1486. size_t indicator_length;
  1487. yaml_string_t string;
  1488. indicator_length = strlen(indicator);
  1489. STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length);
  1490. if (need_whitespace && !emitter->whitespace) {
  1491. if (!PUT(emitter, ' ')) return 0;
  1492. }
  1493. while (string.pointer != string.end) {
  1494. if (!WRITE(emitter, string)) return 0;
  1495. }
  1496. emitter->whitespace = is_whitespace;
  1497. emitter->indention = (emitter->indention && is_indention);
  1498. return 1;
  1499. }
  1500. static int
  1501. yaml_emitter_write_anchor(yaml_emitter_t *emitter,
  1502. yaml_char_t *value, size_t length)
  1503. {
  1504. yaml_string_t string;
  1505. STRING_ASSIGN(string, value, length);
  1506. while (string.pointer != string.end) {
  1507. if (!WRITE(emitter, string)) return 0;
  1508. }
  1509. emitter->whitespace = 0;
  1510. emitter->indention = 0;
  1511. return 1;
  1512. }
  1513. static int
  1514. yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
  1515. yaml_char_t *value, size_t length)
  1516. {
  1517. yaml_string_t string;
  1518. STRING_ASSIGN(string, value, length);
  1519. if (!emitter->whitespace) {
  1520. if (!PUT(emitter, ' ')) return 0;
  1521. }
  1522. while (string.pointer != string.end) {
  1523. if (!WRITE(emitter, string)) return 0;
  1524. }
  1525. emitter->whitespace = 0;
  1526. emitter->indention = 0;
  1527. return 1;
  1528. }
  1529. static int
  1530. yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
  1531. yaml_char_t *value, size_t length,
  1532. int need_whitespace)
  1533. {
  1534. yaml_string_t string;
  1535. STRING_ASSIGN(string, value, length);
  1536. if (need_whitespace && !emitter->whitespace) {
  1537. if (!PUT(emitter, ' ')) return 0;
  1538. }
  1539. while (string.pointer != string.end) {
  1540. if (IS_ALPHA(string)
  1541. || CHECK(string, ';') || CHECK(string, '/')
  1542. || CHECK(string, '?') || CHECK(string, ':')
  1543. || CHECK(string, '@') || CHECK(string, '&')
  1544. || CHECK(string, '=') || CHECK(string, '+')
  1545. || CHECK(string, '$') || CHECK(string, ',')
  1546. || CHECK(string, '_') || CHECK(string, '.')
  1547. || CHECK(string, '~') || CHECK(string, '*')
  1548. || CHECK(string, '\'') || CHECK(string, '(')
  1549. || CHECK(string, ')') || CHECK(string, '[')
  1550. || CHECK(string, ']')) {
  1551. if (!WRITE(emitter, string)) return 0;
  1552. }
  1553. else {
  1554. int width = WIDTH(string);
  1555. unsigned int value;
  1556. while (width --) {
  1557. value = *(string.pointer++);
  1558. if (!PUT(emitter, '%')) return 0;
  1559. if (!PUT(emitter, (value >> 4)
  1560. + ((value >> 4) < 10 ? '0' : 'A' - 10)))
  1561. return 0;
  1562. if (!PUT(emitter, (value & 0x0F)
  1563. + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
  1564. return 0;
  1565. }
  1566. }
  1567. }
  1568. emitter->whitespace = 0;
  1569. emitter->indention = 0;
  1570. return 1;
  1571. }
  1572. static int
  1573. yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
  1574. yaml_char_t *value, size_t length, int allow_breaks)
  1575. {
  1576. yaml_string_t string;
  1577. int spaces = 0;
  1578. int breaks = 0;
  1579. STRING_ASSIGN(string, value, length);
  1580. /**
  1581. * Avoid trailing spaces for empty values in block mode.
  1582. * In flow mode, we still want the space to prevent ambiguous things
  1583. * like {a:}.
  1584. * Currently, the emitter forbids any plain empty scalar in flow mode
  1585. * (e.g. it outputs {a: ''} instead), so emitter->flow_level will
  1586. * never be true here.
  1587. * But if the emitter is ever changed to allow emitting empty values,
  1588. * the check for flow_level is already here.
  1589. */
  1590. if (!emitter->whitespace && (length || emitter->flow_level)) {
  1591. if (!PUT(emitter, ' ')) return 0;
  1592. }
  1593. while (string.pointer != string.end)
  1594. {
  1595. if (IS_SPACE(string))
  1596. {
  1597. if (allow_breaks && !spaces
  1598. && emitter->column > emitter->best_width
  1599. && !IS_SPACE_AT(string, 1)) {
  1600. if (!yaml_emitter_write_indent(emitter)) return 0;
  1601. MOVE(string);
  1602. }
  1603. else {
  1604. if (!WRITE(emitter, string)) return 0;
  1605. }
  1606. spaces = 1;
  1607. }
  1608. else if (IS_BREAK(string))
  1609. {
  1610. if (!breaks && CHECK(string, '\n')) {
  1611. if (!PUT_BREAK(emitter)) return 0;
  1612. }
  1613. if (!WRITE_BREAK(emitter, string)) return 0;
  1614. emitter->indention = 1;
  1615. breaks = 1;
  1616. }
  1617. else
  1618. {
  1619. if (breaks) {
  1620. if (!yaml_emitter_write_indent(emitter)) return 0;
  1621. }
  1622. if (!WRITE(emitter, string)) return 0;
  1623. emitter->indention = 0;
  1624. spaces = 0;
  1625. breaks = 0;
  1626. }
  1627. }
  1628. emitter->whitespace = 0;
  1629. emitter->indention = 0;
  1630. return 1;
  1631. }
  1632. static int
  1633. yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
  1634. yaml_char_t *value, size_t length, int allow_breaks)
  1635. {
  1636. yaml_string_t string;
  1637. int spaces = 0;
  1638. int breaks = 0;
  1639. STRING_ASSIGN(string, value, length);
  1640. if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
  1641. return 0;
  1642. while (string.pointer != string.end)
  1643. {
  1644. if (IS_SPACE(string))
  1645. {
  1646. if (allow_breaks && !spaces
  1647. && emitter->column > emitter->best_width
  1648. && string.pointer != string.start
  1649. && string.pointer != string.end - 1
  1650. && !IS_SPACE_AT(string, 1)) {
  1651. if (!yaml_emitter_write_indent(emitter)) return 0;
  1652. MOVE(string);
  1653. }
  1654. else {
  1655. if (!WRITE(emitter, string)) return 0;
  1656. }
  1657. spaces = 1;
  1658. }
  1659. else if (IS_BREAK(string))
  1660. {
  1661. if (!breaks && CHECK(string, '\n')) {
  1662. if (!PUT_BREAK(emitter)) return 0;
  1663. }
  1664. if (!WRITE_BREAK(emitter, string)) return 0;
  1665. emitter->indention = 1;
  1666. breaks = 1;
  1667. }
  1668. else
  1669. {
  1670. if (breaks) {
  1671. if (!yaml_emitter_write_indent(emitter)) return 0;
  1672. }
  1673. if (CHECK(string, '\'')) {
  1674. if (!PUT(emitter, '\'')) return 0;
  1675. }
  1676. if (!WRITE(emitter, string)) return 0;
  1677. emitter->indention = 0;
  1678. spaces = 0;
  1679. breaks = 0;
  1680. }
  1681. }
  1682. if (breaks)
  1683. if (!yaml_emitter_write_indent(emitter)) return 0;
  1684. if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
  1685. return 0;
  1686. emitter->whitespace = 0;
  1687. emitter->indention = 0;
  1688. return 1;
  1689. }
  1690. static int
  1691. yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
  1692. yaml_char_t *value, size_t length, int allow_breaks)
  1693. {
  1694. yaml_string_t string;
  1695. int spaces = 0;
  1696. STRING_ASSIGN(string, value, length);
  1697. if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
  1698. return 0;
  1699. while (string.pointer != string.end)
  1700. {
  1701. if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
  1702. || IS_BOM(string) || IS_BREAK(string)
  1703. || CHECK(string, '"') || CHECK(string, '\\'))
  1704. {
  1705. unsigned char octet;
  1706. unsigned int width;
  1707. unsigned int value;
  1708. int k;
  1709. octet = string.pointer[0];
  1710. width = (octet & 0x80) == 0x00 ? 1 :
  1711. (octet & 0xE0) == 0xC0 ? 2 :
  1712. (octet & 0xF0) == 0xE0 ? 3 :
  1713. (octet & 0xF8) == 0xF0 ? 4 : 0;
  1714. value = (octet & 0x80) == 0x00 ? octet & 0x7F :
  1715. (octet & 0xE0) == 0xC0 ? octet & 0x1F :
  1716. (octet & 0xF0) == 0xE0 ? octet & 0x0F :
  1717. (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
  1718. for (k = 1; k < (int)width; k ++) {
  1719. octet = string.pointer[k];
  1720. value = (value << 6) + (octet & 0x3F);
  1721. }
  1722. string.pointer += width;
  1723. if (!PUT(emitter, '\\')) return 0;
  1724. switch (value)
  1725. {
  1726. case 0x00:
  1727. if (!PUT(emitter, '0')) return 0;
  1728. break;
  1729. case 0x07:
  1730. if (!PUT(emitter, 'a')) return 0;
  1731. break;
  1732. case 0x08:
  1733. if (!PUT(emitter, 'b')) return 0;
  1734. break;
  1735. case 0x09:
  1736. if (!PUT(emitter, 't')) return 0;
  1737. break;
  1738. case 0x0A:
  1739. if (!PUT(emitter, 'n')) return 0;
  1740. break;
  1741. case 0x0B:
  1742. if (!PUT(emitter, 'v')) return 0;
  1743. break;
  1744. case 0x0C:
  1745. if (!PUT(emitter, 'f')) return 0;
  1746. break;
  1747. case 0x0D:
  1748. if (!PUT(emitter, 'r')) return 0;
  1749. break;
  1750. case 0x1B:
  1751. if (!PUT(emitter, 'e')) return 0;
  1752. break;
  1753. case 0x22:
  1754. if (!PUT(emitter, '\"')) return 0;
  1755. break;
  1756. case 0x5C:
  1757. if (!PUT(emitter, '\\')) return 0;
  1758. break;
  1759. case 0x85:
  1760. if (!PUT(emitter, 'N')) return 0;
  1761. break;
  1762. case 0xA0:
  1763. if (!PUT(emitter, '_')) return 0;
  1764. break;
  1765. case 0x2028:
  1766. if (!PUT(emitter, 'L')) return 0;
  1767. break;
  1768. case 0x2029:
  1769. if (!PUT(emitter, 'P')) return 0;
  1770. break;
  1771. default:
  1772. if (value <= 0xFF) {
  1773. if (!PUT(emitter, 'x')) return 0;
  1774. width = 2;
  1775. }
  1776. else if (value <= 0xFFFF) {
  1777. if (!PUT(emitter, 'u')) return 0;
  1778. width = 4;
  1779. }
  1780. else {
  1781. if (!PUT(emitter, 'U')) return 0;
  1782. width = 8;
  1783. }
  1784. for (k = (width-1)*4; k >= 0; k -= 4) {
  1785. int digit = (value >> k) & 0x0F;
  1786. if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
  1787. return 0;
  1788. }
  1789. }
  1790. spaces = 0;
  1791. }
  1792. else if (IS_SPACE(string))
  1793. {
  1794. if (allow_breaks && !spaces
  1795. && emitter->column > emitter->best_width
  1796. && string.pointer != string.start
  1797. && string.pointer != string.end - 1) {
  1798. if (!yaml_emitter_write_indent(emitter)) return 0;
  1799. if (IS_SPACE_AT(string, 1)) {
  1800. if (!PUT(emitter, '\\')) return 0;
  1801. }
  1802. MOVE(string);
  1803. }
  1804. else {
  1805. if (!WRITE(emitter, string)) return 0;
  1806. }
  1807. spaces = 1;
  1808. }
  1809. else
  1810. {
  1811. if (!WRITE(emitter, string)) return 0;
  1812. spaces = 0;
  1813. }
  1814. }
  1815. if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
  1816. return 0;
  1817. emitter->whitespace = 0;
  1818. emitter->indention = 0;
  1819. return 1;
  1820. }
  1821. static int
  1822. yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
  1823. yaml_string_t string)
  1824. {
  1825. char indent_hint[2];
  1826. const char *chomp_hint = NULL;
  1827. if (IS_SPACE(string) || IS_BREAK(string))
  1828. {
  1829. indent_hint[0] = '0' + (char)emitter->best_indent;
  1830. indent_hint[1] = '\0';
  1831. if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0))
  1832. return 0;
  1833. }
  1834. emitter->open_ended = 0;
  1835. string.pointer = string.end;
  1836. if (string.start == string.pointer)
  1837. {
  1838. chomp_hint = "-";
  1839. }
  1840. else
  1841. {
  1842. do {
  1843. string.pointer --;
  1844. } while ((*string.pointer & 0xC0) == 0x80);
  1845. if (!IS_BREAK(string))
  1846. {
  1847. chomp_hint = "-";
  1848. }
  1849. else if (string.start == string.pointer)
  1850. {
  1851. chomp_hint = "+";
  1852. emitter->open_ended = 2;
  1853. }
  1854. else
  1855. {
  1856. do {
  1857. string.pointer --;
  1858. } while ((*string.pointer & 0xC0) == 0x80);
  1859. if (IS_BREAK(string))
  1860. {
  1861. chomp_hint = "+";
  1862. emitter->open_ended = 2;
  1863. }
  1864. }
  1865. }
  1866. if (chomp_hint)
  1867. {
  1868. if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0))
  1869. return 0;
  1870. }
  1871. return 1;
  1872. }
  1873. static int
  1874. yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
  1875. yaml_char_t *value, size_t length)
  1876. {
  1877. yaml_string_t string;
  1878. int breaks = 1;
  1879. STRING_ASSIGN(string, value, length);
  1880. if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0))
  1881. return 0;
  1882. if (!yaml_emitter_write_block_scalar_hints(emitter, string))
  1883. return 0;
  1884. if (!PUT_BREAK(emitter)) return 0;
  1885. emitter->indention = 1;
  1886. emitter->whitespace = 1;
  1887. while (string.pointer != string.end)
  1888. {
  1889. if (IS_BREAK(string))
  1890. {
  1891. if (!WRITE_BREAK(emitter, string)) return 0;
  1892. emitter->indention = 1;
  1893. breaks = 1;
  1894. }
  1895. else
  1896. {
  1897. if (breaks) {
  1898. if (!yaml_emitter_write_indent(emitter)) return 0;
  1899. }
  1900. if (!WRITE(emitter, string)) return 0;
  1901. emitter->indention = 0;
  1902. breaks = 0;
  1903. }
  1904. }
  1905. return 1;
  1906. }
  1907. static int
  1908. yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
  1909. yaml_char_t *value, size_t length)
  1910. {
  1911. yaml_string_t string;
  1912. int breaks = 1;
  1913. int leading_spaces = 1;
  1914. STRING_ASSIGN(string, value, length);
  1915. if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0))
  1916. return 0;
  1917. if (!yaml_emitter_write_block_scalar_hints(emitter, string))
  1918. return 0;
  1919. if (!PUT_BREAK(emitter)) return 0;
  1920. emitter->indention = 1;
  1921. emitter->whitespace = 1;
  1922. while (string.pointer != string.end)
  1923. {
  1924. if (IS_BREAK(string))
  1925. {
  1926. if (!breaks && !leading_spaces && CHECK(string, '\n')) {
  1927. int k = 0;
  1928. while (IS_BREAK_AT(string, k)) {
  1929. k += WIDTH_AT(string, k);
  1930. }
  1931. if (!IS_BLANKZ_AT(string, k)) {
  1932. if (!PUT_BREAK(emitter)) return 0;
  1933. }
  1934. }
  1935. if (!WRITE_BREAK(emitter, string)) return 0;
  1936. emitter->indention = 1;
  1937. breaks = 1;
  1938. }
  1939. else
  1940. {
  1941. if (breaks) {
  1942. if (!yaml_emitter_write_indent(emitter)) return 0;
  1943. leading_spaces = IS_BLANK(string);
  1944. }
  1945. if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
  1946. && emitter->column > emitter->best_width) {
  1947. if (!yaml_emitter_write_indent(emitter)) return 0;
  1948. MOVE(string);
  1949. }
  1950. else {
  1951. if (!WRITE(emitter, string)) return 0;
  1952. }
  1953. emitter->indention = 0;
  1954. breaks = 0;
  1955. }
  1956. }
  1957. return 1;
  1958. }