| 1 | /* liblouis Braille Translation and Back-Translation Library |
| 2 | |
| 3 | Copyright (C) 2012 Swiss Library for the Blind, Visually Impaired and Print Disabled |
| 4 | |
| 5 | This program is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <config.h> |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include "internal.h" |
| 25 | #include <getopt.h> |
| 26 | #include "progname.h" |
| 27 | #include "unistr.h" |
| 28 | #include "version-etc.h" |
| 29 | |
| 30 | const char version_etc_copyright[] = |
| 31 | "Copyright %s %d Swiss Library for the Blind, Visually Impaired and Print " |
| 32 | "Disabled."; |
| 33 | |
| 34 | #define AUTHORS "Bert Frees" |
| 35 | |
| 36 | static void |
| 37 | print_help(void) { |
| 38 | printf("\ |
| 39 | Usage: %s [OPTIONS] TABLE[,TABLE,...]\n", |
| 40 | program_name); |
| 41 | fputs("\ |
| 42 | Examine and debug Braille translation tables. This program allows you\n\ |
| 43 | to inspect liblouis translation tables by printing out the list of\n\ |
| 44 | applied translation rules for a given input.\n\n", |
| 45 | stdout); |
| 46 | fputs("\ |
| 47 | -h, --help display this help and exit\n\ |
| 48 | -v, --version display version information and exit\n\ |
| 49 | -f, --forward forward translation using the given table\n\ |
| 50 | -b, --backward backward translation using the given table\n\ |
| 51 | --noContractions Use no contractions\n\ |
| 52 | --dotsIO Display dot patterns\n\ |
| 53 | --ucBrl Use Unicode Braille patterns\n\ |
| 54 | --noUndefined Disable output of undefined dot numbers during back-translation\n\ |
| 55 | --partialTrans Use partial back-translation\n\ |
| 56 | If neither -f nor -b are specified forward translation\n\ |
| 57 | is assumed\n", |
| 58 | stdout); |
| 59 | printf("Report bugs to %s.\n", PACKAGE_BUGREPORT); |
| 60 | #ifdef PACKAGE_PACKAGER_BUG_REPORTS |
| 61 | printf("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS); |
| 62 | #endif |
| 63 | #ifdef PACKAGE_URL |
| 64 | printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | #define BUFSIZE 512 |
| 69 | #define RULESSIZE 512 |
| 70 | |
| 71 | static char * |
| 72 | get_input(void) { |
| 73 | static char input_buffer[BUFSIZE]; |
| 74 | size_t input_length; |
| 75 | input_buffer[0] = 0; |
| 76 | if (!fgets(input_buffer, sizeof(input_buffer), stdin)) exit(EXIT_FAILURE); |
| 77 | input_length = strlen(input_buffer) - 1; |
| 78 | if (input_length < 0) exit(0); |
| 79 | input_buffer[input_length] = 0; |
| 80 | return input_buffer; |
| 81 | } |
| 82 | |
| 83 | static int |
| 84 | get_wide_input(widechar *buffer) { |
| 85 | return _lou_extParseChars(get_input(), buffer); |
| 86 | } |
| 87 | |
| 88 | static char * |
| 89 | print_chars(const widechar *buffer, int length) { |
| 90 | static uint8_t result_buf[BUFSIZE]; |
| 91 | size_t result_len = BUFSIZE - 1; |
| 92 | #ifdef WIDECHARS_ARE_UCS4 |
| 93 | u32_to_u8(buffer, length, result_buf, &result_len); |
| 94 | #else |
| 95 | u16_to_u8(buffer, length, result_buf, &result_len); |
| 96 | #endif |
| 97 | result_buf[result_len] = 0; |
| 98 | return result_buf; |
| 99 | } |
| 100 | |
| 101 | static char * |
| 102 | print_dots(const widechar *buffer, int length) { |
| 103 | static char dots[BUFSIZE]; |
| 104 | strcpy(dots, _lou_showDots(buffer, length)); |
| 105 | return dots; |
| 106 | } |
| 107 | |
| 108 | static char * |
| 109 | print_number(widechar c) { |
| 110 | static char number[BUFSIZE]; |
| 111 | sprintf(number, "%d", c); |
| 112 | return number; |
| 113 | } |
| 114 | |
| 115 | static char * |
| 116 | print_attributes(unsigned long long a) { |
| 117 | static char attr[BUFSIZE]; |
| 118 | strcpy(attr, _lou_showAttributes(a)); |
| 119 | return attr; |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | append_char(char *destination, int *length, char source) { |
| 124 | destination[(*length)++] = source; |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | append_string(char *destination, int *length, char *source) { |
| 129 | strcpy(&destination[(*length)], source); |
| 130 | (*length) += strlen(source); |
| 131 | } |
| 132 | |
| 133 | static char * |
| 134 | print_script(const widechar *buffer, int length, const TranslationTableHeader *table) { |
| 135 | static char script[BUFSIZE]; |
| 136 | int i = 0; |
| 137 | int j = 0; |
| 138 | while (i < length) { |
| 139 | switch (buffer[i]) { |
| 140 | case pass_first: |
| 141 | case pass_last: |
| 142 | case pass_not: |
| 143 | case pass_startReplace: |
| 144 | case pass_endReplace: |
| 145 | case pass_search: |
| 146 | case pass_copy: |
| 147 | case pass_omit: |
| 148 | append_char(script, &j, buffer[i++]); |
| 149 | break; |
| 150 | case pass_lookback: |
| 151 | append_char(script, &j, buffer[i++]); |
| 152 | if (buffer[i] > 1) append_string(script, &j, print_number(buffer[i++])); |
| 153 | break; |
| 154 | case pass_string: |
| 155 | append_char(script, &j, buffer[i]); |
| 156 | append_string(script, &j, print_chars(&buffer[i + 2], buffer[i + 1])); |
| 157 | append_char(script, &j, buffer[i]); |
| 158 | i += (2 + buffer[i + 1]); |
| 159 | break; |
| 160 | case pass_dots: |
| 161 | append_char(script, &j, buffer[i++]); |
| 162 | append_string(script, &j, print_dots(&buffer[i + 1], buffer[i])); |
| 163 | i += (1 + buffer[i]); |
| 164 | break; |
| 165 | case pass_eq: |
| 166 | case pass_lt: |
| 167 | case pass_gt: |
| 168 | case pass_lteq: |
| 169 | case pass_gteq: |
| 170 | append_char(script, &j, '#'); |
| 171 | append_string(script, &j, print_number(buffer[i + 1])); |
| 172 | append_char(script, &j, buffer[i]); |
| 173 | append_string(script, &j, print_number(buffer[i + 2])); |
| 174 | i += 3; |
| 175 | break; |
| 176 | case pass_hyphen: |
| 177 | case pass_plus: |
| 178 | append_char(script, &j, '#'); |
| 179 | append_string(script, &j, print_number(buffer[i + 1])); |
| 180 | append_char(script, &j, buffer[i]); |
| 181 | i += 2; |
| 182 | break; |
| 183 | case pass_attributes: |
| 184 | append_char(script, &j, buffer[i++]); |
| 185 | int attr_start = j - 1; |
| 186 | unsigned long long a = buffer[i++]; |
| 187 | a <<= 16; |
| 188 | a |= buffer[i++]; |
| 189 | a <<= 16; |
| 190 | a |= buffer[i++]; |
| 191 | a <<= 16; |
| 192 | a |= buffer[i++]; |
| 193 | append_string(script, &j, print_attributes(a)); |
| 194 | // If this returned "$", "$w", "$x", "$y" or "$z", we're dealing with a single |
| 195 | // custom attribute. In this case look up the name of the attribute and use |
| 196 | // the "%xxx" syntax. |
| 197 | if (j - attr_start == 1 || |
| 198 | (j - attr_start == 2 && script[j - 1] >= 'w' && |
| 199 | script[j - 1] <= 'z')) { |
| 200 | j = attr_start; |
| 201 | append_char(script, &j, '%'); |
| 202 | const CharacterClass *class = table->characterClasses; |
| 203 | while (class) { |
| 204 | if (class->attribute == a) { |
| 205 | append_string( |
| 206 | script, &j, print_chars(class->name, class->length)); |
| 207 | break; |
| 208 | } |
| 209 | class = class->next; |
| 210 | } |
| 211 | } |
| 212 | if (buffer[i] == 1 && buffer[i + 1] == 1) { |
| 213 | } else if (buffer[i] == 1 && buffer[i + 1] == 0xffff) |
| 214 | append_char(script, &j, pass_until); |
| 215 | else if (buffer[i] == buffer[i + 1]) |
| 216 | append_string(script, &j, print_number(buffer[i])); |
| 217 | else { |
| 218 | append_string(script, &j, print_number(buffer[i])); |
| 219 | append_char(script, &j, '-'); |
| 220 | append_string(script, &j, print_number(buffer[i + 1])); |
| 221 | } |
| 222 | i += 2; |
| 223 | break; |
| 224 | case pass_swap: |
| 225 | append_char(script, &j, buffer[i++]); |
| 226 | TranslationTableOffset swapRuleOffset = buffer[i++]; |
| 227 | swapRuleOffset <<= 16; |
| 228 | swapRuleOffset |= buffer[i++]; |
| 229 | const RuleName *nameRule = table->ruleNames; |
| 230 | while (nameRule) { |
| 231 | if (nameRule->ruleOffset == swapRuleOffset) { |
| 232 | append_string( |
| 233 | script, &j, print_chars(nameRule->name, nameRule->length)); |
| 234 | break; |
| 235 | } |
| 236 | nameRule = nameRule->next; |
| 237 | } |
| 238 | break; |
| 239 | case pass_endTest: |
| 240 | append_char(script, &j, '\t'); |
| 241 | i++; |
| 242 | break; |
| 243 | case pass_groupstart: |
| 244 | case pass_groupend: |
| 245 | case pass_groupreplace: |
| 246 | /* TBD */ |
| 247 | default: |
| 248 | i++; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | script[j] = 0; |
| 253 | return script; |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | print_rule(const TranslationTableRule *rule, const TranslationTableHeader *table) { |
| 258 | const char *opcode = _lou_findOpcodeName(rule->opcode); |
| 259 | char *chars; |
| 260 | char *dots; |
| 261 | char *script; |
| 262 | switch (rule->opcode) { |
| 263 | case CTO_Context: |
| 264 | case CTO_Correct: |
| 265 | case CTO_SwapCd: |
| 266 | case CTO_SwapDd: |
| 267 | case CTO_Pass2: |
| 268 | case CTO_Pass3: |
| 269 | case CTO_Pass4: |
| 270 | script = print_script(&rule->charsdots[rule->charslen], rule->dotslen, table); |
| 271 | printf("%s\t%s\n", opcode, script); |
| 272 | break; |
| 273 | default: |
| 274 | chars = print_chars(rule->charsdots, rule->charslen); |
| 275 | dots = print_dots(&rule->charsdots[rule->charslen], rule->dotslen); |
| 276 | printf("%s\t%s\t%s\n", opcode, chars, dots); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | main_loop(int backward_translation, char *table, int mode) { |
| 283 | widechar inbuf[BUFSIZE]; |
| 284 | widechar outbuf[BUFSIZE]; |
| 285 | int inlen; |
| 286 | int outlen; |
| 287 | const TranslationTableHeader *translationTable; |
| 288 | const DisplayTableHeader *displayTable; |
| 289 | const TranslationTableRule **rules = malloc(512 * sizeof(TranslationTableRule)); |
| 290 | int ruleslen; |
| 291 | int i, j; |
| 292 | while (1) { |
| 293 | inlen = get_wide_input(inbuf); |
| 294 | outlen = BUFSIZE; |
| 295 | ruleslen = RULESSIZE; |
| 296 | _lou_getTable(table, table, &translationTable, &displayTable); |
| 297 | if (backward_translation) { |
| 298 | if (!_lou_backTranslate(table, table, inbuf, &inlen, outbuf, &outlen, NULL, |
| 299 | NULL, NULL, NULL, NULL, mode, rules, &ruleslen)) |
| 300 | break; |
| 301 | } else if (!_lou_translate(table, table, inbuf, &inlen, outbuf, &outlen, NULL, |
| 302 | NULL, NULL, NULL, NULL, mode, rules, &ruleslen)) |
| 303 | break; |
| 304 | if ((mode & dotsIO) && !backward_translation) |
| 305 | printf("Dot patterns: %s\n", _lou_showDots(outbuf, outlen)); |
| 306 | else |
| 307 | printf("%s\n", print_chars(outbuf, outlen)); |
| 308 | j = 0; |
| 309 | for (i = 0; i < ruleslen; i++) { |
| 310 | if (rules[i]->opcode < 0 || rules[i]->opcode >= CTO_None) continue; |
| 311 | printf("%d.\t", ++j); |
| 312 | print_rule(rules[i], translationTable); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | int |
| 318 | main(int argc, char **argv) { |
| 319 | int optc; |
| 320 | char *table; |
| 321 | |
| 322 | int forward_flag = 0; |
| 323 | int backward_flag = 0; |
| 324 | int mode = 0; |
| 325 | int noContractionsMode = 0; |
| 326 | int dotsIOMode = 0; |
| 327 | int ucBrlMode = 0; |
| 328 | int noUndefinedMode = 0; |
| 329 | int noUndefinedDotsMode = 0; |
| 330 | int partialTransMode = 0; |
| 331 | |
| 332 | const struct option longopts[] = { |
| 333 | { "help", no_argument, NULL, 'h' }, |
| 334 | { "version", no_argument, NULL, 'v' }, |
| 335 | { "forward", no_argument, NULL, 'f' }, |
| 336 | { "backward", no_argument, NULL, 'b' }, |
| 337 | { "noContractions", no_argument, &noContractionsMode, noContractions }, |
| 338 | { "dotsIO", no_argument, &dotsIOMode, dotsIO }, |
| 339 | { "ucBrl", no_argument, &ucBrlMode, ucBrl }, |
| 340 | { "noUndefined", no_argument, &noUndefinedMode, noUndefined }, |
| 341 | { "noUndefinedDots", no_argument, &noUndefinedDotsMode, noUndefined }, |
| 342 | { "partialTrans", no_argument, &partialTransMode, partialTrans }, |
| 343 | { NULL, 0, NULL, 0 }, |
| 344 | }; |
| 345 | |
| 346 | set_program_name(argv[0]); |
| 347 | while ((optc = getopt_long(argc, argv, "hvfb", longopts, NULL)) != -1) { |
| 348 | switch (optc) { |
| 349 | case 'v': |
| 350 | version_etc( |
| 351 | stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *)NULL); |
| 352 | exit(EXIT_SUCCESS); |
| 353 | break; |
| 354 | case 'h': |
| 355 | print_help(); |
| 356 | exit(EXIT_SUCCESS); |
| 357 | break; |
| 358 | case 'f': |
| 359 | forward_flag = 1; |
| 360 | break; |
| 361 | case 'b': |
| 362 | backward_flag = 1; |
| 363 | break; |
| 364 | case 0: |
| 365 | break; |
| 366 | default: |
| 367 | fprintf(stderr, "Try `%s --help' for more information.\n", program_name); |
| 368 | exit(EXIT_FAILURE); |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | if (noUndefinedDotsMode) { |
| 373 | fprintf(stderr, "%s: did you mean --noUndefined?\n", program_name); |
| 374 | fprintf(stderr, "Try `%s --help' for more information.\n", program_name); |
| 375 | exit(EXIT_FAILURE); |
| 376 | } |
| 377 | mode |= noContractionsMode | dotsIOMode | ucBrlMode | noUndefinedMode | |
| 378 | partialTransMode; |
| 379 | if (forward_flag && backward_flag) { |
| 380 | fprintf(stderr, "%s: specify either -f or -b but not both\n", program_name); |
| 381 | fprintf(stderr, "Try `%s --help' for more information.\n", program_name); |
| 382 | exit(EXIT_FAILURE); |
| 383 | } |
| 384 | if (optind != argc - 1) { |
| 385 | if (optind < argc - 1) |
| 386 | fprintf(stderr, "%s: extra operand: %s\n", program_name, argv[optind + 1]); |
| 387 | else |
| 388 | fprintf(stderr, "%s: no table specified\n", program_name); |
| 389 | fprintf(stderr, "Try `%s --help' for more information.\n", program_name); |
| 390 | exit(EXIT_FAILURE); |
| 391 | } |
| 392 | table = argv[optind]; |
| 393 | if (!lou_getTable(table)) { |
| 394 | lou_free(); |
| 395 | exit(EXIT_FAILURE); |
| 396 | } |
| 397 | main_loop(backward_flag, table, mode); |
| 398 | lou_free(); |
| 399 | exit(EXIT_SUCCESS); |
| 400 | } |