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