| 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
| 3 | #include <errno.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include <yaml.h> |
| 8 | |
| 9 | #include "yaml-path.h" |
| 10 | |
| 11 | |
| 12 | static int |
| 13 | parse_and_emit (yaml_parser_t *parser, yaml_emitter_t *emitter, yaml_path_t *path, int use_flow_style) |
| 14 | { |
| 15 | yaml_event_t event; |
| 16 | yaml_event_type_t event_type, prev_event_type = YAML_NO_EVENT; |
| 17 | yaml_path_filter_result_t result, prev_result = YAML_PATH_FILTER_RESULT_OUT; |
| 18 | |
| 19 | do { |
| 20 | if (!yaml_parser_parse(parser, &event)) { |
| 21 | switch (parser->error) { |
| 22 | case YAML_MEMORY_ERROR: |
| 23 | fprintf(stderr, "Memory error: Not enough memory for parsing\n"); |
| 24 | break; |
| 25 | case YAML_READER_ERROR: |
| 26 | if (parser->problem_value != -1) { |
| 27 | fprintf(stderr, "Reader error: %s: #%X at %ld\n", parser->problem, parser->problem_value, (long)parser->problem_offset); |
| 28 | } else { |
| 29 | fprintf(stderr, "Reader error: %s at %ld\n", parser->problem, (long)parser->problem_offset); |
| 30 | } |
| 31 | break; |
| 32 | case YAML_SCANNER_ERROR: |
| 33 | if (parser->context) { |
| 34 | fprintf(stderr, "Scanner error: %s at line %d, column %d\n%s at line %d, column %d\n", parser->context, |
| 35 | (int)parser->context_mark.line+1,(int)parser->context_mark.column+1, parser->problem, |
| 36 | (int)parser->problem_mark.line+1, (int)parser->problem_mark.column+1); |
| 37 | } else { |
| 38 | fprintf(stderr, "Scanner error: %s at line %d, column %d\n", parser->problem, (int)parser->problem_mark.line+1, (int)parser->problem_mark.column+1); |
| 39 | } |
| 40 | break; |
| 41 | case YAML_PARSER_ERROR: |
| 42 | if (parser->context) { |
| 43 | fprintf(stderr, "Parser error: %s at line %d, column %d\n%s at line %d, column %d\n", parser->context, |
| 44 | (int)parser->context_mark.line+1, (int)parser->context_mark.column+1, parser->problem, |
| 45 | (int)parser->problem_mark.line+1, (int)parser->problem_mark.column+1); |
| 46 | } else { |
| 47 | fprintf(stderr, "Parser error: %s at line %d, column %d\n", parser->problem, (int)parser->problem_mark.line+1, (int)parser->problem_mark.column+1); |
| 48 | } |
| 49 | break; |
| 50 | default: |
| 51 | fprintf(stderr, "Internal error\n"); |
| 52 | break; |
| 53 | } |
| 54 | return 1; |
| 55 | } else { |
| 56 | event_type = event.type; |
| 57 | result = yaml_path_filter_event(path, parser, &event); |
| 58 | if (result == YAML_PATH_FILTER_RESULT_OUT) { |
| 59 | yaml_event_delete(&event); |
| 60 | } else { |
| 61 | if (use_flow_style) { |
| 62 | switch (event.type) { |
| 63 | case YAML_SEQUENCE_START_EVENT: |
| 64 | event.data.sequence_start.style = YAML_FLOW_SEQUENCE_STYLE; |
| 65 | break; |
| 66 | case YAML_MAPPING_START_EVENT: |
| 67 | event.data.mapping_start.style = YAML_FLOW_MAPPING_STYLE; |
| 68 | break; |
| 69 | default: |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | if ((prev_event_type == YAML_DOCUMENT_START_EVENT && event_type == YAML_DOCUMENT_END_EVENT) |
| 74 | || (prev_result == YAML_PATH_FILTER_RESULT_IN_DANGLING_KEY |
| 75 | && (event_type == YAML_MAPPING_END_EVENT |
| 76 | || event_type == YAML_SEQUENCE_END_EVENT |
| 77 | || result == YAML_PATH_FILTER_RESULT_IN_DANGLING_KEY))) { |
| 78 | yaml_event_t null_event= {0}; |
| 79 | yaml_scalar_event_initialize(&null_event, NULL, (yaml_char_t *)"!!null", (yaml_char_t *)"null", 4, 1, 0, YAML_ANY_SCALAR_STYLE); |
| 80 | yaml_emitter_emit(emitter, &null_event); |
| 81 | } |
| 82 | prev_result = result; |
| 83 | prev_event_type = event_type; |
| 84 | if (!yaml_emitter_emit(emitter, &event)) { |
| 85 | switch (emitter->error) |
| 86 | { |
| 87 | case YAML_MEMORY_ERROR: |
| 88 | fprintf(stderr, "Memory error: Not enough memory for emitting\n"); |
| 89 | break; |
| 90 | case YAML_WRITER_ERROR: |
| 91 | fprintf(stderr, "Writer error: %s\n", emitter->problem); |
| 92 | break; |
| 93 | case YAML_EMITTER_ERROR: |
| 94 | fprintf(stderr, "Emitter error: %s\n", emitter->problem); |
| 95 | break; |
| 96 | default: |
| 97 | fprintf(stderr, "Internal error\n"); |
| 98 | break; |
| 99 | } |
| 100 | return 2; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } while (event_type != YAML_STREAM_END_EVENT); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static void |
| 111 | help (void) |
| 112 | { |
| 113 | printf("yamlp - filtering utility for YAML documents\n"); |
| 114 | printf("\n"); |
| 115 | printf("Usage: yamlp [-F] [-W <width>] [-f <file>] <path>\n"); |
| 116 | printf(" yamlp -h\n"); |
| 117 | printf("\n"); |
| 118 | printf("The tool will take the input YAML document from <stdin> or a <file> (-f option),\n"); |
| 119 | printf("and it will then return the portion of the document marked with the given <path>.\n"); |
| 120 | printf("\n"); |
| 121 | printf("Options:\n"); |
| 122 | printf(" -f a filename to get the YAML document from,\n"); |
| 123 | printf(" <stdin> will be used if omitted;\n"); |
| 124 | printf("\n"); |
| 125 | printf(" -F forced 'flow' style for the output YAML document;\n"); |
| 126 | printf("\n"); |
| 127 | printf(" -h help;\n"); |
| 128 | printf("\n"); |
| 129 | printf(" -W line wrap width, no wrapping if omitted.\n"); |
| 130 | printf("\n"); |
| 131 | } |
| 132 | |
| 133 | int main(int argc, char *argv[]) |
| 134 | { |
| 135 | int flow = 0; |
| 136 | char *file_name = NULL; |
| 137 | char *path_string = NULL; |
| 138 | long wrap = -1; |
| 139 | |
| 140 | int opt; |
| 141 | while ((opt = getopt(argc, argv, ":f:W:vhSF")) != -1) { |
| 142 | switch (opt) { |
| 143 | case 'h': |
| 144 | help(); |
| 145 | return 0; |
| 146 | case 'F': |
| 147 | flow = 1; |
| 148 | break; |
| 149 | case 'W': |
| 150 | wrap = strtol(optarg, NULL, 10); |
| 151 | if (!wrap) { |
| 152 | fprintf(stderr, "Invalid value for wrap width '%s'\n", optarg); |
| 153 | return 1; |
| 154 | } |
| 155 | break; |
| 156 | case 'f': |
| 157 | file_name = optarg; |
| 158 | break; |
| 159 | case ':': |
| 160 | fprintf(stderr, "Option needs a value\n"); |
| 161 | return 1; |
| 162 | case '?': |
| 163 | fprintf(stderr, "Unknown option '%c'\n", optopt); |
| 164 | return 1; |
| 165 | default: |
| 166 | fprintf(stderr, "Unhandled option '%c'\n", opt); |
| 167 | return 1; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | for (; optind < argc; optind++) { |
| 172 | path_string = argv[optind]; |
| 173 | } |
| 174 | |
| 175 | FILE *file = NULL; |
| 176 | if (file_name != NULL) { |
| 177 | file = fopen(file_name, "r"); |
| 178 | if (file == NULL) { |
| 179 | fprintf(stderr, "Unable to open file '%s' (%s)\n", file_name, strerror(errno)); |
| 180 | return 2; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (path_string == NULL || path_string[0] == 0) { |
| 185 | fprintf(stderr, "Empty path\n"); |
| 186 | return 3; |
| 187 | } |
| 188 | |
| 189 | yaml_path_t *path = yaml_path_create(); |
| 190 | if (yaml_path_parse(path, path_string)) { |
| 191 | fprintf(stderr, "Invalid path: '%s'\n", path_string); |
| 192 | fprintf(stderr, " %*s^ %s [at position %zu]\n", (int)yaml_path_error_get(path)->pos, " ", yaml_path_error_get(path)->message, yaml_path_error_get(path)->pos); |
| 193 | return 3; |
| 194 | } |
| 195 | |
| 196 | yaml_parser_t parser; |
| 197 | yaml_emitter_t emitter; |
| 198 | |
| 199 | yaml_parser_initialize(&parser); |
| 200 | yaml_parser_set_input_file(&parser, file != NULL ? file : stdin); |
| 201 | |
| 202 | yaml_emitter_initialize(&emitter); |
| 203 | yaml_emitter_set_output_file(&emitter, stdout); |
| 204 | yaml_emitter_set_width(&emitter, (int) wrap); |
| 205 | |
| 206 | if (parse_and_emit(&parser, &emitter, path, flow)) { |
| 207 | return 4; |
| 208 | } |
| 209 | |
| 210 | yaml_parser_delete(&parser); |
| 211 | yaml_emitter_delete(&emitter); |
| 212 | |
| 213 | yaml_path_destroy(path); |
| 214 | if (file != NULL) |
| 215 | fclose(file); |
| 216 | |
| 217 | return 0; |
| 218 | } |