Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / lib / intl / explodename.c
1 /* explodename.c */
2
3 /* Copyright (C) 1995-1998, 2000, 2001, 2005-2009 Free Software Foundation, Inc.
4    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5
6    This file is part of GNU Bash.
7
8    Bash is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12
13    Bash is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29
30 #include "loadinfo.h"
31
32 /* On some strange systems still no definition of NULL is found.  Sigh!  */
33 #ifndef NULL
34 # if defined __STDC__ && __STDC__
35 #  define NULL ((void *) 0)
36 # else
37 #  define NULL 0
38 # endif
39 #endif
40
41 /* @@ end of prolog @@ */
42
43 char *
44 _nl_find_language (name)
45      const char *name;
46 {
47   while (name[0] != '\0' && name[0] != '_' && name[0] != '@'
48          && name[0] != '+' && name[0] != ',')
49     ++name;
50
51   return (char *) name;
52 }
53
54
55 int
56 _nl_explode_name (name, language, modifier, territory, codeset,
57                   normalized_codeset, special, sponsor, revision)
58      char *name;
59      const char **language;
60      const char **modifier;
61      const char **territory;
62      const char **codeset;
63      const char **normalized_codeset;
64      const char **special;
65      const char **sponsor;
66      const char **revision;
67 {
68   enum { undecided, xpg, cen } syntax;
69   char *cp;
70   int mask;
71
72   *modifier = NULL;
73   *territory = NULL;
74   *codeset = NULL;
75   *normalized_codeset = NULL;
76   *special = NULL;
77   *sponsor = NULL;
78   *revision = NULL;
79
80   /* Now we determine the single parts of the locale name.  First
81      look for the language.  Termination symbols are `_' and `@' if
82      we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
83   mask = 0;
84   syntax = undecided;
85   *language = cp = name;
86   cp = _nl_find_language (*language);
87
88   if (*language == cp)
89     /* This does not make sense: language has to be specified.  Use
90        this entry as it is without exploding.  Perhaps it is an alias.  */
91     cp = strchr (*language, '\0');
92   else if (cp[0] == '_')
93     {
94       /* Next is the territory.  */
95       cp[0] = '\0';
96       *territory = ++cp;
97
98       while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@'
99              && cp[0] != '+' && cp[0] != ',' && cp[0] != '_')
100         ++cp;
101
102       mask |= TERRITORY;
103
104       if (cp[0] == '.')
105         {
106           /* Next is the codeset.  */
107           syntax = xpg;
108           cp[0] = '\0';
109           *codeset = ++cp;
110
111           while (cp[0] != '\0' && cp[0] != '@')
112             ++cp;
113
114           mask |= XPG_CODESET;
115
116           if (*codeset != cp && (*codeset)[0] != '\0')
117             {
118               *normalized_codeset = _nl_normalize_codeset (*codeset,
119                                                            cp - *codeset);
120               if (strcmp (*codeset, *normalized_codeset) == 0)
121                 free ((char *) *normalized_codeset);
122               else
123                 mask |= XPG_NORM_CODESET;
124             }
125         }
126     }
127
128   if (cp[0] == '@' || (syntax != xpg && cp[0] == '+'))
129     {
130       /* Next is the modifier.  */
131       syntax = cp[0] == '@' ? xpg : cen;
132       cp[0] = '\0';
133       *modifier = ++cp;
134
135       while (syntax == cen && cp[0] != '\0' && cp[0] != '+'
136              && cp[0] != ',' && cp[0] != '_')
137         ++cp;
138
139       mask |= XPG_MODIFIER | CEN_AUDIENCE;
140     }
141
142   if (syntax != xpg && (cp[0] == '+' || cp[0] == ',' || cp[0] == '_'))
143     {
144       syntax = cen;
145
146       if (cp[0] == '+')
147         {
148           /* Next is special application (CEN syntax).  */
149           cp[0] = '\0';
150           *special = ++cp;
151
152           while (cp[0] != '\0' && cp[0] != ',' && cp[0] != '_')
153             ++cp;
154
155           mask |= CEN_SPECIAL;
156         }
157
158       if (cp[0] == ',')
159         {
160           /* Next is sponsor (CEN syntax).  */
161           cp[0] = '\0';
162           *sponsor = ++cp;
163
164           while (cp[0] != '\0' && cp[0] != '_')
165             ++cp;
166
167           mask |= CEN_SPONSOR;
168         }
169
170       if (cp[0] == '_')
171         {
172           /* Next is revision (CEN syntax).  */
173           cp[0] = '\0';
174           *revision = ++cp;
175
176           mask |= CEN_REVISION;
177         }
178     }
179
180   /* For CEN syntax values it might be important to have the
181      separator character in the file name, not for XPG syntax.  */
182   if (syntax == xpg)
183     {
184       if (*territory != NULL && (*territory)[0] == '\0')
185         mask &= ~TERRITORY;
186
187       if (*codeset != NULL && (*codeset)[0] == '\0')
188         mask &= ~XPG_CODESET;
189
190       if (*modifier != NULL && (*modifier)[0] == '\0')
191         mask &= ~XPG_MODIFIER;
192     }
193
194   return mask;
195 }