Newer
Older
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* 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-2018 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.com/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <string.h>
#include <errno.h>
#include "log.h"
#include "ctb_translate.h"
#include "brl_dots.h"
#include "file.h"
#include "parse.h"
#include "charset.h"
static int
putExternalRequests (BrailleContractionData *bcd) {
typedef enum {
REQ_TEXT,
REQ_NUMBER
} ExternalRequestType;
typedef struct {
const char *name;
ExternalRequestType type;
union {
struct {
const wchar_t *start;
size_t count;
} text;
unsigned int number;
} value;
} ExternalRequestEntry;
const ExternalRequestEntry externalRequestTable[] = {
{ .name = "cursor-position",
.type = REQ_NUMBER,
.value.number = bcd->input.cursor? bcd->input.cursor-bcd->input.begin+1: 0
},
{ .name = "expand-current-word",
.type = REQ_NUMBER,
.value.number = prefs.expandCurrentWord
},
{ .name = "capitalization-mode",
.type = REQ_NUMBER,
.value.number = prefs.capitalizationMode
},
{ .name = "maximum-length",
.type = REQ_NUMBER,
.value.number = getOutputCount(bcd)
},
{ .name = "text",
.type = REQ_TEXT,
.value.text = {
.start = bcd->input.begin,
.count = getInputCount(bcd)
}
},
{ .name = NULL }
};
FILE *stream = bcd->table->data.external.standardInput;
const ExternalRequestEntry *req = externalRequestTable;
while (req->name) {
if (fputs(req->name, stream) == EOF) goto outputError;
if (fputc('=', stream) == EOF) goto outputError;
switch (req->type) {
case REQ_TEXT: {
const wchar_t *character = req->value.text.start;
const wchar_t *end = character + req->value.text.count;
while (character < end) {
Utf8Buffer utf8;
size_t utfs = convertWcharToUtf8(*character++, utf8);
if (!utfs) return 0;
if (fputs(utf8, stream) == EOF) goto outputError;
}
break;
}
case REQ_NUMBER:
if (fprintf(stream, "%u", req->value.number) == EOF) goto outputError;
break;
default:
logMessage(LOG_WARNING, "unimplemented external contraction request property type: %s: %u (%s)", bcd->table->data.external.command, req->type, req->name);
return 0;
}
if (fputc('\n', stream) == EOF) goto outputError;
req += 1;
}
if (fflush(stream) == EOF) goto outputError;
return 1;
outputError:
logMessage(LOG_WARNING, "external contraction output error: %s: %s", bcd->table->data.external.command, strerror(errno));
return 0;
}
static const unsigned char brfTable[0X40] = {
/* 0X20 */ 0,
/* 0X21 ! */ BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_6,
/* 0X22 " */ BRL_DOT_5,
/* 0X23 # */ BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6,
/* 0X24 $ */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_4 | BRL_DOT_6,
/* 0X25 % */ BRL_DOT_1 | BRL_DOT_4 | BRL_DOT_6,
/* 0X26 & */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_6,
/* 0X27 ' */ BRL_DOT_3,
/* 0X28 ( */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_5 | BRL_DOT_6,
/* 0X29 ) */ BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6,
/* 0X2A * */ BRL_DOT_1 | BRL_DOT_6,
/* 0X2B + */ BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_6,
/* 0X2C , */ BRL_DOT_6,
/* 0X2D - */ BRL_DOT_3 | BRL_DOT_6,
/* 0X2E . */ BRL_DOT_4 | BRL_DOT_6,
/* 0X2F / */ BRL_DOT_3 | BRL_DOT_4,
/* 0X30 0 */ BRL_DOT_3 | BRL_DOT_5 | BRL_DOT_6,
/* 0X31 1 */ BRL_DOT_2,
/* 0X32 2 */ BRL_DOT_2 | BRL_DOT_3,
/* 0X33 3 */ BRL_DOT_2 | BRL_DOT_5,
/* 0X34 4 */ BRL_DOT_2 | BRL_DOT_5 | BRL_DOT_6,
/* 0X35 5 */ BRL_DOT_2 | BRL_DOT_6,
/* 0X36 6 */ BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_5,
/* 0X37 7 */ BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_5 | BRL_DOT_6,
/* 0X38 8 */ BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_6,
/* 0X39 9 */ BRL_DOT_3 | BRL_DOT_5,
/* 0X3A : */ BRL_DOT_1 | BRL_DOT_5 | BRL_DOT_6,
/* 0X3B ; */ BRL_DOT_5 | BRL_DOT_6,
/* 0X3C < */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_6,
/* 0X3D = */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6,
/* 0X3E > */ BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5,
/* 0X3F ? */ BRL_DOT_1 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6,
/* 0X40 @ */ BRL_DOT_4,
/* 0X41 A */ BRL_DOT_1,
/* 0X42 B */ BRL_DOT_1 | BRL_DOT_2,
/* 0X43 C */ BRL_DOT_1 | BRL_DOT_4,
/* 0X44 D */ BRL_DOT_1 | BRL_DOT_4 | BRL_DOT_5,
/* 0X45 E */ BRL_DOT_1 | BRL_DOT_5,
/* 0X46 F */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_4,
/* 0X47 G */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_4 | BRL_DOT_5,
/* 0X48 H */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_5,
/* 0X49 I */ BRL_DOT_2 | BRL_DOT_4,
/* 0X4A J */ BRL_DOT_2 | BRL_DOT_4 | BRL_DOT_5,
/* 0X4B K */ BRL_DOT_1 | BRL_DOT_3,
/* 0X4C L */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3,
/* 0X4D M */ BRL_DOT_1 | BRL_DOT_3 | BRL_DOT_4,
/* 0X4E N */ BRL_DOT_1 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5,
/* 0X4F O */ BRL_DOT_1 | BRL_DOT_3 | BRL_DOT_5,
/* 0X50 P */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4,
/* 0X51 Q */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5,
/* 0X52 R */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_5,
/* 0X53 S */ BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4,
/* 0X54 T */ BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5,
/* 0X55 U */ BRL_DOT_1 | BRL_DOT_3 | BRL_DOT_6,
/* 0X56 V */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_6,
/* 0X57 W */ BRL_DOT_2 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6,
/* 0X58 X */ BRL_DOT_1 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_6,
/* 0X59 Y */ BRL_DOT_1 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6,
/* 0X5A Z */ BRL_DOT_1 | BRL_DOT_3 | BRL_DOT_5 | BRL_DOT_6,
/* 0X5B [ */ BRL_DOT_2 | BRL_DOT_4 | BRL_DOT_6,
/* 0X5C \ */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_5 | BRL_DOT_6,
/* 0X5D ] */ BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6,
/* 0X5E ^ */ BRL_DOT_4 | BRL_DOT_5,
/* 0X5F _ */ BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6
};
static int
handleExternalResponse_brf (BrailleContractionData *bcd, const char *value) {
int useDot7 = prefs.capitalizationMode == CTB_CAP_DOT7;
while (*value && (bcd->output.current < bcd->output.end)) {
unsigned char brf = *value++ & 0XFF;
unsigned char dots = 0;
unsigned char superimpose = 0;
if ((brf >= 0X60) && (brf <= 0X7F)) {
brf -= 0X20;
} else if ((brf >= 0X41) && (brf <= 0X5A)) {
if (useDot7) superimpose |= BRL_DOT_7;
}
if ((brf >= 0X20) && (brf <= 0X5F)) dots = brfTable[brf - 0X20] | superimpose;
*bcd->output.current++ = dots;
}
return 1;
}
static int
handleExternalResponse_consumedLength (BrailleContractionData *bcd, const char *value) {
int length;
if (!isInteger(&length, value)) return 0;
if (length < 1) return 0;
if (length > getInputCount(bcd)) return 0;
bcd->input.current = bcd->input.begin + length;
return 1;
}
static int
handleExternalResponse_outputOffsets (BrailleContractionData *bcd, const char *value) {
if (bcd->input.offsets) {
int previous = CTB_NO_OFFSET;
unsigned int count = getInputCount(bcd);
unsigned int index = 0;
while (*value && (index < count)) {
int offset;
{
char *delimiter = strchr(value, ',');
if (delimiter) {
int ok;
{
char oldDelimiter = *delimiter;
*delimiter = 0;
ok = isInteger(&offset, value);
*delimiter = oldDelimiter;
}
if (!ok) return 0;
value = delimiter + 1;
} else if (isInteger(&offset, value)) {
value += strlen(value);
} else {
return 0;
}
}
if (offset < ((index == 0)? 0: previous)) return 0;
if (offset >= getOutputCount(bcd)) return 0;
bcd->input.offsets[index++] = (offset == previous)? CTB_NO_OFFSET: offset;
previous = offset;
}
}
return 1;
}
typedef struct {
const char *name;
int (*handler) (BrailleContractionData *bcd, const char *value);
unsigned stop:1;
} ExternalResponseEntry;
static const ExternalResponseEntry externalResponseTable[] = {
{ .name = "brf",
.stop = 1,
.handler = handleExternalResponse_brf
},
{ .name = "consumed-length",
.handler = handleExternalResponse_consumedLength
},
{ .name = "output-offsets",
.handler = handleExternalResponse_outputOffsets
},
{ .name = NULL }
};
static int
getExternalResponses (BrailleContractionData *bcd) {
FILE *stream = bcd->table->data.external.standardOutput;
while (readLine(stream, &bcd->table->data.external.input.buffer, &bcd->table->data.external.input.size)) {
int ok = 0;
int stop = 0;
char *delimiter = strchr(bcd->table->data.external.input.buffer, '=');
if (delimiter) {
const char *value = delimiter + 1;
const ExternalResponseEntry *rsp = externalResponseTable;
char oldDelimiter = *delimiter;
*delimiter = 0;
while (rsp->name) {
if (strcmp(bcd->table->data.external.input.buffer, rsp->name) == 0) {
if (rsp->handler(bcd, value)) ok = 1;
if (rsp->stop) stop = 1;
break;
}
rsp += 1;
}
*delimiter = oldDelimiter;
}
if (!ok) logMessage(LOG_WARNING, "unexpected external contraction response: %s: %s", bcd->table->data.external.command, bcd->table->data.external.input.buffer);
if (stop) return 1;
}
logMessage(LOG_WARNING, "incomplete external contraction response: %s", bcd->table->data.external.command);
return 0;
}
static int
contractText_external (BrailleContractionData *bcd) {
setOffset(bcd);
while (++bcd->input.current < bcd->input.end) clearOffset(bcd);
if (startContractionCommand(bcd->table)) {
if (putExternalRequests(bcd)) {
if (getExternalResponses(bcd)) {
return 1;
}
}
}
stopContractionCommand(bcd->table);
return 0;
}
static void
finishCharacterEntry_external (BrailleContractionData *bcd, CharacterEntry *entry) {
}
static const ContractionTableTranslationMethods externalTranslationMethods = {
.contractText = contractText_external,
.finishCharacterEntry = finishCharacterEntry_external
};
const ContractionTableTranslationMethods *
getContractionTableTranslationMethods_external (void) {
return &externalTranslationMethods;
}