/
cldr.c
317 lines (257 loc) · 7.35 KB
/
cldr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2020 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "log.h"
#include "cldr.h"
#include "file.h"
#ifdef HAVE_EXPAT
#include <expat.h>
struct CLDR_DocumentParserObjectStruct {
struct {
CLDR_AnnotationHandler *handler;
void *data;
} caller;
struct {
XML_Parser parser;
unsigned int depth;
} document;
struct {
char *sequence;
char *name;
unsigned int depth;
} annotation;
};
static void
abortParser (CLDR_DocumentParserObject *dpo) {
XML_StopParser(dpo->document.parser, 0);
}
static void
appendAnnotationText (void *userData, const char *characters, int count) {
CLDR_DocumentParserObject *dpo = userData;
if (dpo->document.depth == dpo->annotation.depth) {
if (count > 0) {
char *name = dpo->annotation.name;
if (name) {
size_t oldLength = strlen(name);
size_t newLength = oldLength + count;
char *newName = realloc(name, newLength+1);
if (!newName) {
logMallocError();
abortParser(dpo);
return;
}
memcpy(&newName[oldLength], characters, count);
newName[newLength] = 0;
name = newName;
} else {
if (!(name = malloc(count+1))) {
logMallocError();
abortParser(dpo);
return;
}
memcpy(name, characters, count);
name[count] = 0;
}
dpo->annotation.name = name;
}
}
}
static void
handleElementStart (void *userData, const char *element, const char **attributes) {
CLDR_DocumentParserObject *dpo = userData;
dpo->document.depth += 1;
if (dpo->annotation.depth) {
logMessage(LOG_WARNING, "nested annotation");
abortParser(dpo);
return;
}
if (strcmp(element, "annotation") == 0) {
const char *sequence = NULL;
int tts = 0;
while (*attributes) {
const char *name = *attributes++;
const char *value = *attributes++;
if (strcmp(name, "type") == 0) {
if (strcmp(value, "tts") == 0) tts = 1;
} else if (strcmp(name, "cp") == 0) {
sequence = value;
}
}
if (tts) {
if (sequence) {
if ((dpo->annotation.sequence = strdup(sequence))) {
dpo->annotation.depth = dpo->document.depth;
} else {
logMallocError();
abortParser(dpo);
}
}
}
}
}
static void
handleElementEnd (void *userData, const char *name) {
CLDR_DocumentParserObject *dpo = userData;
if (dpo->document.depth == dpo->annotation.depth) {
if (dpo->annotation.name) {
CLDR_AnnotationHandlerParameters parameters = {
.sequence = dpo->annotation.sequence,
.name = dpo->annotation.name,
.data = dpo->caller.data
};
if (!dpo->caller.handler(¶meters)) {
abortParser(dpo);
return;
}
free(dpo->annotation.name);
dpo->annotation.name = NULL;
}
free(dpo->annotation.sequence);
dpo->annotation.sequence = NULL;
dpo->annotation.depth = 0;
}
dpo->document.depth -= 1;
}
#endif /* HAVE_EXPAT */
CLDR_DocumentParserObject *
cldrNewDocumentParser (CLDR_AnnotationHandler *handler, void *data) {
#ifdef HAVE_EXPAT
CLDR_DocumentParserObject *dpo;
if ((dpo = malloc(sizeof(*dpo)))) {
memset(dpo, 0, sizeof(*dpo));
dpo->caller.handler = handler;
dpo->caller.data = data;
dpo->document.depth = 0;
dpo->annotation.sequence = NULL;
dpo->annotation.name = NULL;
dpo->annotation.depth = 0;
if ((dpo->document.parser = XML_ParserCreate(NULL))) {
XML_SetUserData(dpo->document.parser, dpo);
XML_SetElementHandler(dpo->document.parser, handleElementStart, handleElementEnd);
XML_SetCharacterDataHandler(dpo->document.parser, appendAnnotationText);
return dpo;
} else {
logMallocError();
}
free(dpo);
} else {
logMallocError();
}
#else /* HAVE_EXPAT */
logMessage(LOG_WARNING, "XML parsing support has not been included");
#endif /* HAVE_EXPAT */
return NULL;
}
void
cldrDestroyDocumentParser (CLDR_DocumentParserObject *dpo) {
#ifdef HAVE_EXPAT
if (dpo->annotation.sequence) {
free(dpo->annotation.sequence);
dpo->annotation.sequence = NULL;
}
if (dpo->annotation.name) {
free(dpo->annotation.name);
dpo->annotation.name = NULL;
}
XML_ParserFree(dpo->document.parser);
free(dpo);
#endif /* HAVE_EXPAT */
}
int
cldrParseText (CLDR_DocumentParserObject *dpo, const char *text, size_t size, int final) {
#ifdef HAVE_EXPAT
enum XML_Status status = XML_Parse(dpo->document.parser, text, size, final);
switch (status) {
case XML_STATUS_OK:
return 1;
case XML_STATUS_ERROR:
logMessage(LOG_WARNING, "CLDR parse error: %s", XML_ErrorString(XML_GetErrorCode(dpo->document.parser)));
break;
default:
logMessage(LOG_WARNING, "unrecognized CLDR parse status: %d", status);
break;
}
#endif /* HAVE_EXPAT */
return 0;
}
int
cldrParseDocument (
const char *document, size_t size,
CLDR_AnnotationHandler *handler, void *data
) {
int ok = 0;
CLDR_DocumentParserObject *dpo = cldrNewDocumentParser(handler, data);
if (dpo) {
if (cldrParseText(dpo, document, size, 1)) ok = 1;
cldrDestroyDocumentParser(dpo);
}
return ok;
}
const char cldrAnnotationsDirectory[] = "/usr/share/unicode/cldr/common/annotations";
const char cldrAnnotationsExtension[] = ".xml";
int
cldrParseFile (
const char *name,
CLDR_AnnotationHandler *handler, void *data
) {
int ok = 0;
char *path = makeFilePath(cldrAnnotationsDirectory, name, cldrAnnotationsExtension);
if (path) {
logMessage(LOG_DEBUG, "processing CLDR annotations file: %s", path);
int fd = open(path, O_RDONLY);
if (fd != -1) {
CLDR_DocumentParserObject *dpo = cldrNewDocumentParser(handler, data);
if (dpo) {
while (1) {
char buffer[0X2000];
size_t size = sizeof(buffer);
ssize_t count = read(fd, buffer, size);
if (count == -1) {
if (errno == EINTR) continue;
logMessage(LOG_WARNING, "CLDR read error: %s: %s", strerror(errno), path);
break;
}
int final = count == 0;
if (!cldrParseText(dpo, buffer, count, final)) break;
if (final) {
ok = 1;
break;
}
}
cldrDestroyDocumentParser(dpo);
}
close(fd);
fd = -1;
} else {
logMessage(LOG_WARNING, "CLDR open error: %s: %s", strerror(errno), path);
if (errno == ENOENT) {
if (!isAbsolutePath(name)) {
if (!testDirectoryPath(cldrAnnotationsDirectory)) {
logPossibleCause("the package that defines the CLDR annotations directory is not installed");
}
}
}
}
free(path);
}
return ok;
}