Debian Codegraph @

tools/lou_debug.c

raw symbols SCIP
1 /* liblouis Braille Translation and Back-Translation Library
2
3 Based on 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 "internal.h"
30 #include <getopt.h>
31 #include "progname.h"
32 #include "unistr.h"
33 #include "version-etc.h"
34
35 static const struct option longopts[] = {
36 { "help", no_argument, NULL, 'h' },
37 { "version", no_argument, NULL, 'v' },
38 { NULL, 0, NULL, 0 },
39 };
40
41 const char version_etc_copyright[] =
42 "Copyright %s %d ViewPlus Technologies, Inc. and JJB Software, Inc.";
43
44 #define AUTHORS "John J. Boyer"
45
46 static void
47 print_help(void) {
48 printf("\
49 Usage: %s [OPTIONS] TABLE[,TABLE,...]\n",
50 program_name);
51
52 fputs("\
53 Examine and debug Braille translation tables. This program allows you\n\
54 to inspect liblouis translation tables and gather information about\n\
55 them, such as forward and backward rules, characters and dot patterns,\n\
56 specific opcodes, the size of a table, whether a hyphenation\n\
57 table is used, how many passes the translation takes and much\n\
58 more.\n\n",
59 stdout);
60
61 fputs("\
62 -h, --help display this help and exit\n\
63 -v, --version display version information and exit\n",
64 stdout);
65
66 printf("\n");
67 printf("Report bugs to %s.\n", PACKAGE_BUGREPORT);
68
69 #ifdef PACKAGE_PACKAGER_BUG_REPORTS
70 printf("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS);
71 #endif
72 #ifdef PACKAGE_URL
73 printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
74 #endif
75 }
76
77 #define BUFSIZE 256
78
79 static const TranslationTableHeader *table;
80 static const DisplayTableHeader *displayTable;
81 static char inputBuffer[BUFSIZE];
82
83 static int
84 getInput(void) {
85 int inputLength;
86 inputBuffer[0] = 0;
87 if (!fgets(inputBuffer, sizeof(inputBuffer), stdin)) exit(EXIT_FAILURE);
88 inputLength = strlen(inputBuffer) - 1;
89 if (inputLength < 0) /* EOF on script */
90 exit(EXIT_FAILURE);
91 inputBuffer[inputLength] = 0;
92 return inputLength;
93 }
94
95 static char *
96 print_chars(const widechar *buffer, int length) {
97 static uint8_t result_buf[BUFSIZE];
98 size_t result_len = BUFSIZE - 1;
99 #ifdef WIDECHARS_ARE_UCS4
100 u32_to_u8(buffer, length, result_buf, &result_len);
101 #else
102 u16_to_u8(buffer, length, result_buf, &result_len);
103 #endif
104 result_buf[result_len] = 0;
105 return result_buf;
106 }
107
108 static int
109 printRule(TranslationTableRule *thisRule, int mode) {
110 printf("Rule: ");
111 printf("opcode=%s, ", _lou_findOpcodeName(thisRule->opcode));
112 if (thisRule->before) printf("before=%llx, ", thisRule->before);
113 if (thisRule->after) printf("after=%llx, ", thisRule->after);
114 switch (thisRule->opcode) {
115 case CTO_Context:
116 case CTO_Correct:
117 case CTO_SwapCd:
118 case CTO_SwapDd:
119 case CTO_Pass2:
120 case CTO_Pass3:
121 case CTO_Pass4:
122 printf("code=%s ",
123 print_chars(thisRule->charsdots, thisRule->charslen + thisRule->dotslen));
124 break;
125 default:
126 if (mode == 0) {
127 printf("chars=%s, ", print_chars(thisRule->charsdots, thisRule->charslen));
128 printf("dots=%s, ",
129 _lou_showDots(
130 &thisRule->charsdots[thisRule->charslen], thisRule->dotslen));
131 } else {
132 printf("dots=%s, ",
133 _lou_showDots(
134 &thisRule->charsdots[thisRule->charslen], thisRule->dotslen));
135 printf("chars=%s, ", print_chars(thisRule->charsdots, thisRule->charslen));
136 }
137 break;
138 }
139 return 1;
140 }
141
142 static int
143 printCharacter(TranslationTableCharacter *thisChar, int mode) {
144 TranslationTableRule *thisRule;
145 TranslationTableOffset nextRule;
146 if (mode == 0) {
147 printf("Char: ");
148 printf("value=%s, ", print_chars(&thisChar->value, 1));
149 } else
150 printf("Dots: value=%s, ", _lou_showDots(&thisChar->value, 1));
151 printf("attr=%s, ", _lou_showAttributes(thisChar->attributes));
152 nextRule = thisChar->otherRules;
153 while (nextRule) {
154 thisRule = (TranslationTableRule *)&table->ruleArea[nextRule];
155 if (nextRule == thisChar->definitionRule) printf("definition ");
156 printRule(thisRule, mode);
157 printf("\n");
158 if (mode == 0)
159 nextRule = thisRule->charsnext;
160 else
161 nextRule = thisRule->dotsnext;
162 }
163 if (mode == 0 && thisChar->compRule) {
164 TranslationTableRule *compRule =
165 (TranslationTableRule *)&table->ruleArea[thisChar->compRule];
166 printf("comp6 ");
167 printRule(compRule, 0);
168 printf("\n");
169 }
170 return 1;
171 }
172
173 static int
174 show_characters(int startHash) {
175 int k;
176 TranslationTableCharacter *thisChar;
177 TranslationTableOffset nextChar;
178 printf("Press enter for next or (e)xit, next-(h)ash, then enter\n");
179 if (startHash < 0)
180 k = 0;
181 else
182 k = startHash;
183 for (; k < HASHNUM; k++)
184 if (table->characters[k]) {
185 printf("Hash=%d\n", k);
186 nextChar = table->characters[k];
187 while (nextChar) {
188 thisChar = (TranslationTableCharacter *)&table->ruleArea[nextChar];
189 printCharacter(thisChar, 0);
190 printf("=> ");
191 getInput();
192 if (*inputBuffer == 'h') break;
193 if (*inputBuffer == 'e') return 1;
194 nextChar = thisChar->next;
195 }
196 }
197 return 1;
198 }
199
200 static int
201 show_dots(int startHash) {
202 int k;
203 TranslationTableCharacter *thisDots;
204 TranslationTableOffset nextDots;
205 printf("Press enter for next or (e)xit, next-(h)ash, then enter\n");
206 if (startHash < 0)
207 k = 0;
208 else
209 k = startHash;
210 for (; k < HASHNUM; k++)
211 if (table->dots[k]) {
212 printf("Hash=%d\n", k);
213 nextDots = table->dots[k];
214 while (nextDots) {
215 thisDots = (TranslationTableCharacter *)&table->ruleArea[nextDots];
216 printCharacter(thisDots, 1);
217 printf("=> ");
218 getInput();
219 if (*inputBuffer == 'h') break;
220 if (*inputBuffer == 'e') return 1;
221 nextDots = thisDots->next;
222 }
223 }
224 return 1;
225 }
226
227 static int
228 show_forRules(int startHash) {
229 int k;
230 TranslationTableRule *thisRule;
231 TranslationTableOffset nextRule;
232 printf("Press enter for next or (e)xit, next-(h)ash, then enter\n");
233 if (startHash < 0)
234 k = 0;
235 else
236 k = startHash;
237 for (; k < HASHNUM; k++)
238 if (table->forRules[k]) {
239 printf("Hash=%d\n", k);
240 nextRule = table->forRules[k];
241 while (nextRule) {
242 thisRule = (TranslationTableRule *)&table->ruleArea[nextRule];
243 printRule(thisRule, 0);
244 printf("=> ");
245 getInput();
246 if (*inputBuffer == 'h') break;
247 if (*inputBuffer == 'e') return 1;
248 nextRule = thisRule->charsnext;
249 }
250 }
251 return 1;
252 }
253
254 static int
255 show_backRules(int startHash) {
256 int k;
257 TranslationTableRule *thisRule;
258 TranslationTableOffset nextRule;
259 printf("Press enter for next or (e)xit, next-(h)ash, then enter\n");
260 if (startHash < 0)
261 k = 0;
262 else
263 k = startHash;
264 for (; k < HASHNUM; k++)
265 if (table->backRules[k]) {
266 printf("Hash=%d\n", k);
267 nextRule = table->backRules[k];
268 while (nextRule) {
269 thisRule = (TranslationTableRule *)&table->ruleArea[nextRule];
270 printRule(thisRule, 1);
271 printf("=> ");
272 getInput();
273 if (*inputBuffer == 'h') break;
274 if (*inputBuffer == 'e') return 1;
275 nextRule = thisRule->dotsnext;
276 }
277 }
278 return 1;
279 }
280
281 static int
282 print_brailleIndicator(TranslationTableOffset offset, const char *opcode) {
283 TranslationTableRule *thisRule;
284 if (!offset) return 0;
285 thisRule = (TranslationTableRule *)&table->ruleArea[offset];
286 printf("%s %s\n", opcode, _lou_showDots(&thisRule->charsdots[0], thisRule->dotslen));
287 return 1;
288 }
289
290 static int
291 print_phraseLength(TranslationTableOffset offset, const char *opcode) {
292 if (!offset) return 0;
293 printf("%s %u\n", opcode, offset);
294 return 1;
295 }
296
297 static int
298 show_brailleIndicators(void) {
299 char name[BUFSIZE];
300 const char *emphNames[] = { "begemphphrase %s", "endemphphrase %s before",
301 "endemphphrase %s after", "begemphword %s", "endemphword %s", "emphletter %s",
302 "begemph %s", "endemph %s", NULL };
303 const char *capsNames[] = { "firstwordcaps", "lastwordcapsbefore",
304 "lastwordcapsafter", "begcaps", "endcaps", "capsletter", "capsword",
305 "capswordstop", NULL };
306
307 // FIXME: update to include all UEB opcodes.
308
309 for (EmphCodeOffset offset = 0; capsNames[offset]; offset++) {
310 print_brailleIndicator(
311 table->emphRules[MAX_EMPH_CLASSES][offset], capsNames[offset]);
312 }
313 print_phraseLength(
314 table->emphRules[MAX_EMPH_CLASSES][lenPhraseOffset], "lencapsphrase");
315 print_brailleIndicator(table->letterSign, "letsign");
316 print_brailleIndicator(table->numberSign, "numsign");
317 print_brailleIndicator(table->noNumberSign, "nonumsign");
318 print_brailleIndicator(table->noContractSign, "nocontractsign");
319
320 for (int i = 0; i < MAX_EMPH_CLASSES && table->emphClassNames[i]; i++) {
321 for (EmphCodeOffset offset = 0; emphNames[offset]; offset++) {
322 snprintf(name, BUFSIZE, emphNames[offset], table->emphClasses[i]);
323 print_brailleIndicator(table->emphRules[i][offset], name);
324 }
325 snprintf(name, BUFSIZE, "lenemphphrase %s", table->emphClasses[i]);
326 print_phraseLength(table->emphRules[i][lenPhraseOffset], name);
327 }
328 print_brailleIndicator(table->begComp, "begcomp");
329 print_brailleIndicator(table->endComp, "endcomp");
330 return 1;
331 }
332
333 static const char *
334 pickYN(int a) {
335 if (!a) return "no";
336 return "yes";
337 }
338
339 static int
340 show_misc(void) {
341 printf("Table size: %u\n", table->tableSize);
342 printf("Bytes used: %u\n", table->bytesUsed);
343 printf("Number of passes: %d\n", table->numPasses);
344 printf("'correct' opcodes: %s\n", pickYN(table->corrections));
345 printf("'syllable' opcodes: %s\n", pickYN(table->syllables));
346 printf("'capsnocont' opcode: %s\n", pickYN(table->capsNoCont));
347 printf("Hyphenation table: %s\n", pickYN(table->hyphenStatesArray));
348 printf("noletsignbefore %s\n",
349 print_chars(&table->noLetsignBefore[0], table->noLetsignBeforeCount));
350 printf("noletsign %s\n", print_chars(&table->noLetsign[0], table->noLetsignCount));
351 printf("noletsignafter %s\n",
352 print_chars(&table->noLetsignAfter[0], table->noLetsignAfterCount));
353 return 1;
354 }
355
356 static int
357 show_charMap(int startHash) {
358 int k;
359 CharDotsMapping *thisChar;
360 TranslationTableOffset nextChar;
361 printf("Press enter for next or (e)xit, next-(h)ash, then enter\n");
362 if (startHash < 0)
363 k = 0;
364 else
365 k = startHash;
366 for (; k < HASHNUM; k++)
367 if (displayTable->charToDots[k]) {
368 printf("Hash=%d\n", k);
369 nextChar = displayTable->charToDots[k];
370 while (nextChar) {
371 thisChar = (CharDotsMapping *)&displayTable->ruleArea[nextChar];
372 printf("Char: %s ", print_chars(&thisChar->lookFor, 1));
373 printf("dots=%s\n", _lou_showDots(&thisChar->found, 1));
374 printf("=> ");
375 getInput();
376 if (*inputBuffer == 'h') break;
377 if (*inputBuffer == 'e') return 1;
378 nextChar = thisChar->next;
379 }
380 }
381 return 1;
382 }
383
384 static int
385 show_dotsMap(int startHash) {
386 int k;
387 CharDotsMapping *thisDots;
388 TranslationTableOffset nextDots;
389 printf("Press enter for next or (e)xit, next-(h)ash, then enter\n");
390 if (startHash < 0)
391 k = 0;
392 else
393 k = startHash;
394 for (; k < HASHNUM; k++)
395 if (displayTable->dotsToChar[k]) {
396 printf("Hash=%d\n", k);
397 nextDots = displayTable->dotsToChar[k];
398 while (nextDots) {
399 thisDots = (CharDotsMapping *)&displayTable->ruleArea[nextDots];
400 printf("Dots: %s ", _lou_showDots(&thisDots->lookFor, 1));
401 printf("char=%s\n", print_chars(&thisDots->found, 1));
402 printf("=> ");
403 getInput();
404 if (*inputBuffer == 'h') break;
405 if (*inputBuffer == 'e') return 1;
406 nextDots = thisDots->next;
407 }
408 }
409 return 1;
410 }
411
412 static void
413 part_paramLetters(void) {
414 printf("show particular hash chains.\n");
415 printf("show-(f)orward-rules, show-(b)ackward-rules, show-(c)haracters, \n");
416 printf("show-(d)ot-patterns, show-(C)har-to-dots, show-(D)ots-tochar\n");
417 printf("(h)elp, e(x)it\n");
418 }
419
420 static void
421 particularHelp(void) {
422 part_paramLetters();
423 }
424
425 static int
426 particular(void) {
427 int startHash;
428 widechar parsed[BUFSIZE];
429 part_paramLetters();
430 do {
431 printf("particular: ");
432 getInput();
433 switch (inputBuffer[0]) {
434 case 0:
435 break;
436 case 'h':
437 particularHelp();
438 break;
439 case 'c':
440 printf("-> ");
441 getInput();
442 if (!_lou_extParseChars(inputBuffer, parsed)) break;
443 startHash = _lou_charHash(*parsed);
444 if (table->characters[startHash] == 0) {
445 printf("Character not in table.\n");
446 break;
447 }
448 show_characters(startHash);
449 break;
450 case 'd':
451 printf("-> ");
452 getInput();
453 if (!_lou_extParseDots(inputBuffer, parsed)) break;
454 startHash = _lou_charHash(*parsed);
455 if (table->dots[startHash] == 0) {
456 printf("Dot pattern not in table.\n");
457 break;
458 }
459 show_dots(startHash);
460 break;
461 case 'C':
462 printf("-> ");
463 getInput();
464 if (!_lou_extParseChars(inputBuffer, parsed)) break;
465 startHash = _lou_charHash(*parsed);
466 if (displayTable->charToDots[startHash] == 0) {
467 printf("Character not in table.\n");
468 break;
469 }
470 show_charMap(startHash);
471 break;
472 case 'D':
473 printf("-> ");
474 getInput();
475 if (!_lou_extParseDots(inputBuffer, parsed)) break;
476 startHash = _lou_charHash(*parsed);
477 if (displayTable->dotsToChar[startHash] == 0) {
478 printf("Dot pattern not in table.\n");
479 break;
480 }
481 show_dotsMap(startHash);
482 break;
483 case 'f':
484 printf("-> ");
485 getInput();
486 if (!_lou_extParseChars(inputBuffer, parsed)) break;
487 startHash = _lou_stringHash(parsed, 0, NULL);
488 if (table->forRules[startHash] == 0) {
489 printf("Character string not in table.\n");
490 break;
491 }
492 show_forRules(startHash);
493 break;
494 case 'b':
495 printf("-> ");
496 getInput();
497 if (!_lou_extParseDots(inputBuffer, parsed)) break;
498 startHash = _lou_stringHash(parsed, 0, NULL);
499 if (table->backRules[startHash] == 0) {
500 printf("Dot pattern not in table.\n");
501 break;
502 }
503 show_backRules(startHash);
504 break;
505 case 'x':
506 return 1;
507 default:
508 printf("Bad choice.\n");
509 break;
510 }
511 } while (inputBuffer[0] != 'x');
512 return 1;
513 }
514
515 static void
516 paramLetters(void) {
517 printf("Press one of the letters in parentheses, then enter.\n");
518 printf("show-(f)orward-rules, show-(b)ackward-rules, show-(c)haracters, \n");
519 printf("show-(d)ot-patterns, show-(C)har-to-dots, show-(D)ots-tochar\n");
520 printf("show-(m)isc, show-braille(i)ndicators, show-(p)articulars\n");
521 printf("(h)elp, (q)uit\n");
522 }
523
524 static void
525 commandHelp(void) {
526 paramLetters();
527 }
528
529 static int
530 getCommands(void) {
531 paramLetters();
532 do {
533 printf("Command: ");
534 getInput();
535 switch (inputBuffer[0]) {
536 case 0:
537 break;
538 case 'h':
539 commandHelp();
540 break;
541 case 'C':
542 show_charMap(-1);
543 break;
544 case 'D':
545 show_dotsMap(-1);
546 break;
547 case 'c':
548 show_characters(-1);
549 break;
550 case 'd':
551 show_dots(-1);
552 break;
553 case 'f':
554 show_forRules(-1);
555 break;
556 case 'b':
557 show_backRules(-1);
558 break;
559 case 'i':
560 show_brailleIndicators();
561 break;
562 case 'm':
563 show_misc();
564 break;
565 case 'p':
566 particular();
567 break;
568 case 'q':
569 return 1;
570 default:
571 printf("Bad choice.\n");
572 break;
573 }
574 } while (inputBuffer[0] != 'q');
575 return 1;
576 }
577
578 int
579 main(int argc, char **argv) {
580 int optc;
581
582 set_program_name(argv[0]);
583
584 while ((optc = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) switch (optc) {
585 /* --help and --version exit immediately, per GNU coding standards. */
586 case 'v':
587 version_etc(
588 stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *)NULL);
589 exit(EXIT_SUCCESS);
590 break;
591 case 'h':
592 print_help();
593 exit(EXIT_SUCCESS);
594 break;
595 default:
596 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
597 exit(EXIT_FAILURE);
598 break;
599 }
600
601 if (optind != argc - 1) {
602 /* Print error message and exit. */
603 if (optind < argc - 1)
604 fprintf(stderr, "%s: extra operand: %s\n", program_name, argv[optind + 1]);
605 else
606 fprintf(stderr, "%s: no table specified\n", program_name);
607 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
608 exit(EXIT_FAILURE);
609 }
610
611 _lou_getTable(argv[optind], argv[optind], &table, &displayTable);
612 if (!table) {
613 lou_free();
614 exit(EXIT_FAILURE);
615 }
616 getCommands();
617 lou_free();
618 exit(EXIT_SUCCESS);
619 }