static void conf_yaml_deinit(struct conf *cf) { if (cf->valid_parser) { yaml_parser_delete(&cf->parser); cf->valid_parser = 0; } }
static void dealloc(void * ptr) { yaml_parser_t * parser; parser = (yaml_parser_t *)ptr; yaml_parser_delete(parser); xfree(parser); }
int main ( int argc, char *argv []) { FILE *fh = fopen("/etc/cpanel/ea4/php.conf", "r"); if( !fh ) fh = fopen("/etc/apache2/conf.d/php.conf.yaml", "r"); yaml_parser_t parser; yaml_event_t event; /* New variable */ /* Initialize parser */ if(!yaml_parser_initialize(&parser)) fputs("Failed to initialize parser!\n", stderr); if(fh == NULL) fputs("Failed to open file!\n", stderr); /* Set input file */ yaml_parser_set_input_file(&parser, fh); /* START new code */ do { if (!yaml_parser_parse(&parser, &event)) { printf("Parser error %d\n", parser.error); exit(EXIT_FAILURE); } switch(event.type) { case YAML_NO_EVENT: puts("No event!"); break; /* Stream start/end */ case YAML_STREAM_START_EVENT: puts("STREAM START"); break; case YAML_STREAM_END_EVENT: puts("STREAM END"); break; /* Block delimeters */ case YAML_DOCUMENT_START_EVENT: puts("<b>Start Document</b>"); break; case YAML_DOCUMENT_END_EVENT: puts("<b>End Document</b>"); break; case YAML_SEQUENCE_START_EVENT: puts("<b>Start Sequence</b>"); break; case YAML_SEQUENCE_END_EVENT: puts("<b>End Sequence</b>"); break; case YAML_MAPPING_START_EVENT: puts("<b>Start Mapping</b>"); break; case YAML_MAPPING_END_EVENT: puts("<b>End Mapping</b>"); break; /* Data */ case YAML_ALIAS_EVENT: printf("Got alias (anchor %s)\n", event.data.alias.anchor); break; case YAML_SCALAR_EVENT: printf("Got scalar (value %s)\n", event.data.scalar.value); break; } if(event.type != YAML_STREAM_END_EVENT) yaml_event_delete(&event); } while (event.type != YAML_STREAM_END_EVENT); yaml_event_delete(&event); /* END new code */ /* Cleanup */ yaml_parser_delete(&parser); fclose(fh); return 0; }
static void rb_yaml_parser_finalize(void *rcv, SEL sel) { // TODO: is this reentrant? rb_yaml_parser_t *rbparser = RYAMLParser(rcv); yaml_parser_delete(&rbparser->parser); if (rb_yaml_parser_finalize_super != NULL) { ((void(*)(void *, SEL))rb_yaml_parser_finalize_super)(rcv, sel); } }
static int parser_gc (lua_State *L) { lyaml_parser *parser = (lyaml_parser *) lua_touserdata (L, 1); if (parser) { parser_delete_event (parser); yaml_parser_delete (&parser->parser); } return 0; }
static yoml_t *parse(const char *fn, const char *s) { yaml_parser_t parser; yoml_t *doc; yaml_parser_initialize(&parser); yaml_parser_set_input_string(&parser, (yaml_char_t*)s, strlen(s)); doc = yoml_parse_document(&parser, NULL, NULL, fn); yaml_parser_delete(&parser); return doc; }
int main(int argc, char *argv[]) { int number; if (argc < 2) { printf("Usage: %s file1.yaml ...\n", argv[0]); return 0; } for (number = 1; number < argc; number ++) { FILE *file; yaml_parser_t parser; yaml_document_t document; int done = 0; int count = 0; int error = 0; printf("[%d] Loading '%s': ", number, argv[number]); fflush(stdout); file = fopen(argv[number], "rb"); assert(file); assert(yaml_parser_initialize(&parser)); yaml_parser_set_input_file(&parser, file); while (!done) { if (!yaml_parser_load(&parser, &document)) { error = 1; break; } done = (!yaml_document_get_root_node(&document)); yaml_document_delete(&document); if (!done) count ++; } yaml_parser_delete(&parser); assert(!fclose(file)); printf("%s (%d documents)\n", (error ? "FAILURE" : "SUCCESS"), count); } return 0; }
int main (int argc, char* argv[]) { int number; if (argc < 2) { printf ("Usage: %s file1.yaml ...\n", argv[0]); return 0; } for (number = 1; number < argc; number ++) { FILE* file; yaml_parser_t parser; yaml_token_t token; int done = 0; int count = 0; int error = 0; printf ("[%d] Scanning '%s': ", number, argv[number]); fflush (stdout); file = fopen (argv[number], "rb"); assert (file); assert (yaml_parser_initialize (&parser)); yaml_parser_set_input_file (&parser, file); while (!done) { if (!yaml_parser_scan (&parser, &token)) { error = 1; break; } done = (token.type == YAML_STREAM_END_TOKEN); yaml_token_delete (&token); count ++; } yaml_parser_delete (&parser); assert (!fclose (file)); printf ("%s (%d tokens)\n", (error ? "FAILURE" : "SUCCESS"), count); } return 0; }
int check_boms (void) { yaml_parser_t parser; int failed = 0; int k; printf ("checking boms...\n"); for (k = 0; boms[k].test; k++) { char* title = boms[k].title; int check = boms[k].result; int result; char* start = boms[k].test; char* end = start; while (*end != '!') { end++; } printf ("\t%s: ", title); yaml_parser_initialize (&parser); yaml_parser_set_input_string (&parser, (unsigned char*) start, end - start); result = yaml_parser_update_buffer (&parser, end - start); if (!result) { printf ("- (reader error: %s at %d)\n", parser.problem, parser.problem_offset); failed++; } else { if (parser.unread != check) { printf ("- (length=%d while expected length=%d)\n", parser.unread, check); failed++; } else if (memcmp (parser.buffer.start, bom_original, check) != 0) { printf ("- (value '%s' does not equal to the original value '%s')\n", parser.buffer.start, bom_original); failed++; } else { printf ("+\n"); } } yaml_parser_delete (&parser); } printf ("checking boms: %d fail(s)\n", failed); return failed; }
int check_utf8_sequences(void) { yaml_parser_t parser; int failed = 0; int k; printf("checking utf-8 sequences...\n"); for (k = 0; utf8_sequences[k].test; k++) { char *title = utf8_sequences[k].title; int check = utf8_sequences[k].result; int result; char *start = utf8_sequences[k].test; char *end = start; printf("\t%s:\n", title); while(1) { while (*end != '|' && *end != '!') end++; yaml_parser_initialize(&parser); yaml_parser_set_input_string(&parser, (unsigned char *)start, end-start); result = yaml_parser_update_buffer(&parser, end-start); if (result != check) { printf("\t\t- "); failed ++; } else { printf("\t\t+ "); } if (!parser.error) { printf("(no error)\n"); } else if (parser.error == YAML_READER_ERROR) { if (parser.problem_value != -1) { printf("(reader error: %s: #%X at %d)\n", parser.problem, parser.problem_value, parser.problem_offset); } else { printf("(reader error: %s at %d)\n", parser.problem, parser.problem_offset); } } if (*end == '!') break; start = ++end; yaml_parser_delete(&parser); }; printf("\n"); } printf("checking utf-8 sequences: %d fail(s)\n", failed); return failed; }
bool YamlDocument::load(char *fname) { FILE *fh = fopen(fname, "rb"); if(fh == NULL){ std::cerr << "Fail to open file: " << fname << std::endl; return false; } if(!yaml_parser_initialize(&parser)){ std::cerr << "Fail to initialize parser." << std::endl; fclose(fh); return false; } yaml_parser_set_input_file(&parser, fh); do{ yaml_parser_parse(&parser, &event); switch(event.type){ case YAML_STREAM_START_EVENT: break; case YAML_STREAM_END_EVENT: break; case YAML_DOCUMENT_START_EVENT: root = ParseYamlDoc(); break; case YAML_DOCUMENT_END_EVENT: break; default: std::cerr << "YAML Format error." << std::endl; break; } if(event.type != YAML_STREAM_END_EVENT){ yaml_event_delete(&event); } }while(event.type != YAML_STREAM_END_EVENT); yaml_event_delete(&event); yaml_parser_delete(&parser); fclose(fh); return (root != NULL); }
void parse_file(char * fileData, int length) { // start yaml parser yaml_parser_t parser; yaml_document_t document; yaml_parser_initialize(&parser); const unsigned char * input = reinterpret_cast<const unsigned char *>(fileData); yaml_parser_set_input_string(&parser, input, length); yaml_parser_load(&parser, &document); mustache_spec_parse_document(&document); yaml_document_delete(&document); yaml_parser_delete(&parser); }
/*C PetscOptionsInsertFileYAML - Insert a YAML-formatted file in the option database Collective on MPI_Comm Input Parameter: + comm - the processes that will share the options (usually PETSC_COMM_WORLD) . file - name of file - require - if PETSC_TRUE will generate an error if the file does not exist Only a small subset of the YAML standard is implemented. Sequences and alias are NOT supported. The algorithm recursively parses the yaml file, pushing and popping prefixes and inserting key + values pairs using PetscOptionsInsertString. Inspired by http://stackoverflow.com/a/621451 Level: developer .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(), PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(), PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), PetscOptionsFList(), PetscOptionsEList(), PetscOptionsInsertFile() C*/ extern PetscErrorCode PetscOptionsInsertFileYAML(MPI_Comm comm,const char file[],PetscBool require) { PetscErrorCode ierr; PetscMPIInt rank; char fname[PETSC_MAX_PATH_LEN]; unsigned char *optionsStr; int yamlLength; yaml_parser_t parser; int lvl=0; FILE *source; PetscInt offset; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscFixFilename(file,fname);CHKERRQ(ierr); source = fopen(fname,"r"); if (source) { fseek(source,0,SEEK_END); yamlLength = ftell(source); fseek(source,0,SEEK_SET); ierr = PetscMalloc1(yamlLength+1,&optionsStr);CHKERRQ(ierr); /* Read the content of the YAML file one char at a time*/ for (offset = 0; offset < yamlLength; offset++) { fread(&(optionsStr[offset]), sizeof(unsigned char),1,source); } fclose(source); optionsStr[yamlLength] = '\0'; } else if (require) { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open YAML option file %s\n",fname); } ierr = MPI_Bcast(&yamlLength,1,MPI_INT,0,comm);CHKERRQ(ierr); ierr = MPI_Bcast(optionsStr,yamlLength+1,MPI_UNSIGNED_CHAR,0,comm);CHKERRQ(ierr); } else { ierr = MPI_Bcast(&yamlLength,1,MPI_INT,0,comm);CHKERRQ(ierr); ierr = PetscMalloc1(yamlLength+1,&optionsStr);CHKERRQ(ierr); ierr = MPI_Bcast(optionsStr,yamlLength+1,MPI_UNSIGNED_CHAR,0,comm);CHKERRQ(ierr); } yaml_parser_initialize(&parser); yaml_parser_set_input_string(&parser,optionsStr,(size_t) yamlLength); ierr = PetscParseLayerYAML(&parser,&lvl); yaml_parser_delete(&parser); ierr = PetscFree(optionsStr);CHKERRQ(ierr); PetscFunctionReturn(0); }
void tap_yaml_write(const char *yaml, va_list vl) { yaml_parser_t parser; yaml_document_t document; yaml_emitter_t emitter; char buffer[8192]; size_t length; if (yaml == NULL) { return; } length = vsnprintf(buffer, 8192, yaml, vl); yaml_parser_initialize(&parser); yaml_parser_set_input_string(&parser, (unsigned char*) buffer, length); if (yaml_parser_load(&parser, &document)) { document.start_implicit = 0; document.end_implicit = 0; tap_yaml_document_set_block_style(&document); yaml_emitter_initialize(&emitter); yaml_emitter_set_output(&emitter, tap_yaml_write_handler, NULL); yaml_emitter_set_indent(&emitter, 2); // Initial indent tap_puts(" "); yaml_emitter_open(&emitter); yaml_emitter_dump(&emitter, &document); yaml_emitter_close(&emitter); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); yaml_document_delete(&document); } else { fprintf(stderr, "Error: %s %s at offset %zu\n", parser.problem, parser.context, parser.problem_mark.index); } }
static ERL_NIF_TERM decode(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifBinary input; ERL_NIF_TERM result; unsigned int flags; yaml_parser_t parser; if (argc == 2) { if (enif_inspect_iolist_as_binary(env, argv[0], &input) && enif_get_uint(env, argv[1], &flags)) { yaml_parser_initialize(&parser); result = parse(env, &parser, flags, input.data, input.size); yaml_parser_delete(&parser); return result; } } return enif_make_badarg(env); }
int main(void) { FILE *fh = fopen("c.yaml", "r"); yaml_parser_t parser; /* Initialize parser */ if(!yaml_parser_initialize(&parser)) fputs("Failed to initialize parser!\n", stderr); if(fh == NULL) fputs("Failed to open file!\n", stderr); /* Set input file */ yaml_parser_set_input_file(&parser, fh); /* CODE HERE */ /* Cleanup */ yaml_parser_delete(&parser); fclose(fh); return 0; }
int load_documents(char *config_file_path) { int rv = 0; FILE *config_file = NULL; yaml_parser_t parser; yaml_document_t document; config_file = fopen(config_file_path, "r"); if (config_file == NULL) { printf("ERROR: failed to open file %s\n", config_file_path); return ERR_FILE_OPEN_FAIL; } if (!yaml_parser_initialize(&parser)) { printf("ERROR: failed to initialize YAML parser\n"); return ERR_YAML_INIT_FAIL; } yaml_parser_set_input_file(&parser, config_file); int done = 0; while (!done) { if (!yaml_parser_load(&parser, &document)) { printf("ERROR: failed to load document\n"); return ERR_DOC_LOAD_FAIL; } yaml_node_t *root_node = yaml_document_get_root_node(&document); if (root_node == NULL) { done = 1; } else { print_document(&document, root_node); } yaml_document_delete(&document); } yaml_parser_delete(&parser); fclose(config_file); return rv; }
bool DataLoader::load(const char* name, IObject* receiver) { bool r = true; clearCurrentName(); yaml_parser_initialize(&parser); FILE *input = fopen(name, "rb"); yaml_parser_set_input_file(&parser, input); int done = 0; while (!done) { if (!yaml_parser_parse(&parser, &event)) { r = false; } if (!notify(receiver)) { r = false; } if (!r) break; done = (event.type == YAML_STREAM_END_EVENT); yaml_event_delete(&event); } yaml_parser_delete(&parser); fclose(input); return r; }
yoml_t *load_config(const char *fn) { FILE *fp; yaml_parser_t parser; yoml_t *yoml; if ((fp = fopen(fn, "rb")) == NULL) { fprintf(stderr, "could not open configuration file:%s:%s\n", fn, strerror(errno)); return NULL; } yaml_parser_initialize(&parser); yaml_parser_set_input_file(&parser, fp); yoml = yoml_parse_document(&parser, NULL); if (yoml == NULL) fprintf(stderr, "failed to parse configuration file:%s:%s\n", fn, parser.problem); yaml_parser_delete(&parser); return yoml; }
mrb_value mrb_yaml_load(mrb_state *mrb, mrb_value self) { yaml_parser_t parser; yaml_document_t document; yaml_node_t *root; mrb_value yaml_str; mrb_value result; /* Extract arguments */ mrb_get_args(mrb, "S", &yaml_str); /* Initialize the YAML parser */ yaml_parser_initialize(&parser); yaml_parser_set_input_string(&parser, (unsigned char *) RSTRING_PTR(yaml_str), RSTRING_LEN(yaml_str)); /* Load the document */ yaml_parser_load(&parser, &document); /* Error handling */ if (parser.error != YAML_NO_ERROR) { raise_parser_problem(mrb, &parser); return mrb_nil_value(); } /* Convert the root node to an MRuby value */ root = yaml_document_get_root_node(&document); result = node_to_value(mrb, &document, root); /* Clean up */ yaml_document_delete(&document); yaml_parser_delete(&parser); return result; }
eolConfig *eol_config_load(char* filename) { yaml_parser_t parser; char *buffer = NULL; size_t size = 0; FILE *input = NULL; eolConfig *config = NULL; if(!yaml_parser_initialize(&parser)) { return NULL; } config= eol_config_new(); if (config == NULL)return NULL; eol_line_cpy(config->filename, filename); config->_node = eol_keychain_new_hash(); if(config->_node == NULL) { return NULL; } input = fopen(filename,"r"); if(input == NULL) { //TODO CLEANUP return NULL; } /*TODO: test the following on alternate endianness architectures before deleting the above*/ buffer = eol_get_file_buffer(&size,input); yaml_parser_set_input_string(&parser, (const unsigned char *)buffer, size); eol_config_parse_tier(&parser, config->_node); yaml_parser_delete(&parser); fclose(input); return config; }
int cloud_config_main(const gchar* filename) { yaml_parser_t parser; GList* handlers = NULL; int i; cloud_config_global_data = g_hash_table_new(g_str_hash, g_str_equal); LOG("Parsing user data file %s\n", filename); GNode* userdata = g_node_new(g_strdup(filename)); FILE* cloud_config_file = fopen(filename, "rb"); yaml_parser_initialize(&parser); yaml_parser_set_input_file(&parser, cloud_config_file); cloud_config_parse(&parser, userdata, 0); yaml_parser_delete(&parser); fclose(cloud_config_file); g_node_traverse(userdata, G_POST_ORDER, G_TRAVERSE_ALL, -1, cloud_config_simplify, NULL); cloud_config_dump(userdata); /* built-in handlers */ for (i = 0; cc_module_structs[i] != NULL; ++i) { LOG("Loaded handler for block \"%s\"\n", cc_module_structs[i]->name); handlers = g_list_prepend(handlers, cc_module_structs[i]); } cloud_config_process(userdata, handlers); g_node_traverse(userdata, G_POST_ORDER, G_TRAVERSE_ALL, -1, cloud_config_free, NULL); g_node_destroy(userdata); g_list_free(handlers); g_hash_table_destroy(cloud_config_global_data); return 0; }
MapObject MapObject::processYaml(FILE* fh) { int debug = 0; #ifdef DEBUG debug = 0; #endif MapObject yamlMap; if (fh == NULL) { yamlMap._type = MapObject::MAP_OBJ_FAILED; if (debug) fputs("Failed to open file!\n", stderr); return yamlMap; } yaml_parser_t parser; yaml_event_t event; if (! yaml_parser_initialize(&parser)) { yamlMap._type = MapObject::MAP_OBJ_FAILED; if (debug) fputs("Failed to initialize parser!\n", stderr); return yamlMap; } yaml_parser_set_input_file(&parser, fh); hardcoreYamlProcess(&yamlMap, &parser, &event); yaml_event_delete(&event); yaml_parser_delete(&parser); fclose(fh); return yamlMap; }
void moloch_rules_load(char **names) { int i; // Make a copy of current items to free later MolochRulesInfo_t *freeing = MOLOCH_TYPE_ALLOC0(MolochRulesInfo_t); memcpy(freeing, ¤t, sizeof(current)); moloch_free_later(freeing, (GDestroyNotify) moloch_rules_free); // Load all the rule files for (i = 0; names[i]; i++) { yaml_parser_t parser; yaml_parser_initialize(&parser); FILE *input = fopen(names[i], "rb"); if (!input) LOGEXIT("ERROR - can not open rules file %s", names[i]); yaml_parser_set_input_file(&parser, input); YamlNode_t *parent = moloch_rules_parse_yaml(names[i], NULL, &parser, FALSE); yaml_parser_delete(&parser); if (!parent) { LOG("WARNING %s - has no rules", names[i]); continue; } #ifdef RULES_DEBUG moloch_rules_parse_print(parent, 0); #endif moloch_rules_load_file(names[i], parent); moloch_rules_free_node(parent); fclose(input); } // Part 2, which will also copy loading to current moloch_rules_load_complete(); }
/* * This is the main Load function. * It takes a yaml stream and turns it into 0 or more Perl objects. */ void Load(SV *yaml_sv) { dXSARGS; perl_yaml_loader_t loader; SV *node; char *yaml_str; STRLEN yaml_len; /* If UTF8, make copy and downgrade */ if (SvPV_nolen(yaml_sv) && SvUTF8(yaml_sv)) { yaml_sv = sv_mortalcopy(yaml_sv); } yaml_str = SvPVbyte(yaml_sv, yaml_len); sp = mark; if (0 && (items || ax)) {} /* XXX Quiet the -Wall warnings for now. */ yaml_parser_initialize(&loader.parser); loader.document = 0; yaml_parser_set_input_string( &loader.parser, (unsigned char *)yaml_str, yaml_len ); /* Get the first event. Must be a STREAM_START */ if (!yaml_parser_parse(&loader.parser, &loader.event)) goto load_error; if (loader.event.type != YAML_STREAM_START_EVENT) croak(ERRMSG "Expected STREAM_START_EVENT; Got: %d != %d", loader.event.type, YAML_STREAM_START_EVENT ); loader.anchors = newHV(); sv_2mortal((SV*)loader.anchors); /* Keep calling load_node until end of stream */ while (1) { loader.document++; if (!yaml_parser_parse(&loader.parser, &loader.event)) goto load_error; if (loader.event.type == YAML_STREAM_END_EVENT) break; node = load_node(&loader); hv_clear(loader.anchors); if (! node) break; XPUSHs(sv_2mortal(node)); if (!yaml_parser_parse(&loader.parser, &loader.event)) goto load_error; if (loader.event.type != YAML_DOCUMENT_END_EVENT) croak(ERRMSG "Expected DOCUMENT_END_EVENT"); } /* Make sure the last event is a STREAM_END */ if (loader.event.type != YAML_STREAM_END_EVENT) croak(ERRMSG "Expected STREAM_END_EVENT; Got: %d != %d", loader.event.type, YAML_STREAM_END_EVENT ); yaml_parser_delete(&loader.parser); PUTBACK; return; load_error: croak(loader_error_msg(&loader, NULL)); }
int main(int argc, char *argv[]) { int help = 0; int canonical = 0; int unicode = 0; int k; int done = 0; yaml_parser_t parser; yaml_emitter_t emitter; yaml_event_t input_event; yaml_document_t output_document; int root; /* Clear the objects. */ memset(&parser, 0, sizeof(parser)); memset(&emitter, 0, sizeof(emitter)); memset(&input_event, 0, sizeof(input_event)); memset(&output_document, 0, sizeof(output_document)); /* Analyze command line options. */ for (k = 1; k < argc; k ++) { if (strcmp(argv[k], "-h") == 0 || strcmp(argv[k], "--help") == 0) { help = 1; } else if (strcmp(argv[k], "-c") == 0 || strcmp(argv[k], "--canonical") == 0) { canonical = 1; } else if (strcmp(argv[k], "-u") == 0 || strcmp(argv[k], "--unicode") == 0) { unicode = 1; } else { fprintf(stderr, "Unrecognized option: %s\n" "Try `%s --help` for more information.\n", argv[k], argv[0]); return 1; } } /* Display the help string. */ if (help) { printf("%s <input\n" "or\n%s -h | --help\nDeconstruct a YAML stream\n\nOptions:\n" "-h, --help\t\tdisplay this help and exit\n" "-c, --canonical\t\toutput in the canonical YAML format\n" "-u, --unicode\t\toutput unescaped non-ASCII characters\n", argv[0], argv[0]); return 0; } /* Initialize the parser and emitter objects. */ if (!yaml_parser_initialize(&parser)) { fprintf(stderr, "Could not initialize the parser object\n"); return 1; } if (!yaml_emitter_initialize(&emitter)) { yaml_parser_delete(&parser); fprintf(stderr, "Could not inialize the emitter object\n"); return 1; } /* Set the parser parameters. */ yaml_parser_set_input_file(&parser, stdin); /* Set the emitter parameters. */ yaml_emitter_set_output_file(&emitter, stdout); yaml_emitter_set_canonical(&emitter, canonical); yaml_emitter_set_unicode(&emitter, unicode); /* Create and emit the STREAM-START event. */ if (!yaml_emitter_open(&emitter)) goto emitter_error; /* Create a output_document object. */ if (!yaml_document_initialize(&output_document, NULL, NULL, NULL, 0, 0)) goto document_error; /* Create the root sequence. */ root = yaml_document_add_sequence(&output_document, NULL, YAML_BLOCK_SEQUENCE_STYLE); if (!root) goto document_error; /* Loop through the input events. */ while (!done) { int properties, key, value, map, seq; /* Get the next event. */ if (!yaml_parser_parse(&parser, &input_event)) goto parser_error; /* Check if this is the stream end. */ if (input_event.type == YAML_STREAM_END_EVENT) { done = 1; } /* Create a mapping node and attach it to the root sequence. */ properties = yaml_document_add_mapping(&output_document, NULL, YAML_BLOCK_MAPPING_STYLE); if (!properties) goto document_error; if (!yaml_document_append_sequence_item(&output_document, root, properties)) goto document_error; /* Analyze the event. */ switch (input_event.type) { case YAML_STREAM_START_EVENT: /* Add 'type': 'STREAM-START'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "STREAM-START", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Add 'encoding': <encoding>. */ if (input_event.data.stream_start.encoding) { yaml_encoding_t encoding = input_event.data.stream_start.encoding; key = yaml_document_add_scalar(&output_document, NULL, "encoding", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, (encoding == YAML_UTF8_ENCODING ? "utf-8" : encoding == YAML_UTF16LE_ENCODING ? "utf-16-le" : encoding == YAML_UTF16BE_ENCODING ? "utf-16-be" : "unknown"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } break; case YAML_STREAM_END_EVENT: /* Add 'type': 'STREAM-END'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "STREAM-END", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; break; case YAML_DOCUMENT_START_EVENT: /* Add 'type': 'DOCUMENT-START'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "DOCUMENT-START", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Display the output_document version numbers. */ if (input_event.data.document_start.version_directive) { yaml_version_directive_t *version = input_event.data.document_start.version_directive; char number[64]; /* Add 'version': {}. */ key = yaml_document_add_scalar(&output_document, NULL, "version", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; map = yaml_document_add_mapping(&output_document, NULL, YAML_FLOW_MAPPING_STYLE); if (!map) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, map)) goto document_error; /* Add 'major': <number>. */ key = yaml_document_add_scalar(&output_document, NULL, "major", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; sprintf(number, "%d", version->major); value = yaml_document_add_scalar(&output_document, YAML_INT_TAG, number, -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, map, key, value)) goto document_error; /* Add 'minor': <number>. */ key = yaml_document_add_scalar(&output_document, NULL, "minor", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; sprintf(number, "%d", version->minor); value = yaml_document_add_scalar(&output_document, YAML_INT_TAG, number, -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, map, key, value)) goto document_error; } /* Display the output_document tag directives. */ if (input_event.data.document_start.tag_directives.start != input_event.data.document_start.tag_directives.end) { yaml_tag_directive_t *tag; /* Add 'tags': []. */ key = yaml_document_add_scalar(&output_document, NULL, "tags", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; seq = yaml_document_add_sequence(&output_document, NULL, YAML_BLOCK_SEQUENCE_STYLE); if (!seq) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, seq)) goto document_error; for (tag = input_event.data.document_start.tag_directives.start; tag != input_event.data.document_start.tag_directives.end; tag ++) { /* Add {}. */ map = yaml_document_add_mapping(&output_document, NULL, YAML_FLOW_MAPPING_STYLE); if (!map) goto document_error; if (!yaml_document_append_sequence_item(&output_document, seq, map)) goto document_error; /* Add 'handle': <handle>. */ key = yaml_document_add_scalar(&output_document, NULL, "handle", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, tag->handle, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, map, key, value)) goto document_error; /* Add 'prefix': <prefix>. */ key = yaml_document_add_scalar(&output_document, NULL, "prefix", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, tag->prefix, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, map, key, value)) goto document_error; } } /* Add 'implicit': <flag>. */ key = yaml_document_add_scalar(&output_document, NULL, "implicit", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, (input_event.data.document_start.implicit ? "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; break; case YAML_DOCUMENT_END_EVENT: /* Add 'type': 'DOCUMENT-END'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "DOCUMENT-END", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Add 'implicit': <flag>. */ key = yaml_document_add_scalar(&output_document, NULL, "implicit", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, (input_event.data.document_end.implicit ? "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; break; case YAML_ALIAS_EVENT: /* Add 'type': 'ALIAS'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "ALIAS", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Add 'anchor': <anchor>. */ key = yaml_document_add_scalar(&output_document, NULL, "anchor", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, input_event.data.alias.anchor, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; break; case YAML_SCALAR_EVENT: /* Add 'type': 'SCALAR'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "SCALAR", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Add 'anchor': <anchor>. */ if (input_event.data.scalar.anchor) { key = yaml_document_add_scalar(&output_document, NULL, "anchor", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, input_event.data.scalar.anchor, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } /* Add 'tag': <tag>. */ if (input_event.data.scalar.tag) { key = yaml_document_add_scalar(&output_document, NULL, "tag", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, input_event.data.scalar.tag, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } /* Add 'value': <value>. */ key = yaml_document_add_scalar(&output_document, NULL, "value", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, input_event.data.scalar.value, input_event.data.scalar.length, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Display if the scalar tag is implicit. */ /* Add 'implicit': {} */ key = yaml_document_add_scalar(&output_document, NULL, "version", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; map = yaml_document_add_mapping(&output_document, NULL, YAML_FLOW_MAPPING_STYLE); if (!map) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, map)) goto document_error; /* Add 'plain': <flag>. */ key = yaml_document_add_scalar(&output_document, NULL, "plain", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, (input_event.data.scalar.plain_implicit ? "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, map, key, value)) goto document_error; /* Add 'quoted': <flag>. */ key = yaml_document_add_scalar(&output_document, NULL, "quoted", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, (input_event.data.scalar.quoted_implicit ? "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, map, key, value)) goto document_error; /* Display the style information. */ if (input_event.data.scalar.style) { yaml_scalar_style_t style = input_event.data.scalar.style; /* Add 'style': <style>. */ key = yaml_document_add_scalar(&output_document, NULL, "style", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, (style == YAML_PLAIN_SCALAR_STYLE ? "plain" : style == YAML_SINGLE_QUOTED_SCALAR_STYLE ? "single-quoted" : style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ? "double-quoted" : style == YAML_LITERAL_SCALAR_STYLE ? "literal" : style == YAML_FOLDED_SCALAR_STYLE ? "folded" : "unknown"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } break; case YAML_SEQUENCE_START_EVENT: /* Add 'type': 'SEQUENCE-START'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "SEQUENCE-START", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Add 'anchor': <anchor>. */ if (input_event.data.sequence_start.anchor) { key = yaml_document_add_scalar(&output_document, NULL, "anchor", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, input_event.data.sequence_start.anchor, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } /* Add 'tag': <tag>. */ if (input_event.data.sequence_start.tag) { key = yaml_document_add_scalar(&output_document, NULL, "tag", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, input_event.data.sequence_start.tag, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } /* Add 'implicit': <flag>. */ key = yaml_document_add_scalar(&output_document, NULL, "implicit", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, (input_event.data.sequence_start.implicit ? "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Display the style information. */ if (input_event.data.sequence_start.style) { yaml_sequence_style_t style = input_event.data.sequence_start.style; /* Add 'style': <style>. */ key = yaml_document_add_scalar(&output_document, NULL, "style", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, (style == YAML_BLOCK_SEQUENCE_STYLE ? "block" : style == YAML_FLOW_SEQUENCE_STYLE ? "flow" : "unknown"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } break; case YAML_SEQUENCE_END_EVENT: /* Add 'type': 'SEQUENCE-END'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "SEQUENCE-END", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; break; case YAML_MAPPING_START_EVENT: /* Add 'type': 'MAPPING-START'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "MAPPING-START", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Add 'anchor': <anchor>. */ if (input_event.data.mapping_start.anchor) { key = yaml_document_add_scalar(&output_document, NULL, "anchor", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, input_event.data.mapping_start.anchor, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } /* Add 'tag': <tag>. */ if (input_event.data.mapping_start.tag) { key = yaml_document_add_scalar(&output_document, NULL, "tag", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, input_event.data.mapping_start.tag, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } /* Add 'implicit': <flag>. */ key = yaml_document_add_scalar(&output_document, NULL, "implicit", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, (input_event.data.mapping_start.implicit ? "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; /* Display the style information. */ if (input_event.data.sequence_start.style) { yaml_sequence_style_t style = input_event.data.mapping_start.style; /* Add 'style': <style>. */ key = yaml_document_add_scalar(&output_document, NULL, "style", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, (style == YAML_BLOCK_MAPPING_STYLE ? "block" : style == YAML_FLOW_MAPPING_STYLE ? "flow" : "unknown"), -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; } break; case YAML_MAPPING_END_EVENT: /* Add 'type': 'MAPPING-END'. */ key = yaml_document_add_scalar(&output_document, NULL, "type", -1, YAML_PLAIN_SCALAR_STYLE); if (!key) goto document_error; value = yaml_document_add_scalar(&output_document, NULL, "MAPPING-END", -1, YAML_PLAIN_SCALAR_STYLE); if (!value) goto document_error; if (!yaml_document_append_mapping_pair(&output_document, properties, key, value)) goto document_error; break; default: /* It couldn't really happen. */ break; } /* Delete the event object. */ yaml_event_delete(&input_event); } if (!yaml_emitter_dump(&emitter, &output_document)) goto emitter_error; if (!yaml_emitter_close(&emitter)) goto emitter_error; yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 0; parser_error: /* Display a parser error message. */ switch (parser.error) { case YAML_MEMORY_ERROR: fprintf(stderr, "Memory error: Not enough memory for parsing\n"); break; case YAML_READER_ERROR: if (parser.problem_value != -1) { fprintf(stderr, "Reader error: %s: #%X at %zd\n", parser.problem, parser.problem_value, parser.problem_offset); } else { fprintf(stderr, "Reader error: %s at %zd\n", parser.problem, parser.problem_offset); } break; case YAML_SCANNER_ERROR: if (parser.context) { fprintf(stderr, "Scanner error: %s at line %lu, column %lu\n" "%s at line %lu, column %lu\n", parser.context, parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } else { fprintf(stderr, "Scanner error: %s at line %lu, column %lu\n", parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } break; case YAML_PARSER_ERROR: if (parser.context) { fprintf(stderr, "Parser error: %s at line %lu, column %lu\n" "%s at line %lu, column %lu\n", parser.context, parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } else { fprintf(stderr, "Parser error: %s at line %lu, column %lu\n", parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } break; default: /* Couldn't happen. */ fprintf(stderr, "Internal error\n"); break; } yaml_event_delete(&input_event); yaml_document_delete(&output_document); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; emitter_error: /* Display an emitter error message. */ switch (emitter.error) { case YAML_MEMORY_ERROR: fprintf(stderr, "Memory error: Not enough memory for emitting\n"); break; case YAML_WRITER_ERROR: fprintf(stderr, "Writer error: %s\n", emitter.problem); break; case YAML_EMITTER_ERROR: fprintf(stderr, "Emitter error: %s\n", emitter.problem); break; default: /* Couldn't happen. */ fprintf(stderr, "Internal error\n"); break; } yaml_event_delete(&input_event); yaml_document_delete(&output_document); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; document_error: fprintf(stderr, "Memory error: Not enough memory for creating a document\n"); yaml_event_delete(&input_event); yaml_document_delete(&output_document); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; }
int main(int argc, char *argv[]) { int help = 0; int canonical = 0; int unicode = 0; int k; int done = 0; yaml_parser_t parser; yaml_emitter_t emitter; yaml_event_t input_event; yaml_event_t output_event; /* Clear the objects. */ memset(&parser, 0, sizeof(parser)); memset(&emitter, 0, sizeof(emitter)); memset(&input_event, 0, sizeof(input_event)); memset(&output_event, 0, sizeof(output_event)); /* Analyze command line options. */ for (k = 1; k < argc; k ++) { if (strcmp(argv[k], "-h") == 0 || strcmp(argv[k], "--help") == 0) { help = 1; } else if (strcmp(argv[k], "-c") == 0 || strcmp(argv[k], "--canonical") == 0) { canonical = 1; } else if (strcmp(argv[k], "-u") == 0 || strcmp(argv[k], "--unicode") == 0) { unicode = 1; } else { fprintf(stderr, "Unrecognized option: %s\n" "Try `%s --help` for more information.\n", argv[k], argv[0]); return 1; } } /* Display the help string. */ if (help) { printf("%s <input\n" "or\n%s -h | --help\nDeconstruct a YAML stream\n\nOptions:\n" "-h, --help\t\tdisplay this help and exit\n" "-c, --canonical\t\toutput in the canonical YAML format\n" "-u, --unicode\t\toutput unescaped non-ASCII characters\n", argv[0], argv[0]); return 0; } /* Initialize the parser and emitter objects. */ if (!yaml_parser_initialize(&parser)) { fprintf(stderr, "Could not initialize the parser object\n"); return 1; } if (!yaml_emitter_initialize(&emitter)) { yaml_parser_delete(&parser); fprintf(stderr, "Could not inialize the emitter object\n"); return 1; } /* Set the parser parameters. */ yaml_parser_set_input_file(&parser, stdin); /* Set the emitter parameters. */ yaml_emitter_set_output_file(&emitter, stdout); yaml_emitter_set_canonical(&emitter, canonical); yaml_emitter_set_unicode(&emitter, unicode); /* Create and emit the STREAM-START event. */ if (!yaml_stream_start_event_initialize(&output_event, YAML_UTF8_ENCODING)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Create and emit the DOCUMENT-START event. */ if (!yaml_document_start_event_initialize(&output_event, NULL, NULL, NULL, 0)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Create and emit the SEQUENCE-START event. */ if (!yaml_sequence_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:seq", 1, YAML_BLOCK_SEQUENCE_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Loop through the input events. */ while (!done) { /* Get the next event. */ if (!yaml_parser_parse(&parser, &input_event)) goto parser_error; /* Check if this is the stream end. */ if (input_event.type == YAML_STREAM_END_EVENT) { done = 1; } /* Create and emit a MAPPING-START event. */ if (!yaml_mapping_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:map", 1, YAML_BLOCK_MAPPING_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Analyze the event. */ switch (input_event.type) { case YAML_STREAM_START_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'STREAM-START'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "STREAM-START", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display encoding information. */ if (input_event.data.stream_start.encoding) { yaml_encoding_t encoding = input_event.data.stream_start.encoding; /* Write 'encoding'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "encoding", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the stream encoding. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", (encoding == YAML_UTF8_ENCODING ? "utf-8" : encoding == YAML_UTF16LE_ENCODING ? "utf-16-le" : encoding == YAML_UTF16BE_ENCODING ? "utf-16-be" : "unknown"), -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } break; case YAML_STREAM_END_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'STREAM-END'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "STREAM-END", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_DOCUMENT_START_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'DOCUMENT-START'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "DOCUMENT-START", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the document version numbers. */ if (input_event.data.document_start.version_directive) { yaml_version_directive_t *version = input_event.data.document_start.version_directive; char number[64]; /* Write 'version'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "version", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '{'. */ if (!yaml_mapping_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:map", 1, YAML_FLOW_MAPPING_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'major'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "major", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write a number. */ sprintf(number, "%d", version->major); if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:int", number, -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'minor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "minor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write a number. */ sprintf(number, "%d", version->minor); if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:int", number, -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '}'. */ if (!yaml_mapping_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the document tag directives. */ if (input_event.data.document_start.tag_directives.start != input_event.data.document_start.tag_directives.end) { yaml_tag_directive_t *tag; /* Write 'tags'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "tags", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Start a block sequence. */ if (!yaml_sequence_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:seq", 1, YAML_BLOCK_SEQUENCE_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; for (tag = input_event.data.document_start.tag_directives.start; tag != input_event.data.document_start.tag_directives.end; tag ++) { /* Write '{'. */ if (!yaml_mapping_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:map", 1, YAML_FLOW_MAPPING_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'handle'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "handle", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the tag directive handle. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", tag->handle, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'prefix'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "prefix", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the tag directive prefix. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", tag->prefix, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '}'. */ if (!yaml_mapping_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* End a block sequence. */ if (!yaml_sequence_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the document is implicit. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.document_start.implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_DOCUMENT_END_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'DOCUMENT-END'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "DOCUMENT-END", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the document is implicit. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.document_end.implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_ALIAS_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'ALIAS'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "ALIAS", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'anchor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "anchor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the alias anchor. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.alias.anchor, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_SCALAR_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'SCALAR'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "SCALAR", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the scalar anchor. */ if (input_event.data.scalar.anchor) { /* Write 'anchor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "anchor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar anchor. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.scalar.anchor, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the scalar tag. */ if (input_event.data.scalar.tag) { /* Write 'tag'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "tag", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar tag. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.scalar.tag, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the scalar value. */ /* Write 'value'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "value", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar value. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.scalar.value, input_event.data.scalar.length, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display if the scalar tag is implicit. */ /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '{'. */ if (!yaml_mapping_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:map", 1, YAML_FLOW_MAPPING_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'plain'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "plain", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the scalar is implicit in the plain style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.scalar.plain_implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'quoted'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "non-plain", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the scalar is implicit in a non-plain style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.scalar.quoted_implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '}'. */ if (!yaml_mapping_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the style information. */ if (input_event.data.scalar.style) { yaml_scalar_style_t style = input_event.data.scalar.style; /* Write 'style'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "style", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", (style == YAML_PLAIN_SCALAR_STYLE ? "plain" : style == YAML_SINGLE_QUOTED_SCALAR_STYLE ? "single-quoted" : style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ? "double-quoted" : style == YAML_LITERAL_SCALAR_STYLE ? "literal" : style == YAML_FOLDED_SCALAR_STYLE ? "folded" : "unknown"), -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } break; case YAML_SEQUENCE_START_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'SEQUENCE-START'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "SEQUENCE-START", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the sequence anchor. */ if (input_event.data.sequence_start.anchor) { /* Write 'anchor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "anchor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the sequence anchor. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.sequence_start.anchor, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the sequence tag. */ if (input_event.data.sequence_start.tag) { /* Write 'tag'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "tag", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the sequence tag. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.sequence_start.tag, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the sequence tag is implicit. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.sequence_start.implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the style information. */ if (input_event.data.sequence_start.style) { yaml_sequence_style_t style = input_event.data.sequence_start.style; /* Write 'style'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "style", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", (style == YAML_BLOCK_SEQUENCE_STYLE ? "block" : style == YAML_FLOW_SEQUENCE_STYLE ? "flow" : "unknown"), -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } break; case YAML_SEQUENCE_END_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'SEQUENCE-END'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "SEQUENCE-END", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_MAPPING_START_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'MAPPING-START'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "MAPPING-START", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the mapping anchor. */ if (input_event.data.mapping_start.anchor) { /* Write 'anchor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "anchor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the mapping anchor. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.mapping_start.anchor, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the mapping tag. */ if (input_event.data.mapping_start.tag) { /* Write 'tag'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "tag", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the mapping tag. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.mapping_start.tag, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the mapping tag is implicit. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.mapping_start.implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the style information. */ if (input_event.data.mapping_start.style) { yaml_mapping_style_t style = input_event.data.mapping_start.style; /* Write 'style'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "style", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", (style == YAML_BLOCK_MAPPING_STYLE ? "block" : style == YAML_FLOW_MAPPING_STYLE ? "flow" : "unknown"), -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } break; case YAML_MAPPING_END_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'MAPPING-END'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "MAPPING-END", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; default: /* It couldn't really happen. */ break; } /* Delete the event object. */ yaml_event_delete(&input_event); /* Create and emit a MAPPING-END event. */ if (!yaml_mapping_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Create and emit the SEQUENCE-END event. */ if (!yaml_sequence_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Create and emit the DOCUMENT-END event. */ if (!yaml_document_end_event_initialize(&output_event, 0)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Create and emit the STREAM-END event. */ if (!yaml_stream_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 0; parser_error: /* Display a parser error message. */ switch (parser.error) { case YAML_MEMORY_ERROR: fprintf(stderr, "Memory error: Not enough memory for parsing\n"); break; case YAML_READER_ERROR: if (parser.problem_value != -1) { fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem, parser.problem_value, parser.problem_offset); } else { fprintf(stderr, "Reader error: %s at %d\n", parser.problem, parser.problem_offset); } break; case YAML_SCANNER_ERROR: if (parser.context) { fprintf(stderr, "Scanner error: %s at line %d, column %d\n" "%s at line %d, column %d\n", parser.context, parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } else { fprintf(stderr, "Scanner error: %s at line %d, column %d\n", parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } break; case YAML_PARSER_ERROR: if (parser.context) { fprintf(stderr, "Parser error: %s at line %d, column %d\n" "%s at line %d, column %d\n", parser.context, parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } else { fprintf(stderr, "Parser error: %s at line %d, column %d\n", parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } break; default: /* Couldn't happen. */ fprintf(stderr, "Internal error\n"); break; } yaml_event_delete(&input_event); yaml_event_delete(&output_event); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; emitter_error: /* Display an emitter error message. */ switch (emitter.error) { case YAML_MEMORY_ERROR: fprintf(stderr, "Memory error: Not enough memory for emitting\n"); break; case YAML_WRITER_ERROR: fprintf(stderr, "Writer error: %s\n", emitter.problem); break; case YAML_EMITTER_ERROR: fprintf(stderr, "Emitter error: %s\n", emitter.problem); break; default: /* Couldn't happen. */ fprintf(stderr, "Internal error\n"); break; } yaml_event_delete(&input_event); yaml_event_delete(&output_event); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; event_error: fprintf(stderr, "Memory error: Not enough memory for creating an event\n"); yaml_event_delete(&input_event); yaml_event_delete(&output_event); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; }
int stage1_parse(struct streamstate* stp, FILE* file, int verbose) { int rc = 0; int done = 0; int error = 0; rc = yaml_parser_initialize(&stp->parser); if (! rc) return error_parser_initialize_failed(stp); yaml_parser_set_input_file(&stp->parser, file); while (!done) { rc = yaml_parser_parse(&stp->parser, &stp->event); if (!rc) { error = error_parser_parse_failed(stp); break; } if (verbose) debug_event_type(&stp->event); rc = 0; switch (stp->event.type) { case YAML_STREAM_START_EVENT: if (stp->stream_start) rc = error_invalid_stream_start(stp); else stp->stream_start = 1; break; case YAML_DOCUMENT_START_EVENT: if (stp->document_start) rc = error_invalid_document_start(stp); else stp->document_start = 1; break; case YAML_MAPPING_START_EVENT: if (! stp->stream_start) rc = error_stream_not_started(stp, "mapping_start_event"); else if (! stp->document_start) rc = error_document_not_started(stp, "mapping_start_event"); else rc = handle_mapping_start(stp); break; case YAML_MAPPING_END_EVENT: if (! stp->stream_start) rc = error_stream_not_started(stp, "mapping_end_event"); else if (! stp->document_start) rc = error_document_not_started(stp, "mapping_end_event"); else if (stp->curmap == NULL) rc = error_no_current_mapping(stp, "mapping_end_event"); else rc = handle_mapping_end(stp); break; case YAML_SCALAR_EVENT: if (! stp->stream_start) rc = error_stream_not_started(stp, "scalar_event"); else if (! stp->document_start) rc = error_document_not_started(stp, "scalar_event"); else if (stp->curmap == NULL) rc = error_no_current_mapping(stp, "scalar_event"); else rc = handle_scalar(stp); break; case YAML_DOCUMENT_END_EVENT: if (! stp->stream_start) rc = error_stream_not_started(stp, "document_end_event"); else if (! stp->document_start) rc = error_document_not_started(stp, "document_end_event"); else stp->document_start = 0; break; case YAML_STREAM_END_EVENT: if (! stp->stream_start) rc = error_stream_not_started(stp, "stream_end_event"); else { stp->stream_start = 0; done = 1; } break; default: rc = unexpected_event(stp); break; } yaml_event_delete(&stp->event); if (rc) { error = 1; break; } } yaml_parser_delete(&stp->parser); return error; }
int check_long_utf16(void) { yaml_parser_t parser; int k = 0; int j; int failed = 0; unsigned char ch0, ch1; unsigned char *buffer = malloc(2+LONG*2); assert(buffer); printf("checking a long utf16 sequence...\n"); buffer[k++] = '\xff'; buffer[k++] = '\xfe'; for (j = 0; j < LONG; j ++) { if (j % 2) { buffer[k++] = '\x10'; buffer[k++] = '\x04'; } else { buffer[k++] = '/'; buffer[k++] = '\x04'; } } yaml_parser_initialize(&parser); yaml_parser_set_input_string(&parser, buffer, 2+LONG*2); for (k = 0; k < LONG; k++) { if (!parser.unread) { if (!yaml_parser_update_buffer(&parser, 1)) { printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); failed = 1; break; } } if (!parser.unread) { printf("\tnot enough characters at %d\n", k); failed = 1; break; } if (k % 2) { ch0 = '\xd0'; ch1 = '\x90'; } else { ch0 = '\xd0'; ch1 = '\xaf'; } if (parser.buffer.pointer[0] != ch0 || parser.buffer.pointer[1] != ch1) { printf("\tincorrect UTF-8 sequence: %X %X instead of %X %X\n", (int)parser.buffer.pointer[0], (int)parser.buffer.pointer[1], (int)ch0, (int)ch1); failed = 1; break; } parser.buffer.pointer += 2; parser.unread -= 1; } if (!failed) { if (!yaml_parser_update_buffer(&parser, 1)) { printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); failed = 1; } else if (parser.buffer.pointer[0] != '\0') { printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser.buffer.pointer[0], parser.eof, parser.unread); failed = 1; } } yaml_parser_delete(&parser); free(buffer); printf("checking a long utf16 sequence: %d fail(s)\n", failed); return failed; }
/* this is where the magic happens */ static set* _cluster_keys(range_request* rr, apr_pool_t* pool, const char* cluster, const char* cluster_file) { apr_array_header_t* working_range; set* sections; char* section; char* cur_section; apr_pool_t* req_pool = range_request_pool(rr); yaml_node_t *node; yaml_node_t *rootnode; yaml_node_t *keynode; yaml_node_t *valuenode; yaml_parser_t parser; yaml_node_item_t *item; yaml_node_pair_t *pair; yaml_document_t document; FILE* fp = fopen(cluster_file, "r"); /* make sure we can open the file and parse it */ if (!fp) { range_request_warn(rr, "%s: %s not readable", cluster, cluster_file); return set_new(pool, 0); } if (!yaml_parser_initialize(&parser)) { range_request_warn(rr, "%s: cannot initialize yaml parser", cluster); fclose(fp); return set_new(pool, 0); } yaml_parser_set_input_file(&parser, fp); if(!yaml_parser_load(&parser, &document)) { range_request_warn(rr, "%s: malformatted cluster definition %s", cluster, cluster_file); fclose(fp); yaml_parser_delete(&parser); return set_new(pool, 0); } fclose(fp); rootnode = yaml_document_get_root_node(&document); /* make sure it's just a simple dictionary */ if(rootnode->type != YAML_MAPPING_NODE) { range_request_warn(rr, "%s: malformatted cluster definition %s", cluster, cluster_file); yaml_document_delete(&document); yaml_parser_delete(&parser); return set_new(pool, 0); } /* "sections" refers to cluster sections - %cluster:SECTION it's what we're going to return */ sections = set_new(pool, 0); section = cur_section = NULL; for(pair = rootnode->data.mapping.pairs.start; pair < rootnode->data.mapping.pairs.top; pair++) { /* these are the keys */ keynode = yaml_document_get_node(&document, pair->key); /* cur_section is the keyname - the WHATEVER in %cluster:WHATEVER */ cur_section = apr_pstrdup(pool, (char *)(keynode->data.scalar.value)); valuenode = yaml_document_get_node(&document, pair->value); /* if the value is a scalar, that's our answer */ if(valuenode->type == YAML_SCALAR_NODE) { set_add(sections, cur_section, apr_psprintf(pool, "%s", valuenode->data.scalar.value)); } else if (valuenode->type == YAML_SEQUENCE_NODE) { /* otherwise, glue together all the values in the list */ working_range = apr_array_make(req_pool, 1, sizeof(char*)); for(item = valuenode->data.sequence.items.start; item < valuenode->data.sequence.items.top; item++) { node = yaml_document_get_node(&document, (int)*item); if(node->type != YAML_SCALAR_NODE) { /* only scalars allowed */ range_request_warn(rr, "%s: malformed cluster definition %s", cluster, cluster_file); yaml_document_delete(&document); yaml_parser_delete(&parser); return set_new(pool, 0); } else { /* add to the working set */ /* include it in () because we're going to comma it together later */ *(char**)apr_array_push(working_range) = apr_psprintf(pool, "(%s)", _substitute_dollars(pool, cluster, node->data.scalar.value)); } } /* glue the list items together with commas */ set_add(sections, cur_section, apr_array_pstrcat(pool, working_range, ',')); } } /* Add a "KEYS" toplevel key that lists all the other keys */ /* TODO: make an error if somebody tries to specify KEYS manually? */ set_add(sections, "KEYS", _join_elements(pool, ',', sections)); yaml_document_delete(&document); yaml_parser_delete(&parser); return sections; }