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 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <getopt.h>
30 #include "liblouis.h"
31 #include "internal.h"
32 #include "progname.h"
33 #include "unistr.h"
34 #include "version-etc.h"
35
36 const char version_etc_copyright[] =
37 "Copyright %s %d ViewPlus Technologies, Inc. and JJB Software, Inc.";
38
39 #define AUTHORS "John J. Boyer"
40
41 static void
translate_input(int forward_translation,char * table_name,FILE * input)42 translate_input(int forward_translation, char *table_name, FILE *input) {
43 char charbuf[MAXSTRING];
44 uint8_t *outputbuf;
45 size_t outlen;
46 widechar inbuf[MAXSTRING];
47 widechar transbuf[MAXSTRING];
48 int inlen;
49 int translen;
50 int k;
51 int ch = 0;
52 int result;
53 while (1) {
54 translen = MAXSTRING;
55 k = 0;
56 while ((ch = fgetc(input)) != '\n' && ch != EOF && k < MAXSTRING - 1)
57 charbuf[k++] = ch;
58 if (ch == EOF && k == 0) break;
59 charbuf[k] = 0;
60 inlen = _lou_extParseChars(charbuf, inbuf);
61 if (forward_translation)
62 result = lou_translateString(
63 table_name, inbuf, &inlen, transbuf, &translen, NULL, NULL, 0);
64 else
65 result = lou_backTranslateString(
66 table_name, inbuf, &inlen, transbuf, &translen, NULL, NULL, 0);
67 if (!result) break;
68 #ifdef WIDECHARS_ARE_UCS4
69 outputbuf = u32_to_u8(transbuf, translen, NULL, &outlen);
70 #else
71 outputbuf = u16_to_u8(transbuf, translen, NULL, &outlen);
72 #endif
73 printf(ch == EOF ? "%.*s" : "%.*s\n", (int)outlen, outputbuf);
74 free(outputbuf);
75 }
76 lou_free();
77 }
78
79 static void
print_help(void)80 print_help(void) {
81 printf("\
82 Usage: %s [OPTIONS] TABLE[,TABLE,...]\n",
83 program_name);
84
85 fputs("\
86 Translate whatever is on standard input and print it on standard\n\
87 output. It is intended for large-scale testing of the accuracy of\n\
88 Braille translation and back-translation.\n\n",
89 stdout);
90
91 fputs("\
92 Options:\n\
93 -h, --help display this help and exit\n\
94 -v, --version display version information and exit\n\
95 -f, --forward forward translation using the given table\n\
96 -b, --backward backward translation using the given table\n\
97 If neither -f nor -b are specified forward translation\n\
98 is assumed\n",
99 stdout);
100 fputs("\
101 Examples:\n\
102 lou_translate --forward en-us-g2.ctb < input.txt\n\
103 \n\
104 Do a forward translation with table en-us-g2.ctb. The resulting braille is\n\
105 ASCII encoded.\n\
106 \n\
107 lou_translate unicode.dis,en-us-g2.ctb < input.txt\n\
108 \n\
109 Do a forward translation with table en-us-g2.ctb. The resulting braille is\n\
110 encoded as Unicode dot patterns.\n\
111 \n\
112 echo \",! qk br{n fox\" | lou_translate --backward en-us-g2.ctb\n\
113 \n\
114 Do a backward translation with table en-us-g2.ctb.\n",
115 stdout);
116 printf("\n");
117 printf("Report bugs to %s.\n", PACKAGE_BUGREPORT);
118
119 #ifdef PACKAGE_PACKAGER_BUG_REPORTS
120 printf("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS);
121 #endif
122 #ifdef PACKAGE_URL
123 printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
124 #endif
125 }
126
127 int
main(int argc,char ** argv)128 main(int argc, char **argv) {
129 int optc;
130
131 int forward_flag = 0;
132 int backward_flag = 0;
133
134 const struct option longopts[] = {
135 { "help", no_argument, NULL, 'h' },
136 { "version", no_argument, NULL, 'v' },
137 { "forward", no_argument, NULL, 'f' },
138 { "backward", no_argument, NULL, 'b' },
139 { NULL, 0, NULL, 0 },
140 };
141
142 set_program_name(argv[0]);
143 while ((optc = getopt_long(argc, argv, "hvfb", longopts, NULL)) != -1) {
144 switch (optc) {
145 /* --help and --version exit immediately, per GNU coding standards. */
146 case 'v':
147 version_etc(
148 stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *)NULL);
149 exit(EXIT_SUCCESS);
150 break;
151 case 'h':
152 print_help();
153 exit(EXIT_SUCCESS);
154 break;
155 case 'f':
156 forward_flag = 1;
157 break;
158 case 'b':
159 backward_flag = 1;
160 break;
161 default:
162 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
163 exit(EXIT_FAILURE);
164 break;
165 }
166 }
167
168 if (forward_flag && backward_flag) {
169 fprintf(stderr, "%s: specify either -f or -b but not both\n", program_name);
170 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
171 exit(EXIT_FAILURE);
172 }
173
174 if (optind != argc - 1) {
175 // Print error message and exit.
176 if (optind < argc - 1)
177 fprintf(stderr, "%s: extra operand: %s\n", program_name, argv[optind + 1]);
178 else
179 fprintf(stderr, "%s: no table specified\n", program_name);
180 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
181 exit(EXIT_FAILURE);
182 }
183 /* assume forward translation by default */
184 translate_input(!backward_flag, argv[optind], stdin);
185 exit(EXIT_SUCCESS);
186 }
187