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