File: ttb_native.c

package info (click to toggle)
brltty 6.7-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,240 kB
  • sloc: ansic: 148,859; java: 13,418; sh: 9,623; xml: 5,699; tcl: 2,634; makefile: 2,333; awk: 713; lisp: 366; python: 321; ml: 301
file content (324 lines) | stat: -rw-r--r-- 8,223 bytes parent folder | download | duplicates (3)
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
/*
 * 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-2024 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 "file.h"
#include "ttb.h"
#include "ttb_internal.h"
#include "ttb_compile.h"

static int
getByteOperand (DataFile *file, unsigned char *byte) {
  DataString string;
  const char *description = "local character";

  if (getDataString(file, &string, 1, description)) {
    if ((string.length == 1) && iswLatin1(string.characters[0])) {
      *byte = string.characters[0];
      return 1;
    } else {
      reportDataError(file, "invalid %s: %.*" PRIws,
                      description, string.length, string.characters);
    }
  }

  return 0;
}

static const char characterDescription[] = "Unicode character";

static int
isCharacterOperand (
  DataFile *file, wchar_t *character,
  const wchar_t *characters, int length
) {
  if (length == 1) {
    wchar_t wc = characters[0];

    if (!(wc & ~UNICODE_CHARACTER_MASK)) {
      *character = wc;
      return 1;
    } else {
      reportDataError(file, "%s out of range: %.*" PRIws,
                      characterDescription, length, characters);
    }
  } else {
    reportDataError(file, "not a single %s: %.*" PRIws,
                    characterDescription, length, characters);
  }

  return 0;
}

static int
getCharacterOperand (DataFile *file, wchar_t *character) {
  DataString string;

  if (getDataString(file, &string, 0, characterDescription)) {
    if (isCharacterOperand(file, character, string.characters, string.length)) {
      return 1;
    }
  }

  return 0;
}

static int
getDotsOperand (DataFile *file, unsigned char *dots) {
  if (findDataOperand(file, "cell")) {
    wchar_t character;

    if (getDataCharacter(file, &character)) {
      int noDots = 0;
      wchar_t enclosed = (character == WC_C('('))? WC_C(')'):
                         0;
      *dots = 0;

      if (!enclosed) {
        if (wcschr(WS_C("0"), character)) {
          noDots = 1;
        } else {
          ungetDataCharacters(file, 1);
        }
      }

      while (getDataCharacter(file, &character)) {
        int space = iswspace(character);

        if (enclosed) {
          if (character == enclosed) {
            enclosed = 0;
            break;
          }

          if (space) continue;
        } else if (space) {
          ungetDataCharacters(file, 1);
          break;
        }

        {
          int dot;

          if (noDots || !brlDotNumberToIndex(character, &dot)) {
            reportDataError(file, "invalid dot number: %.1" PRIws, &character);
            return 0;
          }

          {
            unsigned char bit = brlDotBits[dot];

            if (*dots & bit) {
              reportDataError(file, "duplicate dot number: %.1" PRIws, &character);
              return 0;
            }

            *dots |= bit;
          }
        }
      }

      if (enclosed) {
        reportDataError(file, "incomplete cell");
        return 0;
      }

      return 1;
    }
  }

  return 0;
}

static DATA_OPERANDS_PROCESSOR(processAliasOperands) {
  TextTableData *ttd = data;
  wchar_t from;

  if (getCharacterOperand(file, &from)) {
    wchar_t to;

    if (getCharacterOperand(file, &to)) {
      if (!addTextTableAlias(ttd, from, to)) return 0;
    }
  }

  return 1;
}

static DATA_OPERANDS_PROCESSOR(processByteOperands) {
  TextTableData *ttd = data;
  unsigned char byte;

  if (getByteOperand(file, &byte)) {
    unsigned char dots;

    if (getDotsOperand(file, &dots)) {
      if (!setTextTableByte(ttd, byte, dots)) return 0;
    }
  }

  return 1;
}

static DATA_OPERANDS_PROCESSOR(processCharOperands) {
  TextTableData *ttd = data;
  wchar_t character;

  if (getCharacterOperand(file, &character)) {
    unsigned char dots;

    if (getDotsOperand(file, &dots)) {
      if (!setTextTableCharacter(ttd, character, dots)) return 0;
    }
  }

  return 1;
}

static DATA_OPERANDS_PROCESSOR(processGlyphOperands) {
  TextTableData *ttd = data;
  wchar_t character;

  if (getCharacterOperand(file, &character)) {
    unsigned char dots;

    if (getDotsOperand(file, &dots)) {
      if (!setTextTableGlyph(ttd, character, dots)) return 0;
    }
  }

  return 1;
}

static DATA_OPERANDS_PROCESSOR(processInputOperands) {
  TextTableData *ttd = data;
  wchar_t character;

  if (getCharacterOperand(file, &character)) {
    unsigned char dots;

    if (getDotsOperand(file, &dots)) {
      if (!setTextTableInput(ttd, character, dots)) return 0;
    }
  }

  return 1;
}

static DATA_CONDITION_TESTER(testGlyphDefined) {
  TextTableData *ttd = data;

  wchar_t character;
  if (!isCharacterOperand(file, &character, identifier->characters, identifier->length)) return 0;

  UnicodeRowEntry *row = getUnicodeRowEntry(ttd, character, 0);
  if (!row) return 0;

  unsigned int cellNumber = UNICODE_CELL_NUMBER(character);
  return !!BITMASK_TEST(row->cellDefined, cellNumber);
}

static int
processGlyphTestOperands (DataFile *file, int not, void *data) {
  return processConditionOperands(file, testGlyphDefined, not, characterDescription, data);
}

static DATA_OPERANDS_PROCESSOR(processIfGlyphOperands) {
  return processGlyphTestOperands(file, 0, data);
}

static DATA_OPERANDS_PROCESSOR(processIfNotGlyphOperands) {
  return processGlyphTestOperands(file, 1, data);
}

static const char inputDescription[] = "dot number(s)";

static DATA_CONDITION_TESTER(testInputDefined) {
  TextTableData *ttd = data;

  ByteOperand cells;
  if (!parseCellsOperand(file, &cells, identifier->characters, identifier->length)) return 0;

  if (cells.length != 1) {
    reportDataError(file, "not a single %s: %.*" PRIws,
                    inputDescription, identifier->length, identifier->characters);

    return 0;
  }

  const TextTableHeader *header = getTextTableHeader(ttd);
  return !!BITMASK_TEST(header->inputCharacterDefined, cells.bytes[0]);
}

static int
processInputTestOperands (DataFile *file, int not, void *data) {
  return processConditionOperands(file, testInputDefined, not, inputDescription, data);
}

static DATA_OPERANDS_PROCESSOR(processIfInputOperands) {
  return processInputTestOperands(file, 0, data);
}

static DATA_OPERANDS_PROCESSOR(processIfNotInputOperands) {
  return processInputTestOperands(file, 1, data);
}

static DATA_OPERANDS_PROCESSOR(processNativeTextTableOperands) {
  BEGIN_DATA_DIRECTIVE_TABLE
    DATA_NESTING_DIRECTIVES,
    DATA_VARIABLE_DIRECTIVES,
    DATA_CONDITION_DIRECTIVES,
    {.name=WS_C("alias"), .processor=processAliasOperands},
    {.name=WS_C("byte"), .processor=processByteOperands},
    {.name=WS_C("char"), .processor=processCharOperands},
    {.name=WS_C("glyph"), .processor=processGlyphOperands},
    {.name=WS_C("input"), .processor=processInputOperands},
    {.name=WS_C("ifglyph"), .processor=processIfGlyphOperands, .unconditional=1},
    {.name=WS_C("ifnotglyph"), .processor=processIfNotGlyphOperands, .unconditional=1},
    {.name=WS_C("ifinput"), .processor=processIfInputOperands, .unconditional=1},
    {.name=WS_C("ifnotinput"), .processor=processIfNotInputOperands, .unconditional=1},
  END_DATA_DIRECTIVE_TABLE

  return processDirectiveOperand(file, &directives, "text table directive", data);
}

TextTableData *
processTextTableStream (FILE *stream, const char *name) {
  return processTextTableLines(stream, name, processNativeTextTableOperands);
}

TextTable *
compileTextTable (const char *name) {
  TextTable *table = NULL;
  FILE *stream;

  if ((stream = openDataFile(name, "r", 0))) {
    TextTableData *ttd;

    if ((ttd = processTextTableStream(stream, name))) {
      table = makeTextTable(ttd);

      destroyTextTableData(ttd);
    }

    fclose(stream);
  }

  return table;
}