OSDN Git Service

Mon May 31 02:22:55 1999 Philippe De Muyter <phdm@macqel.be>
[pf3gnuchains/gcc-fork.git] / texinfo / intl / explodename.c
1 /* Copyright (C) 1995, 1996, 1997 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 #include <string.h>
26 #include <sys/types.h>
27
28 #include "loadinfo.h"
29
30 /* On some strange systems still no definition of NULL is found.  Sigh!  */
31 #ifndef NULL
32 # if defined __STDC__ && __STDC__
33 #  define NULL ((void *) 0)
34 # else
35 #  define NULL 0
36 # endif
37 #endif
38
39 /* @@ end of prolog @@ */
40
41 int
42 _nl_explode_name (name, language, modifier, territory, codeset,
43                   normalized_codeset, special, sponsor, revision)
44      char *name;
45      const char **language;
46      const char **modifier;
47      const char **territory;
48      const char **codeset;
49      const char **normalized_codeset;
50      const char **special;
51      const char **sponsor;
52      const char **revision;
53 {
54   enum { undecided, xpg, cen } syntax;
55   char *cp;
56   int mask;
57
58   *modifier = NULL;
59   *territory = NULL;
60   *codeset = NULL;
61   *normalized_codeset = NULL;
62   *special = NULL;
63   *sponsor = NULL;
64   *revision = NULL;
65
66   /* Now we determine the single parts of the locale name.  First
67      look for the language.  Termination symbols are `_' and `@' if
68      we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
69   mask = 0;
70   syntax = undecided;
71   *language = cp = name;
72   while (cp[0] != '\0' && cp[0] != '_' && cp[0] != '@'
73          && cp[0] != '+' && cp[0] != ',')
74     ++cp;
75
76   if (*language == cp)
77     /* This does not make sense: language has to be specified.  Use
78        this entry as it is without exploding.  Perhaps it is an alias.  */
79     cp = strchr (*language, '\0');
80   else if (cp[0] == '_')
81     {
82       /* Next is the territory.  */
83       cp[0] = '\0';
84       *territory = ++cp;
85
86       while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@'
87              && cp[0] != '+' && cp[0] != ',' && cp[0] != '_')
88         ++cp;
89
90       mask |= TERRITORY;
91
92       if (cp[0] == '.')
93         {
94           /* Next is the codeset.  */
95           syntax = xpg;
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 (strcmp (*codeset, *normalized_codeset) == 0)
109                 free ((char *) *normalized_codeset);
110               else
111                 mask |= XPG_NORM_CODESET;
112             }
113         }
114     }
115
116   if (cp[0] == '@' || (syntax != xpg && cp[0] == '+'))
117     {
118       /* Next is the modifier.  */
119       syntax = cp[0] == '@' ? xpg : cen;
120       cp[0] = '\0';
121       *modifier = ++cp;
122
123       while (syntax == cen && cp[0] != '\0' && cp[0] != '+'
124              && cp[0] != ',' && cp[0] != '_')
125         ++cp;
126
127       mask |= XPG_MODIFIER | CEN_AUDIENCE;
128     }
129
130   if (syntax != xpg && (cp[0] == '+' || cp[0] == ',' || cp[0] == '_'))
131     {
132       syntax = cen;
133
134       if (cp[0] == '+')
135         {
136           /* Next is special application (CEN syntax).  */
137           cp[0] = '\0';
138           *special = ++cp;
139
140           while (cp[0] != '\0' && cp[0] != ',' && cp[0] != '_')
141             ++cp;
142
143           mask |= CEN_SPECIAL;
144         }
145
146       if (cp[0] == ',')
147         {
148           /* Next is sponsor (CEN syntax).  */
149           cp[0] = '\0';
150           *sponsor = ++cp;
151
152           while (cp[0] != '\0' && cp[0] != '_')
153             ++cp;
154
155           mask |= CEN_SPONSOR;
156         }
157
158       if (cp[0] == '_')
159         {
160           /* Next is revision (CEN syntax).  */
161           cp[0] = '\0';
162           *revision = ++cp;
163
164           mask |= CEN_REVISION;
165         }
166     }
167
168   /* For CEN syntax values it might be important to have the
169      separator character in the file name, not for XPG syntax.  */
170   if (syntax == xpg)
171     {
172       if (*territory != NULL && (*territory)[0] == '\0')
173         mask &= ~TERRITORY;
174
175       if (*codeset != NULL && (*codeset)[0] == '\0')
176         mask &= ~XPG_CODESET;
177
178       if (*modifier != NULL && (*modifier)[0] == '\0')
179         mask &= ~XPG_MODIFIER;
180     }
181
182   return mask;
183 }