1 /* Copyright (C) 1995-1998, 2000-2001, 2003, 2005, 2007 Free Software Foundation, Inc.
2    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
3 
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU Library General Public License as published
6    by the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8 
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 GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public
15    License along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17    USA.  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 
27 #include "loadinfo.h"
28 
29 /* On some strange systems still no definition of NULL is found.  Sigh!  */
30 #ifndef NULL
31 # if defined __STDC__ && __STDC__
32 #  define NULL ((void *) 0)
33 # else
34 #  define NULL 0
35 # endif
36 #endif
37 
38 /* @@ end of prolog @@ */
39 
40 /* Split a locale name NAME into a leading language part and all the
41    rest.  Return a pointer to the first character after the language,
42    i.e. to the first byte of the rest.  */
43 static char *_nl_find_language (const char *name);
44 
45 static char *
_nl_find_language(const char * name)46 _nl_find_language (const char *name)
47 {
48   while (name[0] != '\0' && name[0] != '_' && name[0] != '@' && name[0] != '.')
49     ++name;
50 
51   return (char *) name;
52 }
53 
54 
55 int
_nl_explode_name(char * name,const char ** language,const char ** modifier,const char ** territory,const char ** codeset,const char ** normalized_codeset)56 _nl_explode_name (char *name,
57 		  const char **language, const char **modifier,
58 		  const char **territory, const char **codeset,
59 		  const char **normalized_codeset)
60 {
61   char *cp;
62   int mask;
63 
64   *modifier = NULL;
65   *territory = NULL;
66   *codeset = NULL;
67   *normalized_codeset = NULL;
68 
69   /* Now we determine the single parts of the locale name.  First
70      look for the language.  Termination symbols are `_', '.', and `@'.  */
71   mask = 0;
72   *language = cp = name;
73   cp = _nl_find_language (*language);
74 
75   if (*language == cp)
76     /* This does not make sense: language has to be specified.  Use
77        this entry as it is without exploding.  Perhaps it is an alias.  */
78     cp = strchr (*language, '\0');
79   else
80     {
81       if (cp[0] == '_')
82 	{
83 	  /* Next is the territory.  */
84 	  cp[0] = '\0';
85 	  *territory = ++cp;
86 
87 	  while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@')
88 	    ++cp;
89 
90 	  mask |= XPG_TERRITORY;
91 	}
92 
93       if (cp[0] == '.')
94 	{
95 	  /* Next is the codeset.  */
96 	  cp[0] = '\0';
97 	  *codeset = ++cp;
98 
99 	  while (cp[0] != '\0' && cp[0] != '@')
100 	    ++cp;
101 
102 	  mask |= XPG_CODESET;
103 
104 	  if (*codeset != cp && (*codeset)[0] != '\0')
105 	    {
106 	      *normalized_codeset = _nl_normalize_codeset (*codeset,
107 							   cp - *codeset);
108 	      if (*normalized_codeset == NULL)
109 		return -1;
110 	      else if (strcmp (*codeset, *normalized_codeset) == 0)
111 		free ((char *) *normalized_codeset);
112 	      else
113 		mask |= XPG_NORM_CODESET;
114 	    }
115 	}
116     }
117 
118   if (cp[0] == '@')
119     {
120       /* Next is the modifier.  */
121       cp[0] = '\0';
122       *modifier = ++cp;
123 
124       if (cp[0] != '\0')
125 	mask |= XPG_MODIFIER;
126     }
127 
128   if (*territory != NULL && (*territory)[0] == '\0')
129     mask &= ~XPG_TERRITORY;
130 
131   if (*codeset != NULL && (*codeset)[0] == '\0')
132     mask &= ~XPG_CODESET;
133 
134   return mask;
135 }
136