emitter.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  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. 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. if (event->type == YAML_STREAM_START_EVENT)
  386. {
  387. if (!emitter->encoding) {
  388. emitter->encoding = event->data.stream_start.encoding;
  389. }
  390. if (!emitter->encoding) {
  391. emitter->encoding = YAML_UTF8_ENCODING;
  392. }
  393. if (emitter->best_indent < 2 || emitter->best_indent > 9) {
  394. emitter->best_indent = 2;
  395. }
  396. if (emitter->best_width >= 0
  397. && emitter->best_width <= emitter->best_indent*2) {
  398. emitter->best_width = 80;
  399. }
  400. if (emitter->best_width < 0) {
  401. emitter->best_width = INT_MAX;
  402. }
  403. if (!emitter->line_break) {
  404. emitter->line_break = YAML_LN_BREAK;
  405. }
  406. emitter->indent = -1;
  407. emitter->line = 0;
  408. emitter->column = 0;
  409. emitter->whitespace = 1;
  410. emitter->indention = 1;
  411. if (emitter->encoding != YAML_UTF8_ENCODING) {
  412. if (!yaml_emitter_write_bom(emitter))
  413. return 0;
  414. }
  415. emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE;
  416. return 1;
  417. }
  418. return yaml_emitter_set_emitter_error(emitter,
  419. "expected STREAM-START");
  420. }
  421. /*
  422. * Expect DOCUMENT-START or STREAM-END.
  423. */
  424. static int
  425. yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
  426. yaml_event_t *event, int first)
  427. {
  428. if (event->type == YAML_DOCUMENT_START_EVENT)
  429. {
  430. yaml_tag_directive_t default_tag_directives[] = {
  431. {(yaml_char_t *)"!", (yaml_char_t *)"!"},
  432. {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"},
  433. {NULL, NULL}
  434. };
  435. yaml_tag_directive_t *tag_directive;
  436. int implicit;
  437. if (event->data.document_start.version_directive) {
  438. if (!yaml_emitter_analyze_version_directive(emitter,
  439. *event->data.document_start.version_directive))
  440. return 0;
  441. }
  442. for (tag_directive = event->data.document_start.tag_directives.start;
  443. tag_directive != event->data.document_start.tag_directives.end;
  444. tag_directive ++) {
  445. if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive))
  446. return 0;
  447. if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0))
  448. return 0;
  449. }
  450. for (tag_directive = default_tag_directives;
  451. tag_directive->handle; tag_directive ++) {
  452. if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1))
  453. return 0;
  454. }
  455. implicit = event->data.document_start.implicit;
  456. if (!first || emitter->canonical) {
  457. implicit = 0;
  458. }
  459. if ((event->data.document_start.version_directive ||
  460. (event->data.document_start.tag_directives.start
  461. != event->data.document_start.tag_directives.end)) &&
  462. emitter->open_ended)
  463. {
  464. if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
  465. return 0;
  466. if (!yaml_emitter_write_indent(emitter))
  467. return 0;
  468. }
  469. if (event->data.document_start.version_directive) {
  470. implicit = 0;
  471. if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
  472. return 0;
  473. if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
  474. return 0;
  475. if (!yaml_emitter_write_indent(emitter))
  476. return 0;
  477. }
  478. if (event->data.document_start.tag_directives.start
  479. != event->data.document_start.tag_directives.end) {
  480. implicit = 0;
  481. for (tag_directive = event->data.document_start.tag_directives.start;
  482. tag_directive != event->data.document_start.tag_directives.end;
  483. tag_directive ++) {
  484. if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0))
  485. return 0;
  486. if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle,
  487. strlen((char *)tag_directive->handle)))
  488. return 0;
  489. if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix,
  490. strlen((char *)tag_directive->prefix), 1))
  491. return 0;
  492. if (!yaml_emitter_write_indent(emitter))
  493. return 0;
  494. }
  495. }
  496. if (yaml_emitter_check_empty_document(emitter)) {
  497. implicit = 0;
  498. }
  499. if (!implicit) {
  500. if (!yaml_emitter_write_indent(emitter))
  501. return 0;
  502. if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0))
  503. return 0;
  504. if (emitter->canonical) {
  505. if (!yaml_emitter_write_indent(emitter))
  506. return 0;
  507. }
  508. }
  509. emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE;
  510. return 1;
  511. }
  512. else if (event->type == YAML_STREAM_END_EVENT)
  513. {
  514. if (emitter->open_ended)
  515. {
  516. if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
  517. return 0;
  518. if (!yaml_emitter_write_indent(emitter))
  519. return 0;
  520. }
  521. if (!yaml_emitter_flush(emitter))
  522. return 0;
  523. emitter->state = YAML_EMIT_END_STATE;
  524. return 1;
  525. }
  526. return yaml_emitter_set_emitter_error(emitter,
  527. "expected DOCUMENT-START or STREAM-END");
  528. }
  529. /*
  530. * Expect the root node.
  531. */
  532. static int
  533. yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
  534. yaml_event_t *event)
  535. {
  536. if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE))
  537. return 0;
  538. return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0);
  539. }
  540. /*
  541. * Expect DOCUMENT-END.
  542. */
  543. static int
  544. yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
  545. yaml_event_t *event)
  546. {
  547. if (event->type == YAML_DOCUMENT_END_EVENT)
  548. {
  549. if (!yaml_emitter_write_indent(emitter))
  550. return 0;
  551. if (!event->data.document_end.implicit) {
  552. if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
  553. return 0;
  554. if (!yaml_emitter_write_indent(emitter))
  555. return 0;
  556. }
  557. if (!yaml_emitter_flush(emitter))
  558. return 0;
  559. emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
  560. while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
  561. yaml_tag_directive_t tag_directive = POP(emitter,
  562. emitter->tag_directives);
  563. yaml_free(tag_directive.handle);
  564. yaml_free(tag_directive.prefix);
  565. }
  566. return 1;
  567. }
  568. return yaml_emitter_set_emitter_error(emitter,
  569. "expected DOCUMENT-END");
  570. }
  571. /*
  572. *
  573. * Expect a flow item node.
  574. */
  575. static int
  576. yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
  577. yaml_event_t *event, int first)
  578. {
  579. if (first)
  580. {
  581. if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
  582. return 0;
  583. if (!yaml_emitter_increase_indent(emitter, 1, 0))
  584. return 0;
  585. emitter->flow_level ++;
  586. }
  587. if (event->type == YAML_SEQUENCE_END_EVENT)
  588. {
  589. emitter->flow_level --;
  590. emitter->indent = POP(emitter, emitter->indents);
  591. if (emitter->canonical && !first) {
  592. if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
  593. return 0;
  594. if (!yaml_emitter_write_indent(emitter))
  595. return 0;
  596. }
  597. if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
  598. return 0;
  599. emitter->state = POP(emitter, emitter->states);
  600. return 1;
  601. }
  602. if (!first) {
  603. if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
  604. return 0;
  605. }
  606. if (emitter->canonical || emitter->column > emitter->best_width) {
  607. if (!yaml_emitter_write_indent(emitter))
  608. return 0;
  609. }
  610. if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
  611. return 0;
  612. return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
  613. }
  614. /*
  615. * Expect a flow key node.
  616. */
  617. static int
  618. yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
  619. yaml_event_t *event, int first)
  620. {
  621. if (first)
  622. {
  623. if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
  624. return 0;
  625. if (!yaml_emitter_increase_indent(emitter, 1, 0))
  626. return 0;
  627. emitter->flow_level ++;
  628. }
  629. if (event->type == YAML_MAPPING_END_EVENT)
  630. {
  631. emitter->flow_level --;
  632. emitter->indent = POP(emitter, emitter->indents);
  633. if (emitter->canonical && !first) {
  634. if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
  635. return 0;
  636. if (!yaml_emitter_write_indent(emitter))
  637. return 0;
  638. }
  639. if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
  640. return 0;
  641. emitter->state = POP(emitter, emitter->states);
  642. return 1;
  643. }
  644. if (!first) {
  645. if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
  646. return 0;
  647. }
  648. if (emitter->canonical || emitter->column > emitter->best_width) {
  649. if (!yaml_emitter_write_indent(emitter))
  650. return 0;
  651. }
  652. if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
  653. {
  654. if (!PUSH(emitter, emitter->states,
  655. YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
  656. return 0;
  657. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
  658. }
  659. else
  660. {
  661. if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
  662. return 0;
  663. if (!PUSH(emitter, emitter->states,
  664. YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
  665. return 0;
  666. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
  667. }
  668. }
  669. /*
  670. * Expect a flow value node.
  671. */
  672. static int
  673. yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
  674. yaml_event_t *event, int simple)
  675. {
  676. if (simple) {
  677. if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
  678. return 0;
  679. }
  680. else {
  681. if (emitter->canonical || emitter->column > emitter->best_width) {
  682. if (!yaml_emitter_write_indent(emitter))
  683. return 0;
  684. }
  685. if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
  686. return 0;
  687. }
  688. if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
  689. return 0;
  690. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
  691. }
  692. /*
  693. * Expect a block item node.
  694. */
  695. static int
  696. yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
  697. yaml_event_t *event, int first)
  698. {
  699. if (first)
  700. {
  701. if (!yaml_emitter_increase_indent(emitter, 0,
  702. (emitter->mapping_context && !emitter->indention)))
  703. return 0;
  704. }
  705. if (event->type == YAML_SEQUENCE_END_EVENT)
  706. {
  707. emitter->indent = POP(emitter, emitter->indents);
  708. emitter->state = POP(emitter, emitter->states);
  709. return 1;
  710. }
  711. if (!yaml_emitter_write_indent(emitter))
  712. return 0;
  713. if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
  714. return 0;
  715. if (!PUSH(emitter, emitter->states,
  716. YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
  717. return 0;
  718. return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
  719. }
  720. /*
  721. * Expect a block key node.
  722. */
  723. static int
  724. yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
  725. yaml_event_t *event, int first)
  726. {
  727. if (first)
  728. {
  729. if (!yaml_emitter_increase_indent(emitter, 0, 0))
  730. return 0;
  731. }
  732. if (event->type == YAML_MAPPING_END_EVENT)
  733. {
  734. emitter->indent = POP(emitter, emitter->indents);
  735. emitter->state = POP(emitter, emitter->states);
  736. return 1;
  737. }
  738. if (!yaml_emitter_write_indent(emitter))
  739. return 0;
  740. if (yaml_emitter_check_simple_key(emitter))
  741. {
  742. if (!PUSH(emitter, emitter->states,
  743. YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
  744. return 0;
  745. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
  746. }
  747. else
  748. {
  749. if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
  750. return 0;
  751. if (!PUSH(emitter, emitter->states,
  752. YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
  753. return 0;
  754. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
  755. }
  756. }
  757. /*
  758. * Expect a block value node.
  759. */
  760. static int
  761. yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
  762. yaml_event_t *event, int simple)
  763. {
  764. if (simple) {
  765. if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
  766. return 0;
  767. }
  768. else {
  769. if (!yaml_emitter_write_indent(emitter))
  770. return 0;
  771. if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
  772. return 0;
  773. }
  774. if (!PUSH(emitter, emitter->states,
  775. YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
  776. return 0;
  777. return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
  778. }
  779. /*
  780. * Expect a node.
  781. */
  782. static int
  783. yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
  784. int root, int sequence, int mapping, int simple_key)
  785. {
  786. emitter->root_context = root;
  787. emitter->sequence_context = sequence;
  788. emitter->mapping_context = mapping;
  789. emitter->simple_key_context = simple_key;
  790. switch (event->type)
  791. {
  792. case YAML_ALIAS_EVENT:
  793. return yaml_emitter_emit_alias(emitter, event);
  794. case YAML_SCALAR_EVENT:
  795. return yaml_emitter_emit_scalar(emitter, event);
  796. case YAML_SEQUENCE_START_EVENT:
  797. return yaml_emitter_emit_sequence_start(emitter, event);
  798. case YAML_MAPPING_START_EVENT:
  799. return yaml_emitter_emit_mapping_start(emitter, event);
  800. default:
  801. return yaml_emitter_set_emitter_error(emitter,
  802. "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
  803. }
  804. return 0;
  805. }
  806. /*
  807. * Expect ALIAS.
  808. */
  809. static int
  810. yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event)
  811. {
  812. if (!yaml_emitter_process_anchor(emitter))
  813. return 0;
  814. emitter->state = POP(emitter, emitter->states);
  815. return 1;
  816. }
  817. /*
  818. * Expect SCALAR.
  819. */
  820. static int
  821. yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
  822. {
  823. if (!yaml_emitter_select_scalar_style(emitter, event))
  824. return 0;
  825. if (!yaml_emitter_process_anchor(emitter))
  826. return 0;
  827. if (!yaml_emitter_process_tag(emitter))
  828. return 0;
  829. if (!yaml_emitter_increase_indent(emitter, 1, 0))
  830. return 0;
  831. if (!yaml_emitter_process_scalar(emitter))
  832. return 0;
  833. emitter->indent = POP(emitter, emitter->indents);
  834. emitter->state = POP(emitter, emitter->states);
  835. return 1;
  836. }
  837. /*
  838. * Expect SEQUENCE-START.
  839. */
  840. static int
  841. yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
  842. {
  843. if (!yaml_emitter_process_anchor(emitter))
  844. return 0;
  845. if (!yaml_emitter_process_tag(emitter))
  846. return 0;
  847. if (emitter->flow_level || emitter->canonical
  848. || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
  849. || yaml_emitter_check_empty_sequence(emitter)) {
  850. emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
  851. }
  852. else {
  853. emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
  854. }
  855. return 1;
  856. }
  857. /*
  858. * Expect MAPPING-START.
  859. */
  860. static int
  861. yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
  862. {
  863. if (!yaml_emitter_process_anchor(emitter))
  864. return 0;
  865. if (!yaml_emitter_process_tag(emitter))
  866. return 0;
  867. if (emitter->flow_level || emitter->canonical
  868. || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
  869. || yaml_emitter_check_empty_mapping(emitter)) {
  870. emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
  871. }
  872. else {
  873. emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
  874. }
  875. return 1;
  876. }
  877. /*
  878. * Check if the document content is an empty scalar.
  879. */
  880. static int
  881. yaml_emitter_check_empty_document(yaml_emitter_t *emitter)
  882. {
  883. return 0;
  884. }
  885. /*
  886. * Check if the next events represent an empty sequence.
  887. */
  888. static int
  889. yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
  890. {
  891. if (emitter->events.tail - emitter->events.head < 2)
  892. return 0;
  893. return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
  894. && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
  895. }
  896. /*
  897. * Check if the next events represent an empty mapping.
  898. */
  899. static int
  900. yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
  901. {
  902. if (emitter->events.tail - emitter->events.head < 2)
  903. return 0;
  904. return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
  905. && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
  906. }
  907. /*
  908. * Check if the next node can be expressed as a simple key.
  909. */
  910. static int
  911. yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
  912. {
  913. yaml_event_t *event = emitter->events.head;
  914. size_t length = 0;
  915. switch (event->type)
  916. {
  917. case YAML_ALIAS_EVENT:
  918. length += emitter->anchor_data.anchor_length;
  919. break;
  920. case YAML_SCALAR_EVENT:
  921. if (emitter->scalar_data.multiline)
  922. return 0;
  923. length += emitter->anchor_data.anchor_length
  924. + emitter->tag_data.handle_length
  925. + emitter->tag_data.suffix_length
  926. + emitter->scalar_data.length;
  927. break;
  928. case YAML_SEQUENCE_START_EVENT:
  929. if (!yaml_emitter_check_empty_sequence(emitter))
  930. return 0;
  931. length += emitter->anchor_data.anchor_length
  932. + emitter->tag_data.handle_length
  933. + emitter->tag_data.suffix_length;
  934. break;
  935. case YAML_MAPPING_START_EVENT:
  936. if (!yaml_emitter_check_empty_sequence(emitter))
  937. return 0;
  938. length += emitter->anchor_data.anchor_length
  939. + emitter->tag_data.handle_length
  940. + emitter->tag_data.suffix_length;
  941. break;
  942. default:
  943. return 0;
  944. }
  945. if (length > 128)
  946. return 0;
  947. return 1;
  948. }
  949. /*
  950. * Determine an acceptable scalar style.
  951. */
  952. static int
  953. yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
  954. {
  955. yaml_scalar_style_t style = event->data.scalar.style;
  956. int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix);
  957. if (no_tag && !event->data.scalar.plain_implicit
  958. && !event->data.scalar.quoted_implicit) {
  959. return yaml_emitter_set_emitter_error(emitter,
  960. "neither tag nor implicit flags are specified");
  961. }
  962. if (style == YAML_ANY_SCALAR_STYLE)
  963. style = YAML_PLAIN_SCALAR_STYLE;
  964. if (emitter->canonical)
  965. style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
  966. if (emitter->simple_key_context && emitter->scalar_data.multiline)
  967. style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
  968. if (style == YAML_PLAIN_SCALAR_STYLE)
  969. {
  970. if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
  971. || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
  972. style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
  973. if (!emitter->scalar_data.length
  974. && (emitter->flow_level || emitter->simple_key_context))
  975. style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
  976. if (no_tag && !event->data.scalar.plain_implicit)
  977. style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
  978. }
  979. if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
  980. {
  981. if (!emitter->scalar_data.single_quoted_allowed)
  982. style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
  983. }
  984. if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
  985. {
  986. if (!emitter->scalar_data.block_allowed
  987. || emitter->flow_level || emitter->simple_key_context)
  988. style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
  989. }
  990. if (no_tag && !event->data.scalar.quoted_implicit
  991. && style != YAML_PLAIN_SCALAR_STYLE)
  992. {
  993. emitter->tag_data.handle = (yaml_char_t *)"!";
  994. emitter->tag_data.handle_length = 1;
  995. }
  996. emitter->scalar_data.style = style;
  997. return 1;
  998. }
  999. /*
  1000. * Write an achor.
  1001. */
  1002. static int
  1003. yaml_emitter_process_anchor(yaml_emitter_t *emitter)
  1004. {
  1005. if (!emitter->anchor_data.anchor)
  1006. return 1;
  1007. if (!yaml_emitter_write_indicator(emitter,
  1008. (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
  1009. return 0;
  1010. return yaml_emitter_write_anchor(emitter,
  1011. emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
  1012. }
  1013. /*
  1014. * Write a tag.
  1015. */
  1016. static int
  1017. yaml_emitter_process_tag(yaml_emitter_t *emitter)
  1018. {
  1019. if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
  1020. return 1;
  1021. if (emitter->tag_data.handle)
  1022. {
  1023. if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
  1024. emitter->tag_data.handle_length))
  1025. return 0;
  1026. if (emitter->tag_data.suffix) {
  1027. if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
  1028. emitter->tag_data.suffix_length, 0))
  1029. return 0;
  1030. }
  1031. }
  1032. else
  1033. {
  1034. if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
  1035. return 0;
  1036. if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
  1037. emitter->tag_data.suffix_length, 0))
  1038. return 0;
  1039. if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
  1040. return 0;
  1041. }
  1042. return 1;
  1043. }
  1044. /*
  1045. * Write a scalar.
  1046. */
  1047. static int
  1048. yaml_emitter_process_scalar(yaml_emitter_t *emitter)
  1049. {
  1050. switch (emitter->scalar_data.style)
  1051. {
  1052. case YAML_PLAIN_SCALAR_STYLE:
  1053. return yaml_emitter_write_plain_scalar(emitter,
  1054. emitter->scalar_data.value, emitter->scalar_data.length,
  1055. !emitter->simple_key_context);
  1056. case YAML_SINGLE_QUOTED_SCALAR_STYLE:
  1057. return yaml_emitter_write_single_quoted_scalar(emitter,
  1058. emitter->scalar_data.value, emitter->scalar_data.length,
  1059. !emitter->simple_key_context);
  1060. case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
  1061. return yaml_emitter_write_double_quoted_scalar(emitter,
  1062. emitter->scalar_data.value, emitter->scalar_data.length,
  1063. !emitter->simple_key_context);
  1064. case YAML_LITERAL_SCALAR_STYLE:
  1065. return yaml_emitter_write_literal_scalar(emitter,
  1066. emitter->scalar_data.value, emitter->scalar_data.length);
  1067. case YAML_FOLDED_SCALAR_STYLE:
  1068. return yaml_emitter_write_folded_scalar(emitter,
  1069. emitter->scalar_data.value, emitter->scalar_data.length);
  1070. default:
  1071. assert(1); /* Impossible. */
  1072. }
  1073. return 0;
  1074. }
  1075. /*
  1076. * Check if a %YAML directive is valid.
  1077. */
  1078. static int
  1079. yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
  1080. yaml_version_directive_t version_directive)
  1081. {
  1082. if (version_directive.major != 1 || version_directive.minor != 1) {
  1083. return yaml_emitter_set_emitter_error(emitter,
  1084. "incompatible %YAML directive");
  1085. }
  1086. return 1;
  1087. }
  1088. /*
  1089. * Check if a %TAG directive is valid.
  1090. */
  1091. static int
  1092. yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
  1093. yaml_tag_directive_t tag_directive)
  1094. {
  1095. yaml_string_t handle;
  1096. yaml_string_t prefix;
  1097. size_t handle_length;
  1098. size_t prefix_length;
  1099. handle_length = strlen((char *)tag_directive.handle);
  1100. prefix_length = strlen((char *)tag_directive.prefix);
  1101. STRING_ASSIGN(handle, tag_directive.handle, handle_length);
  1102. STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length);
  1103. if (handle.start == handle.end) {
  1104. return yaml_emitter_set_emitter_error(emitter,
  1105. "tag handle must not be empty");
  1106. }
  1107. if (handle.start[0] != '!') {
  1108. return yaml_emitter_set_emitter_error(emitter,
  1109. "tag handle must start with '!'");
  1110. }
  1111. if (handle.end[-1] != '!') {
  1112. return yaml_emitter_set_emitter_error(emitter,
  1113. "tag handle must end with '!'");
  1114. }
  1115. handle.pointer ++;
  1116. while (handle.pointer < handle.end-1) {
  1117. if (!IS_ALPHA(handle)) {
  1118. return yaml_emitter_set_emitter_error(emitter,
  1119. "tag handle must contain alphanumerical characters only");
  1120. }
  1121. MOVE(handle);
  1122. }
  1123. if (prefix.start == prefix.end) {
  1124. return yaml_emitter_set_emitter_error(emitter,
  1125. "tag prefix must not be empty");
  1126. }
  1127. return 1;
  1128. }
  1129. /*
  1130. * Check if an anchor is valid.
  1131. */
  1132. static int
  1133. yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
  1134. yaml_char_t *anchor, int alias)
  1135. {
  1136. size_t anchor_length;
  1137. yaml_string_t string;
  1138. anchor_length = strlen((char *)anchor);
  1139. STRING_ASSIGN(string, anchor, anchor_length);
  1140. if (string.start == string.end) {
  1141. return yaml_emitter_set_emitter_error(emitter, alias ?
  1142. "alias value must not be empty" :
  1143. "anchor value must not be empty");
  1144. }
  1145. while (string.pointer != string.end) {
  1146. if (!IS_ALPHA(string)) {
  1147. return yaml_emitter_set_emitter_error(emitter, alias ?
  1148. "alias value must contain alphanumerical characters only" :
  1149. "anchor value must contain alphanumerical characters only");
  1150. }
  1151. MOVE(string);
  1152. }
  1153. emitter->anchor_data.anchor = string.start;
  1154. emitter->anchor_data.anchor_length = string.end - string.start;
  1155. emitter->anchor_data.alias = alias;
  1156. return 1;
  1157. }
  1158. /*
  1159. * Check if a tag is valid.
  1160. */
  1161. static int
  1162. yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
  1163. yaml_char_t *tag)
  1164. {
  1165. size_t tag_length;
  1166. yaml_string_t string;
  1167. yaml_tag_directive_t *tag_directive;
  1168. tag_length = strlen((char *)tag);
  1169. STRING_ASSIGN(string, tag, tag_length);
  1170. if (string.start == string.end) {
  1171. return yaml_emitter_set_emitter_error(emitter,
  1172. "tag value must not be empty");
  1173. }
  1174. for (tag_directive = emitter->tag_directives.start;
  1175. tag_directive != emitter->tag_directives.top; tag_directive ++) {
  1176. size_t prefix_length = strlen((char *)tag_directive->prefix);
  1177. if (prefix_length < (size_t)(string.end - string.start)
  1178. && strncmp((char *)tag_directive->prefix, (char *)string.start,
  1179. prefix_length) == 0)
  1180. {
  1181. emitter->tag_data.handle = tag_directive->handle;
  1182. emitter->tag_data.handle_length =
  1183. strlen((char *)tag_directive->handle);
  1184. emitter->tag_data.suffix = string.start + prefix_length;
  1185. emitter->tag_data.suffix_length =
  1186. (string.end - string.start) - prefix_length;
  1187. return 1;
  1188. }
  1189. }
  1190. emitter->tag_data.suffix = string.start;
  1191. emitter->tag_data.suffix_length = string.end - string.start;
  1192. return 1;
  1193. }
  1194. /*
  1195. * Check if a scalar is valid.
  1196. */
  1197. static int
  1198. yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
  1199. yaml_char_t *value, size_t length)
  1200. {
  1201. yaml_string_t string;
  1202. int block_indicators = 0;
  1203. int flow_indicators = 0;
  1204. int line_breaks = 0;
  1205. int special_characters = 0;
  1206. int leading_space = 0;
  1207. int leading_break = 0;
  1208. int trailing_space = 0;
  1209. int trailing_break = 0;
  1210. int break_space = 0;
  1211. int space_break = 0;
  1212. int preceeded_by_whitespace = 0;
  1213. int followed_by_whitespace = 0;
  1214. int previous_space = 0;
  1215. int previous_break = 0;
  1216. STRING_ASSIGN(string, value, length);
  1217. emitter->scalar_data.value = value;
  1218. emitter->scalar_data.length = length;
  1219. if (string.start == string.end)
  1220. {
  1221. emitter->scalar_data.multiline = 0;
  1222. emitter->scalar_data.flow_plain_allowed = 0;
  1223. emitter->scalar_data.block_plain_allowed = 1;
  1224. emitter->scalar_data.single_quoted_allowed = 1;
  1225. emitter->scalar_data.block_allowed = 0;
  1226. return 1;
  1227. }
  1228. if ((CHECK_AT(string, '-', 0)
  1229. && CHECK_AT(string, '-', 1)
  1230. && CHECK_AT(string, '-', 2))
  1231. || (CHECK_AT(string, '.', 0)
  1232. && CHECK_AT(string, '.', 1)
  1233. && CHECK_AT(string, '.', 2))) {
  1234. block_indicators = 1;
  1235. flow_indicators = 1;
  1236. }
  1237. preceeded_by_whitespace = 1;
  1238. followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
  1239. while (string.pointer != string.end)
  1240. {
  1241. if (string.start == string.pointer)
  1242. {
  1243. if (CHECK(string, '#') || CHECK(string, ',')
  1244. || CHECK(string, '[') || CHECK(string, ']')
  1245. || CHECK(string, '{') || CHECK(string, '}')
  1246. || CHECK(string, '&') || CHECK(string, '*')
  1247. || CHECK(string, '!') || CHECK(string, '|')
  1248. || CHECK(string, '>') || CHECK(string, '\'')
  1249. || CHECK(string, '"') || CHECK(string, '%')
  1250. || CHECK(string, '@') || CHECK(string, '`')) {
  1251. flow_indicators = 1;
  1252. block_indicators = 1;
  1253. }
  1254. if (CHECK(string, '?') || CHECK(string, ':')) {
  1255. flow_indicators = 1;
  1256. if (followed_by_whitespace) {
  1257. block_indicators = 1;
  1258. }
  1259. }
  1260. if (CHECK(string, '-') && followed_by_whitespace) {
  1261. flow_indicators = 1;
  1262. block_indicators = 1;
  1263. }
  1264. }
  1265. else
  1266. {
  1267. if (CHECK(string, ',') || CHECK(string, '?')
  1268. || CHECK(string, '[') || CHECK(string, ']')
  1269. || CHECK(string, '{') || CHECK(string, '}')) {
  1270. flow_indicators = 1;
  1271. }
  1272. if (CHECK(string, ':')) {
  1273. flow_indicators = 1;
  1274. if (followed_by_whitespace) {
  1275. block_indicators = 1;
  1276. }
  1277. }
  1278. if (CHECK(string, '#') && preceeded_by_whitespace) {
  1279. flow_indicators = 1;
  1280. block_indicators = 1;
  1281. }
  1282. }
  1283. if (!IS_PRINTABLE(string)
  1284. || (!IS_ASCII(string) && !emitter->unicode)) {
  1285. special_characters = 1;
  1286. }
  1287. if (IS_BREAK(string)) {
  1288. line_breaks = 1;
  1289. }
  1290. if (IS_SPACE(string))
  1291. {
  1292. if (string.start == string.pointer) {
  1293. leading_space = 1;
  1294. }
  1295. if (string.pointer+WIDTH(string) == string.end) {
  1296. trailing_space = 1;
  1297. }
  1298. if (previous_break) {
  1299. break_space = 1;
  1300. }
  1301. previous_space = 1;
  1302. previous_break = 0;
  1303. }
  1304. else if (IS_BREAK(string))
  1305. {
  1306. if (string.start == string.pointer) {
  1307. leading_break = 1;
  1308. }
  1309. if (string.pointer+WIDTH(string) == string.end) {
  1310. trailing_break = 1;
  1311. }
  1312. if (previous_space) {
  1313. space_break = 1;
  1314. }
  1315. previous_space = 0;
  1316. previous_break = 1;
  1317. }
  1318. else
  1319. {
  1320. previous_space = 0;
  1321. previous_break = 0;
  1322. }
  1323. preceeded_by_whitespace = IS_BLANKZ(string);
  1324. MOVE(string);
  1325. if (string.pointer != string.end) {
  1326. followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
  1327. }
  1328. }
  1329. emitter->scalar_data.multiline = line_breaks;
  1330. emitter->scalar_data.flow_plain_allowed = 1;
  1331. emitter->scalar_data.block_plain_allowed = 1;
  1332. emitter->scalar_data.single_quoted_allowed = 1;
  1333. emitter->scalar_data.block_allowed = 1;
  1334. if (leading_space || leading_break || trailing_space || trailing_break) {
  1335. emitter->scalar_data.flow_plain_allowed = 0;
  1336. emitter->scalar_data.block_plain_allowed = 0;
  1337. }
  1338. if (trailing_space) {
  1339. emitter->scalar_data.block_allowed = 0;
  1340. }
  1341. if (break_space) {
  1342. emitter->scalar_data.flow_plain_allowed = 0;
  1343. emitter->scalar_data.block_plain_allowed = 0;
  1344. emitter->scalar_data.single_quoted_allowed = 0;
  1345. }
  1346. if (space_break || special_characters) {
  1347. emitter->scalar_data.flow_plain_allowed = 0;
  1348. emitter->scalar_data.block_plain_allowed = 0;
  1349. emitter->scalar_data.single_quoted_allowed = 0;
  1350. emitter->scalar_data.block_allowed = 0;
  1351. }
  1352. if (line_breaks) {
  1353. emitter->scalar_data.flow_plain_allowed = 0;
  1354. emitter->scalar_data.block_plain_allowed = 0;
  1355. }
  1356. if (flow_indicators) {
  1357. emitter->scalar_data.flow_plain_allowed = 0;
  1358. }
  1359. if (block_indicators) {
  1360. emitter->scalar_data.block_plain_allowed = 0;
  1361. }
  1362. return 1;
  1363. }
  1364. /*
  1365. * Check if the event data is valid.
  1366. */
  1367. static int
  1368. yaml_emitter_analyze_event(yaml_emitter_t *emitter,
  1369. yaml_event_t *event)
  1370. {
  1371. emitter->anchor_data.anchor = NULL;
  1372. emitter->anchor_data.anchor_length = 0;
  1373. emitter->tag_data.handle = NULL;
  1374. emitter->tag_data.handle_length = 0;
  1375. emitter->tag_data.suffix = NULL;
  1376. emitter->tag_data.suffix_length = 0;
  1377. emitter->scalar_data.value = NULL;
  1378. emitter->scalar_data.length = 0;
  1379. switch (event->type)
  1380. {
  1381. case YAML_ALIAS_EVENT:
  1382. if (!yaml_emitter_analyze_anchor(emitter,
  1383. event->data.alias.anchor, 1))
  1384. return 0;
  1385. return 1;
  1386. case YAML_SCALAR_EVENT:
  1387. if (event->data.scalar.anchor) {
  1388. if (!yaml_emitter_analyze_anchor(emitter,
  1389. event->data.scalar.anchor, 0))
  1390. return 0;
  1391. }
  1392. if (event->data.scalar.tag && (emitter->canonical ||
  1393. (!event->data.scalar.plain_implicit
  1394. && !event->data.scalar.quoted_implicit))) {
  1395. if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
  1396. return 0;
  1397. }
  1398. if (!yaml_emitter_analyze_scalar(emitter,
  1399. event->data.scalar.value, event->data.scalar.length))
  1400. return 0;
  1401. return 1;
  1402. case YAML_SEQUENCE_START_EVENT:
  1403. if (event->data.sequence_start.anchor) {
  1404. if (!yaml_emitter_analyze_anchor(emitter,
  1405. event->data.sequence_start.anchor, 0))
  1406. return 0;
  1407. }
  1408. if (event->data.sequence_start.tag && (emitter->canonical ||
  1409. !event->data.sequence_start.implicit)) {
  1410. if (!yaml_emitter_analyze_tag(emitter,
  1411. event->data.sequence_start.tag))
  1412. return 0;
  1413. }
  1414. return 1;
  1415. case YAML_MAPPING_START_EVENT:
  1416. if (event->data.mapping_start.anchor) {
  1417. if (!yaml_emitter_analyze_anchor(emitter,
  1418. event->data.mapping_start.anchor, 0))
  1419. return 0;
  1420. }
  1421. if (event->data.mapping_start.tag && (emitter->canonical ||
  1422. !event->data.mapping_start.implicit)) {
  1423. if (!yaml_emitter_analyze_tag(emitter,
  1424. event->data.mapping_start.tag))
  1425. return 0;
  1426. }
  1427. return 1;
  1428. default:
  1429. return 1;
  1430. }
  1431. }
  1432. /*
  1433. * Write the BOM character.
  1434. */
  1435. static int
  1436. yaml_emitter_write_bom(yaml_emitter_t *emitter)
  1437. {
  1438. if (!FLUSH(emitter)) return 0;
  1439. *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
  1440. *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
  1441. *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
  1442. return 1;
  1443. }
  1444. static int
  1445. yaml_emitter_write_indent(yaml_emitter_t *emitter)
  1446. {
  1447. int indent = (emitter->indent >= 0) ? emitter->indent : 0;
  1448. if (!emitter->indention || emitter->column > indent
  1449. || (emitter->column == indent && !emitter->whitespace)) {
  1450. if (!PUT_BREAK(emitter)) return 0;
  1451. }
  1452. while (emitter->column < indent) {
  1453. if (!PUT(emitter, ' ')) return 0;
  1454. }
  1455. emitter->whitespace = 1;
  1456. emitter->indention = 1;
  1457. return 1;
  1458. }
  1459. static int
  1460. yaml_emitter_write_indicator(yaml_emitter_t *emitter,
  1461. char *indicator, int need_whitespace,
  1462. int is_whitespace, int is_indention)
  1463. {
  1464. size_t indicator_length;
  1465. yaml_string_t string;
  1466. indicator_length = strlen(indicator);
  1467. STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length);
  1468. if (need_whitespace && !emitter->whitespace) {
  1469. if (!PUT(emitter, ' ')) return 0;
  1470. }
  1471. while (string.pointer != string.end) {
  1472. if (!WRITE(emitter, string)) return 0;
  1473. }
  1474. emitter->whitespace = is_whitespace;
  1475. emitter->indention = (emitter->indention && is_indention);
  1476. emitter->open_ended = 0;
  1477. return 1;
  1478. }
  1479. static int
  1480. yaml_emitter_write_anchor(yaml_emitter_t *emitter,
  1481. yaml_char_t *value, size_t length)
  1482. {
  1483. yaml_string_t string;
  1484. STRING_ASSIGN(string, value, length);
  1485. while (string.pointer != string.end) {
  1486. if (!WRITE(emitter, string)) return 0;
  1487. }
  1488. emitter->whitespace = 0;
  1489. emitter->indention = 0;
  1490. return 1;
  1491. }
  1492. static int
  1493. yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
  1494. yaml_char_t *value, size_t length)
  1495. {
  1496. yaml_string_t string;
  1497. STRING_ASSIGN(string, value, length);
  1498. if (!emitter->whitespace) {
  1499. if (!PUT(emitter, ' ')) return 0;
  1500. }
  1501. while (string.pointer != string.end) {
  1502. if (!WRITE(emitter, string)) return 0;
  1503. }
  1504. emitter->whitespace = 0;
  1505. emitter->indention = 0;
  1506. return 1;
  1507. }
  1508. static int
  1509. yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
  1510. yaml_char_t *value, size_t length,
  1511. int need_whitespace)
  1512. {
  1513. yaml_string_t string;
  1514. STRING_ASSIGN(string, value, length);
  1515. if (need_whitespace && !emitter->whitespace) {
  1516. if (!PUT(emitter, ' ')) return 0;
  1517. }
  1518. while (string.pointer != string.end) {
  1519. if (IS_ALPHA(string)
  1520. || CHECK(string, ';') || CHECK(string, '/')
  1521. || CHECK(string, '?') || CHECK(string, ':')
  1522. || CHECK(string, '@') || CHECK(string, '&')
  1523. || CHECK(string, '=') || CHECK(string, '+')
  1524. || CHECK(string, '$') || CHECK(string, ',')
  1525. || CHECK(string, '_') || CHECK(string, '.')
  1526. || CHECK(string, '~') || CHECK(string, '*')
  1527. || CHECK(string, '\'') || CHECK(string, '(')
  1528. || CHECK(string, ')') || CHECK(string, '[')
  1529. || CHECK(string, ']')) {
  1530. if (!WRITE(emitter, string)) return 0;
  1531. }
  1532. else {
  1533. int width = WIDTH(string);
  1534. unsigned int value;
  1535. while (width --) {
  1536. value = *(string.pointer++);
  1537. if (!PUT(emitter, '%')) return 0;
  1538. if (!PUT(emitter, (value >> 4)
  1539. + ((value >> 4) < 10 ? '0' : 'A' - 10)))
  1540. return 0;
  1541. if (!PUT(emitter, (value & 0x0F)
  1542. + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
  1543. return 0;
  1544. }
  1545. }
  1546. }
  1547. emitter->whitespace = 0;
  1548. emitter->indention = 0;
  1549. return 1;
  1550. }
  1551. static int
  1552. yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
  1553. yaml_char_t *value, size_t length, int allow_breaks)
  1554. {
  1555. yaml_string_t string;
  1556. int spaces = 0;
  1557. int breaks = 0;
  1558. STRING_ASSIGN(string, value, length);
  1559. if (!emitter->whitespace) {
  1560. if (!PUT(emitter, ' ')) return 0;
  1561. }
  1562. while (string.pointer != string.end)
  1563. {
  1564. if (IS_SPACE(string))
  1565. {
  1566. if (allow_breaks && !spaces
  1567. && emitter->column > emitter->best_width
  1568. && !IS_SPACE_AT(string, 1)) {
  1569. if (!yaml_emitter_write_indent(emitter)) return 0;
  1570. MOVE(string);
  1571. }
  1572. else {
  1573. if (!WRITE(emitter, string)) return 0;
  1574. }
  1575. spaces = 1;
  1576. }
  1577. else if (IS_BREAK(string))
  1578. {
  1579. if (!breaks && CHECK(string, '\n')) {
  1580. if (!PUT_BREAK(emitter)) return 0;
  1581. }
  1582. if (!WRITE_BREAK(emitter, string)) return 0;
  1583. emitter->indention = 1;
  1584. breaks = 1;
  1585. }
  1586. else
  1587. {
  1588. if (breaks) {
  1589. if (!yaml_emitter_write_indent(emitter)) return 0;
  1590. }
  1591. if (!WRITE(emitter, string)) return 0;
  1592. emitter->indention = 0;
  1593. spaces = 0;
  1594. breaks = 0;
  1595. }
  1596. }
  1597. emitter->whitespace = 0;
  1598. emitter->indention = 0;
  1599. if (emitter->root_context)
  1600. {
  1601. emitter->open_ended = 1;
  1602. }
  1603. return 1;
  1604. }
  1605. static int
  1606. yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
  1607. yaml_char_t *value, size_t length, int allow_breaks)
  1608. {
  1609. yaml_string_t string;
  1610. int spaces = 0;
  1611. int breaks = 0;
  1612. STRING_ASSIGN(string, value, length);
  1613. if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
  1614. return 0;
  1615. while (string.pointer != string.end)
  1616. {
  1617. if (IS_SPACE(string))
  1618. {
  1619. if (allow_breaks && !spaces
  1620. && emitter->column > emitter->best_width
  1621. && string.pointer != string.start
  1622. && string.pointer != string.end - 1
  1623. && !IS_SPACE_AT(string, 1)) {
  1624. if (!yaml_emitter_write_indent(emitter)) return 0;
  1625. MOVE(string);
  1626. }
  1627. else {
  1628. if (!WRITE(emitter, string)) return 0;
  1629. }
  1630. spaces = 1;
  1631. }
  1632. else if (IS_BREAK(string))
  1633. {
  1634. if (!breaks && CHECK(string, '\n')) {
  1635. if (!PUT_BREAK(emitter)) return 0;
  1636. }
  1637. if (!WRITE_BREAK(emitter, string)) return 0;
  1638. emitter->indention = 1;
  1639. breaks = 1;
  1640. }
  1641. else
  1642. {
  1643. if (breaks) {
  1644. if (!yaml_emitter_write_indent(emitter)) return 0;
  1645. }
  1646. if (CHECK(string, '\'')) {
  1647. if (!PUT(emitter, '\'')) return 0;
  1648. }
  1649. if (!WRITE(emitter, string)) return 0;
  1650. emitter->indention = 0;
  1651. spaces = 0;
  1652. breaks = 0;
  1653. }
  1654. }
  1655. if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
  1656. return 0;
  1657. emitter->whitespace = 0;
  1658. emitter->indention = 0;
  1659. return 1;
  1660. }
  1661. static int
  1662. yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
  1663. yaml_char_t *value, size_t length, int allow_breaks)
  1664. {
  1665. yaml_string_t string;
  1666. int spaces = 0;
  1667. STRING_ASSIGN(string, value, length);
  1668. if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
  1669. return 0;
  1670. while (string.pointer != string.end)
  1671. {
  1672. if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
  1673. || IS_BOM(string) || IS_BREAK(string)
  1674. || CHECK(string, '"') || CHECK(string, '\\'))
  1675. {
  1676. unsigned char octet;
  1677. unsigned int width;
  1678. unsigned int value;
  1679. int k;
  1680. octet = string.pointer[0];
  1681. width = (octet & 0x80) == 0x00 ? 1 :
  1682. (octet & 0xE0) == 0xC0 ? 2 :
  1683. (octet & 0xF0) == 0xE0 ? 3 :
  1684. (octet & 0xF8) == 0xF0 ? 4 : 0;
  1685. value = (octet & 0x80) == 0x00 ? octet & 0x7F :
  1686. (octet & 0xE0) == 0xC0 ? octet & 0x1F :
  1687. (octet & 0xF0) == 0xE0 ? octet & 0x0F :
  1688. (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
  1689. for (k = 1; k < (int)width; k ++) {
  1690. octet = string.pointer[k];
  1691. value = (value << 6) + (octet & 0x3F);
  1692. }
  1693. string.pointer += width;
  1694. if (!PUT(emitter, '\\')) return 0;
  1695. switch (value)
  1696. {
  1697. case 0x00:
  1698. if (!PUT(emitter, '0')) return 0;
  1699. break;
  1700. case 0x07:
  1701. if (!PUT(emitter, 'a')) return 0;
  1702. break;
  1703. case 0x08:
  1704. if (!PUT(emitter, 'b')) return 0;
  1705. break;
  1706. case 0x09:
  1707. if (!PUT(emitter, 't')) return 0;
  1708. break;
  1709. case 0x0A:
  1710. if (!PUT(emitter, 'n')) return 0;
  1711. break;
  1712. case 0x0B:
  1713. if (!PUT(emitter, 'v')) return 0;
  1714. break;
  1715. case 0x0C:
  1716. if (!PUT(emitter, 'f')) return 0;
  1717. break;
  1718. case 0x0D:
  1719. if (!PUT(emitter, 'r')) return 0;
  1720. break;
  1721. case 0x1B:
  1722. if (!PUT(emitter, 'e')) return 0;
  1723. break;
  1724. case 0x22:
  1725. if (!PUT(emitter, '\"')) return 0;
  1726. break;
  1727. case 0x5C:
  1728. if (!PUT(emitter, '\\')) return 0;
  1729. break;
  1730. case 0x85:
  1731. if (!PUT(emitter, 'N')) return 0;
  1732. break;
  1733. case 0xA0:
  1734. if (!PUT(emitter, '_')) return 0;
  1735. break;
  1736. case 0x2028:
  1737. if (!PUT(emitter, 'L')) return 0;
  1738. break;
  1739. case 0x2029:
  1740. if (!PUT(emitter, 'P')) return 0;
  1741. break;
  1742. default:
  1743. if (value <= 0xFF) {
  1744. if (!PUT(emitter, 'x')) return 0;
  1745. width = 2;
  1746. }
  1747. else if (value <= 0xFFFF) {
  1748. if (!PUT(emitter, 'u')) return 0;
  1749. width = 4;
  1750. }
  1751. else {
  1752. if (!PUT(emitter, 'U')) return 0;
  1753. width = 8;
  1754. }
  1755. for (k = (width-1)*4; k >= 0; k -= 4) {
  1756. int digit = (value >> k) & 0x0F;
  1757. if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
  1758. return 0;
  1759. }
  1760. }
  1761. spaces = 0;
  1762. }
  1763. else if (IS_SPACE(string))
  1764. {
  1765. if (allow_breaks && !spaces
  1766. && emitter->column > emitter->best_width
  1767. && string.pointer != string.start
  1768. && string.pointer != string.end - 1) {
  1769. if (!yaml_emitter_write_indent(emitter)) return 0;
  1770. if (IS_SPACE_AT(string, 1)) {
  1771. if (!PUT(emitter, '\\')) return 0;
  1772. }
  1773. MOVE(string);
  1774. }
  1775. else {
  1776. if (!WRITE(emitter, string)) return 0;
  1777. }
  1778. spaces = 1;
  1779. }
  1780. else
  1781. {
  1782. if (!WRITE(emitter, string)) return 0;
  1783. spaces = 0;
  1784. }
  1785. }
  1786. if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
  1787. return 0;
  1788. emitter->whitespace = 0;
  1789. emitter->indention = 0;
  1790. return 1;
  1791. }
  1792. static int
  1793. yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
  1794. yaml_string_t string)
  1795. {
  1796. char indent_hint[2];
  1797. char *chomp_hint = NULL;
  1798. if (IS_SPACE(string) || IS_BREAK(string))
  1799. {
  1800. indent_hint[0] = '0' + (char)emitter->best_indent;
  1801. indent_hint[1] = '\0';
  1802. if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0))
  1803. return 0;
  1804. }
  1805. emitter->open_ended = 0;
  1806. string.pointer = string.end;
  1807. if (string.start == string.pointer)
  1808. {
  1809. chomp_hint = "-";
  1810. }
  1811. else
  1812. {
  1813. do {
  1814. string.pointer --;
  1815. } while ((*string.pointer & 0xC0) == 0x80);
  1816. if (!IS_BREAK(string))
  1817. {
  1818. chomp_hint = "-";
  1819. }
  1820. else if (string.start == string.pointer)
  1821. {
  1822. chomp_hint = "+";
  1823. emitter->open_ended = 1;
  1824. }
  1825. else
  1826. {
  1827. do {
  1828. string.pointer --;
  1829. } while ((*string.pointer & 0xC0) == 0x80);
  1830. if (IS_BREAK(string))
  1831. {
  1832. chomp_hint = "+";
  1833. emitter->open_ended = 1;
  1834. }
  1835. }
  1836. }
  1837. if (chomp_hint)
  1838. {
  1839. if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0))
  1840. return 0;
  1841. }
  1842. return 1;
  1843. }
  1844. static int
  1845. yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
  1846. yaml_char_t *value, size_t length)
  1847. {
  1848. yaml_string_t string;
  1849. int breaks = 1;
  1850. STRING_ASSIGN(string, value, length);
  1851. if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0))
  1852. return 0;
  1853. if (!yaml_emitter_write_block_scalar_hints(emitter, string))
  1854. return 0;
  1855. if (!PUT_BREAK(emitter)) return 0;
  1856. emitter->indention = 1;
  1857. emitter->whitespace = 1;
  1858. while (string.pointer != string.end)
  1859. {
  1860. if (IS_BREAK(string))
  1861. {
  1862. if (!WRITE_BREAK(emitter, string)) return 0;
  1863. emitter->indention = 1;
  1864. breaks = 1;
  1865. }
  1866. else
  1867. {
  1868. if (breaks) {
  1869. if (!yaml_emitter_write_indent(emitter)) return 0;
  1870. }
  1871. if (!WRITE(emitter, string)) return 0;
  1872. emitter->indention = 0;
  1873. breaks = 0;
  1874. }
  1875. }
  1876. return 1;
  1877. }
  1878. static int
  1879. yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
  1880. yaml_char_t *value, size_t length)
  1881. {
  1882. yaml_string_t string;
  1883. int breaks = 1;
  1884. int leading_spaces = 1;
  1885. STRING_ASSIGN(string, value, length);
  1886. if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0))
  1887. return 0;
  1888. if (!yaml_emitter_write_block_scalar_hints(emitter, string))
  1889. return 0;
  1890. if (!PUT_BREAK(emitter)) return 0;
  1891. emitter->indention = 1;
  1892. emitter->whitespace = 1;
  1893. while (string.pointer != string.end)
  1894. {
  1895. if (IS_BREAK(string))
  1896. {
  1897. if (!breaks && !leading_spaces && CHECK(string, '\n')) {
  1898. int k = 0;
  1899. while (IS_BREAK_AT(string, k)) {
  1900. k += WIDTH_AT(string, k);
  1901. }
  1902. if (!IS_BLANKZ_AT(string, k)) {
  1903. if (!PUT_BREAK(emitter)) return 0;
  1904. }
  1905. }
  1906. if (!WRITE_BREAK(emitter, string)) return 0;
  1907. emitter->indention = 1;
  1908. breaks = 1;
  1909. }
  1910. else
  1911. {
  1912. if (breaks) {
  1913. if (!yaml_emitter_write_indent(emitter)) return 0;
  1914. leading_spaces = IS_BLANK(string);
  1915. }
  1916. if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
  1917. && emitter->column > emitter->best_width) {
  1918. if (!yaml_emitter_write_indent(emitter)) return 0;
  1919. MOVE(string);
  1920. }
  1921. else {
  1922. if (!WRITE(emitter, string)) return 0;
  1923. }
  1924. emitter->indention = 0;
  1925. breaks = 0;
  1926. }
  1927. }
  1928. return 1;
  1929. }