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