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