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