| 1 | /* liblouis Braille Translation and Back-Translation Library |
| 2 | |
| 3 | Based on the Linux screenreader BRLTTY, copyright (C) 1999-2006 by |
| 4 | The BRLTTY Team |
| 5 | |
| 6 | Copyright (C) 2004, 2005, 2006, 2009 |
| 7 | ViewPlus Technologies, Inc. www.viewplus.com and |
| 8 | JJB Software, Inc. www.jjb-software.com |
| 9 | |
| 10 | This program is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #include <config.h> |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdint.h> |
| 30 | #include "liblouis.h" |
| 31 | #include "internal.h" |
| 32 | #include <getopt.h> |
| 33 | #include "progname.h" |
| 34 | #include "unistr.h" |
| 35 | #include "version-etc.h" |
| 36 | |
| 37 | static const struct option longopts[] = { |
| 38 | { "help", no_argument, NULL, 'h' }, |
| 39 | { "version", no_argument, NULL, 'v' }, |
| 40 | { NULL, 0, NULL, 0 }, |
| 41 | }; |
| 42 | |
| 43 | const char version_etc_copyright[] = |
| 44 | "Copyright %s %d ViewPlus Technologies, Inc. and JJB Software, Inc."; |
| 45 | |
| 46 | #define AUTHORS "John J. Boyer" |
| 47 | |
| 48 | static void |
| 49 | print_help(void) { |
| 50 | printf("\ |
| 51 | Usage: %s [OPTIONS]\n", |
| 52 | program_name); |
| 53 | |
| 54 | fputs("\ |
| 55 | This program tests every capability of the liblouis library. It is\n\ |
| 56 | completely interactive. \n\n", |
| 57 | stdout); |
| 58 | |
| 59 | fputs("\ |
| 60 | -h, --help display this help and exit\n\ |
| 61 | -v, --version display version information and exit\n", |
| 62 | stdout); |
| 63 | |
| 64 | printf("\n"); |
| 65 | printf("Report bugs to %s.\n", PACKAGE_BUGREPORT); |
| 66 | |
| 67 | #ifdef PACKAGE_PACKAGER_BUG_REPORTS |
| 68 | printf("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS); |
| 69 | #endif |
| 70 | #ifdef PACKAGE_URL |
| 71 | printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL); |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | #define BUFSIZE 256 |
| 76 | |
| 77 | static char inputBuffer[BUFSIZE]; |
| 78 | static const void *validTable = NULL; |
| 79 | static int forwardOnly = 0; |
| 80 | static int backOnly = 0; |
| 81 | static int showPositions = 0; |
| 82 | static int minimalist = 0; |
| 83 | static int outputSize = BUFSIZE; |
| 84 | static int showSizes = 0; |
| 85 | static int enteredCursorPos = -1; |
| 86 | static unsigned int mode; |
| 87 | static char table[BUFSIZE]; |
| 88 | static formtype emphasis[BUFSIZE]; |
| 89 | static char enteredEmphasis[BUFSIZE]; |
| 90 | |
| 91 | static int |
| 92 | getInput(void) { |
| 93 | int inputLength; |
| 94 | inputBuffer[0] = 0; |
| 95 | if (!fgets(inputBuffer, sizeof(inputBuffer), stdin)) exit(EXIT_FAILURE); |
| 96 | inputLength = strlen(inputBuffer) - 1; |
| 97 | if (inputLength < 0) /* EOF on script */ |
| 98 | { |
| 99 | lou_free(); |
| 100 | exit(EXIT_SUCCESS); |
| 101 | } |
| 102 | inputBuffer[inputLength] = 0; |
| 103 | return inputLength; |
| 104 | } |
| 105 | |
| 106 | static int |
| 107 | getYN(void) { |
| 108 | printf("? y/n: "); |
| 109 | getInput(); |
| 110 | if (inputBuffer[0] == 'y') return 1; |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | paramLetters(void) { |
| 116 | printf("Press one of the letters in parentheses, then enter.\n"); |
| 117 | printf("(t)able, (r)un, (m)ode, (c)ursor, (e)mphasis, (h)elp,\n"); |
| 118 | printf("(q)uit, (f)orward-only, (b)ack-only, show-(p)ositions m(i)nimal.\n"); |
| 119 | printf("test-(l)engths.\n"); |
| 120 | } |
| 121 | |
| 122 | static int |
| 123 | getCommands(void) { |
| 124 | paramLetters(); |
| 125 | do { |
| 126 | printf("Command: "); |
| 127 | getInput(); |
| 128 | switch (inputBuffer[0]) { |
| 129 | case 0: |
| 130 | break; |
| 131 | case 't': |
| 132 | do { |
| 133 | printf("Enter the name of a table: "); |
| 134 | getInput(); |
| 135 | strcpy(table, inputBuffer); |
| 136 | } while ((validTable = lou_getTable(table)) == NULL); |
| 137 | break; |
| 138 | case 'r': |
| 139 | if (validTable == NULL) { |
| 140 | printf("You must enter a valid table name.\n"); |
| 141 | inputBuffer[0] = 0; |
| 142 | } |
| 143 | break; |
| 144 | case 'm': |
| 145 | printf("Reset mode"); |
| 146 | if (getYN()) mode = 0; |
| 147 | printf("No contractions"); |
| 148 | mode |= noContractions * getYN(); |
| 149 | printf("Computer Braille at cursor"); |
| 150 | mode |= compbrlAtCursor * getYN(); |
| 151 | printf("Dots input and output"); |
| 152 | mode |= dotsIO * getYN(); |
| 153 | printf("Computer Braille left of cursor"); |
| 154 | mode |= compbrlLeftCursor * getYN(); |
| 155 | printf("Unicode Braille"); |
| 156 | mode |= ucBrl * getYN(); |
| 157 | printf("No undefined dots"); |
| 158 | mode |= noUndefined * getYN(); |
| 159 | printf("Partial back-translation"); |
| 160 | mode |= partialTrans * getYN(); |
| 161 | break; |
| 162 | case 'l': |
| 163 | printf("Do you want to test input and output lengths"); |
| 164 | showSizes = getYN(); |
| 165 | if (!showSizes) { |
| 166 | outputSize = BUFSIZE; |
| 167 | break; |
| 168 | } |
| 169 | printf("Enter a maximum output size: "); |
| 170 | getInput(); |
| 171 | outputSize = atoi(inputBuffer); |
| 172 | if (outputSize < 0 || outputSize > BUFSIZE) { |
| 173 | printf("Output size must be from 0 tu %d.\n", BUFSIZE); |
| 174 | outputSize = BUFSIZE; |
| 175 | showSizes = 0; |
| 176 | } |
| 177 | break; |
| 178 | case 'c': |
| 179 | printf("Enter a cursor position: "); |
| 180 | getInput(); |
| 181 | enteredCursorPos = atoi(inputBuffer); |
| 182 | if (enteredCursorPos < -1 || enteredCursorPos > outputSize) { |
| 183 | printf("Cursor position must be from -1 to %d.\n", outputSize); |
| 184 | enteredCursorPos = -1; |
| 185 | } |
| 186 | break; |
| 187 | case 'e': |
| 188 | printf("(Enter an x to cancel emphasis.)\n"); |
| 189 | printf("Enter an emphasis string: "); |
| 190 | getInput(); |
| 191 | strcpy(enteredEmphasis, inputBuffer); |
| 192 | break; |
| 193 | case 'h': |
| 194 | printf("Commands: action\n"); |
| 195 | printf("(t)able: Enter a table name\n"); |
| 196 | printf("(r)un: run the translation/back-translation loop\n"); |
| 197 | printf("(m)ode: Enter a mode parameter\n"); |
| 198 | printf("(c)ursor: Enter a cursor position\n"); |
| 199 | printf("(e)mphasis: Enter an emphasis string\n"); |
| 200 | printf("(h)elp: print this page\n"); |
| 201 | printf("(q)uit: leave the program\n"); |
| 202 | printf("(f)orward-only: do only forward translation\n"); |
| 203 | printf("(b)ack-only: do only back-translation\n"); |
| 204 | printf("show-(p)ositions: show input and output positions\n"); |
| 205 | printf("m(i)nimal: test translator and back-translator with minimal " |
| 206 | "parameters\n"); |
| 207 | printf("test-(l)engths: test accuracy of returned lengths\n"); |
| 208 | printf("\n"); |
| 209 | paramLetters(); |
| 210 | break; |
| 211 | case 'q': |
| 212 | lou_free(); |
| 213 | exit(EXIT_SUCCESS); |
| 214 | case 'f': |
| 215 | printf("Do only forward translation"); |
| 216 | forwardOnly = getYN(); |
| 217 | break; |
| 218 | case 'b': |
| 219 | printf("Do only backward translation"); |
| 220 | backOnly = getYN(); |
| 221 | break; |
| 222 | case 'p': |
| 223 | printf("Show input and output positions"); |
| 224 | showPositions = getYN(); |
| 225 | break; |
| 226 | case 'i': |
| 227 | printf("Test translation/back-translation loop with minimal parameters"); |
| 228 | minimalist = getYN(); |
| 229 | break; |
| 230 | default: |
| 231 | printf("Bad choice.\n"); |
| 232 | break; |
| 233 | } |
| 234 | if (forwardOnly && backOnly) |
| 235 | printf("You cannot specify both forward-only and backward-only " |
| 236 | "translation.\n"); |
| 237 | } while (inputBuffer[0] != 'r'); |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | int |
| 242 | main(int argc, char **argv) { |
| 243 | uint8_t *charbuf; |
| 244 | size_t charlen; |
| 245 | widechar inbuf[BUFSIZE]; |
| 246 | widechar transbuf[BUFSIZE]; |
| 247 | widechar outbuf[BUFSIZE]; |
| 248 | int outputPos[BUFSIZE]; |
| 249 | int inputPos[BUFSIZE]; |
| 250 | int inlen; |
| 251 | int translen; |
| 252 | int outlen; |
| 253 | int cursorPos = -1; |
| 254 | int realInlen = 0; |
| 255 | int optc; |
| 256 | |
| 257 | set_program_name(argv[0]); |
| 258 | |
| 259 | while ((optc = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) { |
| 260 | switch (optc) { |
| 261 | /* --help and --version exit immediately, per GNU coding standards. */ |
| 262 | case 'v': |
| 263 | version_etc( |
| 264 | stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *)NULL); |
| 265 | exit(EXIT_SUCCESS); |
| 266 | break; |
| 267 | case 'h': |
| 268 | print_help(); |
| 269 | exit(EXIT_SUCCESS); |
| 270 | break; |
| 271 | default: |
| 272 | fprintf(stderr, "Try `%s --help' for more information.\n", program_name); |
| 273 | exit(EXIT_FAILURE); |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (optind < argc) { |
| 279 | /* Print error message and exit. */ |
| 280 | fprintf(stderr, "%s: extra operand: %s\n", program_name, argv[optind]); |
| 281 | fprintf(stderr, "Try `%s --help' for more information.\n", program_name); |
| 282 | exit(EXIT_FAILURE); |
| 283 | } |
| 284 | |
| 285 | validTable = NULL; |
| 286 | enteredCursorPos = -1; |
| 287 | mode = 0; |
| 288 | while (1) { |
| 289 | getCommands(); |
| 290 | printf("Type something, press enter, and view the results.\n"); |
| 291 | printf("A blank line returns to command entry.\n"); |
| 292 | if (minimalist) |
| 293 | while (1) { |
| 294 | translen = outputSize; |
| 295 | outlen = outputSize; |
| 296 | inlen = getInput(); |
| 297 | if (inlen == 0) break; |
| 298 | if (!(realInlen = _lou_extParseChars(inputBuffer, inbuf))) break; |
| 299 | inlen = realInlen; |
| 300 | if (!lou_translateString( |
| 301 | table, inbuf, &inlen, transbuf, &translen, NULL, NULL, 0)) |
| 302 | break; |
| 303 | transbuf[translen] = 0; |
| 304 | printf("Translation:\n"); |
| 305 | #ifdef WIDECHARS_ARE_UCS4 |
| 306 | charbuf = u32_to_u8(transbuf, translen, NULL, &charlen); |
| 307 | #else |
| 308 | charbuf = u16_to_u8(transbuf, translen, NULL, &charlen); |
| 309 | #endif |
| 310 | printf("%.*s\n", (int)charlen, charbuf); |
| 311 | free(charbuf); |
| 312 | if (showSizes) |
| 313 | printf("input length = %d; output length = %d\n", inlen, translen); |
| 314 | lou_backTranslateString( |
| 315 | table, transbuf, &translen, outbuf, &outlen, NULL, NULL, 0); |
| 316 | printf("Back-translation:\n"); |
| 317 | #ifdef WIDECHARS_ARE_UCS4 |
| 318 | charbuf = u32_to_u8(outbuf, outlen, NULL, &charlen); |
| 319 | #else |
| 320 | charbuf = u16_to_u8(outbuf, outlen, NULL, &charlen); |
| 321 | #endif |
| 322 | printf("%.*s\n", (int)charlen, charbuf); |
| 323 | free(charbuf); |
| 324 | if (showSizes) |
| 325 | printf("input length = %d; output length = %d.\n", translen, outlen); |
| 326 | if (outlen == realInlen) { |
| 327 | int k; |
| 328 | for (k = 0; k < realInlen; k++) |
| 329 | if (inbuf[k] != outbuf[k]) break; |
| 330 | if (k == realInlen) printf("Perfect roundtrip!\n"); |
| 331 | } |
| 332 | } |
| 333 | else |
| 334 | while (1) { |
| 335 | memset(emphasis, 0, sizeof(formtype) * BUFSIZE); |
| 336 | { |
| 337 | size_t k = 0; |
| 338 | for (k = 0; k < strlen(enteredEmphasis); k++) |
| 339 | emphasis[k] = (formtype)enteredEmphasis[k] - '0'; |
| 340 | emphasis[k] = 0; |
| 341 | } |
| 342 | cursorPos = enteredCursorPos; |
| 343 | inlen = getInput(); |
| 344 | if (inlen == 0) break; |
| 345 | outlen = outputSize; |
| 346 | if (backOnly) { |
| 347 | if (!(translen = _lou_extParseChars(inputBuffer, transbuf))) break; |
| 348 | inlen = realInlen; |
| 349 | } else { |
| 350 | translen = outputSize; |
| 351 | if (!(realInlen = _lou_extParseChars(inputBuffer, inbuf))) break; |
| 352 | inlen = realInlen; |
| 353 | if (!lou_translate(table, inbuf, &inlen, transbuf, &translen, |
| 354 | emphasis, NULL, &outputPos[0], &inputPos[0], &cursorPos, |
| 355 | mode)) |
| 356 | break; |
| 357 | transbuf[translen] = 0; |
| 358 | if (mode & dotsIO) { |
| 359 | printf("Translation dot patterns:\n"); |
| 360 | printf("%s\n", _lou_showDots(transbuf, translen)); |
| 361 | } else { |
| 362 | printf("Translation:\n"); |
| 363 | #ifdef WIDECHARS_ARE_UCS4 |
| 364 | charbuf = u32_to_u8(transbuf, translen, NULL, &charlen); |
| 365 | #else |
| 366 | charbuf = u16_to_u8(transbuf, translen, NULL, &charlen); |
| 367 | #endif |
| 368 | printf("%.*s\n", (int)charlen, charbuf); |
| 369 | free(charbuf); |
| 370 | if (showSizes) |
| 371 | printf("input length = %d; output length = %d\n", inlen, |
| 372 | translen); |
| 373 | } |
| 374 | } |
| 375 | if (cursorPos != -1) printf("Cursor position: %d\n", cursorPos); |
| 376 | if (showPositions) { |
| 377 | printf("Output positions:\n"); |
| 378 | for (int k = 0; k < inlen; k++) printf("%d ", outputPos[k]); |
| 379 | printf("\n"); |
| 380 | printf("Input positions:\n"); |
| 381 | for (int k = 0; k < translen; k++) printf("%d ", inputPos[k]); |
| 382 | printf("\n"); |
| 383 | } |
| 384 | if (!forwardOnly) { |
| 385 | if (!lou_backTranslate(table, transbuf, &translen, outbuf, &outlen, |
| 386 | emphasis, NULL, &outputPos[0], &inputPos[0], &cursorPos, |
| 387 | mode)) |
| 388 | break; |
| 389 | printf("Back-translation:\n"); |
| 390 | #ifdef WIDECHARS_ARE_UCS4 |
| 391 | charbuf = u32_to_u8(outbuf, outlen, NULL, &charlen); |
| 392 | #else |
| 393 | charbuf = u16_to_u8(outbuf, outlen, NULL, &charlen); |
| 394 | #endif |
| 395 | printf("%.*s\n", (int)charlen, charbuf); |
| 396 | free(charbuf); |
| 397 | if (showSizes) |
| 398 | printf("input length = %d; output length = %d\n", translen, |
| 399 | outlen); |
| 400 | if (cursorPos != -1) printf("Cursor position: %d\n", cursorPos); |
| 401 | if (showPositions) { |
| 402 | printf("Output positions:\n"); |
| 403 | for (int k = 0; k < translen; k++) printf("%d ", outputPos[k]); |
| 404 | printf("\n"); |
| 405 | printf("Input positions:\n"); |
| 406 | for (int k = 0; k < outlen; k++) printf("%d ", inputPos[k]); |
| 407 | printf("\n"); |
| 408 | } |
| 409 | } |
| 410 | if (!(forwardOnly || backOnly)) { |
| 411 | if (outlen == realInlen) { |
| 412 | int k; |
| 413 | for (k = 0; k < realInlen; k++) |
| 414 | if (inbuf[k] != outbuf[k]) break; |
| 415 | if (k == realInlen) printf("Perfect roundtrip!\n"); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | lou_free(); |
| 421 | exit(EXIT_SUCCESS); |
| 422 | } |