1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003-2021 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <mailutils/nls.h>
26
27 static int
_parse_lc_all(const char * arg,struct mu_lc_all * str,int flags)28 _parse_lc_all (const char *arg, struct mu_lc_all *str, int flags)
29 {
30 char *s;
31 size_t n;
32
33 str->flags = 0;
34
35 n = strcspn (arg, "_.@");
36 if (flags & MU_LC_LANG)
37 {
38 s = malloc (n + 1);
39 if (!s)
40 return ENOMEM;
41 memcpy (s, arg, n);
42 s[n] = 0;
43 str->language = s;
44 str->flags |= MU_LC_LANG;
45 }
46 else
47 str->language = NULL;
48 arg += n;
49
50 if (arg[0] == '_')
51 {
52 arg++;
53
54 n = strcspn (arg, ".@");
55 if (flags & MU_LC_TERR)
56 {
57 s = malloc (n + 1);
58 if (!s)
59 return ENOMEM;
60 memcpy (s, arg, n);
61 s[n] = 0;
62 str->territory = s;
63 str->flags |= MU_LC_TERR;
64 }
65 else
66 str->territory = NULL;
67 arg += n;
68 }
69
70 if (arg[0] == '.')
71 {
72 arg++;
73
74 n = strcspn (arg, "@");
75 if (flags & MU_LC_CSET)
76 {
77 s = malloc (n + 1);
78 if (!s)
79 return ENOMEM;
80 memcpy (s, arg, n);
81 s[n] = 0;
82 str->charset = s;
83 str->flags |= MU_LC_CSET;
84 }
85 else
86 str->charset = NULL;
87 arg += n;
88 }
89
90 if (arg[0])
91 {
92 arg++;
93 if (flags & MU_LC_MOD)
94 {
95 str->modifier = strdup (arg);
96 if (!str->modifier)
97 return ENOMEM;
98 str->flags |= MU_LC_MOD;
99 }
100 }
101 return 0;
102 }
103
104 void
mu_lc_all_free(struct mu_lc_all * str)105 mu_lc_all_free (struct mu_lc_all *str)
106 {
107 if (str->flags & MU_LC_LANG)
108 free (str->language);
109 if (str->flags & MU_LC_TERR)
110 free (str->territory);
111 if (str->flags & MU_LC_CSET)
112 free (str->charset);
113 if (str->flags & MU_LC_MOD)
114 free (str->modifier);
115 str->flags = 0;
116 }
117
118 int
mu_parse_lc_all(const char * arg,struct mu_lc_all * str,int flags)119 mu_parse_lc_all (const char *arg, struct mu_lc_all *str, int flags)
120 {
121 int rc;
122
123 memset (str, 0, sizeof (str[0]));
124 if (!arg)
125 {
126 if (flags & MU_LC_LANG)
127 {
128 str->language = strdup ("C");
129 if (!str->language)
130 return ENOMEM;
131 }
132 return 0;
133 }
134
135 /* If charset is requested (MU_LC_CSET), request also language and
136 territory. These will be used if ARG doesn't provide the charset
137 information. In any case, the surplus data will be discarded before
138 returning. */
139 rc = _parse_lc_all (arg, str,
140 (flags & MU_LC_CSET)
141 ? (flags | MU_LC_LANG | MU_LC_TERR)
142 : flags);
143 if (rc == 0 && (flags & MU_LC_CSET))
144 {
145 if (!str->charset)
146 {
147 /* The caller requested charset, but we're unable to satisfy
148 the request based on the ARG only. Try the charset table
149 lookup. */
150 const char *charset =
151 mu_charset_lookup (str->language, str->territory);
152 if (charset)
153 {
154 /* Found it. Fill in the charset field. */
155 str->charset = strdup (charset);
156 if (!str->charset)
157 {
158 rc = ENOMEM;
159 goto err;
160 }
161 str->flags |= MU_LC_CSET;
162 }
163 }
164
165 /* The STR struct most probably contains data not requested by
166 the caller. First, see what these are. The following leaves
167 in FLAGS only those bits that weren't requested, but were filled
168 in just in case: */
169 flags = ~flags & str->flags;
170
171 /* Free the surplus data and clear the corresponding flag bits. */
172 if (flags & MU_LC_LANG)
173 {
174 free (str->language);
175 str->language = NULL;
176 str->flags &= ~MU_LC_LANG;
177 }
178
179 if (flags & MU_LC_TERR)
180 {
181 free (str->territory);
182 str->territory = NULL;
183 str->flags &= ~MU_LC_TERR;
184 }
185 }
186
187 err:
188 if (rc)
189 mu_lc_all_free (str);
190 return rc;
191 }
192