iconv: Bail out of the loop when an illegal sequence of bytes occurs.
[elinks/elinks-j605.git] / src / intl / gettext / explodename.c
blobba55fb2941ffbb93839aba4533078e8dc5313788
1 /* Copyright (C) 1995-1998, 2000, 2001 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
26 #include "elinks.h"
28 #include "intl/gettext/loadinfo.h"
30 /* On some strange systems still no definition of NULL is found. Sigh! */
31 #ifndef NULL
32 #if defined __STDC__ && __STDC__
33 #define NULL ((void *) 0)
34 #else
35 #define NULL 0
36 #endif
37 #endif
39 /* @@ end of prolog @@ */
41 unsigned char *
42 _nl_find_language(const unsigned char *name)
44 while (name[0] != '\0' && name[0] != '_' && name[0] != '@'
45 && name[0] != '+' && name[0] != ',')
46 ++name;
48 return (unsigned char *) name;
51 int
52 _nl_explode_name(unsigned char *name, const unsigned char **language, const unsigned char **modifier,
53 const unsigned char **territory, const unsigned char **codeset,
54 const unsigned char **normalized_codeset, const unsigned char **special,
55 const unsigned char **sponsor, const unsigned char **revision)
57 enum { undecided, xpg, cen } syntax;
58 unsigned char *cp;
59 int mask;
61 *modifier = NULL;
62 *territory = NULL;
63 *codeset = NULL;
64 *normalized_codeset = NULL;
65 *special = NULL;
66 *sponsor = NULL;
67 *revision = NULL;
69 /* Now we determine the single parts of the locale name. First
70 look for the language. Termination symbols are `_' and `@' if
71 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
72 mask = 0;
73 syntax = undecided;
74 *language = cp = name;
75 cp = _nl_find_language(*language);
77 if (*language == cp)
78 /* This does not make sense: language has to be specified. Use
79 this entry as it is without exploding. Perhaps it is an alias. */
80 cp = strchr(*language, '\0');
81 else if (cp[0] == '_') {
82 /* Next is the territory. */
83 cp[0] = '\0';
84 *territory = ++cp;
86 while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@'
87 && cp[0] != '+' && cp[0] != ',' && cp[0] != '_')
88 ++cp;
90 mask |= TERRITORY;
92 if (cp[0] == '.') {
93 /* Next is the codeset. */
94 syntax = xpg;
95 cp[0] = '\0';
96 *codeset = ++cp;
98 while (cp[0] != '\0' && cp[0] != '@')
99 ++cp;
101 mask |= XPG_CODESET;
103 if (*codeset != cp && (*codeset)[0] != '\0') {
104 *normalized_codeset =
105 _nl_normalize_codeset(*codeset,
106 cp - *codeset);
107 if (strcmp(*codeset, *normalized_codeset) == 0)
108 free((unsigned char *) *normalized_codeset);
109 else
110 mask |= XPG_NORM_CODESET;
115 if (cp[0] == '@' || (syntax != xpg && cp[0] == '+')) {
116 /* Next is the modifier. */
117 syntax = cp[0] == '@' ? xpg : cen;
118 cp[0] = '\0';
119 *modifier = ++cp;
121 while (syntax == cen && cp[0] != '\0' && cp[0] != '+'
122 && cp[0] != ',' && cp[0] != '_')
123 ++cp;
125 mask |= XPG_MODIFIER | CEN_AUDIENCE;
128 if (syntax != xpg && (cp[0] == '+' || cp[0] == ',' || cp[0] == '_')) {
129 syntax = cen;
131 if (cp[0] == '+') {
132 /* Next is special application (CEN syntax). */
133 cp[0] = '\0';
134 *special = ++cp;
136 while (cp[0] != '\0' && cp[0] != ',' && cp[0] != '_')
137 ++cp;
139 mask |= CEN_SPECIAL;
142 if (cp[0] == ',') {
143 /* Next is sponsor (CEN syntax). */
144 cp[0] = '\0';
145 *sponsor = ++cp;
147 while (cp[0] != '\0' && cp[0] != '_')
148 ++cp;
150 mask |= CEN_SPONSOR;
153 if (cp[0] == '_') {
154 /* Next is revision (CEN syntax). */
155 cp[0] = '\0';
156 *revision = ++cp;
158 mask |= CEN_REVISION;
162 /* For CEN syntax values it might be important to have the
163 separator character in the file name, not for XPG syntax. */
164 if (syntax == xpg) {
165 if (*territory != NULL && (*territory)[0] == '\0')
166 mask &= ~TERRITORY;
168 if (*codeset != NULL && (*codeset)[0] == '\0')
169 mask &= ~XPG_CODESET;
171 if (*modifier != NULL && (*modifier)[0] == '\0')
172 mask &= ~XPG_MODIFIER;
175 return mask;