1 /* liblouis Braille Translation and Back-Translation Library
3 Copyright (C) 2012 Swiss Library for the Blind, Visually Impaired and Print Disabled
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "version-etc.h"
29 static const struct option longopts[] = {
30 {"help", no_argument, NULL, 'h'},
31 {"version", no_argument, NULL, 'v'},
35 const char version_etc_copyright[] =
36 "Copyright %s %d Swiss Library for the Blind, Visually Impaired and Print Disabled.";
38 #define AUTHORS "Bert Frees"
43 Usage: %s [OPTIONS] TABLE[,TABLE,...]\n", program_name);
45 Examine and debug Braille translation tables. This program allows you\n\
46 to inspect liblouis translation tables by printing out the list of\n\
47 applied translation rules for a given input.\n\n", stdout);
49 -h, --help display this help and exit\n\
50 -v, --version display version information and exit\n\n", stdout);
51 printf("Report bugs to %s.\n", PACKAGE_BUGREPORT);
52 #ifdef PACKAGE_PACKAGER_BUG_REPORTS
53 printf("Report %s bugs to: %s\n", PACKAGE_PACKAGER,
54 PACKAGE_PACKAGER_BUG_REPORTS);
57 printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
66 static char input_buffer[BUFSIZE];
69 fgets(input_buffer, sizeof(input_buffer), stdin);
70 input_length = strlen(input_buffer) - 1;
73 input_buffer[input_length] = 0;
78 get_wide_input(widechar * buffer) {
79 return extParseChars(get_input(), buffer);
83 print_chars(const widechar * buffer, int length) {
84 static char chars[BUFSIZE];
85 strcpy(chars, &showString(buffer, length)[1]);
86 chars[strlen(chars) - 1] = 0;
91 print_dots(const widechar * buffer, int length) {
92 static char dots[BUFSIZE];
93 strcpy(dots, showDots(buffer, length));
98 print_number(widechar c) {
99 static char number[BUFSIZE];
100 sprintf(number, "%d", c);
105 print_attributes(unsigned int a) {
106 static char attr[BUFSIZE];
107 strcpy(attr, showAttributes(a));
112 append_char(char *destination, int *length, char source) {
113 destination[(*length)++] = source;
117 append_string(char *destination, int *length, char *source) {
118 strcpy(&destination[(*length)], source);
119 (*length) += strlen(source);
123 print_script(const widechar * buffer, int length) {
124 static char script[BUFSIZE];
132 case pass_startReplace:
133 case pass_endReplace:
137 append_char(script, &j, buffer[i++]);
140 append_char(script, &j, buffer[i++]);
142 append_string(script, &j, print_number(buffer[i++]));
145 append_char(script, &j, buffer[i]);
146 append_string(script, &j, print_chars(&buffer[i + 2], buffer[i + 1]));
147 append_char(script, &j, buffer[i]);
148 i += (2 + buffer[i + 1]);
151 append_char(script, &j, buffer[i++]);
152 append_string(script, &j, print_dots(&buffer[i + 1], buffer[i]));
153 i += (1 + buffer[i]);
160 append_char(script, &j, '#');
161 append_string(script, &j, print_number(buffer[i + 1]));
162 append_char(script, &j, buffer[i]);
163 append_string(script, &j, print_number(buffer[i + 2]));
168 append_char(script, &j, '#');
169 append_string(script, &j, print_number(buffer[i + 1]));
170 append_char(script, &j, buffer[i]);
173 case pass_attributes:
174 append_char(script, &j, buffer[i]);
175 append_string(script, &j,
176 print_attributes(buffer[i + 1] << 16 | buffer[i + 2]));
178 if (buffer[i] == 1 && buffer[i + 1] == 1) {
179 } else if (buffer[i] == 1 && buffer[i + 1] == 0xffff)
180 append_char(script, &j, pass_until);
181 else if (buffer[i] == buffer[i + 1])
182 append_string(script, &j, print_number(buffer[i]));
184 append_string(script, &j, print_number(buffer[i]));
185 append_char(script, &j, '-');
186 append_string(script, &j, print_number(buffer[i + 1]));
191 append_char(script, &j, '\t');
195 case pass_groupstart:
197 case pass_groupreplace:
209 print_rule(const TranslationTableRule * rule) {
210 const char *opcode = findOpcodeName(rule->opcode);
214 switch (rule->opcode) {
222 script = print_script(&rule->charsdots[rule->charslen], rule->dotslen);
223 printf("%s\t%s\n", opcode, script);
226 chars = print_chars(rule->charsdots, rule->charslen);
227 dots = print_dots(&rule->charsdots[rule->charslen], rule->dotslen);
228 printf("%s\t%s\t%s\n", opcode, chars, dots);
234 main_loop(char *table) {
235 widechar inbuf[BUFSIZE];
236 widechar outbuf[BUFSIZE];
239 const TranslationTableRule **rules =
240 malloc(512 * sizeof(TranslationTableRule));
244 inlen = get_wide_input(inbuf);
246 ruleslen = RULESSIZE;
247 if (!trace_translate(table, inbuf, &inlen, outbuf, &outlen,
248 NULL, NULL, NULL, NULL, NULL, rules, &ruleslen, 0))
250 printf("%s\n", print_chars(outbuf, outlen));
251 for (i = 0; i < ruleslen; i++) {
252 printf("%d.\t", i + 1);
253 print_rule(rules[i]);
259 main(int argc, char **argv) {
262 set_program_name(argv[0]);
263 while ((optc = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) {
266 version_etc(stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS,
275 fprintf(stderr, "Try `%s --help' for more information.\n",
281 if (optind != argc - 1) {
282 if (optind < argc - 1)
283 fprintf(stderr, "%s: extra operand: %s\n", program_name,
286 fprintf(stderr, "%s: no table specified\n", program_name);
287 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
290 table = argv[optind];
291 if (!lou_getTable(table)) {