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