summaryrefslogtreecommitdiff
path: root/src/icu-glue.cc
blob: 6350386f3455237fe22901c044e564f99e23e1e2 (plain)
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
/*
 * Copyright © 2019 Christian Persch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib.h>

#include <cstring>

#include <algorithm>
#include <memory>
#include <string>
#include <vector>

#include <unicode/errorcode.h>
#include <unicode/ucnv.h>

#include "icu-glue.hh"

namespace vte::base {

/* VTE cannot use the converters for ECMA-35 (ISO-2022-*), since they
 * interpret escape sequences (for charset designation), and do not
 * (and *cannot*, without having a complete escape sequence parser)
 * let through the non-designation sequences.
 *
 * The user will need to use luit(1) instead.
 */

bool
get_icu_charset_is_ecma35(char const* charset)
{
        return strstr(charset, "2022") != nullptr;
}

char**
get_icu_charsets(bool aliases)
{
        auto count = ucnv_countAvailable();
        auto names = std::vector<std::string>{};
        names.reserve(count);

        for (auto i = 0; i < count; ++i) {
                auto name = ucnv_getAvailableName(i);
                if (get_icu_charset_is_ecma35(name))
                        continue;

                if (!aliases) {
                        names.push_back(name);
                        continue;
                }

                auto err = icu::ErrorCode{};
                auto acount = ucnv_countAliases(name, err);
                if (err.isFailure()) {
                        names.push_back(name);
                        continue;
                }

                /* The aliases will include @name */

                for (auto j = 0; j < acount; ++j) {
                        err.reset();
                        auto alias = ucnv_getAlias(name, j, err);
                        if (err.isFailure())
                                continue;
                        names.push_back(alias);
                }
        }

        /* Sort the charsets list */
        std::sort(names.begin(), names.end());

        auto r = g_new0(char*, names.size() + 1);
        auto n = r;
        for (auto const& name : names)
                *n++ = g_strdup(name.c_str());
        *n++ = nullptr;

        return r;
}

bool
get_icu_charset_supported(char const* charset)
{
        if (get_icu_charset_is_ecma35(charset))
                return false;

        auto err = icu::ErrorCode{};
        auto count = ucnv_countAliases(charset, err);
        return err.isSuccess() && count != 0;
}

std::shared_ptr<UConverter>
make_icu_converter(char const* charset,
                   GError** error)
{
        auto err = icu::ErrorCode{};
        auto converter = std::shared_ptr<UConverter>{ucnv_open(charset, err), &ucnv_close};
        if (err.isFailure()) {
                g_set_error(error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
                            "Failed to open converter for charset \"%s\": %s",
                            charset, err.errorName());
                return {};
        }

        /* The unicode->target conversion is only used when converting
         * user input (keyboard, clipboard) to be sent to the PTY, and
         * we don't want the ucnv_fromUChars to substitute the SUB character
         * for illegal input, since SUB is U+001A which is Ctrl-Z, which
         * the default UCNV_FROM_U_CALLBACK_SUBSTITUTE callback does.
         * Use UCNV_FROM_U_CALLBACK_STOP to stop conversion when encountering
         * illegal input.
         */
        err.reset();
        ucnv_setFromUCallBack(converter.get(),
                              UCNV_FROM_U_CALLBACK_STOP,
                              nullptr,
                              nullptr,
                              nullptr,
                              err);
        if (err.isFailure()) {
                g_set_error(error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
                            "Failed ucnv_setFromUCallBack for charset \"%s\": %s",
                            charset, err.errorName());
                return {};
        }

        return converter;
}

} // namespace vte::base