Skip to content

Commit 4e5cea6

Browse files
committed
Allow emitting 1.2 directive
Before `1.1` was hardcoded in the emitter. * Also add --directive option to run-emitter-test-suite Allows to easily test how output looks like if %YAML directives are output.
1 parent 47bf3f0 commit 4e5cea6

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/emitter.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,14 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
603603
implicit = 0;
604604
if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
605605
return 0;
606-
if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
607-
return 0;
606+
if (event->data.document_start.version_directive->minor == 1) {
607+
if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
608+
return 0;
609+
}
610+
else {
611+
if (!yaml_emitter_write_indicator(emitter, "1.2", 1, 0, 0))
612+
return 0;
613+
}
608614
if (!yaml_emitter_write_indent(emitter))
609615
return 0;
610616
}

tests/run-emitter-test-suite.c

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,58 @@
33
#include <stdlib.h>
44
#include <stdio.h>
55
#include <assert.h>
6+
#include "../src/yaml_private.h"
67

78
int get_line(FILE * input, char *line);
89
char *get_anchor(char sigil, char *line, char *anchor);
910
char *get_tag(char *line, char *tag);
1011
void get_value(char *line, char *value, int *style);
12+
int usage(int ret);
1113

1214
int main(int argc, char *argv[])
1315
{
1416
FILE *input;
1517
yaml_emitter_t emitter;
1618
yaml_event_t event;
19+
yaml_version_directive_t *version_directive = NULL;
1720

1821
int canonical = 0;
1922
int unicode = 0;
2023
char line[1024];
24+
int foundfile = 0;
25+
int i = 0;
26+
int minor = 0;
27+
28+
for (i = 1; i < argc; i++) {
29+
if (strncmp(argv[i], "--help", 6) == 0)
30+
return usage(0);
31+
if (strncmp(argv[i], "-h", 2) == 0)
32+
return usage(0);
33+
if (strncmp(argv[i], "--directive", 11) == 0) {
34+
if (i+1 == argc)
35+
return usage(1);
36+
i++;
37+
if (strncmp(argv[i], "1.1", 3) == 0)
38+
minor = 1;
39+
else if (strncmp(argv[i], "1.2", 3) == 0)
40+
minor = 2;
41+
else
42+
return usage(1);
43+
}
44+
else if (!foundfile) {
45+
input = fopen(argv[i], "rb");
46+
foundfile = 1;
47+
}
2148

22-
if (argc == 1)
23-
input = stdin;
24-
else if (argc == 2)
25-
input = fopen(argv[1], "rb");
26-
else {
27-
fprintf(stderr, "Usage: libyaml-emitter [<input-file>]\n");
28-
return 1;
2949
}
50+
if (minor) {
51+
version_directive = YAML_MALLOC_STATIC(yaml_version_directive_t);
52+
version_directive->major = 1;
53+
version_directive->minor = minor;
54+
}
55+
if (!foundfile)
56+
input = stdin;
57+
3058
assert(input);
3159

3260
if (!yaml_emitter_initialize(&emitter)) {
@@ -37,6 +65,7 @@ int main(int argc, char *argv[])
3765
yaml_emitter_set_canonical(&emitter, canonical);
3866
yaml_emitter_set_unicode(&emitter, unicode);
3967

68+
4069
while (get_line(input, line)) {
4170
int ok;
4271
char anchor[256];
@@ -51,7 +80,7 @@ int main(int argc, char *argv[])
5180
}
5281
else if (strncmp(line, "+DOC", 4) == 0) {
5382
implicit = strncmp(line, "+DOC ---", 8) != 0;
54-
ok = yaml_document_start_event_initialize(&event, NULL, NULL, NULL, implicit);
83+
ok = yaml_document_start_event_initialize(&event, version_directive, NULL, NULL, implicit);
5584
}
5685
else if (strncmp(line, "-DOC", 4) == 0) {
5786
implicit = strncmp(line, "-DOC ...", 8) != 0;
@@ -229,3 +258,8 @@ void get_value(char *line, char *value, int *style)
229258
}
230259
value[i] = '\0';
231260
}
261+
262+
int usage(int ret) {
263+
fprintf(stderr, "Usage: run-emitter-test-suite [--directive (1.1|1.2)] [<input-file>]\n");
264+
return ret;
265+
}

0 commit comments

Comments
 (0)