]> andersk Git - libyaml.git/blame - src/emitter.c
Set YAML_AGE to 0 as the current interface is not really compatible with the previous...
[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 251static int
40cc5d15 252yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
784340c2
KS
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
561ca6de
KS
590 if ((event->data.document_start.version_directive ||
591 (event->data.document_start.tag_directives.start
592 != event->data.document_start.tag_directives.end)) &&
593 emitter->open_ended)
594 {
595 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
596 return 0;
597 if (!yaml_emitter_write_indent(emitter))
598 return 0;
599 }
600
5a00d8fe
KS
601 if (event->data.document_start.version_directive) {
602 implicit = 0;
e35af832
KS
603 if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
604 return 0;
605 if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
606 return 0;
607 if (!yaml_emitter_write_indent(emitter))
5a00d8fe
KS
608 return 0;
609 }
610
611 if (event->data.document_start.tag_directives.start
612 != event->data.document_start.tag_directives.end) {
613 implicit = 0;
614 for (tag_directive = event->data.document_start.tag_directives.start;
615 tag_directive != event->data.document_start.tag_directives.end;
616 tag_directive ++) {
e35af832
KS
617 if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0))
618 return 0;
619 if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle,
620 strlen((char *)tag_directive->handle)))
621 return 0;
622 if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix,
784340c2 623 strlen((char *)tag_directive->prefix), 1))
e35af832
KS
624 return 0;
625 if (!yaml_emitter_write_indent(emitter))
5a00d8fe
KS
626 return 0;
627 }
628 }
629
630 if (yaml_emitter_check_empty_document(emitter)) {
631 implicit = 0;
632 }
633
634 if (!implicit) {
635 if (!yaml_emitter_write_indent(emitter))
636 return 0;
637 if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0))
638 return 0;
639 if (emitter->canonical) {
640 if (!yaml_emitter_write_indent(emitter))
641 return 0;
642 }
643 }
644
645 emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE;
cf616166 646
cf616166
KS
647 return 1;
648 }
649
5a00d8fe
KS
650 else if (event->type == YAML_STREAM_END_EVENT)
651 {
561ca6de
KS
652 if (emitter->open_ended)
653 {
654 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
655 return 0;
656 if (!yaml_emitter_write_indent(emitter))
657 return 0;
658 }
659
5a00d8fe
KS
660 if (!yaml_emitter_flush(emitter))
661 return 0;
cf616166 662
5a00d8fe
KS
663 emitter->state = YAML_EMIT_END_STATE;
664
665 return 1;
666 }
667
668 return yaml_emitter_set_emitter_error(emitter,
669 "expected DOCUMENT-START or STREAM-END");
cf616166
KS
670}
671
e35af832
KS
672/*
673 * Expect the root node.
674 */
675
5a00d8fe
KS
676static int
677yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
678 yaml_event_t *event)
679{
680 if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE))
681 return 0;
cf616166 682
5a00d8fe
KS
683 return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0);
684}
685
e35af832
KS
686/*
687 * Expect DOCUMENT-END.
688 */
689
5a00d8fe
KS
690static int
691yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
692 yaml_event_t *event)
cf616166 693{
5a00d8fe
KS
694 if (event->type == YAML_DOCUMENT_END_EVENT)
695 {
696 if (!yaml_emitter_write_indent(emitter))
697 return 0;
698 if (!event->data.document_end.implicit) {
699 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
700 return 0;
701 if (!yaml_emitter_write_indent(emitter))
702 return 0;
cf616166 703 }
5a00d8fe
KS
704 if (!yaml_emitter_flush(emitter))
705 return 0;
706
707 emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
708
6d167281
KS
709 while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
710 yaml_tag_directive_t tag_directive = POP(emitter,
711 emitter->tag_directives);
712 yaml_free(tag_directive.handle);
713 yaml_free(tag_directive.prefix);
714 }
715
5a00d8fe 716 return 1;
cf616166
KS
717 }
718
5a00d8fe
KS
719 return yaml_emitter_set_emitter_error(emitter,
720 "expected DOCUMENT-END");
721}
722
e35af832 723/*
6d167281 724 *
e35af832
KS
725 * Expect a flow item node.
726 */
727
5a00d8fe
KS
728static int
729yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
730 yaml_event_t *event, int first)
731{
732 if (first)
733 {
734 if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
735 return 0;
736 if (!yaml_emitter_increase_indent(emitter, 1, 0))
737 return 0;
738 emitter->flow_level ++;
cf616166
KS
739 }
740
5a00d8fe
KS
741 if (event->type == YAML_SEQUENCE_END_EVENT)
742 {
743 emitter->flow_level --;
744 emitter->indent = POP(emitter, emitter->indents);
745 if (emitter->canonical && !first) {
746 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
747 return 0;
748 if (!yaml_emitter_write_indent(emitter))
749 return 0;
750 }
751 if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
752 return 0;
753 emitter->state = POP(emitter, emitter->states);
cf616166 754
cf616166
KS
755 return 1;
756 }
757
6d167281
KS
758 if (!first) {
759 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
760 return 0;
761 }
762
5a00d8fe
KS
763 if (emitter->canonical || emitter->column > emitter->best_width) {
764 if (!yaml_emitter_write_indent(emitter))
765 return 0;
766 }
6d167281 767 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
5a00d8fe 768 return 0;
cf616166 769
5a00d8fe 770 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
cf616166
KS
771}
772
e35af832
KS
773/*
774 * Expect a flow key node.
775 */
776
5a00d8fe
KS
777static int
778yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
779 yaml_event_t *event, int first)
cf616166 780{
5a00d8fe
KS
781 if (first)
782 {
783 if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
784 return 0;
785 if (!yaml_emitter_increase_indent(emitter, 1, 0))
786 return 0;
787 emitter->flow_level ++;
788 }
cf616166 789
5a00d8fe
KS
790 if (event->type == YAML_MAPPING_END_EVENT)
791 {
792 emitter->flow_level --;
793 emitter->indent = POP(emitter, emitter->indents);
794 if (emitter->canonical && !first) {
795 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
796 return 0;
797 if (!yaml_emitter_write_indent(emitter))
798 return 0;
799 }
800 if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
801 return 0;
802 emitter->state = POP(emitter, emitter->states);
cf616166 803
5a00d8fe
KS
804 return 1;
805 }
cf616166 806
5a00d8fe
KS
807 if (!first) {
808 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
809 return 0;
810 }
811 if (emitter->canonical || emitter->column > emitter->best_width) {
812 if (!yaml_emitter_write_indent(emitter))
813 return 0;
814 }
cf616166 815
5a00d8fe
KS
816 if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
817 {
818 if (!PUSH(emitter, emitter->states,
819 YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
820 return 0;
cf616166 821
5a00d8fe
KS
822 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
823 }
824 else
825 {
826 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
827 return 0;
828 if (!PUSH(emitter, emitter->states,
829 YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
830 return 0;
831
832 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
833 }
834}
835
e35af832
KS
836/*
837 * Expect a flow value node.
838 */
839
5a00d8fe
KS
840static int
841yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
842 yaml_event_t *event, int simple)
cf616166 843{
5a00d8fe
KS
844 if (simple) {
845 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
846 return 0;
847 }
848 else {
849 if (emitter->canonical || emitter->column > emitter->best_width) {
850 if (!yaml_emitter_write_indent(emitter))
851 return 0;
cf616166 852 }
5a00d8fe
KS
853 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
854 return 0;
cf616166 855 }
5a00d8fe
KS
856 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
857 return 0;
858 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
859}
cf616166 860
e35af832
KS
861/*
862 * Expect a block item node.
863 */
864
5a00d8fe
KS
865static int
866yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
867 yaml_event_t *event, int first)
868{
869 if (first)
870 {
871 if (!yaml_emitter_increase_indent(emitter, 0,
872 (emitter->mapping_context && !emitter->indention)))
873 return 0;
cf616166
KS
874 }
875
5a00d8fe
KS
876 if (event->type == YAML_SEQUENCE_END_EVENT)
877 {
878 emitter->indent = POP(emitter, emitter->indents);
879 emitter->state = POP(emitter, emitter->states);
cf616166 880
cf616166
KS
881 return 1;
882 }
883
5a00d8fe
KS
884 if (!yaml_emitter_write_indent(emitter))
885 return 0;
886 if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
887 return 0;
888 if (!PUSH(emitter, emitter->states,
889 YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
890 return 0;
cf616166 891
5a00d8fe 892 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
cf616166
KS
893}
894
e35af832
KS
895/*
896 * Expect a block key node.
897 */
898
5a00d8fe
KS
899static int
900yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
901 yaml_event_t *event, int first)
cf616166 902{
5a00d8fe
KS
903 if (first)
904 {
905 if (!yaml_emitter_increase_indent(emitter, 0, 0))
906 return 0;
907 }
908
909 if (event->type == YAML_MAPPING_END_EVENT)
910 {
911 emitter->indent = POP(emitter, emitter->indents);
912 emitter->state = POP(emitter, emitter->states);
913
914 return 1;
915 }
cf616166 916
5a00d8fe
KS
917 if (!yaml_emitter_write_indent(emitter))
918 return 0;
cf616166 919
5a00d8fe
KS
920 if (yaml_emitter_check_simple_key(emitter))
921 {
922 if (!PUSH(emitter, emitter->states,
923 YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
924 return 0;
cf616166 925
5a00d8fe
KS
926 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
927 }
928 else
929 {
930 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
931 return 0;
932 if (!PUSH(emitter, emitter->states,
933 YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
934 return 0;
935
936 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
937 }
cf616166
KS
938}
939
e35af832
KS
940/*
941 * Expect a block value node.
942 */
943
5a00d8fe
KS
944static int
945yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
946 yaml_event_t *event, int simple)
947{
948 if (simple) {
949 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
950 return 0;
951 }
952 else {
953 if (!yaml_emitter_write_indent(emitter))
954 return 0;
955 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
956 return 0;
957 }
958 if (!PUSH(emitter, emitter->states,
959 YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
960 return 0;
cf616166 961
5a00d8fe
KS
962 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
963}
964
e35af832
KS
965/*
966 * Expect a node.
967 */
968
5a00d8fe
KS
969static int
970yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
971 int root, int sequence, int mapping, int simple_key)
cf616166 972{
5a00d8fe
KS
973 emitter->root_context = root;
974 emitter->sequence_context = sequence;
975 emitter->mapping_context = mapping;
976 emitter->simple_key_context = simple_key;
977
978 switch (event->type)
979 {
980 case YAML_ALIAS_EVENT:
981 return yaml_emitter_emit_alias(emitter, event);
982
983 case YAML_SCALAR_EVENT:
984 return yaml_emitter_emit_scalar(emitter, event);
985
986 case YAML_SEQUENCE_START_EVENT:
987 return yaml_emitter_emit_sequence_start(emitter, event);
988
989 case YAML_MAPPING_START_EVENT:
990 return yaml_emitter_emit_mapping_start(emitter, event);
991
992 default:
993 return yaml_emitter_set_emitter_error(emitter,
994 "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
995 }
996
cf616166
KS
997 return 0;
998}
999
e35af832
KS
1000/*
1001 * Expect ALIAS.
1002 */
1003
5a00d8fe
KS
1004static int
1005yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event)
1006{
e35af832 1007 if (!yaml_emitter_process_anchor(emitter))
5a00d8fe
KS
1008 return 0;
1009 emitter->state = POP(emitter, emitter->states);
1010
1011 return 1;
1012}
1013
e35af832
KS
1014/*
1015 * Expect SCALAR.
1016 */
1017
5a00d8fe
KS
1018static int
1019yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
1020{
e35af832 1021 if (!yaml_emitter_select_scalar_style(emitter, event))
5a00d8fe 1022 return 0;
e35af832
KS
1023 if (!yaml_emitter_process_anchor(emitter))
1024 return 0;
1025 if (!yaml_emitter_process_tag(emitter))
5a00d8fe
KS
1026 return 0;
1027 if (!yaml_emitter_increase_indent(emitter, 1, 0))
1028 return 0;
e35af832 1029 if (!yaml_emitter_process_scalar(emitter))
5a00d8fe
KS
1030 return 0;
1031 emitter->indent = POP(emitter, emitter->indents);
1032 emitter->state = POP(emitter, emitter->states);
1033
1034 return 1;
1035}
1036
e35af832
KS
1037/*
1038 * Expect SEQUENCE-START.
1039 */
1040
5a00d8fe
KS
1041static int
1042yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
1043{
e35af832 1044 if (!yaml_emitter_process_anchor(emitter))
5a00d8fe 1045 return 0;
e35af832 1046 if (!yaml_emitter_process_tag(emitter))
5a00d8fe
KS
1047 return 0;
1048
1049 if (emitter->flow_level || emitter->canonical
1050 || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
1051 || yaml_emitter_check_empty_sequence(emitter)) {
1052 emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
1053 }
1054 else {
1055 emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
1056 }
1057
1058 return 1;
1059}
1060
e35af832
KS
1061/*
1062 * Expect MAPPING-START.
1063 */
1064
5a00d8fe
KS
1065static int
1066yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
1067{
e35af832 1068 if (!yaml_emitter_process_anchor(emitter))
5a00d8fe 1069 return 0;
e35af832 1070 if (!yaml_emitter_process_tag(emitter))
5a00d8fe
KS
1071 return 0;
1072
1073 if (emitter->flow_level || emitter->canonical
1074 || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
1075 || yaml_emitter_check_empty_mapping(emitter)) {
1076 emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
1077 }
1078 else {
1079 emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
1080 }
1081
1082 return 1;
1083}
1084
e35af832
KS
1085/*
1086 * Check if the document content is an empty scalar.
1087 */
1088
1089static int
1090yaml_emitter_check_empty_document(yaml_emitter_t *emitter)
1091{
1092 return 0;
1093}
1094
1095/*
1096 * Check if the next events represent an empty sequence.
1097 */
1098
1099static int
1100yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
1101{
1102 if (emitter->events.tail - emitter->events.head < 2)
1103 return 0;
1104
1105 return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
1106 && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
1107}
1108
1109/*
1110 * Check if the next events represent an empty mapping.
1111 */
1112
1113static int
1114yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
1115{
1116 if (emitter->events.tail - emitter->events.head < 2)
1117 return 0;
1118
1119 return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
1120 && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
1121}
1122
1123/*
1124 * Check if the next node can be expressed as a simple key.
1125 */
1126
1127static int
1128yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
1129{
1130 yaml_event_t *event = emitter->events.head;
1131 size_t length = 0;
1132
1133 switch (event->type)
1134 {
1135 case YAML_ALIAS_EVENT:
1136 length += emitter->anchor_data.anchor_length;
1137 break;
1138
1139 case YAML_SCALAR_EVENT:
1140 if (emitter->scalar_data.multiline)
1141 return 0;
1142 length += emitter->anchor_data.anchor_length
1143 + emitter->tag_data.handle_length
1144 + emitter->tag_data.suffix_length
1145 + emitter->scalar_data.length;
1146 break;
1147
1148 case YAML_SEQUENCE_START_EVENT:
1149 if (!yaml_emitter_check_empty_sequence(emitter))
1150 return 0;
1151 length += emitter->anchor_data.anchor_length
1152 + emitter->tag_data.handle_length
1153 + emitter->tag_data.suffix_length;
1154 break;
1155
1156 case YAML_MAPPING_START_EVENT:
1157 if (!yaml_emitter_check_empty_sequence(emitter))
1158 return 0;
1159 length += emitter->anchor_data.anchor_length
1160 + emitter->tag_data.handle_length
1161 + emitter->tag_data.suffix_length;
1162 break;
1163
1164 default:
1165 return 0;
1166 }
1167
1168 if (length > 128)
1169 return 0;
1170
1171 return 1;
1172}
1173
1174/*
1175 * Determine an acceptable scalar style.
1176 */
1177
1178static int
1179yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
1180{
1181 yaml_scalar_style_t style = event->data.scalar.style;
e27a3c88
KS
1182 int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix);
1183
1184 if (no_tag && !event->data.scalar.plain_implicit
1185 && !event->data.scalar.quoted_implicit) {
1186 return yaml_emitter_set_emitter_error(emitter,
1187 "neither tag nor implicit flags are specified");
1188 }
e35af832
KS
1189
1190 if (style == YAML_ANY_SCALAR_STYLE)
1191 style = YAML_PLAIN_SCALAR_STYLE;
1192
1193 if (emitter->canonical)
1194 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1195
1196 if (emitter->simple_key_context && emitter->scalar_data.multiline)
1197 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1198
1199 if (style == YAML_PLAIN_SCALAR_STYLE)
1200 {
1201 if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
1202 || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
1203 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1204 if (!emitter->scalar_data.length
1205 && (emitter->flow_level || emitter->simple_key_context))
1206 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
e27a3c88 1207 if (no_tag && !event->data.scalar.plain_implicit)
e35af832
KS
1208 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1209 }
1210
1211 if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
1212 {
1213 if (!emitter->scalar_data.single_quoted_allowed)
1214 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1215 }
1216
1217 if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
1218 {
6d167281
KS
1219 if (!emitter->scalar_data.block_allowed
1220 || emitter->flow_level || emitter->simple_key_context)
e35af832
KS
1221 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1222 }
1223
e27a3c88
KS
1224 if (no_tag && !event->data.scalar.quoted_implicit
1225 && style != YAML_PLAIN_SCALAR_STYLE)
e35af832 1226 {
e27a3c88
KS
1227 emitter->tag_data.handle = (yaml_char_t *)"!";
1228 emitter->tag_data.handle_length = 1;
e35af832
KS
1229 }
1230
1231 emitter->scalar_data.style = style;
1232
1233 return 1;
1234}
1235
1236/*
1237 * Write an achor.
1238 */
1239
1240static int
1241yaml_emitter_process_anchor(yaml_emitter_t *emitter)
1242{
1243 if (!emitter->anchor_data.anchor)
1244 return 1;
1245
1246 if (!yaml_emitter_write_indicator(emitter,
1247 (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
1248 return 0;
1249
1250 return yaml_emitter_write_anchor(emitter,
1251 emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
1252}
1253
1254/*
1255 * Write a tag.
1256 */
1257
1258static int
1259yaml_emitter_process_tag(yaml_emitter_t *emitter)
1260{
1261 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1262 return 1;
1263
1264 if (emitter->tag_data.handle)
1265 {
1266 if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
1267 emitter->tag_data.handle_length))
1268 return 0;
1269 if (emitter->tag_data.suffix) {
1270 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
784340c2 1271 emitter->tag_data.suffix_length, 0))
e35af832
KS
1272 return 0;
1273 }
1274 }
1275 else
1276 {
1277 if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
1278 return 0;
1279 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
784340c2 1280 emitter->tag_data.suffix_length, 0))
e35af832
KS
1281 return 0;
1282 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
1283 return 0;
1284 }
1285
1286 return 1;
1287}
1288
1289/*
1290 * Write a scalar.
1291 */
1292
1293static int
1294yaml_emitter_process_scalar(yaml_emitter_t *emitter)
1295{
1296 switch (emitter->scalar_data.style)
1297 {
1298 case YAML_PLAIN_SCALAR_STYLE:
1299 return yaml_emitter_write_plain_scalar(emitter,
1300 emitter->scalar_data.value, emitter->scalar_data.length,
1301 !emitter->simple_key_context);
1302
1303 case YAML_SINGLE_QUOTED_SCALAR_STYLE:
1304 return yaml_emitter_write_single_quoted_scalar(emitter,
1305 emitter->scalar_data.value, emitter->scalar_data.length,
1306 !emitter->simple_key_context);
1307
1308 case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
1309 return yaml_emitter_write_double_quoted_scalar(emitter,
1310 emitter->scalar_data.value, emitter->scalar_data.length,
1311 !emitter->simple_key_context);
1312
1313 case YAML_LITERAL_SCALAR_STYLE:
1314 return yaml_emitter_write_literal_scalar(emitter,
1315 emitter->scalar_data.value, emitter->scalar_data.length);
1316
1317 case YAML_FOLDED_SCALAR_STYLE:
1318 return yaml_emitter_write_folded_scalar(emitter,
1319 emitter->scalar_data.value, emitter->scalar_data.length);
1320
1321 default:
1322 assert(1); /* Impossible. */
1323 }
1324
1325 return 0;
1326}
1327
1328/*
1329 * Check if a %YAML directive is valid.
1330 */
1331
1332static int
1333yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
1334 yaml_version_directive_t version_directive)
1335{
1336 if (version_directive.major != 1 || version_directive.minor != 1) {
1337 return yaml_emitter_set_emitter_error(emitter,
1338 "incompatible %YAML directive");
1339 }
1340
1341 return 1;
1342}
1343
1344/*
1345 * Check if a %TAG directive is valid.
1346 */
1347
1348static int
1349yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
1350 yaml_tag_directive_t tag_directive)
1351{
1352 yaml_string_t handle = STRING(tag_directive.handle,
1353 strlen((char *)tag_directive.handle));
1354 yaml_string_t prefix = STRING(tag_directive.prefix,
1355 strlen((char *)tag_directive.prefix));
1356
1357 if (handle.start == handle.end) {
1358 return yaml_emitter_set_emitter_error(emitter,
1359 "tag handle must not be empty");
1360 }
1361
1362 if (handle.start[0] != '!') {
1363 return yaml_emitter_set_emitter_error(emitter,
1364 "tag handle must start with '!'");
1365 }
1366
1367 if (handle.end[-1] != '!') {
1368 return yaml_emitter_set_emitter_error(emitter,
1369 "tag handle must end with '!'");
1370 }
1371
1372 handle.pointer ++;
1373
6d167281 1374 while (handle.pointer < handle.end-1) {
e35af832
KS
1375 if (!IS_ALPHA(handle)) {
1376 return yaml_emitter_set_emitter_error(emitter,
1377 "tag handle must contain alphanumerical characters only");
1378 }
1379 MOVE(handle);
1380 }
1381
1382 if (prefix.start == prefix.end) {
1383 return yaml_emitter_set_emitter_error(emitter,
1384 "tag prefix must not be empty");
1385 }
1386
1387 return 1;
1388}
1389
1390/*
1391 * Check if an anchor is valid.
1392 */
1393
1394static int
1395yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
1396 yaml_char_t *anchor, int alias)
1397{
1398 yaml_string_t string = STRING(anchor, strlen((char *)anchor));
1399
1400 if (string.start == string.end) {
1401 return yaml_emitter_set_emitter_error(emitter, alias ?
1402 "alias value must not be empty" :
1403 "anchor value must not be empty");
1404 }
1405
1406 while (string.pointer != string.end) {
1407 if (!IS_ALPHA(string)) {
1408 return yaml_emitter_set_emitter_error(emitter, alias ?
1409 "alias value must contain alphanumerical characters only" :
1410 "anchor value must contain alphanumerical characters only");
1411 }
1412 MOVE(string);
1413 }
6d167281
KS
1414
1415 emitter->anchor_data.anchor = string.start;
1416 emitter->anchor_data.anchor_length = string.end - string.start;
1417 emitter->anchor_data.alias = alias;
1418
1419 return 1;
e35af832
KS
1420}
1421
1422/*
1423 * Check if a tag is valid.
1424 */
1425
1426static int
1427yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
1428 yaml_char_t *tag)
1429{
1430 yaml_string_t string = STRING(tag, strlen((char *)tag));
1431 yaml_tag_directive_t *tag_directive;
1432
1433 if (string.start == string.end) {
1434 return yaml_emitter_set_emitter_error(emitter,
1435 "tag value must not be empty");
1436 }
1437
1438 for (tag_directive = emitter->tag_directives.start;
6d167281 1439 tag_directive != emitter->tag_directives.top; tag_directive ++) {
e35af832 1440 size_t prefix_length = strlen((char *)tag_directive->prefix);
0174ed6e 1441 if (prefix_length < (size_t)(string.end - string.start)
e35af832
KS
1442 && strncmp((char *)tag_directive->prefix, (char *)string.start,
1443 prefix_length) == 0)
1444 {
1445 emitter->tag_data.handle = tag_directive->handle;
1446 emitter->tag_data.handle_length =
1447 strlen((char *)tag_directive->handle);
1448 emitter->tag_data.suffix = string.start + prefix_length;
1449 emitter->tag_data.suffix_length =
1450 (string.end - string.start) - prefix_length;
1451 return 1;
1452 }
1453 }
1454
1455 emitter->tag_data.suffix = string.start;
1456 emitter->tag_data.suffix_length = string.end - string.start;
1457
1458 return 1;
1459}
1460
1461/*
1462 * Check if a scalar is valid.
1463 */
1464
1465static int
1466yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
1467 yaml_char_t *value, size_t length)
1468{
1469 yaml_string_t string = STRING(value, length);
1470
1471 int block_indicators = 0;
1472 int flow_indicators = 0;
1473 int line_breaks = 0;
1474 int special_characters = 0;
1475
40cc5d15
KS
1476 int leading_space = 0;
1477 int leading_break = 0;
1478 int trailing_space = 0;
1479 int trailing_break = 0;
1480 int break_space = 0;
1481 int space_break = 0;
1482
1483 int preceeded_by_whitespace = 0;
1484 int followed_by_whitespace = 0;
1485 int previous_space = 0;
1486 int previous_break = 0;
e35af832
KS
1487
1488 emitter->scalar_data.value = value;
1489 emitter->scalar_data.length = length;
1490
1491 if (string.start == string.end)
1492 {
1493 emitter->scalar_data.multiline = 0;
1494 emitter->scalar_data.flow_plain_allowed = 0;
1495 emitter->scalar_data.block_plain_allowed = 1;
1496 emitter->scalar_data.single_quoted_allowed = 1;
1497 emitter->scalar_data.block_allowed = 0;
1498
1499 return 1;
1500 }
1501
1502 if ((CHECK_AT(string, '-', 0)
1503 && CHECK_AT(string, '-', 1)
1504 && CHECK_AT(string, '-', 2))
1505 || (CHECK_AT(string, '.', 0)
1506 && CHECK_AT(string, '.', 1)
1507 && CHECK_AT(string, '.', 2))) {
1508 block_indicators = 1;
1509 flow_indicators = 1;
1510 }
1511
40cc5d15
KS
1512 preceeded_by_whitespace = 1;
1513 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
e35af832
KS
1514
1515 while (string.pointer != string.end)
1516 {
1517 if (string.start == string.pointer)
1518 {
1519 if (CHECK(string, '#') || CHECK(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 flow_indicators = 1;
1528 block_indicators = 1;
1529 }
1530
1531 if (CHECK(string, '?') || CHECK(string, ':')) {
1532 flow_indicators = 1;
40cc5d15 1533 if (followed_by_whitespace) {
e35af832
KS
1534 block_indicators = 1;
1535 }
1536 }
1537
40cc5d15 1538 if (CHECK(string, '-') && followed_by_whitespace) {
e35af832
KS
1539 flow_indicators = 1;
1540 block_indicators = 1;
1541 }
1542 }
1543 else
1544 {
1545 if (CHECK(string, ',') || CHECK(string, '?')
1546 || CHECK(string, '[') || CHECK(string, ']')
1547 || CHECK(string, '{') || CHECK(string, '}')) {
1548 flow_indicators = 1;
1549 }
1550
1551 if (CHECK(string, ':')) {
1552 flow_indicators = 1;
40cc5d15 1553 if (followed_by_whitespace) {
e35af832
KS
1554 block_indicators = 1;
1555 }
1556 }
1557
40cc5d15 1558 if (CHECK(string, '#') && preceeded_by_whitespace) {
e35af832
KS
1559 flow_indicators = 1;
1560 block_indicators = 1;
1561 }
1562 }
1563
1564 if (!IS_PRINTABLE(string)
1565 || (!IS_ASCII(string) && !emitter->unicode)) {
1566 special_characters = 1;
1567 }
1568
1569 if (IS_BREAK(string)) {
1570 line_breaks = 1;
1571 }
1572
1573 if (IS_SPACE(string))
1574 {
e35af832 1575 if (string.start == string.pointer) {
40cc5d15
KS
1576 leading_space = 1;
1577 }
1578 if (string.pointer+WIDTH(string) == string.end) {
1579 trailing_space = 1;
e35af832 1580 }
40cc5d15
KS
1581 if (previous_break) {
1582 break_space = 1;
1583 }
1584 previous_space = 1;
1585 previous_break = 0;
e35af832 1586 }
e35af832
KS
1587 else if (IS_BREAK(string))
1588 {
e35af832 1589 if (string.start == string.pointer) {
40cc5d15 1590 leading_break = 1;
e35af832 1591 }
40cc5d15
KS
1592 if (string.pointer+WIDTH(string) == string.end) {
1593 trailing_break = 1;
e35af832 1594 }
40cc5d15
KS
1595 if (previous_space) {
1596 space_break = 1;
e35af832 1597 }
40cc5d15
KS
1598 previous_space = 0;
1599 previous_break = 1;
e35af832 1600 }
40cc5d15 1601 else
6d167281 1602 {
40cc5d15
KS
1603 previous_space = 0;
1604 previous_break = 0;
6d167281
KS
1605 }
1606
40cc5d15 1607 preceeded_by_whitespace = IS_BLANKZ(string);
e35af832
KS
1608 MOVE(string);
1609 if (string.pointer != string.end) {
40cc5d15 1610 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
e35af832
KS
1611 }
1612 }
1613
1614 emitter->scalar_data.multiline = line_breaks;
1615
1616 emitter->scalar_data.flow_plain_allowed = 1;
1617 emitter->scalar_data.block_plain_allowed = 1;
1618 emitter->scalar_data.single_quoted_allowed = 1;
1619 emitter->scalar_data.block_allowed = 1;
1620
40cc5d15 1621 if (leading_space || leading_break || trailing_space || trailing_break) {
e35af832
KS
1622 emitter->scalar_data.flow_plain_allowed = 0;
1623 emitter->scalar_data.block_plain_allowed = 0;
e35af832
KS
1624 }
1625
40cc5d15
KS
1626 if (trailing_space) {
1627 emitter->scalar_data.block_allowed = 0;
e35af832
KS
1628 }
1629
40cc5d15 1630 if (break_space) {
e35af832
KS
1631 emitter->scalar_data.flow_plain_allowed = 0;
1632 emitter->scalar_data.block_plain_allowed = 0;
1633 emitter->scalar_data.single_quoted_allowed = 0;
1634 }
1635
40cc5d15 1636 if (space_break || special_characters) {
e35af832
KS
1637 emitter->scalar_data.flow_plain_allowed = 0;
1638 emitter->scalar_data.block_plain_allowed = 0;
1639 emitter->scalar_data.single_quoted_allowed = 0;
1640 emitter->scalar_data.block_allowed = 0;
1641 }
1642
1643 if (line_breaks) {
1644 emitter->scalar_data.flow_plain_allowed = 0;
1645 emitter->scalar_data.block_plain_allowed = 0;
1646 }
1647
1648 if (flow_indicators) {
1649 emitter->scalar_data.flow_plain_allowed = 0;
1650 }
1651
1652 if (block_indicators) {
1653 emitter->scalar_data.block_plain_allowed = 0;
1654 }
1655
1656 return 1;
1657}
1658
1659/*
1660 * Check if the event data is valid.
1661 */
1662
1663static int
1664yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1665 yaml_event_t *event)
1666{
1667 emitter->anchor_data.anchor = NULL;
1668 emitter->anchor_data.anchor_length = 0;
1669 emitter->tag_data.handle = NULL;
1670 emitter->tag_data.handle_length = 0;
1671 emitter->tag_data.suffix = NULL;
1672 emitter->tag_data.suffix_length = 0;
1673 emitter->scalar_data.value = NULL;
1674 emitter->scalar_data.length = 0;
1675
1676 switch (event->type)
1677 {
1678 case YAML_ALIAS_EVENT:
1679 if (!yaml_emitter_analyze_anchor(emitter,
1680 event->data.alias.anchor, 1))
1681 return 0;
1682 return 1;
1683
1684 case YAML_SCALAR_EVENT:
1685 if (event->data.scalar.anchor) {
1686 if (!yaml_emitter_analyze_anchor(emitter,
1687 event->data.scalar.anchor, 0))
1688 return 0;
1689 }
1690 if (event->data.scalar.tag && (emitter->canonical ||
1691 (!event->data.scalar.plain_implicit
1692 && !event->data.scalar.quoted_implicit))) {
1693 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1694 return 0;
1695 }
1696 if (!yaml_emitter_analyze_scalar(emitter,
1697 event->data.scalar.value, event->data.scalar.length))
1698 return 0;
1699 return 1;
1700
1701 case YAML_SEQUENCE_START_EVENT:
1702 if (event->data.sequence_start.anchor) {
1703 if (!yaml_emitter_analyze_anchor(emitter,
1704 event->data.sequence_start.anchor, 0))
1705 return 0;
1706 }
1707 if (event->data.sequence_start.tag && (emitter->canonical ||
1708 !event->data.sequence_start.implicit)) {
1709 if (!yaml_emitter_analyze_tag(emitter,
1710 event->data.sequence_start.tag))
1711 return 0;
1712 }
1713 return 1;
1714
1715 case YAML_MAPPING_START_EVENT:
1716 if (event->data.mapping_start.anchor) {
1717 if (!yaml_emitter_analyze_anchor(emitter,
1718 event->data.mapping_start.anchor, 0))
1719 return 0;
1720 }
1721 if (event->data.mapping_start.tag && (emitter->canonical ||
1722 !event->data.mapping_start.implicit)) {
1723 if (!yaml_emitter_analyze_tag(emitter,
1724 event->data.mapping_start.tag))
1725 return 0;
1726 }
1727 return 1;
1728
1729 default:
1730 return 1;
1731 }
1732}
1733
1734/*
1735 * Write the BOM character.
1736 */
1737
1738static int
1739yaml_emitter_write_bom(yaml_emitter_t *emitter)
1740{
1741 if (!FLUSH(emitter)) return 0;
1742
1743 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1744 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1745 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1746
1747 return 1;
1748}
1749
1750static int
1751yaml_emitter_write_indent(yaml_emitter_t *emitter)
1752{
1753 int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1754
1755 if (!emitter->indention || emitter->column > indent
1756 || (emitter->column == indent && !emitter->whitespace)) {
1757 if (!PUT_BREAK(emitter)) return 0;
1758 }
1759
1760 while (emitter->column < indent) {
1761 if (!PUT(emitter, ' ')) return 0;
1762 }
1763
1764 emitter->whitespace = 1;
1765 emitter->indention = 1;
1766
1767 return 1;
1768}
1769
1770static int
1771yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1772 char *indicator, int need_whitespace,
1773 int is_whitespace, int is_indention)
1774{
1775 yaml_string_t string = STRING((yaml_char_t *)indicator, strlen(indicator));
1776
1777 if (need_whitespace && !emitter->whitespace) {
1778 if (!PUT(emitter, ' ')) return 0;
1779 }
1780
1781 while (string.pointer != string.end) {
1782 if (!WRITE(emitter, string)) return 0;
1783 }
1784
1785 emitter->whitespace = is_whitespace;
1786 emitter->indention = (emitter->indention && is_indention);
561ca6de 1787 emitter->open_ended = 0;
e35af832
KS
1788
1789 return 1;
1790}
1791
1792static int
1793yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1794 yaml_char_t *value, size_t length)
1795{
1796 yaml_string_t string = STRING(value, length);
1797
1798 while (string.pointer != string.end) {
1799 if (!WRITE(emitter, string)) return 0;
1800 }
1801
1802 emitter->whitespace = 0;
1803 emitter->indention = 0;
1804
1805 return 1;
1806}
1807
1808static int
1809yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
1810 yaml_char_t *value, size_t length)
1811{
1812 yaml_string_t string = STRING(value, length);
1813
784340c2
KS
1814 if (!emitter->whitespace) {
1815 if (!PUT(emitter, ' ')) return 0;
1816 }
1817
e35af832
KS
1818 while (string.pointer != string.end) {
1819 if (!WRITE(emitter, string)) return 0;
1820 }
1821
1822 emitter->whitespace = 0;
1823 emitter->indention = 0;
1824
1825 return 1;
1826}
1827
1828static int
1829yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
784340c2
KS
1830 yaml_char_t *value, size_t length,
1831 int need_whitespace)
e35af832 1832{
784340c2
KS
1833 yaml_string_t string = STRING(value, length);
1834
1835 if (need_whitespace && !emitter->whitespace) {
1836 if (!PUT(emitter, ' ')) return 0;
1837 }
1838
1839 while (string.pointer != string.end) {
1840 if (IS_ALPHA(string)
1841 || CHECK(string, ';') || CHECK(string, '/')
1842 || CHECK(string, '?') || CHECK(string, ':')
1843 || CHECK(string, '@') || CHECK(string, '&')
1844 || CHECK(string, '=') || CHECK(string, '+')
1845 || CHECK(string, '$') || CHECK(string, ',')
1846 || CHECK(string, '_') || CHECK(string, '.')
1847 || CHECK(string, '~') || CHECK(string, '*')
1848 || CHECK(string, '\'') || CHECK(string, '(')
1849 || CHECK(string, ')') || CHECK(string, '[')
1850 || CHECK(string, ']')) {
1851 if (!WRITE(emitter, string)) return 0;
1852 }
1853 else {
1854 int width = WIDTH(string);
1855 unsigned int value;
1856 while (width --) {
1857 value = *(string.pointer++);
1858 if (!PUT(emitter, '%')) return 0;
6d167281
KS
1859 if (!PUT(emitter, (value >> 4)
1860 + ((value >> 4) < 10 ? '0' : 'A' - 10)))
784340c2
KS
1861 return 0;
1862 if (!PUT(emitter, (value & 0x0F)
1863 + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
1864 return 0;
1865 }
1866 }
1867 }
1868
1869 emitter->whitespace = 0;
1870 emitter->indention = 0;
1871
1872 return 1;
e35af832
KS
1873}
1874
1875static int
1876yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1877 yaml_char_t *value, size_t length, int allow_breaks)
1878{
784340c2
KS
1879 yaml_string_t string = STRING(value, length);
1880 int spaces = 0;
1881 int breaks = 0;
1882
1883 if (!emitter->whitespace) {
1884 if (!PUT(emitter, ' ')) return 0;
1885 }
1886
1887 while (string.pointer != string.end)
1888 {
1889 if (IS_SPACE(string))
1890 {
1891 if (allow_breaks && !spaces
1892 && emitter->column > emitter->best_width
1893 && !IS_SPACE_AT(string, 1)) {
1894 if (!yaml_emitter_write_indent(emitter)) return 0;
1895 MOVE(string);
1896 }
1897 else {
1898 if (!WRITE(emitter, string)) return 0;
1899 }
1900 spaces = 1;
1901 }
1902 else if (IS_BREAK(string))
1903 {
1904 if (!breaks && CHECK(string, '\n')) {
1905 if (!PUT_BREAK(emitter)) return 0;
1906 }
1907 if (!WRITE_BREAK(emitter, string)) return 0;
1908 emitter->indention = 1;
1909 breaks = 1;
1910 }
1911 else
1912 {
1913 if (breaks) {
1914 if (!yaml_emitter_write_indent(emitter)) return 0;
1915 }
1916 if (!WRITE(emitter, string)) return 0;
1917 emitter->indention = 0;
1918 spaces = 0;
1919 breaks = 0;
1920 }
1921 }
1922
1923 emitter->whitespace = 0;
1924 emitter->indention = 0;
561ca6de
KS
1925 if (emitter->root_context)
1926 {
1927 emitter->open_ended = 1;
1928 }
784340c2
KS
1929
1930 return 1;
e35af832
KS
1931}
1932
1933static int
1934yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1935 yaml_char_t *value, size_t length, int allow_breaks)
1936{
784340c2
KS
1937 yaml_string_t string = STRING(value, length);
1938 int spaces = 0;
1939 int breaks = 0;
1940
1941 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
1942 return 0;
1943
1944 while (string.pointer != string.end)
1945 {
1946 if (IS_SPACE(string))
1947 {
1948 if (allow_breaks && !spaces
1949 && emitter->column > emitter->best_width
1950 && string.pointer != string.start
1951 && string.pointer != string.end - 1
1952 && !IS_SPACE_AT(string, 1)) {
1953 if (!yaml_emitter_write_indent(emitter)) return 0;
1954 MOVE(string);
1955 }
1956 else {
1957 if (!WRITE(emitter, string)) return 0;
1958 }
1959 spaces = 1;
1960 }
1961 else if (IS_BREAK(string))
1962 {
1963 if (!breaks && CHECK(string, '\n')) {
1964 if (!PUT_BREAK(emitter)) return 0;
1965 }
1966 if (!WRITE_BREAK(emitter, string)) return 0;
1967 emitter->indention = 1;
1968 breaks = 1;
1969 }
1970 else
1971 {
1972 if (breaks) {
1973 if (!yaml_emitter_write_indent(emitter)) return 0;
1974 }
1975 if (CHECK(string, '\'')) {
1976 if (!PUT(emitter, '\'')) return 0;
1977 }
1978 if (!WRITE(emitter, string)) return 0;
1979 emitter->indention = 0;
1980 spaces = 0;
1981 breaks = 0;
1982 }
1983 }
1984
1985 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
1986 return 0;
1987
1988 emitter->whitespace = 0;
1989 emitter->indention = 0;
1990
1991 return 1;
e35af832
KS
1992}
1993
1994static int
1995yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
1996 yaml_char_t *value, size_t length, int allow_breaks)
1997{
784340c2
KS
1998 yaml_string_t string = STRING(value, length);
1999 int spaces = 0;
2000
2001 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
2002 return 0;
2003
2004 while (string.pointer != string.end)
2005 {
2006 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
2007 || IS_BOM(string) || IS_BREAK(string)
2008 || CHECK(string, '"') || CHECK(string, '\\'))
2009 {
2010 unsigned char octet;
2011 unsigned int width;
2012 unsigned int value;
071329ae 2013 int k;
784340c2
KS
2014
2015 octet = string.pointer[0];
2016 width = (octet & 0x80) == 0x00 ? 1 :
2017 (octet & 0xE0) == 0xC0 ? 2 :
2018 (octet & 0xF0) == 0xE0 ? 3 :
2019 (octet & 0xF8) == 0xF0 ? 4 : 0;
2020 value = (octet & 0x80) == 0x00 ? octet & 0x7F :
2021 (octet & 0xE0) == 0xC0 ? octet & 0x1F :
2022 (octet & 0xF0) == 0xE0 ? octet & 0x0F :
2023 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
071329ae 2024 for (k = 1; k < (int)width; k ++) {
784340c2
KS
2025 octet = string.pointer[k];
2026 value = (value << 6) + (octet & 0x3F);
2027 }
2028 string.pointer += width;
2029
2030 if (!PUT(emitter, '\\')) return 0;
2031
2032 switch (value)
2033 {
2034 case 0x00:
2035 if (!PUT(emitter, '0')) return 0;
2036 break;
2037
2038 case 0x07:
2039 if (!PUT(emitter, 'a')) return 0;
2040 break;
2041
2042 case 0x08:
2043 if (!PUT(emitter, 'b')) return 0;
2044 break;
2045
2046 case 0x09:
2047 if (!PUT(emitter, 't')) return 0;
2048 break;
2049
2050 case 0x0A:
2051 if (!PUT(emitter, 'n')) return 0;
2052 break;
2053
2054 case 0x0B:
2055 if (!PUT(emitter, 'v')) return 0;
2056 break;
2057
2058 case 0x0C:
2059 if (!PUT(emitter, 'f')) return 0;
2060 break;
2061
2062 case 0x0D:
2063 if (!PUT(emitter, 'r')) return 0;
2064 break;
2065
2066 case 0x1B:
2067 if (!PUT(emitter, 'e')) return 0;
2068 break;
2069
2070 case 0x22:
2071 if (!PUT(emitter, '\"')) return 0;
2072 break;
2073
2074 case 0x5C:
2075 if (!PUT(emitter, '\\')) return 0;
2076 break;
2077
2078 case 0x85:
2079 if (!PUT(emitter, 'N')) return 0;
2080 break;
2081
2082 case 0xA0:
2083 if (!PUT(emitter, '_')) return 0;
2084 break;
2085
2086 case 0x2028:
2087 if (!PUT(emitter, 'L')) return 0;
2088 break;
2089
2090 case 0x2029:
2091 if (!PUT(emitter, 'P')) return 0;
2092 break;
2093
2094 default:
2095 if (value <= 0xFF) {
2096 if (!PUT(emitter, 'x')) return 0;
2097 width = 2;
2098 }
2099 else if (value <= 0xFFFF) {
2100 if (!PUT(emitter, 'u')) return 0;
2101 width = 4;
2102 }
2103 else {
2104 if (!PUT(emitter, 'U')) return 0;
2105 width = 8;
2106 }
6d167281
KS
2107 for (k = (width-1)*4; k >= 0; k -= 4) {
2108 int digit = (value >> k) & 0x0F;
2109 if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
2110 return 0;
784340c2
KS
2111 }
2112 }
2113 spaces = 0;
2114 }
2115 else if (IS_SPACE(string))
2116 {
2117 if (allow_breaks && !spaces
2118 && emitter->column > emitter->best_width
2119 && string.pointer != string.start
2120 && string.pointer != string.end - 1) {
2121 if (!yaml_emitter_write_indent(emitter)) return 0;
2122 if (IS_SPACE_AT(string, 1)) {
2123 if (!PUT(emitter, '\\')) return 0;
2124 }
2125 MOVE(string);
2126 }
2127 else {
2128 if (!WRITE(emitter, string)) return 0;
2129 }
2130 spaces = 1;
2131 }
2132 else
2133 {
2134 if (!WRITE(emitter, string)) return 0;
2135 spaces = 0;
2136 }
2137 }
2138
2139 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
2140 return 0;
2141
2142 emitter->whitespace = 0;
2143 emitter->indention = 0;
2144
2145 return 1;
2146}
2147
2148static int
40cc5d15 2149yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
784340c2
KS
2150 yaml_string_t string)
2151{
40cc5d15
KS
2152 char indent_hint[2];
2153 char *chomp_hint = NULL;
2154
2155 if (IS_SPACE(string) || IS_BREAK(string))
2156 {
2157 indent_hint[0] = '0' + (char)emitter->best_indent;
2158 indent_hint[1] = '\0';
2159 if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0))
2160 return 0;
2161 }
2162
561ca6de
KS
2163 emitter->open_ended = 0;
2164
784340c2
KS
2165 string.pointer = string.end;
2166 if (string.start == string.pointer)
40cc5d15
KS
2167 {
2168 chomp_hint = "-";
2169 }
2170 else
2171 {
2172 do {
2173 string.pointer --;
2174 } while ((*string.pointer & 0xC0) == 0x80);
2175 if (!IS_BREAK(string))
2176 {
2177 chomp_hint = "-";
2178 }
2179 else if (string.start == string.pointer)
2180 {
2181 chomp_hint = "+";
561ca6de 2182 emitter->open_ended = 1;
40cc5d15
KS
2183 }
2184 else
2185 {
2186 do {
2187 string.pointer --;
2188 } while ((*string.pointer & 0xC0) == 0x80);
2189 if (IS_BREAK(string))
2190 {
2191 chomp_hint = "+";
561ca6de 2192 emitter->open_ended = 1;
40cc5d15
KS
2193 }
2194 }
2195 }
2196
2197 if (chomp_hint)
2198 {
2199 if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0))
2200 return 0;
2201 }
2202
2203 return 1;
e35af832
KS
2204}
2205
2206static int
2207yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
2208 yaml_char_t *value, size_t length)
2209{
784340c2 2210 yaml_string_t string = STRING(value, length);
40cc5d15 2211 int breaks = 1;
784340c2 2212
40cc5d15 2213 if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0))
784340c2 2214 return 0;
40cc5d15 2215 if (!yaml_emitter_write_block_scalar_hints(emitter, string))
784340c2 2216 return 0;
40cc5d15
KS
2217 if (!PUT_BREAK(emitter)) return 0;
2218 emitter->indention = 1;
2219 emitter->whitespace = 1;
784340c2
KS
2220
2221 while (string.pointer != string.end)
2222 {
2223 if (IS_BREAK(string))
2224 {
2225 if (!WRITE_BREAK(emitter, string)) return 0;
2226 emitter->indention = 1;
2227 breaks = 1;
2228 }
2229 else
2230 {
2231 if (breaks) {
2232 if (!yaml_emitter_write_indent(emitter)) return 0;
2233 }
2234 if (!WRITE(emitter, string)) return 0;
2235 emitter->indention = 0;
2236 breaks = 0;
2237 }
2238 }
2239
784340c2 2240 return 1;
e35af832
KS
2241}
2242
2243static int
2244yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
2245 yaml_char_t *value, size_t length)
2246{
784340c2 2247 yaml_string_t string = STRING(value, length);
6d167281 2248 int breaks = 1;
40cc5d15 2249 int leading_spaces = 1;
784340c2 2250
40cc5d15 2251 if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0))
784340c2 2252 return 0;
40cc5d15 2253 if (!yaml_emitter_write_block_scalar_hints(emitter, string))
784340c2 2254 return 0;
40cc5d15
KS
2255 if (!PUT_BREAK(emitter)) return 0;
2256 emitter->indention = 1;
2257 emitter->whitespace = 1;
784340c2
KS
2258
2259 while (string.pointer != string.end)
2260 {
2261 if (IS_BREAK(string))
2262 {
2263 if (!breaks && !leading_spaces && CHECK(string, '\n')) {
2264 int k = 0;
2265 while (IS_BREAK_AT(string, k)) {
2266 k += WIDTH_AT(string, k);
2267 }
561ca6de 2268 if (!IS_BLANKZ_AT(string, k)) {
784340c2
KS
2269 if (!PUT_BREAK(emitter)) return 0;
2270 }
2271 }
2272 if (!WRITE_BREAK(emitter, string)) return 0;
2273 emitter->indention = 1;
2274 breaks = 1;
2275 }
2276 else
2277 {
2278 if (breaks) {
2279 if (!yaml_emitter_write_indent(emitter)) return 0;
2280 leading_spaces = IS_BLANK(string);
2281 }
2282 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
2283 && emitter->column > emitter->best_width) {
2284 if (!yaml_emitter_write_indent(emitter)) return 0;
2285 MOVE(string);
2286 }
2287 else {
2288 if (!WRITE(emitter, string)) return 0;
2289 }
2290 emitter->indention = 0;
2291 breaks = 0;
2292 }
2293 }
2294
784340c2 2295 return 1;
e35af832
KS
2296}
2297
This page took 0.416675 seconds and 5 git commands to generate.