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>cxx . . $ `<file>/liblouis/config.h`/
26
27 #include <stdio.h>cxx . . $ `<file>/tools/gnulib/stdio.h`/
28 #include <string.h>cxx . . $ `<file>/gnulib/string.h`/
29 #include <stdlib.h>cxx . . $ `<file>/gnulib/stdlib.h`/
30 #include <stdint.h>
31 #include <getopt.h>
32 #include "liblouis.h"cxx . . $ `<file>/liblouis/liblouis.h`/
33 #include "internal.h"cxx . . $ `<file>/liblouis/internal.h`/
34 #include "progname.h"cxx . . $ `<file>/tools/gnulib/progname.h`/
35 #include "unistr.h"cxx . . $ `<file>/tools/gnulib/unistr.h`/
36 #include "version-etc.h"cxx . . $ `<file>/tools/gnulib/version-etc.h`/
37
38 const char version_etc_copyrightcxx . . $ version_etc_copyright.[] =
39 "Copyright %s %d ViewPlus Technologies, Inc. and JJB Software, Inc.";
40
41 #define AUTHORScxx . . $ `tools/lou_translate.c:41:9`! "John J. Boyer"
42
43 static void
44 translate_inputcxx . . $ translate_input(c03fc7900de21457).(int forward_translation, char *table_name, char *display_table_name,
45 int mode, FILEcxx . . $ FILE# *input) {
46 char charbuf[MAXSTRINGcxx . . $ `liblouis/internal.h:79:9`!];
47 uint8_tcxx . . $ uint8_t# *outputbuf;
48 size_tcxx . . $ size_t# outlen;
49 widecharcxx . . $ widechar# inbuf[MAXSTRINGcxx . . $ `liblouis/internal.h:79:9`!];
50 widecharcxx . . $ widechar# transbuf[MAXSTRINGcxx . . $ `liblouis/internal.h:79:9`!];
51 int inlen;
52 int translen;
53 int k;
54 int ch = 0;
55 int result;
56 while (1) {
57 translen = MAXSTRINGcxx . . $ `liblouis/internal.h:79:9`!;
58 k = 0;
59 while ((ch = fgetccxx . . $ fgetc(89bc1efcdac0ca22).(input)) != '\n' && ch != EOFcxx . . $ `<external>/232824719582829767/stdio.h:109:9`! && k < MAXSTRINGcxx . . $ `liblouis/internal.h:79:9`! - 1)
60 charbuf[k++] = ch;
61 if (ch == EOFcxx . . $ `<external>/232824719582829767/stdio.h:109:9`! && k == 0) break;
62 charbuf[k] = 0;
63 inlen = _lou_extParseCharscxx . . $ _lou_extParseChars(c9bce2f4bcd31d4).(charbuf, inbuf);
64 if (forward_translation)
65 result = _lou_translatecxx . . $ _lou_translate(5fc2ab337e0d897c).(table_name, display_table_name, inbuf, &inlen,
66 transbuf, &translen, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, mode, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!);
67 else
68 result = _lou_backTranslatecxx . . $ _lou_backTranslate(5fc2ab337e0d897c).(table_name, display_table_name, inbuf, &inlen,
69 transbuf, &translen, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, mode, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!);
70 if (!result) break;
71 #ifdef WIDECHARS_ARE_UCS4
72 outputbuf = u32_to_u8(transbuf, translen, NULL, &outlen);
73 #else
74 outputbuf = u16_to_u8cxx . . $ u16_to_u8(e729bd8f99eea2d2).(transbuf, translen, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &outlen);
75 #endif
76 printfcxx . . $ printf(581fddd72a8db8fc).(ch == EOFcxx . . $ `<external>/232824719582829767/stdio.h:109:9`! ? "%.*s" : "%.*s\n", (int)outlen, outputbuf);
77 freecxx . . $ free(5d22bdacc48458e8).(outputbuf);
78 }
79 }
80
81 // copied from metadata.c
82 static int
83 isValidCharcxx . . $ isValidChar(784f3405e7e545be).(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_helpcxx . . $ print_help(49f6e7a06ebc5aa8).(void) {
90 printfcxx . . $ printf(581fddd72a8db8fc).("\
91 Usage: %s [OPTIONS] TABLE\n\n",
92 program_namecxx . . $ program_name.);
93
94 fputscxx . . $ fputs(874fa6a5e59ab9bd).("\
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 stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!);
99
100 fputscxx . . $ fputs(874fa6a5e59ab9bd).("\
101 TABLE is either:\n\
102 - a query KEY[:VALUE] [KEY[:VALUE] ...]\n\
103 - a file list FILE[,FILE,...]\n\n",
104 stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!);
105
106 fputscxx . . $ fputs(874fa6a5e59ab9bd).("\
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 stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!);
124 fputscxx . . $ fputs(874fa6a5e59ab9bd).("\
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 stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!);
146 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
147 printfcxx . . $ printf(581fddd72a8db8fc).("Report bugs to %s.\n", PACKAGE_BUGREPORTcxx . . $ `liblouis/config.h:628:9`!);
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_URLcxx . . $ `liblouis/config.h:649:9`!
153 printfcxx . . $ printf(581fddd72a8db8fc).("%s home page: <%s>\n", PACKAGE_NAMEcxx . . $ `liblouis/config.h:631:9`!, PACKAGE_URLcxx . . $ `liblouis/config.h:649:9`!);
154 #endif
155 }
156
157 int
158 maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv) {
159 int optc;
160
161 int forward_flag = 0;
162 int backward_flag = 0;
163 char *display_table = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
164
165 const struct optioncxx . . $ option# longopts[] = {
166 { "help", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 'h' },
167 { "version", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 'v' },
168 { "forward", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 'f' },
169 { "backward", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 'b' },
170 { "display-table", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 'd' },
171 { NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 0, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 0 },
172 };
173
174 set_program_namecxx . . $ set_program_name(85c52e162fed56f9).(argv[0]);
175 while ((optc = getopt_longcxx . . $ getopt_long(41f665c090816971).(argc, argv, "hvfbd:", longopts, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)) != -1) {
176 switch (optc) {
177 /* --help and --version exit immediately, per GNU coding standards. */
178 case 'v':
179 version_etccxx . . $ version_etc(b5fd392babfe1cc5).(
180 stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!, program_namecxx . . $ program_name., PACKAGE_NAMEcxx . . $ `liblouis/config.h:631:9`!, VERSIONcxx . . $ `liblouis/config.h:804:9`!, AUTHORScxx . . $ `tools/lou_translate.c:41:9`!, (char *)NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!);
181 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:97:9`!);
182 break;
183 case 'h':
184 print_helpcxx . . $ print_help(49f6e7a06ebc5aa8).();
185 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:97:9`!);
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 = optargcxx . . $ optarg.;
195 break;
196 default:
197 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Try `%s --help' for more information.\n", program_namecxx . . $ program_name.);
198 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!);
199 break;
200 }
201 }
202
203 if (forward_flag && backward_flag) {
204 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s: specify either -f or -b but not both\n", program_namecxx . . $ program_name.);
205 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Try `%s --help' for more information.\n", program_namecxx . . $ program_name.);
206 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!);
207 }
208
209 if (optindcxx . . $ optind. >= argc) {
210 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s: no table specified\n", program_namecxx . . $ program_name.);
211 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Try `%s --help' for more information.\n", program_namecxx . . $ program_name.);
212 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!);
213 }
214
215 if (display_table && !lou_checkTablecxx . . $ lou_checkTable(5c4306d8553b3696).(display_table)) {
216 lou_freecxx . . $ lou_free(49f6e7a06ebc5aa8).();
217 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!);
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 = optindcxx . . $ optind.; i < argc; i++) {
230 int l = strlencxx . . $ strlen(751346f8406fd082).(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 (!isValidCharcxx . . $ isValidChar(784f3405e7e545be).(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 = calloccxx . . $ calloc(63a7bf2941bfcd7a).((1 + len), sizeof(char));
252 for (int i = optindcxx . . $ optind.; i < argc; i++) {
253 if (i > optindcxx . . $ optind.) strcatcxx . . $ strcat(b7b3a83273496486).(tableOption, " ");
254 strcatcxx . . $ strcat(b7b3a83273496486).(tableOption, argv[i]);
255 }
256 }
257
258 char *table;
259 int mode = 0;
260 int exitValue = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
261 {
262 if (optindcxx . . $ optind. == argc - 1 && validQuery) {
263 // could be both a query or a file list
264 if (queryHasColon) {
265 // first try query
266 table = lou_findTablecxx . . $ lou_findTable(a8b8e32347182212).(tableOption);
267 if (table != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`! && !display_table)
268 mode |= dotsIOcxx . . $ dotsIO. | ucBrlcxx . . $ ucBrl.;
269 else
270 table = strdupcxx . . $ strdup(a8b8e32347182212).(argv[optindcxx . . $ optind.]);
271 if (!lou_checkTablecxx . . $ lou_checkTable(5c4306d8553b3696).(table)) goto failure;
272 } else {
273 // first try file list (note that this will currently never happen, but
274 // see #1671)
275 table = argv[optindcxx . . $ optind.];
276 if (lou_checkTablecxx . . $ lou_checkTable(5c4306d8553b3696).(table))
277 table = strdupcxx . . $ strdup(a8b8e32347182212).(table);
278 else {
279 table = lou_findTablecxx . . $ lou_findTable(a8b8e32347182212).(tableOption);
280 if (table == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`! || !lou_checkTablecxx . . $ lou_checkTable(5c4306d8553b3696).(table)) goto failure;
281 if (!display_table) mode |= dotsIOcxx . . $ dotsIO. | ucBrlcxx . . $ ucBrl.;
282 }
283 }
284 } else if (validQuery) {
285 table = lou_findTablecxx . . $ lou_findTable(a8b8e32347182212).(tableOption);
286 if (table == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`! || !lou_checkTablecxx . . $ lou_checkTable(5c4306d8553b3696).(table)) goto failure;
287 if (!display_table) mode |= dotsIOcxx . . $ dotsIO. | ucBrlcxx . . $ ucBrl.;
288 } else if (optindcxx . . $ optind. == argc - 1) {
289 table = strdupcxx . . $ strdup(a8b8e32347182212).(argv[optindcxx . . $ optind.]);
290 if (!lou_checkTablecxx . . $ lou_checkTable(5c4306d8553b3696).(table)) goto failure;
291 } else {
292 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s: no valid table specified: %s\n", program_namecxx . . $ program_name.,
293 tableOption);
294 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Must be a query or table list\n");
295 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Try `%s --help' for more information.\n", program_namecxx . . $ program_name.);
296 freecxx . . $ free(5d22bdacc48458e8).(tableOption);
297 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!);
298 }
299 }
300
301 /* assume forward translation by default */
302 translate_inputcxx . . $ translate_input(c03fc7900de21457).(!backward_flag, table, display_table, mode, stdincxx . . $ `<external>/232824719582829767/stdio.h:157:9`!);
303
304 success:
305 exitValue = EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:97:9`!;
306 failure:
307 freecxx . . $ free(5d22bdacc48458e8).(tableOption);
308 freecxx . . $ free(5d22bdacc48458e8).(table);
309 lou_freecxx . . $ lou_free(49f6e7a06ebc5aa8).();
310 exitcxx . . $ exit(3ca292c338df9bb3).(exitValue);
311 }