lcall.c
4.14 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2003, 2009-2012, 2014-2017 Free Software Foundation,
Inc.
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 Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <mailutils/nls.h>
static int
_parse_lc_all (const char *arg, struct mu_lc_all *str, int flags)
{
char *s;
size_t n;
str->flags = 0;
n = strcspn (arg, "_.@");
if (flags & MU_LC_LANG)
{
s = malloc (n + 1);
if (!s)
return ENOMEM;
memcpy (s, arg, n);
s[n] = 0;
str->language = s;
str->flags |= MU_LC_LANG;
}
else
str->language = NULL;
arg += n;
if (arg[0] == '_')
{
arg++;
n = strcspn (arg, ".@");
if (flags & MU_LC_TERR)
{
s = malloc (n + 1);
if (!s)
return ENOMEM;
memcpy (s, arg, n);
s[n] = 0;
str->territory = s;
str->flags |= MU_LC_TERR;
}
else
str->territory = NULL;
arg += n;
}
if (arg[0] == '.')
{
arg++;
n = strcspn (arg, "@");
if (flags & MU_LC_CSET)
{
s = malloc (n + 1);
if (!s)
return ENOMEM;
memcpy (s, arg, n);
s[n] = 0;
str->charset = s;
str->flags |= MU_LC_CSET;
}
else
str->charset = NULL;
arg += n;
}
if (arg[0])
{
arg++;
if (flags & MU_LC_MOD)
{
str->modifier = strdup (arg);
if (!str->modifier)
return ENOMEM;
str->flags |= MU_LC_MOD;
}
}
return 0;
}
void
mu_lc_all_free (struct mu_lc_all *str)
{
if (str->flags & MU_LC_LANG)
free (str->language);
if (str->flags & MU_LC_TERR)
free (str->territory);
if (str->flags & MU_LC_CSET)
free (str->charset);
if (str->flags & MU_LC_MOD)
free (str->modifier);
str->flags = 0;
}
int
mu_parse_lc_all (const char *arg, struct mu_lc_all *str, int flags)
{
int rc;
memset (str, 0, sizeof (str[0]));
if (!arg)
{
if (flags & MU_LC_LANG)
{
str->language = strdup ("C");
if (!str->language)
return ENOMEM;
}
return 0;
}
/* If charset is requested (MU_LC_CSET), request also language and
territory. These will be used if ARG doesn't provide the charset
information. In any case, the surplus data will be discarded before
returning. */
rc = _parse_lc_all (arg, str,
(flags & MU_LC_CSET)
? (flags | MU_LC_LANG | MU_LC_TERR)
: flags);
if (rc == 0 && (flags & MU_LC_CSET))
{
if (!str->charset)
{
/* The caller requested charset, but we're unable to satisfy
the request based on the ARG only. Try the charset table
lookup. */
const char *charset =
mu_charset_lookup (str->language, str->territory);
if (charset)
{
/* Found it. Fill in the charset field. */
str->charset = strdup (charset);
if (!str->charset)
{
rc = ENOMEM;
goto err;
}
str->flags |= MU_LC_CSET;
}
}
/* The STR struct most probably contains data not requested by
the caller. First, see what these are. The following leaves
in FLAGS only those bits that weren't requested, but were filled
in just in case: */
flags = ~flags & str->flags;
/* Free the surplus data and clear the corresponding flag bits. */
if (flags & MU_LC_LANG)
{
free (str->language);
str->language = NULL;
str->flags &= ~MU_LC_LANG;
}
if (flags & MU_LC_TERR)
{
free (str->territory);
str->territory = NULL;
str->flags &= ~MU_LC_TERR;
}
}
err:
if (rc)
mu_lc_all_free (str);
return rc;
}