Path: blob/main/contrib/libyaml/tests/run-emitter-test-suite.c
39507 views
#include <yaml.h>12#include <stdlib.h>3#include <stdio.h>4#include <assert.h>5#include "../src/yaml_private.h"67int get_line(FILE * input, char *line);8char *get_anchor(char sigil, char *line, char *anchor);9char *get_tag(char *line, char *tag);10void get_value(char *line, char *value, int *style);11int usage(int ret);1213int main(int argc, char *argv[])14{15FILE *input;16yaml_emitter_t emitter;17yaml_event_t event;18yaml_version_directive_t *version_directive = NULL;1920int canonical = 0;21int unicode = 0;22char line[1024];23int foundfile = 0;24int i = 0;25int minor = 0;26int flow = -1; /** default no flow style collections */2728for (i = 1; i < argc; i++) {29if (strncmp(argv[i], "--help", 6) == 0)30return usage(0);31if (strncmp(argv[i], "-h", 2) == 0)32return usage(0);33if (strncmp(argv[i], "--flow", 6) == 0) {34if (i+1 == argc)35return usage(1);36i++;37if (strncmp(argv[i], "keep", 4) == 0)38flow = 0;39else if (strncmp(argv[i], "on", 2) == 0)40flow = 1;41else if (strncmp(argv[i], "off", 3) == 0)42flow = -1;43else44return usage(1);45}46else if (strncmp(argv[i], "--directive", 11) == 0) {47if (i+1 == argc)48return usage(1);49i++;50if (strncmp(argv[i], "1.1", 3) == 0)51minor = 1;52else if (strncmp(argv[i], "1.2", 3) == 0)53minor = 2;54else55return usage(1);56}57else if (!foundfile) {58input = fopen(argv[i], "rb");59foundfile = 1;60}6162}63if (minor) {64version_directive = YAML_MALLOC_STATIC(yaml_version_directive_t);65version_directive->major = 1;66version_directive->minor = minor;67}68if (!foundfile)69input = stdin;7071assert(input);7273if (!yaml_emitter_initialize(&emitter)) {74fprintf(stderr, "Could not initalize the emitter object\n");75return 1;76}77yaml_emitter_set_output_file(&emitter, stdout);78yaml_emitter_set_canonical(&emitter, canonical);79yaml_emitter_set_unicode(&emitter, unicode);808182while (get_line(input, line)) {83int ok;84char anchor[256];85char tag[256];86int implicit;87int style;8889if (strncmp(line, "+STR", 4) == 0) {90ok = yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);91}92else if (strncmp(line, "-STR", 4) == 0) {93ok = yaml_stream_end_event_initialize(&event);94}95else if (strncmp(line, "+DOC", 4) == 0) {96implicit = strncmp(line+4, " ---", 4) != 0;97ok = yaml_document_start_event_initialize(&event, version_directive, NULL, NULL, implicit);98}99else if (strncmp(line, "-DOC", 4) == 0) {100implicit = strncmp(line+4, " ...", 4) != 0;101ok = yaml_document_end_event_initialize(&event, implicit);102}103else if (strncmp(line, "+MAP", 4) == 0) {104style = YAML_BLOCK_MAPPING_STYLE;105if (flow == 1)106style = YAML_FLOW_MAPPING_STYLE;107else if (flow == 0 && strncmp(line+5, "{}", 2) == 0)108style = YAML_FLOW_MAPPING_STYLE;109ok = yaml_mapping_start_event_initialize(&event, (yaml_char_t *)110get_anchor('&', line, anchor), (yaml_char_t *)111get_tag(line, tag), 0, style);112}113else if (strncmp(line, "-MAP", 4) == 0) {114ok = yaml_mapping_end_event_initialize(&event);115}116else if (strncmp(line, "+SEQ", 4) == 0) {117style = YAML_BLOCK_SEQUENCE_STYLE;118if (flow == 1)119style = YAML_FLOW_MAPPING_STYLE;120else if (flow == 0 && strncmp(line+5, "[]", 2) == 0)121style = YAML_FLOW_SEQUENCE_STYLE;122ok = yaml_sequence_start_event_initialize(&event, (yaml_char_t *)123get_anchor('&', line, anchor), (yaml_char_t *)124get_tag(line, tag), 0, style);125}126else if (strncmp(line, "-SEQ", 4) == 0) {127ok = yaml_sequence_end_event_initialize(&event);128}129else if (strncmp(line, "=VAL", 4) == 0) {130char value[1024];131int style;132133get_value(line, value, &style);134implicit = (get_tag(line, tag) == NULL);135136ok = yaml_scalar_event_initialize(&event, (yaml_char_t *)137get_anchor('&', line, anchor), (yaml_char_t *) get_tag(line, tag), (yaml_char_t *) value, -1, implicit, implicit, style);138}139else if (strncmp(line, "=ALI", 4) == 0) {140ok = yaml_alias_event_initialize(&event, (yaml_char_t *)141get_anchor('*', line, anchor)142);143}144else {145fprintf(stderr, "Unknown event: '%s'\n", line);146fflush(stdout);147return 1;148}149150if (!ok)151goto event_error;152if (!yaml_emitter_emit(&emitter, &event))153goto emitter_error;154}155156assert(!fclose(input));157yaml_emitter_delete(&emitter);158fflush(stdout);159160return 0;161162emitter_error:163switch (emitter.error) {164case YAML_MEMORY_ERROR:165fprintf(stderr, "Memory error: Not enough memory for emitting\n");166break;167case YAML_WRITER_ERROR:168fprintf(stderr, "Writer error: %s\n", emitter.problem);169break;170case YAML_EMITTER_ERROR:171fprintf(stderr, "Emitter error: %s\n", emitter.problem);172break;173default:174/*175* Couldn't happen.176*/177fprintf(stderr, "Internal error\n");178break;179}180yaml_emitter_delete(&emitter);181return 1;182183event_error:184fprintf(stderr, "Memory error: Not enough memory for creating an event\n");185yaml_emitter_delete(&emitter);186return 1;187}188189int get_line(FILE * input, char *line)190{191char *newline;192193if (!fgets(line, 1024 - 1, input))194return 0;195196if ((newline = strchr(line, '\n')) == NULL) {197fprintf(stderr, "Line too long: '%s'", line);198abort();199}200*newline = '\0';201202return 1;203}204205char *get_anchor(char sigil, char *line, char *anchor)206{207char *start;208char *end;209if ((start = strchr(line, sigil)) == NULL)210return NULL;211start++;212if ((end = strchr(start, ' ')) == NULL)213end = line + strlen(line);214memcpy(anchor, start, end - start);215anchor[end - start] = '\0';216return anchor;217}218219char *get_tag(char *line, char *tag)220{221char *start;222char *end;223if ((start = strchr(line, '<')) == NULL)224return NULL;225if ((end = strchr(line, '>')) == NULL)226return NULL;227memcpy(tag, start + 1, end - start - 1);228tag[end - start - 1] = '\0';229return tag;230}231232void get_value(char *line, char *value, int *style)233{234int i = 0;235char *c;236char *start = NULL;237char *end = line + strlen(line);238239for (c = line + 4; c < end; c++) {240if (*c == ' ') {241start = c + 1;242if (*start == ':')243*style = YAML_PLAIN_SCALAR_STYLE;244else if (*start == '\'')245*style = YAML_SINGLE_QUOTED_SCALAR_STYLE;246else if (*start == '"')247*style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;248else if (*start == '|')249*style = YAML_LITERAL_SCALAR_STYLE;250else if (*start == '>')251*style = YAML_FOLDED_SCALAR_STYLE;252else {253start = NULL;254continue;255}256start++;257break;258}259}260if (!start)261abort();262263for (c = start; c < end; c++) {264if (*c == '\\') {265if (*++c == '\\')266value[i++] = '\\';267else if (*c == '0')268value[i++] = '\0';269else if (*c == 'b')270value[i++] = '\b';271else if (*c == 'n')272value[i++] = '\n';273else if (*c == 'r')274value[i++] = '\r';275else if (*c == 't')276value[i++] = '\t';277else278abort();279}280else281value[i++] = *c;282}283value[i] = '\0';284}285286int usage(int ret) {287fprintf(stderr, "Usage: run-emitter-test-suite [--directive (1.1|1.2)] [--flow (on|off|keep)] [<input-file>]\n");288return ret;289}290291292