Debian Codegraph @

tools/lou_checkhyphens.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
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
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include "liblouis.h"
30 #include "internal.h"
31 #include <getopt.h>
32 #include "progname.h"
33 #include "version-etc.h"
34
35 static const struct option longopts[] = {
36 { "help", no_argument, NULL, 'h' },
37 { "version", no_argument, NULL, 'v' },
38 { NULL, 0, NULL, 0 },
39 };
40
41 const char version_etc_copyright[] =
42 "Copyright %s %d ViewPlus Technologies, Inc. and JJB Software, Inc.";
43
44 #define AUTHORS "John J. Boyer"
45
46 static void
47 print_help(void) {
48 printf("\
49 Usage: %s [OPTIONS]\n",
50 program_name);
51
52 fputs("\
53 Check the accuracy of hyphenation in Braille translation for both\n\
54 translated and untranslated words.\n\n",
55 stdout);
56
57 fputs("\
58 -h, --help display this help and exit\n\
59 -v, --version display version information and exit\n",
60 stdout);
61
62 printf("\n");
63 printf("Report bugs to %s.\n", PACKAGE_BUGREPORT);
64
65 #ifdef PACKAGE_PACKAGER_BUG_REPORTS
66 printf("Report %s bugs to: %s\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS);
67 #endif
68 #ifdef PACKAGE_URL
69 printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
70 #endif
71 }
72
73 #define BUFSIZE 256
74
75 static char inputBuffer[BUFSIZE];
76 static const TranslationTableHeader *validTable = NULL;
77 static unsigned int mode;
78 static char table[BUFSIZE];
79
80 static int
81 getInput(void) {
82 int inputLength;
83 inputBuffer[0] = 0;
84 if (!fgets(inputBuffer, sizeof(inputBuffer), stdin)) exit(EXIT_FAILURE);
85 inputLength = strlen(inputBuffer) - 1;
86 if (inputLength < 0) /* EOF on script */
87 exit(0);
88 inputBuffer[inputLength] = 0;
89 return inputLength;
90 }
91
92 static void
93 paramLetters(void) {
94 printf("Press one of the letters in parentheses, then enter.\n");
95 printf("(t)able, tr(a)nslated, (u)ntranslated, (r)un, (h)elp, (q)uit\n");
96 }
97
98 static int
99 getCommands(void) {
100 paramLetters();
101 do {
102 printf("Command: ");
103 getInput();
104 switch (inputBuffer[0]) {
105 case 0:
106 break;
107 case 't':
108 do {
109 printf("Enter the name of a table or a list: ");
110 getInput();
111 strcpy(table, inputBuffer);
112 validTable = lou_getTable(table);
113 if (validTable != NULL && validTable->hyphenStatesArray == 0) {
114 printf("No hyphenation table.\n");
115 validTable = NULL;
116 }
117 } while (validTable == NULL);
118 break;
119 case 'a':
120 mode = 1;
121 break;
122 case 'u':
123 mode = 0;
124 break;
125 case 'r':
126 if (validTable == NULL) {
127 printf("You must enter a valid table name or list.\n");
128 inputBuffer[0] = 0;
129 }
130 break;
131 case 'h':
132 printf("Commands: action\n");
133 printf("(t)able: Enter a table name or list\n");
134 printf("(r)un: run the hyphenation test loop\n");
135 printf("tr(a)nslated: translated input\n");
136 printf("(u)ntranslated: untranslated input\n");
137 printf("(h)elp: print this page\n");
138 printf("(q)uit: leave the program\n");
139 printf("\n");
140 paramLetters();
141 break;
142 case 'q':
143 exit(0);
144 default:
145 printf("Bad choice.\n");
146 break;
147 }
148 } while (inputBuffer[0] != 'r');
149 return 1;
150 }
151
152 int
153 main(int argc, char **argv) {
154 widechar inbuf[BUFSIZE];
155 char hyphens[BUFSIZE];
156 int inlen;
157 int k;
158 int optc;
159
160 set_program_name(argv[0]);
161
162 while ((optc = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) switch (optc) {
163 /* --help and --version exit immediately, per GNU coding standards. */
164 case 'v':
165 version_etc(
166 stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *)NULL);
167 exit(EXIT_SUCCESS);
168 break;
169 case 'h':
170 print_help();
171 exit(EXIT_SUCCESS);
172 break;
173 default:
174 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
175 exit(EXIT_FAILURE);
176 break;
177 }
178
179 if (optind < argc) {
180 /* Print error message and exit. */
181 fprintf(stderr, "%s: extra operand: %s\n", program_name, argv[optind]);
182 fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
183 exit(EXIT_FAILURE);
184 }
185
186 validTable = NULL;
187 mode = 0;
188
189 while (1) {
190 getCommands();
191 printf("Type something, press enter, and view the results.\n");
192 printf("A blank line returns to command entry.\n");
193 while (1) {
194 inlen = getInput();
195 if (inlen == 0) break;
196 for (k = 0; k < inlen; k++) inbuf[k] = inputBuffer[k];
197 if (!lou_hyphenate(table, inbuf, inlen, hyphens, mode)) {
198 printf("Hyphenation error\n");
199 continue;
200 }
201 printf("Hyphenation mask: %s\n", hyphens);
202 printf("Hyphenated word: ");
203 for (k = 0; k < inlen; k++) {
204 if (hyphens[k] == '1') printf("-");
205 printf("%c", inbuf[k]);
206 }
207 printf("\n");
208 }
209 }
210 lou_free();
211 return 0;
212 }