8 main(int argc, char *argv[])
17 yaml_emitter_t emitter;
18 yaml_document_t document;
20 /* Clear the objects. */
22 memset(&parser, 0, sizeof(parser));
23 memset(&emitter, 0, sizeof(emitter));
24 memset(&document, 0, sizeof(document));
26 /* Analyze command line options. */
28 for (k = 1; k < argc; k ++)
30 if (strcmp(argv[k], "-h") == 0
31 || strcmp(argv[k], "--help") == 0) {
35 else if (strcmp(argv[k], "-c") == 0
36 || strcmp(argv[k], "--canonical") == 0) {
40 else if (strcmp(argv[k], "-u") == 0
41 || strcmp(argv[k], "--unicode") == 0) {
46 fprintf(stderr, "Unrecognized option: %s\n"
47 "Try `%s --help` for more information.\n",
53 /* Display the help string. */
57 printf("%s [--canonical] [--unicode] <input >output\n"
58 "or\n%s -h | --help\nReformat a YAML stream\n\nOptions:\n"
59 "-h, --help\t\tdisplay this help and exit\n"
60 "-c, --canonical\t\toutput in the canonical YAML format\n"
61 "-u, --unicode\t\toutput unescaped non-ASCII characters\n",
66 /* Initialize the parser and emitter objects. */
68 if (!yaml_parser_initialize(&parser))
71 if (!yaml_emitter_initialize(&emitter))
74 /* Set the parser parameters. */
76 yaml_parser_set_input_file(&parser, stdin);
78 /* Set the emitter parameters. */
80 yaml_emitter_set_output_file(&emitter, stdout);
82 yaml_emitter_set_canonical(&emitter, canonical);
83 yaml_emitter_set_unicode(&emitter, unicode);
89 /* Get the next event. */
91 if (!yaml_parser_load(&parser, &document))
94 /* Check if this is the stream end. */
96 if (!yaml_document_get_root_node(&document)) {
100 /* Emit the event. */
102 if (!yaml_emitter_dump(&emitter, &document))
106 yaml_parser_delete(&parser);
107 yaml_emitter_delete(&emitter);
113 /* Display a parser error message. */
115 switch (parser.error)
117 case YAML_MEMORY_ERROR:
118 fprintf(stderr, "Memory error: Not enough memory for parsing\n");
121 case YAML_READER_ERROR:
122 if (parser.problem_value != -1) {
123 fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem,
124 parser.problem_value, parser.problem_offset);
127 fprintf(stderr, "Reader error: %s at %d\n", parser.problem,
128 parser.problem_offset);
132 case YAML_SCANNER_ERROR:
133 if (parser.context) {
134 fprintf(stderr, "Scanner error: %s at line %d, column %d\n"
135 "%s at line %d, column %d\n", parser.context,
136 parser.context_mark.line+1, parser.context_mark.column+1,
137 parser.problem, parser.problem_mark.line+1,
138 parser.problem_mark.column+1);
141 fprintf(stderr, "Scanner error: %s at line %d, column %d\n",
142 parser.problem, parser.problem_mark.line+1,
143 parser.problem_mark.column+1);
147 case YAML_PARSER_ERROR:
148 if (parser.context) {
149 fprintf(stderr, "Parser error: %s at line %d, column %d\n"
150 "%s at line %d, column %d\n", parser.context,
151 parser.context_mark.line+1, parser.context_mark.column+1,
152 parser.problem, parser.problem_mark.line+1,
153 parser.problem_mark.column+1);
156 fprintf(stderr, "Parser error: %s at line %d, column %d\n",
157 parser.problem, parser.problem_mark.line+1,
158 parser.problem_mark.column+1);
162 case YAML_COMPOSER_ERROR:
163 if (parser.context) {
164 fprintf(stderr, "Composer error: %s at line %d, column %d\n"
165 "%s at line %d, column %d\n", parser.context,
166 parser.context_mark.line+1, parser.context_mark.column+1,
167 parser.problem, parser.problem_mark.line+1,
168 parser.problem_mark.column+1);
171 fprintf(stderr, "Composer error: %s at line %d, column %d\n",
172 parser.problem, parser.problem_mark.line+1,
173 parser.problem_mark.column+1);
178 /* Couldn't happen. */
179 fprintf(stderr, "Internal error\n");
183 yaml_parser_delete(&parser);
184 yaml_emitter_delete(&emitter);
190 /* Display an emitter error message. */
192 switch (emitter.error)
194 case YAML_MEMORY_ERROR:
195 fprintf(stderr, "Memory error: Not enough memory for emitting\n");
198 case YAML_WRITER_ERROR:
199 fprintf(stderr, "Writer error: %s\n", emitter.problem);
202 case YAML_EMITTER_ERROR:
203 fprintf(stderr, "Emitter error: %s\n", emitter.problem);
207 /* Couldn't happen. */
208 fprintf(stderr, "Internal error\n");
212 yaml_parser_delete(&parser);
213 yaml_emitter_delete(&emitter);