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