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