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
|
/*
* 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-2025 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 "cmdline.h"
#include "cmdput.h"
#include "options.h"
#include "log.h"
#include "file.h"
#include "unicode.h"
#include "utf8.h"
#include "brl_dots.h"
#include "ttb.h"
char *opt_tablesDirectory;
static char *opt_inputTable;
static char *opt_outputTable;
static int opt_sixDots;
static int opt_noBaseCharacters;
static const char tableName_autoselect[] = "auto";
static const char tableName_unicode[] = "unicode";
BEGIN_COMMAND_LINE_OPTIONS(programOptions)
{ .word = "input-table",
.letter = 'i',
.argument = strtext("file"),
.setting.string = &opt_inputTable,
.internal.setting = tableName_autoselect,
.description = strtext("The name of (or path to) the input text table.")
},
{ .word = "output-table",
.letter = 'o',
.argument = strtext("file"),
.setting.string = &opt_outputTable,
.internal.setting = tableName_unicode,
.description = strtext("The name of (or path to) the output text table.")
},
{ .word = "six-dots",
.letter = '6',
.setting.flag = &opt_sixDots,
.description = strtext("Remove dots seven and eight.")
},
{ .word = "no-base-characters",
.letter = 'b',
.setting.flag = &opt_noBaseCharacters,
.description = strtext("Don't fall back to the Unicode base character.")
},
{ .word = "tables-directory",
.letter = 'T',
.argument = strtext("directory"),
.setting.string = &opt_tablesDirectory,
.internal.setting = TABLES_DIRECTORY,
.internal.adjust = toAbsoluteInstallPath,
.description = strtext("Path to directory for text tables.")
},
END_COMMAND_LINE_OPTIONS(programOptions)
BEGIN_COMMAND_LINE_PARAMETERS(programParameters)
END_COMMAND_LINE_PARAMETERS(programParameters)
BEGIN_COMMAND_LINE_NOTES(programNotes)
"If no files are specified then standard input is translated.",
"",
"Unicode braille patterns are always recognized as valid input characters..",
"If an input text table has been specified then the braille characters defined by that table are also recognized.",
"",
"If an output text table has been specified then the braille characters defined by that table are written.",
"If an output text table hasn't been specified then Unicode braille patterns are written.",
END_COMMAND_LINE_NOTES
BEGIN_COMMAND_LINE_DESCRIPTOR(programDescriptor)
.name = "brltty-trtxt",
.purpose = strtext("Translate one binary braille representation to another."),
.options = &programOptions,
.parameters = &programParameters,
.notes = COMMAND_LINE_NOTES(programNotes),
.extraParameters = {
.name = "file",
.description = "the files to translate (use - for standard input)",
},
END_COMMAND_LINE_DESCRIPTOR
static TextTable *inputTable;
static TextTable *outputTable;
static const char *outputName;
static unsigned char (*toDots) (wchar_t character);
static wchar_t (*toCharacter) (unsigned char dots);
static unsigned char
toDots_mapped (wchar_t character) {
return convertCharacterToDots(inputTable, character);
}
static unsigned char
toDots_unicode (wchar_t character) {
return ((character & UNICODE_ROW_MASK) == UNICODE_BRAILLE_ROW)?
character & UNICODE_CELL_MASK:
(BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6 | BRL_DOT_7 | BRL_DOT_8);
}
static wchar_t
toCharacter_mapped (unsigned char dots) {
return convertDotsToCharacter(outputTable, dots);
}
static wchar_t
toCharacter_unicode (unsigned char dots) {
return UNICODE_BRAILLE_ROW | dots;
}
static int
writeCharacter (const wchar_t *character, mbstate_t *state) {
char bytes[0X1000];
size_t result = wcrtomb(bytes, (character? *character: WC_C('\0')), state);
if (result == (size_t)-1) return 0;
if (!character) result -= 1;
putBytes(bytes, result);
return 1;
}
static int
processStream (FILE *inputStream, const char *inputName) {
mbstate_t inputState;
memset(&inputState, 0, sizeof(inputState));
mbstate_t outputState;
memset(&outputState, 0, sizeof(outputState));
while (!feof(inputStream)) {
char inputBuffer[0X1000];
size_t inputCount = fread(inputBuffer, 1, sizeof(inputBuffer)-1, inputStream);
if (ferror(inputStream)) goto inputError;
if (!inputCount) break;
inputBuffer[inputCount] = 0;
{
char *byte = inputBuffer;
while (inputCount) {
wchar_t character;
{
size_t result = mbrtowc(&character, byte, inputCount, &inputState);
if (result == (size_t)-2) break;
if (result == (size_t)-1) goto inputError;
if (!result) result = 1;
byte += result;
inputCount -= result;
}
if (!iswcntrl(character)) {
unsigned char dots = toDots(character);
if (dots || !iswspace(character)) {
if (opt_sixDots) dots &= ~(BRL_DOT_7 | BRL_DOT_8);
character = toCharacter(dots);
}
}
if (!writeCharacter(&character, &outputState)) goto outputError;
}
}
}
if (!writeCharacter(NULL, &outputState)) goto outputError;
putFlush();
if (!mbsinit(&inputState)) {
#ifdef EILSEQ
errno = EILSEQ;
#else /* EILSEQ */
errno = EINVAL;
#endif /* EILSEQ */
goto inputError;
}
return 1;
inputError:
logMessage(LOG_ERR, "input error: %s: %s", inputName, strerror(errno));
return 0;
outputError:
logMessage(LOG_ERR, "output error: %s: %s", outputName, strerror(errno));
return 0;
}
static int
getTable (TextTable **table, const char *name) {
const char *directory = opt_tablesDirectory;
*table = NULL;
if (strcmp(name, tableName_unicode) != 0) {
char *allocated = NULL;
if (strcmp(name, tableName_autoselect) == 0) {
if (!(name = allocated = getTextTableForLocale(directory))) {
logMessage(LOG_ERR, "cannot find text table for current locale");
}
}
if (name) {
char *path = makeTextTablePath(directory, name);
if (path) {
*table = compileTextTable(path);
free(path);
}
}
if (allocated) free(allocated);
if (opt_noBaseCharacters) setTryBaseCharacter(*table, 0);
if (!*table) return 0;
}
return 1;
}
int
main (int argc, char *argv[]) {
PROCESS_COMMAND_LINE(programDescriptor, argc, argv);
ProgramExitStatus exitStatus = PROG_EXIT_FATAL;
if (getTable(&inputTable, opt_inputTable)) {
if (getTable(&outputTable, opt_outputTable)) {
outputName = standardOutputName;
toDots = inputTable? toDots_mapped: toDots_unicode;
toCharacter = outputTable? toCharacter_mapped: toCharacter_unicode;
if (argc) {
do {
const char *file = argv[0];
FILE *stream;
if (strcmp(file, standardStreamArgument) == 0) {
if (!processStream(stdin, standardInputName)) break;
} else if ((stream = fopen(file, "r"))) {
int ok = processStream(stream, file);
fclose(stream);
if (!ok) break;
} else {
logMessage(LOG_ERR, "cannot open file: %s: %s", file, strerror(errno));
exitStatus = PROG_EXIT_SEMANTIC;
break;
}
argv += 1, argc -= 1;
} while (argc);
if (!argc) exitStatus = PROG_EXIT_SUCCESS;
} else if (processStream(stdin, standardInputName)) {
exitStatus = PROG_EXIT_SUCCESS;
}
if (outputTable) destroyTextTable(outputTable);
}
if (inputTable) destroyTextTable(inputTable);
}
return exitStatus;
}
|