Debian Codegraph @

tools/lou_translate.c

raw symbols SCIP
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 Copyright (C) 2024 Swiss Library for the Blind, Visually Impaired and Print Disabled
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <getopt.h>
32 #include "liblouis.h"
33 #include "internal.h"
34 #include "progname.h"
35 #include "unistr.h"
36 #include "version-etc.h"
37
38 const char version_etc_copyright[] =
39 "Copyright %s %d ViewPlus Technologies, Inc. and JJB Software, Inc.";
40
41 #define AUTHORS "John J. Boyer"
42
43 static void
44 translate_input(int forward_translation, char *table_name, char *display_table_name,
45 int mode, FILE *input) {
46 char charbuf[MAXSTRING];
47 uint8_t *outputbuf;
48 size_t outlen;
49 widechar inbuf[MAXSTRING];
50 widechar transbuf[MAXSTRING];
51 int inlen;
52 int translen;
53 int k;
54 int ch = 0;
55 int result;
56 while (1) {
57 translen = MAXSTRING;
58 k = 0;
59 while ((ch = fgetc(input)) != '\n' && ch != EOF && k < MAXSTRING - 1)
60 charbuf[k++] = ch;
61 if (ch == EOF && k == 0) break;
62 charbuf[k] = 0;
63 inlen = _lou_extParseChars(charbuf, inbuf);
64 if (forward_translation)
65 result = _lou_translate(table_name, display_table_name, inbuf, &inlen,
66 transbuf, &translen, NULL, NULL, NULL, NULL, NULL, mode, NULL, NULL);
67 else
68 result = _lou_backTranslate(table_name, display_table_name, inbuf, &inlen,
69 transbuf, &translen, NULL, NULL, NULL, NULL, NULL, mode, NULL, NULL);
70 if (!result) break;
71 #ifdef WIDECHARS_ARE_UCS4
72 outputbuf = u32_to_u8(transbuf, translen, NULL, &outlen);
73 #else
74 outputbuf = u16_to_u8(transbuf, translen, NULL, &outlen);
75 #endif
76 printf(ch == EOF ? "%.*s" : "%.*s\n", (int)outlen, outputbuf);
77 free(outputbuf);
78 }
79 }
80
81 // copied from metadata.c
82 static int
83 isValidChar(char c) {
84 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
85 c == '-' || c == '.' || c == '_';
86 }
87
88 static void
89 print_help(void) {
90 printf("\
91 Usage: %s [OPTIONS] TABLE\n\n",
92 program_name);
93
94 fputs("\
95 Translate whatever is on standard input and print it on standard\n\
96 output. It is intended for large-scale testing of the accuracy of\n\
97 braille translation and back-translation.\n\n",
98 stdout);
99
100 fputs("\
101 TABLE is either:\n\
102 - a query KEY[:VALUE] [KEY[:VALUE] ...]\n\
103 - a file list FILE[,FILE,...]\n\n",
104 stdout);
105
106 fputs("\
107 Options:\n\
108 -h, --help display this help and exit\n\
109 -v, --version display version information and exit\n\
110 -f, --forward forward translation using the given table\n\
111 -b, --backward backward translation using the given table\n\
112 If neither -f nor -b are specified forward translation\n\
113 is assumed\n\
114 -d, --display-table use the given display table for the translation. The\n\
115 display table determines how braille dot patterns are\n\
116 encoded as text (decoded in case of backward\n\
117 translation). Without a display table, the braille\n\
118 encoding is determined by the translation table, when\n\
119 it is specified as a file list, or Unicode braille when\n\
120 the table is specified as a query. For clarity and\n\
121 reliability, it is recommended to always make the\n\
122 display table explicit.\n",
123 stdout);
124 fputs("\
125 Examples:\n\
126 lou_translate language:en grade:2 region:en-US < input.txt\n\
127 \n\
128 Do a forward translation of English text to grade 2 contracted braille\n\
129 according to the U.S. braille standard.\n\
130 \n\
131 lou_translate -d en-us-brf.dis language:en grade:2 region:en-US < input.txt\n\
132 \n\
133 Here we do the same forward translation but with a specific ASCII braille\n\
134 encoding instead of Unicode braille.\n\
135 \n\
136 lou_translate -d unicode.dis en-us-g2.ctb < input.txt\n\
137 \n\
138 Do a forward translation with table en-us-g2.ctb and Unicode braille encoding.\n\
139 \n\
140 echo \",! qk br{n fox\" | lou_translate -b -d text_nabcc.dis language:en grade:2 region:en-US\n\
141 \n\
142 Do a backward translation with the English grade 2 table. Note that the ASCII\n\
143 braille input (as shown here) must match the specified display table\n\
144 (North American Braille Computer Code in this case).\n",
145 stdout);
146 printf("\n");
147 printf("Report bugs to %s.\n", PACKAGE_BUGREPORT);
148
149 #ifdef PACKAGE_PACKAGER_BUG_REPORTS
150 printf("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS);
151 #endif
152 #ifdef PACKAGE_URL
153 printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
154 #endif
155 }
156
157 int
158 main(int argc, char **argv) {
159 int optc;
160
161 int forward_flag = 0;
162 int backward_flag = 0;
163 char *display_table = NULL;
164
165 const struct option longopts[] = {
166 { "help", no_argument, NULL, 'h' },
167 { "version", no_argument, NULL, 'v' },
168 { "forward", no_argument, NULL, 'f' },
169 { "backward", no_argument, NULL, 'b' },
170 { "display-table", required_argument, NULL, 'd' },
171 { NULL, 0, NULL, 0 },
172 };
173
174 set_program_name(argv[0]);
175 while ((optc = getopt_long(argc, argv, "hvfbd:", longopts, NULL)) != -1) {
176 switch (optc) {
177 /* --help and --version exit immediately, per GNU coding standards. */
178 case 'v':
179 version_etc(
180 stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *)NULL);
181 exit(EXIT_SUCCESS);
182 break;
183 case 'h':
184 print_help();
185 exit(EXIT_SUCCESS);
186 break;
187 case 'f':
188 forward_flag = 1;
189 break;
190 case 'b':
191 backward_flag = 1;
192 break;
193 case 'd':
194 display_table = optarg;
195 break;
196 default:
197 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
198 exit(EXIT_FAILURE);
199 break;
200 }
201 }
202
203 if (forward_flag && backward_flag) {
204 fprintf(stderr, "%s: specify either -f or -b but not both\n", program_name);
205 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
206 exit(EXIT_FAILURE);
207 }
208
209 if (optind >= argc) {
210 fprintf(stderr, "%s: no table specified\n", program_name);
211 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
212 exit(EXIT_FAILURE);
213 }
214
215 if (display_table && !lou_checkTable(display_table)) {
216 lou_free();
217 exit(EXIT_FAILURE);
218 }
219
220 char *tableOption;
221 int validQuery;
222 int queryHasColon; // note that a query must always have a colon now, but we'll keep
223 // some of this code for now because of
224 // https://github.com/liblouis/liblouis/issues/1671
225 {
226 validQuery = 1;
227 queryHasColon = 0;
228 int len = 0;
229 for (int i = optind; i < argc; i++) {
230 int l = strlen(argv[i]);
231 if (validQuery) {
232 int hasColon = 0;
233 for (int j = 0; j < l; j++) {
234 if (argv[i][j] == ':') {
235 if (j == 0 || j == l - 1 || hasColon)
236 validQuery = 0;
237 else
238 hasColon = 1;
239 } else if (!isValidChar(argv[i][j]))
240 validQuery = 0;
241 }
242 if (hasColon)
243 queryHasColon = 1;
244 else
245 validQuery = 0;
246 }
247 len += l;
248 len++;
249 }
250 len--;
251 tableOption = calloc((1 + len), sizeof(char));
252 for (int i = optind; i < argc; i++) {
253 if (i > optind) strcat(tableOption, " ");
254 strcat(tableOption, argv[i]);
255 }
256 }
257
258 char *table;
259 int mode = 0;
260 int exitValue = EXIT_FAILURE;
261 {
262 if (optind == argc - 1 && validQuery) {
263 // could be both a query or a file list
264 if (queryHasColon) {
265 // first try query
266 table = lou_findTable(tableOption);
267 if (table != NULL && !display_table)
268 mode |= dotsIO | ucBrl;
269 else
270 table = strdup(argv[optind]);
271 if (!lou_checkTable(table)) goto failure;
272 } else {
273 // first try file list (note that this will currently never happen, but
274 // see #1671)
275 table = argv[optind];
276 if (lou_checkTable(table))
277 table = strdup(table);
278 else {
279 table = lou_findTable(tableOption);
280 if (table == NULL || !lou_checkTable(table)) goto failure;
281 if (!display_table) mode |= dotsIO | ucBrl;
282 }
283 }
284 } else if (validQuery) {
285 table = lou_findTable(tableOption);
286 if (table == NULL || !lou_checkTable(table)) goto failure;
287 if (!display_table) mode |= dotsIO | ucBrl;
288 } else if (optind == argc - 1) {
289 table = strdup(argv[optind]);
290 if (!lou_checkTable(table)) goto failure;
291 } else {
292 fprintf(stderr, "%s: no valid table specified: %s\n", program_name,
293 tableOption);
294 fprintf(stderr, "Must be a query or table list\n");
295 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
296 free(tableOption);
297 exit(EXIT_FAILURE);
298 }
299 }
300
301 /* assume forward translation by default */
302 translate_input(!backward_flag, table, display_table, mode, stdin);
303
304 success:
305 exitValue = EXIT_SUCCESS;
306 failure:
307 free(tableOption);
308 free(table);
309 lou_free();
310 exit(exitValue);
311 }