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
|
/*
* 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 <stdio.h>
#include <string.h>
#include <errno.h>
#include <iconv.h>
#include <locale.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif /* HAVE_LANGINFO_H */
#include "log.h"
#include "charset_internal.h"
#include "program.h"
#define CHARSET_ICONV_NULL ((iconv_t)-1)
#define CHARSET_ICONV_HANDLE(name) iconv_t iconv##name = CHARSET_ICONV_NULL
static CHARSET_ICONV_HANDLE(CharToWchar);
static CHARSET_ICONV_HANDLE(WcharToChar);
#define CHARSET_CONVERT_TYPE_TO_TYPE(name, from, to, ret, eof) \
ret convert##name (from f) { \
if (getCharset()) { \
from *fp = &f; \
size_t fs = sizeof(f); \
to t; \
to *tp = &t; \
size_t ts = sizeof(t); \
if (iconv(iconv##name, (void *)&fp, &fs, (void *)&tp, &ts) != (size_t)-1) return t; \
logMessage(LOG_DEBUG, "iconv (" #from " -> " #to ") error: %s", strerror(errno)); \
} \
return eof; \
}
CHARSET_CONVERT_TYPE_TO_TYPE(CharToWchar, char, wchar_t, wint_t, WEOF)
CHARSET_CONVERT_TYPE_TO_TYPE(WcharToChar, wchar_t, unsigned char, int, EOF)
#undef CHARSET_CONVERT_TYPE_TO_TYPE
const char *
getLocaleCharset (void) {
const char *locale = setlocale(LC_ALL, "");
if (locale && !isPosixLocale(locale)) {
#ifdef HAVE_NL_LANGINFO
/* some 8-bit locale is set, assume its charset is correct */
return nl_langinfo(CODESET);
#endif /* HAVE_NL_LANGINFO */
}
return defaultCharset;
}
static void
exitCharsetIconv (void *data) {
static iconv_t *const handles[] = {
&iconvCharToWchar,
&iconvWcharToChar
};
iconv_t *const *handle = handles;
iconv_t *const *const end = handle + ARRAY_COUNT(handles);
while (handle < end) {
if (**handle != CHARSET_ICONV_NULL) {
iconv_close(**handle);
**handle = CHARSET_ICONV_NULL;
}
handle += 1;
}
}
int
registerCharacterSet (const char *charset) {
int firstTime = 0;
const char *const wcharCharset = getWcharCharset();
typedef struct {
iconv_t *const handle;
const char *const fromCharset;
const char *const toCharset;
iconv_t newHandle;
} ConvEntry;
ConvEntry convTable[] = {
{ .handle = &iconvCharToWchar,
.fromCharset = charset,
.toCharset = wcharCharset
},
{ .handle = &iconvWcharToChar,
.fromCharset = wcharCharset,
.toCharset = charset
},
};
ConvEntry *conv = convTable;
const ConvEntry *convEnd = conv + ARRAY_COUNT(convTable);
while (conv < convEnd) {
if ((conv->newHandle = iconv_open(conv->toCharset, conv->fromCharset)) == CHARSET_ICONV_NULL) {
logSystemError("iconv_open");
while (conv > convTable) {
conv -= 1;
iconv_close(conv->newHandle);
}
return 0;
}
conv += 1;
}
while (conv > convTable) {
conv -= 1;
if (*conv->handle == CHARSET_ICONV_NULL) {
firstTime = 1;
} else {
iconv_close(*conv->handle);
}
*conv->handle = conv->newHandle;
}
if (firstTime) onProgramExit("charset-iconv", exitCharsetIconv, NULL);
return 1;
}
|