| 1 | /* liblouis Braille Translation and Back-Translation Library |
| 2 | |
| 3 | Copyright (C) 2015, 2016 Christian Egli, Swiss Library for the Blind, Visually Impaired |
| 4 | and Print Disabled |
| 5 | Copyright (C) 2017 Bert Frees |
| 6 | |
| 7 | This program is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <config.h> |
| 22 | |
| 23 | #include <assert.h> |
| 24 | #include <getopt.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <string.h> |
| 29 | #include "internal.h" |
| 30 | #include "error.h" |
| 31 | #include "errno.h" |
| 32 | #include "progname.h" |
| 33 | #include "version-etc.h" |
| 34 | #include "brl_checks.h" |
| 35 | |
| 36 | static int verbose = 0; |
| 37 | static const struct option longopts[] = { |
| 38 | { "help", no_argument, NULL, 'h' }, |
| 39 | { "version", no_argument, NULL, 'v' }, |
| 40 | { "verbose", no_argument, &verbose, 1 }, |
| 41 | { NULL, 0, NULL, 0 }, |
| 42 | }; |
| 43 | |
| 44 | const char version_etc_copyright[] = |
| 45 | "Copyright %s %d Swiss Library for the Blind, Visually Impaired and Print " |
| 46 | "Disabled"; |
| 47 | |
| 48 | #define AUTHORS "Christian Egli" |
| 49 | |
| 50 | #define MODE_TRANSLATION_FORWARD 0 |
| 51 | #define MODE_TRANSLATION_BACKWARD 1 |
| 52 | #define MODE_TRANSLATION_BOTH_DIRECTIONS 2 |
| 53 | #define MODE_HYPHENATION 3 |
| 54 | #define MODE_HYPHENATION_BRAILLE 4 |
| 55 | #define MODE_DISPLAY 5 |
| 56 | #define MODE_DEFAULT MODE_TRANSLATION_FORWARD |
| 57 | |
| 58 | static void |
| 59 | print_help(void) { |
| 60 | printf("\ |
| 61 | Usage: %s YAML_TEST_FILE\n", |
| 62 | program_name); |
| 63 | |
| 64 | fputs("\ |
| 65 | Run the tests defined in the YAML_TEST_FILE. Return 0 if all tests pass\n\ |
| 66 | or 1 if any of the tests fail. The details of failing tests are printed\n\ |
| 67 | to stderr.\n\n", |
| 68 | stdout); |
| 69 | |
| 70 | fputs("\ |
| 71 | -h, --help display this help and exit\n\ |
| 72 | -v, --version display version information and exit\n\ |
| 73 | --verbose report expected failures\n", |
| 74 | stdout); |
| 75 | |
| 76 | printf("\n"); |
| 77 | printf("Report bugs to %s.\n", PACKAGE_BUGREPORT); |
| 78 | |
| 79 | #ifdef PACKAGE_PACKAGER_BUG_REPORTS |
| 80 | printf("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS); |
| 81 | #endif |
| 82 | #ifdef PACKAGE_URL |
| 83 | printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL); |
| 84 | #endif |
| 85 | } |
| 86 | |
| 87 | #define EXIT_SKIPPED 77 |
| 88 | |
| 89 | #ifdef HAVE_LIBYAML |
| 90 | #include <yaml.h> |
| 91 | |
| 92 | typedef struct { |
| 93 | const char *name; |
| 94 | const char *content; // table content in case of an inline table; NULL means name is |
| 95 | // a file |
| 96 | int location; // location in YAML file (line number) where table is defined |
| 97 | int is_display; // whether the table is a display table or a translation table |
| 98 | } table_value; |
| 99 | |
| 100 | const char *event_names[] = { "YAML_NO_EVENT", "YAML_STREAM_START_EVENT", |
| 101 | "YAML_STREAM_END_EVENT", "YAML_DOCUMENT_START_EVENT", "YAML_DOCUMENT_END_EVENT", |
| 102 | "YAML_ALIAS_EVENT", "YAML_SCALAR_EVENT", "YAML_SEQUENCE_START_EVENT", |
| 103 | "YAML_SEQUENCE_END_EVENT", "YAML_MAPPING_START_EVENT", "YAML_MAPPING_END_EVENT" }; |
| 104 | const char *encoding_names[] = { "YAML_ANY_ENCODING", "YAML_UTF8_ENCODING", |
| 105 | "YAML_UTF16LE_ENCODING", "YAML_UTF16BE_ENCODING" }; |
| 106 | |
| 107 | const char *inline_table_prefix = "checkyaml_inline_table_at_line_"; |
| 108 | |
| 109 | char *file_name; |
| 110 | |
| 111 | int errors = 0; |
| 112 | int count = 0; |
| 113 | |
| 114 | static char const **emph_classes = NULL; |
| 115 | |
| 116 | static void |
| 117 | simple_error(const char *msg, yaml_parser_t *parser, yaml_event_t *event) { |
| 118 | error_at_line(EXIT_FAILURE, 0, file_name, |
| 119 | event->start_mark.line ? event->start_mark.line + 1 |
| 120 | : parser->problem_mark.line + 1, |
| 121 | "%s", msg); |
| 122 | } |
| 123 | |
| 124 | static void |
| 125 | yaml_parse_error(yaml_parser_t *parser) { |
| 126 | error_at_line(EXIT_FAILURE, 0, file_name, parser->problem_mark.line + 1, "%s", |
| 127 | parser->problem); |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | yaml_error(yaml_event_type_t expected, yaml_event_t *event) { |
| 132 | error_at_line(EXIT_FAILURE, 0, file_name, event->start_mark.line + 1, |
| 133 | "Expected %s (actual %s)", event_names[expected], event_names[event->type]); |
| 134 | } |
| 135 | |
| 136 | static char * |
| 137 | read_table_query(yaml_parser_t *parser, const char **table_file_name_check) { |
| 138 | yaml_event_t event; |
| 139 | char *query_as_string = malloc(sizeof(char) * MAXSTRING); |
| 140 | char *p = query_as_string; |
| 141 | query_as_string[0] = '\0'; |
| 142 | while (1) { |
| 143 | if (!yaml_parser_parse(parser, &event)) yaml_error(YAML_SCALAR_EVENT, &event); |
| 144 | if (event.type == YAML_SCALAR_EVENT) { |
| 145 | |
| 146 | // (temporary) feature to check whether the table query matches an expected |
| 147 | // table |
| 148 | if (!strcmp((const char *)event.data.scalar.value, "__assert-match")) { |
| 149 | yaml_event_delete(&event); |
| 150 | if (!yaml_parser_parse(parser, &event) || |
| 151 | (event.type != YAML_SCALAR_EVENT)) |
| 152 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 153 | *table_file_name_check = strdup((const char *)event.data.scalar.value); |
| 154 | yaml_event_delete(&event); |
| 155 | } else { |
| 156 | if (query_as_string != p) strcat(p++, " "); |
| 157 | strcat(p, (const char *)event.data.scalar.value); |
| 158 | p += event.data.scalar.length; |
| 159 | strcat(p++, ":"); |
| 160 | yaml_event_delete(&event); |
| 161 | if (!yaml_parser_parse(parser, &event) || |
| 162 | (event.type != YAML_SCALAR_EVENT)) |
| 163 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 164 | strcat(p, (const char *)event.data.scalar.value); |
| 165 | p += event.data.scalar.length; |
| 166 | yaml_event_delete(&event); |
| 167 | } |
| 168 | } else if (event.type == YAML_MAPPING_END_EVENT) { |
| 169 | yaml_event_delete(&event); |
| 170 | break; |
| 171 | } else |
| 172 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 173 | } |
| 174 | return query_as_string; |
| 175 | } |
| 176 | |
| 177 | static void |
| 178 | compile_inline_table(const table_value *table) { |
| 179 | int location = table->location; |
| 180 | char *p = (char *)table->content; |
| 181 | char *line_start = p; |
| 182 | int line_len = 0; |
| 183 | while (*p) { |
| 184 | if (*p == 10 || *p == 13) { |
| 185 | char *line = strndup((const char *)line_start, line_len); |
| 186 | int error = 0; |
| 187 | if (!table->is_display) |
| 188 | error = !_lou_compileTranslationRule(table->name, line); |
| 189 | else |
| 190 | error = !_lou_compileDisplayRule(table->name, line); |
| 191 | if (error) |
| 192 | error_at_line(EXIT_FAILURE, 0, file_name, location, "Error in table %s", |
| 193 | table->name); |
| 194 | location++; |
| 195 | free(line); |
| 196 | line_start = p + 1; |
| 197 | line_len = 0; |
| 198 | } else { |
| 199 | line_len++; |
| 200 | } |
| 201 | p++; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static table_value * |
| 206 | read_table_value(yaml_parser_t *parser, int start_line, int is_display) { |
| 207 | table_value *table; |
| 208 | char *table_name = malloc(sizeof(char) * MAXSTRING); |
| 209 | char *table_content = NULL; |
| 210 | yaml_event_t event; |
| 211 | table_name[0] = '\0'; |
| 212 | if (!yaml_parser_parse(parser, &event) || |
| 213 | !(event.type == YAML_SEQUENCE_START_EVENT || |
| 214 | event.type == YAML_SCALAR_EVENT || |
| 215 | event.type == YAML_MAPPING_START_EVENT)) |
| 216 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 217 | "Expected %s, %s or %s (actual %s)", |
| 218 | event_names[YAML_SEQUENCE_START_EVENT], event_names[YAML_SCALAR_EVENT], |
| 219 | event_names[YAML_MAPPING_START_EVENT], event_names[event.type]); |
| 220 | if (event.type == YAML_SEQUENCE_START_EVENT) { |
| 221 | yaml_event_delete(&event); |
| 222 | int done = 0; |
| 223 | char *p = table_name; |
| 224 | while (!done) { |
| 225 | if (!yaml_parser_parse(parser, &event)) { |
| 226 | yaml_parse_error(parser); |
| 227 | } |
| 228 | if (event.type == YAML_SEQUENCE_END_EVENT) { |
| 229 | done = 1; |
| 230 | } else if (event.type == YAML_SCALAR_EVENT) { |
| 231 | if (table_name != p) strcat(p++, ","); |
| 232 | strcat(p, (const char *)event.data.scalar.value); |
| 233 | p += event.data.scalar.length; |
| 234 | } |
| 235 | yaml_event_delete(&event); |
| 236 | } |
| 237 | } else if (event.type == YAML_SCALAR_EVENT) { |
| 238 | yaml_char_t *p = event.data.scalar.value; |
| 239 | if (*p) |
| 240 | while (p[1]) p++; |
| 241 | if (*p == 10 || *p == 13) { |
| 242 | // If the scalar ends with a newline, assume it is a block |
| 243 | // scalar, so treat as an inline table. (Is there a proper way |
| 244 | // to check for a block scalar?) |
| 245 | sprintf(table_name, "%s%d", inline_table_prefix, start_line); |
| 246 | table_content = strdup((const char *)event.data.scalar.value); |
| 247 | } else { |
| 248 | strcat(table_name, (const char *)event.data.scalar.value); |
| 249 | } |
| 250 | yaml_event_delete(&event); |
| 251 | } else { // event.type == YAML_MAPPING_START_EVENT |
| 252 | char *query; |
| 253 | const char *table_file_name_check = NULL; |
| 254 | yaml_event_delete(&event); |
| 255 | query = read_table_query(parser, &table_file_name_check); |
| 256 | free(table_name); |
| 257 | table_name = lou_findTable(query); |
| 258 | free(query); |
| 259 | if (!table_name) |
| 260 | error_at_line(EXIT_FAILURE, 0, file_name, start_line, |
| 261 | "Table query did not match a table"); |
| 262 | if (table_file_name_check) { |
| 263 | const char *table_file_name = table_name; |
| 264 | do { |
| 265 | table_file_name++; |
| 266 | } while (*table_file_name); |
| 267 | while (table_file_name >= table_name && *table_file_name != '/' && |
| 268 | *table_file_name != '\\') |
| 269 | table_file_name--; |
| 270 | if (strcmp(table_file_name_check, table_file_name + 1)) |
| 271 | error_at_line(EXIT_FAILURE, 0, file_name, start_line, |
| 272 | "Table query did not match expected table: expected '%s' but got " |
| 273 | "'%s'", |
| 274 | table_file_name_check, table_file_name + 1); |
| 275 | free(table_file_name_check); |
| 276 | } |
| 277 | } |
| 278 | table = malloc(sizeof(table_value)); |
| 279 | table->name = table_name; |
| 280 | table->content = table_content; |
| 281 | table->location = start_line + 1; |
| 282 | table->is_display = is_display; |
| 283 | return table; |
| 284 | } |
| 285 | |
| 286 | static void |
| 287 | free_table_value(table_value *table) { |
| 288 | if (table) { |
| 289 | free((char *)table->name); |
| 290 | if (table->content) free((char *)table->content); |
| 291 | free(table); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | static char * |
| 296 | read_table(yaml_event_t *start_event, yaml_parser_t *parser, const char *display_table) { |
| 297 | table_value *v = NULL; |
| 298 | char *table_name = NULL; |
| 299 | if (start_event->type != YAML_SCALAR_EVENT || |
| 300 | strcmp((const char *)start_event->data.scalar.value, "table")) |
| 301 | return 0; |
| 302 | v = read_table_value(parser, start_event->start_mark.line + 1, 0); |
| 303 | if (v->content) |
| 304 | compile_inline_table(v); |
| 305 | else if (!_lou_getTranslationTable(v->name)) |
| 306 | error_at_line(EXIT_FAILURE, 0, file_name, start_event->start_mark.line + 1, |
| 307 | "Table %s not valid", v->name); |
| 308 | free(emph_classes); |
| 309 | emph_classes = lou_getEmphClasses(v->name); // get declared emphasis classes |
| 310 | if (emph_classes == NULL) { |
| 311 | error_at_line(EXIT_FAILURE, 0, file_name, start_event->start_mark.line + 1, |
| 312 | "Failed to fetch the emphasis classes list in table %s", v->name); |
| 313 | } |
| 314 | table_name = strdup((char *)v->name); |
| 315 | if (!display_table) { |
| 316 | if (v->content) { |
| 317 | v->is_display = 1; |
| 318 | compile_inline_table(v); |
| 319 | } else if (!_lou_getDisplayTable(v->name)) |
| 320 | error_at_line(EXIT_FAILURE, 0, file_name, start_event->start_mark.line + 1, |
| 321 | "Table %s not valid", v->name); |
| 322 | } |
| 323 | free_table_value(v); |
| 324 | return table_name; |
| 325 | } |
| 326 | |
| 327 | static void |
| 328 | read_flags(yaml_parser_t *parser, int *testmode) { |
| 329 | yaml_event_t event; |
| 330 | int parse_error = 1; |
| 331 | |
| 332 | *testmode = MODE_DEFAULT; |
| 333 | |
| 334 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_MAPPING_START_EVENT)) |
| 335 | yaml_error(YAML_MAPPING_START_EVENT, &event); |
| 336 | |
| 337 | yaml_event_delete(&event); |
| 338 | |
| 339 | while ((parse_error = yaml_parser_parse(parser, &event)) && |
| 340 | (event.type == YAML_SCALAR_EVENT)) { |
| 341 | if (!strcmp((const char *)event.data.scalar.value, "testmode")) { |
| 342 | yaml_event_delete(&event); |
| 343 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 344 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 345 | if (!strcmp((const char *)event.data.scalar.value, "forward")) { |
| 346 | *testmode = MODE_TRANSLATION_FORWARD; |
| 347 | } else if (!strcmp((const char *)event.data.scalar.value, "backward")) { |
| 348 | *testmode = MODE_TRANSLATION_BACKWARD; |
| 349 | } else if (!strcmp((const char *)event.data.scalar.value, "bothDirections")) { |
| 350 | *testmode = MODE_TRANSLATION_BOTH_DIRECTIONS; |
| 351 | } else if (!strcmp((const char *)event.data.scalar.value, "hyphenate")) { |
| 352 | *testmode = MODE_HYPHENATION; |
| 353 | } else if (!strcmp((const char *)event.data.scalar.value, |
| 354 | "hyphenateBraille")) { |
| 355 | *testmode = MODE_HYPHENATION_BRAILLE; |
| 356 | } else if (!strcmp((const char *)event.data.scalar.value, "display")) { |
| 357 | *testmode = MODE_DISPLAY; |
| 358 | } else { |
| 359 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 360 | "Testmode '%s' not supported\n", event.data.scalar.value); |
| 361 | } |
| 362 | } else { |
| 363 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 364 | "Flag '%s' not supported\n", event.data.scalar.value); |
| 365 | } |
| 366 | yaml_event_delete(&event); |
| 367 | } |
| 368 | if (!parse_error) yaml_parse_error(parser); |
| 369 | if (event.type != YAML_MAPPING_END_EVENT) yaml_error(YAML_MAPPING_END_EVENT, &event); |
| 370 | yaml_event_delete(&event); |
| 371 | } |
| 372 | |
| 373 | #define XFAIL_NONE 0 |
| 374 | #define XFAIL_FORWARD 1 |
| 375 | #define XFAIL_BACKWARD 2 |
| 376 | #define XFAIL_BOTH 3 |
| 377 | |
| 378 | static int |
| 379 | read_xfail_value(yaml_parser_t *parser) { |
| 380 | yaml_event_t event; |
| 381 | int xfail = 1; // assume a truthy value |
| 382 | |
| 383 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 384 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 385 | if (!strcmp((const char *)event.data.scalar.value, "false") || |
| 386 | !strcmp((const char *)event.data.scalar.value, "off")) |
| 387 | xfail = 0; // only "false" and "off" are falsy values |
| 388 | yaml_event_delete(&event); |
| 389 | return xfail; |
| 390 | } |
| 391 | |
| 392 | static int |
| 393 | read_xfail(yaml_parser_t *parser) { |
| 394 | yaml_event_t event; |
| 395 | if (!yaml_parser_parse(parser, &event) || |
| 396 | !(event.type == YAML_SCALAR_EVENT || event.type == YAML_MAPPING_START_EVENT)) |
| 397 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 398 | "Expected %s or %s (actual %s)", event_names[YAML_SCALAR_EVENT], |
| 399 | event_names[YAML_MAPPING_START_EVENT], event_names[event.type]); |
| 400 | |
| 401 | /* xfail can be either a scalar value (`true` or any string, typically |
| 402 | the failure reason) */ |
| 403 | if (event.type == YAML_SCALAR_EVENT) { |
| 404 | /* assume xfail both if there is an xfail key */ |
| 405 | int xfail = XFAIL_BOTH; |
| 406 | if (!strcmp((const char *)event.data.scalar.value, "false") || |
| 407 | !strcmp((const char *)event.data.scalar.value, "off")) |
| 408 | xfail = XFAIL_NONE; |
| 409 | yaml_event_delete(&event); |
| 410 | return xfail; |
| 411 | |
| 412 | /* or xfail can be a map such as {forward: true, backward: false} */ |
| 413 | } else { // event.type == YAML_MAPPING_START_EVENT |
| 414 | int parse_error = 1; |
| 415 | int xfail_forward = 0; /* if either direction is not specified `false` */ |
| 416 | int xfail_backward = 0; /* is assumed */ |
| 417 | while ((parse_error = yaml_parser_parse(parser, &event)) && |
| 418 | (event.type == YAML_SCALAR_EVENT)) { |
| 419 | if (strcmp((const char *)event.data.scalar.value, "forward") == 0) { |
| 420 | yaml_event_delete(&event); |
| 421 | xfail_forward = read_xfail_value(parser); |
| 422 | } else if (strcmp((const char *)event.data.scalar.value, "backward") == 0) { |
| 423 | yaml_event_delete(&event); |
| 424 | xfail_backward = read_xfail_value(parser); |
| 425 | } else { |
| 426 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 427 | "Direction '%s' is not valid\n", event.data.scalar.value); |
| 428 | yaml_event_delete(&event); |
| 429 | break; |
| 430 | } |
| 431 | } |
| 432 | if (!parse_error) yaml_parse_error(parser); |
| 433 | |
| 434 | if (event.type != YAML_MAPPING_END_EVENT) |
| 435 | yaml_error(YAML_MAPPING_END_EVENT, &event); |
| 436 | yaml_event_delete(&event); |
| 437 | |
| 438 | if (xfail_forward && xfail_backward) { |
| 439 | return XFAIL_BOTH; |
| 440 | } else if (xfail_forward) { |
| 441 | return XFAIL_FORWARD; |
| 442 | } else if (xfail_backward) { |
| 443 | return XFAIL_BACKWARD; |
| 444 | } else { |
| 445 | return XFAIL_NONE; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | static translationModes |
| 451 | read_mode(yaml_parser_t *parser) { |
| 452 | yaml_event_t event; |
| 453 | translationModes mode = 0; |
| 454 | int parse_error = 1; |
| 455 | |
| 456 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SEQUENCE_START_EVENT)) |
| 457 | yaml_error(YAML_SEQUENCE_START_EVENT, &event); |
| 458 | yaml_event_delete(&event); |
| 459 | |
| 460 | while ((parse_error = yaml_parser_parse(parser, &event)) && |
| 461 | (event.type == YAML_SCALAR_EVENT)) { |
| 462 | if (!strcmp((const char *)event.data.scalar.value, "noContractions")) { |
| 463 | mode |= noContractions; |
| 464 | } else if (!strcmp((const char *)event.data.scalar.value, "compbrlAtCursor")) { |
| 465 | mode |= compbrlAtCursor; |
| 466 | } else if (!strcmp((const char *)event.data.scalar.value, "dotsIO")) { |
| 467 | mode |= dotsIO; |
| 468 | } else if (!strcmp((const char *)event.data.scalar.value, "compbrlLeftCursor")) { |
| 469 | mode |= compbrlLeftCursor; |
| 470 | } else if (!strcmp((const char *)event.data.scalar.value, "ucBrl")) { |
| 471 | mode |= ucBrl; |
| 472 | } else if (!strcmp((const char *)event.data.scalar.value, "noUndefined")) { |
| 473 | mode |= noUndefined; |
| 474 | } else if (!strcmp((const char *)event.data.scalar.value, "partialTrans")) { |
| 475 | mode |= partialTrans; |
| 476 | } else { |
| 477 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 478 | "Mode '%s' not supported\n", event.data.scalar.value); |
| 479 | } |
| 480 | yaml_event_delete(&event); |
| 481 | } |
| 482 | if (!parse_error) yaml_parse_error(parser); |
| 483 | if (event.type != YAML_SEQUENCE_END_EVENT) |
| 484 | yaml_error(YAML_SEQUENCE_END_EVENT, &event); |
| 485 | yaml_event_delete(&event); |
| 486 | return mode; |
| 487 | } |
| 488 | |
| 489 | static int |
| 490 | parse_number(const char *number, const char *name, int file_line) { |
| 491 | char *tail; |
| 492 | errno = 0; |
| 493 | |
| 494 | int val = strtol(number, &tail, 0); |
| 495 | if (errno != 0) |
| 496 | error_at_line(EXIT_FAILURE, 0, file_name, file_line, |
| 497 | "Not a valid %s '%s'. Must be a number\n", name, number); |
| 498 | if (number == tail) |
| 499 | error_at_line(EXIT_FAILURE, 0, file_name, file_line, |
| 500 | "No digits found in %s '%s'. Must be a number\n", name, number); |
| 501 | return val; |
| 502 | } |
| 503 | |
| 504 | static int * |
| 505 | read_inPos(yaml_parser_t *parser, int translen) { |
| 506 | int *pos = malloc(sizeof(int) * translen); |
| 507 | int i = 0; |
| 508 | yaml_event_t event; |
| 509 | int parse_error = 1; |
| 510 | |
| 511 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SEQUENCE_START_EVENT)) |
| 512 | yaml_error(YAML_SEQUENCE_START_EVENT, &event); |
| 513 | yaml_event_delete(&event); |
| 514 | |
| 515 | while ((parse_error = yaml_parser_parse(parser, &event)) && |
| 516 | (event.type == YAML_SCALAR_EVENT)) { |
| 517 | if (i >= translen) |
| 518 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 519 | "Too many input positions for translation of length %d.", translen); |
| 520 | |
| 521 | pos[i++] = parse_number((const char *)event.data.scalar.value, "input position", |
| 522 | event.start_mark.line + 1); |
| 523 | yaml_event_delete(&event); |
| 524 | } |
| 525 | if (i < translen) |
| 526 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 527 | "Too few input positions (%i) for translation of length %i\n", i, |
| 528 | translen); |
| 529 | if (!parse_error) yaml_parse_error(parser); |
| 530 | if (event.type != YAML_SEQUENCE_END_EVENT) |
| 531 | yaml_error(YAML_SEQUENCE_END_EVENT, &event); |
| 532 | yaml_event_delete(&event); |
| 533 | return pos; |
| 534 | } |
| 535 | |
| 536 | static int * |
| 537 | read_outPos(yaml_parser_t *parser, int wrdlen, int translen) { |
| 538 | int *pos = malloc(sizeof(int) * wrdlen); |
| 539 | int i = 0; |
| 540 | yaml_event_t event; |
| 541 | int parse_error = 1; |
| 542 | |
| 543 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SEQUENCE_START_EVENT)) |
| 544 | yaml_error(YAML_SEQUENCE_START_EVENT, &event); |
| 545 | yaml_event_delete(&event); |
| 546 | |
| 547 | while ((parse_error = yaml_parser_parse(parser, &event)) && |
| 548 | (event.type == YAML_SCALAR_EVENT)) { |
| 549 | if (i >= wrdlen) |
| 550 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 551 | "Too many output positions for input string of length %d.", translen); |
| 552 | |
| 553 | pos[i++] = parse_number((const char *)event.data.scalar.value, "output position", |
| 554 | event.start_mark.line + 1); |
| 555 | yaml_event_delete(&event); |
| 556 | } |
| 557 | if (i < wrdlen) |
| 558 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 559 | "Too few output positions (%i) for input string of length %i\n", i, |
| 560 | wrdlen); |
| 561 | if (!parse_error) yaml_parse_error(parser); |
| 562 | if (event.type != YAML_SEQUENCE_END_EVENT) |
| 563 | yaml_error(YAML_SEQUENCE_END_EVENT, &event); |
| 564 | yaml_event_delete(&event); |
| 565 | return pos; |
| 566 | } |
| 567 | |
| 568 | static void |
| 569 | read_cursorPos(yaml_parser_t *parser, int *cursorPos, int *expected_cursorPos, int wrdlen, |
| 570 | int translen) { |
| 571 | yaml_event_t event; |
| 572 | |
| 573 | if (!yaml_parser_parse(parser, &event) || |
| 574 | !(event.type == YAML_SEQUENCE_START_EVENT || event.type == YAML_SCALAR_EVENT)) |
| 575 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 576 | "Expected %s or %s (actual %s)", event_names[YAML_SEQUENCE_START_EVENT], |
| 577 | event_names[YAML_SCALAR_EVENT], event_names[event.type]); |
| 578 | |
| 579 | if (event.type == YAML_SEQUENCE_START_EVENT) { |
| 580 | /* it's a sequence: read the two cursor positions (before and after) */ |
| 581 | yaml_event_delete(&event); |
| 582 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 583 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 584 | *cursorPos = parse_number((const char *)event.data.scalar.value, |
| 585 | "cursor position", event.start_mark.line + 1); |
| 586 | if ((0 > *cursorPos) || (*cursorPos >= wrdlen)) |
| 587 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 588 | "Cursor position (%i) outside of input string of length %i\n", |
| 589 | *cursorPos, wrdlen); |
| 590 | yaml_event_delete(&event); |
| 591 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 592 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 593 | "Too few cursor positions, 2 are expected (before and after)\n"); |
| 594 | *expected_cursorPos = parse_number((const char *)event.data.scalar.value, |
| 595 | "expected cursor position", event.start_mark.line + 1); |
| 596 | if ((0 > *expected_cursorPos) || (*expected_cursorPos >= wrdlen)) |
| 597 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 598 | "Expected cursor position (%i) outside of output string of length " |
| 599 | "%i\n", |
| 600 | *expected_cursorPos, translen); |
| 601 | yaml_event_delete(&event); |
| 602 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SEQUENCE_END_EVENT)) |
| 603 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 604 | "Too many cursor positions, only 2 are expected (before and " |
| 605 | "after)\n"); |
| 606 | yaml_event_delete(&event); |
| 607 | } else { // YAML_SCALAR_EVENT |
| 608 | /* it's just a single value: just read the initial cursor position */ |
| 609 | *cursorPos = parse_number((const char *)event.data.scalar.value, |
| 610 | "cursor position before", event.start_mark.line + 1); |
| 611 | if ((0 > *cursorPos) || (*cursorPos >= wrdlen)) |
| 612 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 613 | "Cursor position (%i) outside of input string of length %i\n", |
| 614 | *cursorPos, wrdlen); |
| 615 | *expected_cursorPos = -1; |
| 616 | yaml_event_delete(&event); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | static void |
| 621 | read_typeform_string(yaml_parser_t *parser, formtype *typeform, typeforms kind, int len) { |
| 622 | yaml_event_t event; |
| 623 | int typeform_len; |
| 624 | |
| 625 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 626 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 627 | typeform_len = strlen((const char *)event.data.scalar.value); |
| 628 | if (typeform_len != len) |
| 629 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 630 | "Too many or too few typeforms (%i) for word of length %i\n", |
| 631 | typeform_len, len); |
| 632 | update_typeform((const char *)event.data.scalar.value, typeform, kind); |
| 633 | yaml_event_delete(&event); |
| 634 | } |
| 635 | |
| 636 | static formtype * |
| 637 | read_typeforms(yaml_parser_t *parser, int len) { |
| 638 | yaml_event_t event; |
| 639 | formtype *typeform = calloc(len, sizeof(formtype)); |
| 640 | int parse_error = 1; |
| 641 | |
| 642 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_MAPPING_START_EVENT)) |
| 643 | yaml_error(YAML_MAPPING_START_EVENT, &event); |
| 644 | yaml_event_delete(&event); |
| 645 | |
| 646 | while ((parse_error = yaml_parser_parse(parser, &event)) && |
| 647 | (event.type == YAML_SCALAR_EVENT)) { |
| 648 | if (strcmp((const char *)event.data.scalar.value, "computer_braille") == 0) { |
| 649 | yaml_event_delete(&event); |
| 650 | read_typeform_string(parser, typeform, computer_braille, len); |
| 651 | } else if (strcmp((const char *)event.data.scalar.value, "no_translate") == 0) { |
| 652 | yaml_event_delete(&event); |
| 653 | read_typeform_string(parser, typeform, no_translate, len); |
| 654 | } else if (strcmp((const char *)event.data.scalar.value, "no_contract") == 0) { |
| 655 | yaml_event_delete(&event); |
| 656 | read_typeform_string(parser, typeform, no_contract, len); |
| 657 | } else { |
| 658 | int i; |
| 659 | typeforms kind = plain_text; |
| 660 | for (i = 0; emph_classes[i]; i++) { |
| 661 | if (strcmp((const char *)event.data.scalar.value, emph_classes[i]) == 0) { |
| 662 | yaml_event_delete(&event); |
| 663 | kind = italic << i; |
| 664 | if (kind > emph_10) |
| 665 | error_at_line(EXIT_FAILURE, 0, file_name, |
| 666 | event.start_mark.line + 1, |
| 667 | "Typeform '%s' was not declared\n", |
| 668 | event.data.scalar.value); |
| 669 | read_typeform_string(parser, typeform, kind, len); |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | yaml_event_delete(&event); |
| 675 | } |
| 676 | if (!parse_error) yaml_parse_error(parser); |
| 677 | |
| 678 | if (event.type != YAML_MAPPING_END_EVENT) yaml_error(YAML_MAPPING_END_EVENT, &event); |
| 679 | yaml_event_delete(&event); |
| 680 | return typeform; |
| 681 | } |
| 682 | |
| 683 | static void |
| 684 | read_options(yaml_parser_t *parser, int testmode, int wordLen, int translationLen, |
| 685 | int *xfail, translationModes *mode, formtype **typeform, |
| 686 | formtype **expected_typeform, int **inPos, int **outPos, int *cursorPos, |
| 687 | int *cursorOutPos, int *maxOutputLen, int *realInputLen) { |
| 688 | yaml_event_t event; |
| 689 | char *option_name; |
| 690 | int parse_error = 1; |
| 691 | |
| 692 | *mode = 0; |
| 693 | *xfail = XFAIL_NONE; |
| 694 | *typeform = NULL; |
| 695 | *expected_typeform = NULL; |
| 696 | *inPos = NULL; |
| 697 | *outPos = NULL; |
| 698 | |
| 699 | while ((parse_error = yaml_parser_parse(parser, &event)) && |
| 700 | (event.type == YAML_SCALAR_EVENT)) { |
| 701 | option_name = |
| 702 | strndup((const char *)event.data.scalar.value, event.data.scalar.length); |
| 703 | |
| 704 | if (!strcmp(option_name, "xfail")) { |
| 705 | yaml_event_delete(&event); |
| 706 | *xfail = read_xfail(parser); |
| 707 | } else if (!strcmp(option_name, "mode")) { |
| 708 | yaml_event_delete(&event); |
| 709 | *mode = read_mode(parser); |
| 710 | } else if (!strcmp(option_name, "typeform")) { |
| 711 | if (testmode != MODE_TRANSLATION_FORWARD) { |
| 712 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 713 | "typeform only supported with testmode 'forward'\n"); |
| 714 | } |
| 715 | yaml_event_delete(&event); |
| 716 | *typeform = read_typeforms(parser, wordLen); |
| 717 | } else if (!strcmp(option_name, "expected_typeform")) { |
| 718 | if (testmode != MODE_TRANSLATION_BACKWARD) { |
| 719 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 720 | "expected_typeform only supported with testmode 'backward'\n"); |
| 721 | } |
| 722 | yaml_event_delete(&event); |
| 723 | *expected_typeform = read_typeforms(parser, translationLen); |
| 724 | } else if (!strcmp(option_name, "inputPos")) { |
| 725 | yaml_event_delete(&event); |
| 726 | *inPos = read_inPos(parser, translationLen); |
| 727 | } else if (!strcmp(option_name, "outputPos")) { |
| 728 | yaml_event_delete(&event); |
| 729 | *outPos = read_outPos(parser, wordLen, translationLen); |
| 730 | } else if (!strcmp(option_name, "cursorPos")) { |
| 731 | if (testmode == MODE_TRANSLATION_BOTH_DIRECTIONS) { |
| 732 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 733 | "cursorPos not supported with testmode 'bothDirections'\n"); |
| 734 | } |
| 735 | yaml_event_delete(&event); |
| 736 | read_cursorPos(parser, cursorPos, cursorOutPos, wordLen, translationLen); |
| 737 | } else if (!strcmp(option_name, "maxOutputLength")) { |
| 738 | if (testmode == MODE_TRANSLATION_BOTH_DIRECTIONS) { |
| 739 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 740 | "maxOutputLength not supported with testmode 'bothDirections'\n"); |
| 741 | } |
| 742 | yaml_event_delete(&event); |
| 743 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 744 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 745 | *maxOutputLen = parse_number((const char *)event.data.scalar.value, |
| 746 | "Maximum output length", event.start_mark.line + 1); |
| 747 | if (*maxOutputLen <= 0) |
| 748 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 749 | "Maximum output length (%i) must be a positive number\n", |
| 750 | *maxOutputLen); |
| 751 | if (*maxOutputLen < translationLen) |
| 752 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 753 | "Expected translation length (%i) must not exceed maximum output " |
| 754 | "length (%i)\n", |
| 755 | translationLen, *maxOutputLen); |
| 756 | yaml_event_delete(&event); |
| 757 | } else if (!strcmp(option_name, "realInputLength")) { |
| 758 | yaml_event_delete(&event); |
| 759 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 760 | yaml_error(YAML_SCALAR_EVENT, &event); |
| 761 | *realInputLen = parse_number((const char *)event.data.scalar.value, |
| 762 | "Real input length", event.start_mark.line + 1); |
| 763 | if (*realInputLen < 0) |
| 764 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 765 | "Real input length (%i) must not be a negative number\n", |
| 766 | *realInputLen); |
| 767 | if (*realInputLen > wordLen) |
| 768 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 769 | "Real input length (%i) must not exceed total input " |
| 770 | "length (%i)\n", |
| 771 | *realInputLen, wordLen); |
| 772 | yaml_event_delete(&event); |
| 773 | } else { |
| 774 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 775 | "Unsupported option %s", option_name); |
| 776 | } |
| 777 | free(option_name); |
| 778 | } |
| 779 | if (!parse_error) yaml_parse_error(parser); |
| 780 | if (event.type != YAML_MAPPING_END_EVENT) yaml_error(YAML_MAPPING_END_EVENT, &event); |
| 781 | yaml_event_delete(&event); |
| 782 | } |
| 783 | |
| 784 | static void |
| 785 | check_translation(yaml_event_t event, char *table, char *word, char *translation, |
| 786 | const char *display_table, char *description, formtype *typeform, |
| 787 | formtype *expected_typeform, translationModes mode, int *expected_inputPos, |
| 788 | int *expected_outputPos, int cursorPos, int expected_cursorPos, int max_outlen, |
| 789 | int real_inlen, int direction, int xfail) { |
| 790 | int r = 0; |
| 791 | // FIXME: Note that the typeform array was constructed using the |
| 792 | // emphasis classes mapping of the last compiled table. This |
| 793 | // means that if we are testing multiple tables at the same time |
| 794 | // they must have the same mapping (i.e. the emphasis classes |
| 795 | // must be defined in the same order). |
| 796 | r = check(table, word, translation, .display_table = display_table, |
| 797 | .typeform = typeform, .expected_typeform = expected_typeform, .mode = mode, |
| 798 | .expected_inputPos = expected_inputPos, |
| 799 | .expected_outputPos = expected_outputPos, .cursorPos = cursorPos, |
| 800 | .expected_cursorPos = expected_cursorPos, .max_outlen = max_outlen, |
| 801 | .real_inlen = real_inlen, .direction = direction, .diagnostics = !xfail); |
| 802 | |
| 803 | if (xfail != r) { |
| 804 | // FAIL or XPASS |
| 805 | if (description) fprintf(stderr, "%s\n", description); |
| 806 | error_at_line(0, 0, file_name, event.start_mark.line + 1, |
| 807 | (xfail ? "Unexpected Pass" : "Failure")); |
| 808 | errors++; |
| 809 | // on error print the table name, as it isn't always clear |
| 810 | // which table we are testing. You can can define a test |
| 811 | // for multiple tables. |
| 812 | fprintf(stderr, "Table: %s\n", table); |
| 813 | if (display_table) fprintf(stderr, "Display table: %s\n", display_table); |
| 814 | // add an empty line after each error |
| 815 | fprintf(stderr, "\n"); |
| 816 | } else if (xfail && r && verbose) { |
| 817 | // XFAIL |
| 818 | // in verbose mode print expected failures |
| 819 | if (description) fprintf(stderr, "%s\n", description); |
| 820 | error_at_line(0, 0, file_name, event.start_mark.line + 1, "Expected Failure"); |
| 821 | fprintf(stderr, "Table: %s\n", table); |
| 822 | if (display_table) fprintf(stderr, "Display table: %s\n", display_table); |
| 823 | fprintf(stderr, "\n"); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | static void |
| 828 | read_test(yaml_parser_t *parser, char **tables, const char *display_table, int testmode) { |
| 829 | yaml_event_t event; |
| 830 | char *description = NULL; |
| 831 | char *word; |
| 832 | char *translation; |
| 833 | int xfail = XFAIL_NONE; |
| 834 | translationModes mode = 0; |
| 835 | formtype *typeform = NULL; |
| 836 | formtype *expected_typeform = NULL; |
| 837 | int *inPos = NULL; |
| 838 | int *outPos = NULL; |
| 839 | int cursorPos = -1; |
| 840 | int cursorOutPos = -1; |
| 841 | int maxOutputLen = -1; |
| 842 | int realInputLen = -1; |
| 843 | |
| 844 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 845 | simple_error("Word expected", parser, &event); |
| 846 | |
| 847 | word = strndup((const char *)event.data.scalar.value, event.data.scalar.length); |
| 848 | yaml_event_delete(&event); |
| 849 | |
| 850 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SCALAR_EVENT)) |
| 851 | simple_error("Translation expected", parser, &event); |
| 852 | |
| 853 | translation = |
| 854 | strndup((const char *)event.data.scalar.value, event.data.scalar.length); |
| 855 | yaml_event_delete(&event); |
| 856 | |
| 857 | if (!yaml_parser_parse(parser, &event)) yaml_parse_error(parser); |
| 858 | |
| 859 | /* Handle an optional description */ |
| 860 | if (event.type == YAML_SCALAR_EVENT) { |
| 861 | description = word; |
| 862 | word = translation; |
| 863 | translation = |
| 864 | strndup((const char *)event.data.scalar.value, event.data.scalar.length); |
| 865 | yaml_event_delete(&event); |
| 866 | |
| 867 | if (!yaml_parser_parse(parser, &event)) yaml_parse_error(parser); |
| 868 | } |
| 869 | |
| 870 | if (event.type == YAML_MAPPING_START_EVENT) { |
| 871 | yaml_event_delete(&event); |
| 872 | read_options(parser, testmode, parsed_strlen(word), parsed_strlen(translation), |
| 873 | &xfail, &mode, &typeform, &expected_typeform, &inPos, &outPos, &cursorPos, |
| 874 | &cursorOutPos, &maxOutputLen, &realInputLen); |
| 875 | |
| 876 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SEQUENCE_END_EVENT)) |
| 877 | yaml_error(YAML_SEQUENCE_END_EVENT, &event); |
| 878 | } else if (event.type != YAML_SEQUENCE_END_EVENT) { |
| 879 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 880 | "Expected %s or %s (actual %s)", event_names[YAML_MAPPING_START_EVENT], |
| 881 | event_names[YAML_SEQUENCE_END_EVENT], event_names[event.type]); |
| 882 | } |
| 883 | |
| 884 | char **table = tables; |
| 885 | int xfail_forward = (xfail == XFAIL_FORWARD || xfail == XFAIL_BOTH); |
| 886 | int xfail_backward = (xfail == XFAIL_BACKWARD || xfail == XFAIL_BOTH); |
| 887 | while (*table) { |
| 888 | switch (testmode) { |
| 889 | case MODE_HYPHENATION: |
| 890 | case MODE_HYPHENATION_BRAILLE: |
| 891 | check_hyphenation( |
| 892 | *table, word, translation, testmode == MODE_HYPHENATION_BRAILLE); |
| 893 | break; |
| 894 | case MODE_DISPLAY: |
| 895 | check_display(*table, word, translation); |
| 896 | break; |
| 897 | case MODE_TRANSLATION_FORWARD: |
| 898 | check_translation(event, *table, word, translation, display_table, |
| 899 | description, typeform, NULL, mode, inPos, outPos, cursorPos, |
| 900 | cursorOutPos, maxOutputLen, realInputLen, DIRECTION_FORWARD, |
| 901 | xfail_forward); |
| 902 | break; |
| 903 | case MODE_TRANSLATION_BACKWARD: |
| 904 | check_translation(event, *table, word, translation, display_table, |
| 905 | description, NULL, expected_typeform, mode, inPos, outPos, cursorPos, |
| 906 | cursorOutPos, maxOutputLen, realInputLen, DIRECTION_BACKWARD, |
| 907 | xfail_backward); |
| 908 | break; |
| 909 | case MODE_TRANSLATION_BOTH_DIRECTIONS: |
| 910 | check_translation(event, *table, word, translation, display_table, |
| 911 | description, typeform, NULL, mode, inPos, outPos, cursorPos, |
| 912 | cursorOutPos, maxOutputLen, realInputLen, DIRECTION_FORWARD, |
| 913 | xfail_forward); |
| 914 | check_translation(event, *table, translation, word, display_table, |
| 915 | description, NULL, expected_typeform, mode, outPos, inPos, cursorPos, |
| 916 | cursorOutPos, maxOutputLen, realInputLen, DIRECTION_BACKWARD, |
| 917 | xfail_backward); |
| 918 | break; |
| 919 | } |
| 920 | table++; |
| 921 | count++; |
| 922 | } |
| 923 | yaml_event_delete(&event); |
| 924 | |
| 925 | free(description); |
| 926 | free(word); |
| 927 | free(translation); |
| 928 | free(typeform); |
| 929 | free(expected_typeform); |
| 930 | free(inPos); |
| 931 | free(outPos); |
| 932 | } |
| 933 | |
| 934 | static void |
| 935 | read_tests( |
| 936 | yaml_parser_t *parser, char **tables, const char *display_table, int testmode) { |
| 937 | yaml_event_t event; |
| 938 | if (!yaml_parser_parse(parser, &event) || (event.type != YAML_SEQUENCE_START_EVENT)) |
| 939 | yaml_error(YAML_SEQUENCE_START_EVENT, &event); |
| 940 | |
| 941 | yaml_event_delete(&event); |
| 942 | |
| 943 | int done = 0; |
| 944 | while (!done) { |
| 945 | if (!yaml_parser_parse(parser, &event)) { |
| 946 | yaml_parse_error(parser); |
| 947 | } |
| 948 | if (event.type == YAML_SEQUENCE_END_EVENT) { |
| 949 | done = 1; |
| 950 | yaml_event_delete(&event); |
| 951 | } else if (event.type == YAML_SEQUENCE_START_EVENT) { |
| 952 | yaml_event_delete(&event); |
| 953 | read_test(parser, tables, display_table, testmode); |
| 954 | } else { |
| 955 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 956 | "Expected %s or %s (actual %s)", event_names[YAML_SEQUENCE_END_EVENT], |
| 957 | event_names[YAML_SEQUENCE_START_EVENT], event_names[event.type]); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | * This custom table resolver handles magic table names that represent |
| 964 | * inline tables. |
| 965 | */ |
| 966 | static char ** |
| 967 | customTableResolver(const char *tableList, const char *base) { |
| 968 | static char *dummy_table[1]; |
| 969 | static char **ret; |
| 970 | char *p = (char *)tableList; |
| 971 | while (*p != '\0') { |
| 972 | if (strncmp(p, inline_table_prefix, strlen(inline_table_prefix)) == 0) |
| 973 | return dummy_table; |
| 974 | while (*p != '\0' && *p != ',') p++; |
| 975 | if (*p == ',') p++; |
| 976 | } |
| 977 | if (ret) { |
| 978 | for (int i = 0; ret[i]; i++) free(ret[i]); |
| 979 | free(ret); |
| 980 | } |
| 981 | ret = _lou_defaultTableResolver(tableList, base); |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | #endif // HAVE_LIBYAML |
| 986 | |
| 987 | int |
| 988 | main(int argc, char *argv[]) { |
| 989 | int optc; |
| 990 | |
| 991 | set_program_name(argv[0]); |
| 992 | |
| 993 | while ((optc = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) switch (optc) { |
| 994 | /* --help and --version exit immediately, per GNU coding standards. */ |
| 995 | case 'v': |
| 996 | version_etc( |
| 997 | stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *)NULL); |
| 998 | exit(EXIT_SUCCESS); |
| 999 | break; |
| 1000 | case 'h': |
| 1001 | print_help(); |
| 1002 | exit(EXIT_SUCCESS); |
| 1003 | break; |
| 1004 | default: |
| 1005 | fprintf(stderr, "Try `%s --help' for more information.\n", program_name); |
| 1006 | exit(EXIT_FAILURE); |
| 1007 | break; |
| 1008 | } |
| 1009 | |
| 1010 | if (optind != argc - 1) { |
| 1011 | /* Print error message and exit. */ |
| 1012 | if (optind < argc - 1) |
| 1013 | fprintf(stderr, "%s: extra operand: %s\n", program_name, argv[optind + 1]); |
| 1014 | else |
| 1015 | fprintf(stderr, "%s: no YAML test file specified\n", program_name); |
| 1016 | |
| 1017 | fprintf(stderr, "Try `%s --help' for more information.\n", program_name); |
| 1018 | exit(EXIT_FAILURE); |
| 1019 | } |
| 1020 | |
| 1021 | #ifdef WITHOUT_YAML |
| 1022 | fprintf(stderr, |
| 1023 | "Skipping tests for %s as yaml was disabled in configure with " |
| 1024 | "--without-yaml\n", |
| 1025 | argv[1]); |
| 1026 | return EXIT_SKIPPED; |
| 1027 | #else |
| 1028 | #ifndef HAVE_LIBYAML |
| 1029 | fprintf(stderr, "Skipping tests for %s as libyaml was not found\n", argv[1]); |
| 1030 | return EXIT_SKIPPED; |
| 1031 | #endif // not HAVE_LIBYAML |
| 1032 | #endif // WITHOUT_YAML |
| 1033 | |
| 1034 | #ifndef WITHOUT_YAML |
| 1035 | #ifdef HAVE_LIBYAML |
| 1036 | |
| 1037 | FILE *file; |
| 1038 | yaml_parser_t parser; |
| 1039 | yaml_event_t event; |
| 1040 | |
| 1041 | file_name = argv[1]; |
| 1042 | file = fopen(file_name, "rb"); |
| 1043 | if (!file) { |
| 1044 | fprintf(stderr, "%s: file not found: %s\n", program_name, file_name); |
| 1045 | exit(3); |
| 1046 | } |
| 1047 | |
| 1048 | char *dir_name = strdup(file_name); |
| 1049 | int i = strlen(dir_name); |
| 1050 | while (i > 0) { |
| 1051 | if (dir_name[i - 1] == '/' || dir_name[i - 1] == '\\') { |
| 1052 | i--; |
| 1053 | break; |
| 1054 | } |
| 1055 | i--; |
| 1056 | } |
| 1057 | dir_name[i] = '\0'; |
| 1058 | // FIXME: problem with this is that |
| 1059 | // LOUIS_TABLEPATH=$(top_srcdir)/tables,... does not work anymore because |
| 1060 | // $(top_srcdir) == .. (not an absolute path) |
| 1061 | if (i > 0) |
| 1062 | if (chdir(dir_name)) |
| 1063 | error(EXIT_FAILURE, EIO, "Cannot change directory to %s", dir_name); |
| 1064 | free(dir_name); |
| 1065 | |
| 1066 | // register custom table resolver |
| 1067 | lou_registerTableResolver(&customTableResolver); |
| 1068 | |
| 1069 | assert(yaml_parser_initialize(&parser)); |
| 1070 | |
| 1071 | yaml_parser_set_input_file(&parser, file); |
| 1072 | |
| 1073 | if (!yaml_parser_parse(&parser, &event) || (event.type != YAML_STREAM_START_EVENT)) { |
| 1074 | yaml_error(YAML_STREAM_START_EVENT, &event); |
| 1075 | } |
| 1076 | |
| 1077 | if (event.data.stream_start.encoding != YAML_UTF8_ENCODING) |
| 1078 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 1079 | "UTF-8 encoding expected (actual %s)", |
| 1080 | encoding_names[event.data.stream_start.encoding]); |
| 1081 | yaml_event_delete(&event); |
| 1082 | |
| 1083 | if (!yaml_parser_parse(&parser, &event) || |
| 1084 | (event.type != YAML_DOCUMENT_START_EVENT)) { |
| 1085 | yaml_error(YAML_DOCUMENT_START_EVENT, &event); |
| 1086 | } |
| 1087 | yaml_event_delete(&event); |
| 1088 | |
| 1089 | if (!yaml_parser_parse(&parser, &event) || (event.type != YAML_MAPPING_START_EVENT)) { |
| 1090 | yaml_error(YAML_MAPPING_START_EVENT, &event); |
| 1091 | } |
| 1092 | yaml_event_delete(&event); |
| 1093 | |
| 1094 | if (!yaml_parser_parse(&parser, &event)) |
| 1095 | simple_error("table expected", &parser, &event); |
| 1096 | |
| 1097 | int MAXTABLES = 400; |
| 1098 | char *tables[MAXTABLES + 1]; |
| 1099 | char *display_table = NULL; |
| 1100 | while (1) { |
| 1101 | if (event.type == YAML_SCALAR_EVENT && |
| 1102 | !strcmp((const char *)event.data.scalar.value, "display")) { |
| 1103 | table_value *v; |
| 1104 | free(display_table); |
| 1105 | v = read_table_value(&parser, event.start_mark.line + 1, 1); |
| 1106 | display_table = strdup((char *)v->name); |
| 1107 | if (v->content) |
| 1108 | compile_inline_table(v); |
| 1109 | else if (!_lou_getDisplayTable(display_table)) |
| 1110 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 1111 | "Display table %s not valid", display_table); |
| 1112 | free_table_value(v); |
| 1113 | yaml_event_delete(&event); |
| 1114 | if (!yaml_parser_parse(&parser, &event)) |
| 1115 | simple_error("table expected", &parser, &event); |
| 1116 | } |
| 1117 | if (!(tables[0] = read_table(&event, &parser, display_table))) break; |
| 1118 | yaml_event_delete(&event); |
| 1119 | int k = 1; |
| 1120 | while (1) { |
| 1121 | if (!yaml_parser_parse(&parser, &event)) |
| 1122 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 1123 | "Expected table or %s (actual %s)", |
| 1124 | event_names[YAML_SCALAR_EVENT], event_names[event.type]); |
| 1125 | if ((tables[k++] = read_table(&event, &parser, display_table))) { |
| 1126 | if (k == MAXTABLES) |
| 1127 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 1128 | "Only %d tables in one YAML test supported", MAXTABLES); |
| 1129 | yaml_event_delete(&event); |
| 1130 | } else |
| 1131 | break; |
| 1132 | } |
| 1133 | |
| 1134 | if (event.type != YAML_SCALAR_EVENT) yaml_error(YAML_SCALAR_EVENT, &event); |
| 1135 | |
| 1136 | int haveRunTests = 0; |
| 1137 | while (1) { |
| 1138 | int testmode = MODE_DEFAULT; |
| 1139 | if (!strcmp((const char *)event.data.scalar.value, "flags")) { |
| 1140 | yaml_event_delete(&event); |
| 1141 | read_flags(&parser, &testmode); |
| 1142 | |
| 1143 | if (!yaml_parser_parse(&parser, &event) || |
| 1144 | (event.type != YAML_SCALAR_EVENT) || |
| 1145 | strcmp((const char *)event.data.scalar.value, "tests")) { |
| 1146 | simple_error("tests expected", &parser, &event); |
| 1147 | } |
| 1148 | yaml_event_delete(&event); |
| 1149 | read_tests(&parser, tables, display_table, testmode); |
| 1150 | haveRunTests = 1; |
| 1151 | |
| 1152 | } else if (!strcmp((const char *)event.data.scalar.value, "tests")) { |
| 1153 | yaml_event_delete(&event); |
| 1154 | read_tests(&parser, tables, display_table, testmode); |
| 1155 | haveRunTests = 1; |
| 1156 | } else { |
| 1157 | if (haveRunTests) { |
| 1158 | break; |
| 1159 | } else { |
| 1160 | simple_error("flags or tests expected", &parser, &event); |
| 1161 | } |
| 1162 | } |
| 1163 | if (!yaml_parser_parse(&parser, &event)) |
| 1164 | error_at_line(EXIT_FAILURE, 0, file_name, event.start_mark.line + 1, |
| 1165 | "Expected table, flags, tests or %s (actual %s)", |
| 1166 | event_names[YAML_MAPPING_END_EVENT], event_names[event.type]); |
| 1167 | if (event.type != YAML_SCALAR_EVENT) break; |
| 1168 | } |
| 1169 | |
| 1170 | char **p = tables; |
| 1171 | while (*p) free(*(p++)); |
| 1172 | } |
| 1173 | if (event.type != YAML_MAPPING_END_EVENT) yaml_error(YAML_MAPPING_END_EVENT, &event); |
| 1174 | yaml_event_delete(&event); |
| 1175 | |
| 1176 | if (!yaml_parser_parse(&parser, &event) || (event.type != YAML_DOCUMENT_END_EVENT)) { |
| 1177 | yaml_error(YAML_DOCUMENT_END_EVENT, &event); |
| 1178 | } |
| 1179 | yaml_event_delete(&event); |
| 1180 | |
| 1181 | if (!yaml_parser_parse(&parser, &event) || (event.type != YAML_STREAM_END_EVENT)) { |
| 1182 | yaml_error(YAML_STREAM_END_EVENT, &event); |
| 1183 | } |
| 1184 | yaml_event_delete(&event); |
| 1185 | |
| 1186 | yaml_parser_delete(&parser); |
| 1187 | |
| 1188 | free(emph_classes); |
| 1189 | free(display_table); |
| 1190 | lou_free(); |
| 1191 | |
| 1192 | assert(!fclose(file)); |
| 1193 | |
| 1194 | printf("%s (%d tests, %d failure%s)\n", (errors ? "FAILURE" : "SUCCESS"), count, |
| 1195 | errors, ((errors != 1) ? "s" : "")); |
| 1196 | |
| 1197 | return errors ? 1 : 0; |
| 1198 | |
| 1199 | #endif // HAVE_LIBYAML |
| 1200 | #endif // not WITHOUT_YAML |
| 1201 | } |