e3ec90199e9e32c9cace57cf0e0351f40950994b
[platform/upstream/glibc.git] / locale / programs / ld-measurement.c
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <error.h>
25 #include <langinfo.h>
26 #include <string.h>
27 #include <sys/uio.h>
28
29 #include <assert.h>
30
31 #include "localedef.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
34
35
36 /* The real definition of the struct for the LC_MEASUREMENT locale.  */
37 struct locale_measurement_t
38 {
39   unsigned char measurement;
40 };
41
42
43 static void
44 measurement_startup (struct linereader *lr, struct localedef_t *locale,
45                      int ignore_content)
46 {
47   if (!ignore_content)
48     locale->categories[LC_MEASUREMENT].measurement =
49       (struct locale_measurement_t *)
50       xcalloc (1, sizeof (struct locale_measurement_t));
51
52   if (lr != NULL)
53     {
54       lr->translate_strings = 1;
55       lr->return_widestr = 0;
56     }
57 }
58
59
60 void
61 measurement_finish (struct localedef_t *locale,
62                     const struct charmap_t *charmap)
63 {
64   struct locale_measurement_t *measurement =
65     locale->categories[LC_MEASUREMENT].measurement;
66   int nothing = 0;
67
68   /* Now resolve copying and also handle completely missing definitions.  */
69   if (measurement == NULL)
70     {
71       /* First see whether we were supposed to copy.  If yes, find the
72          actual definition.  */
73       if (locale->copy_name[LC_MEASUREMENT] != NULL)
74         {
75           /* Find the copying locale.  This has to happen transitively since
76              the locale we are copying from might also copying another one.  */
77           struct localedef_t *from = locale;
78
79           do
80             from = find_locale (LC_MEASUREMENT,
81                                 from->copy_name[LC_MEASUREMENT],
82                                 from->repertoire_name, charmap);
83           while (from->categories[LC_MEASUREMENT].measurement == NULL
84                  && from->copy_name[LC_MEASUREMENT] != NULL);
85
86           measurement = locale->categories[LC_MEASUREMENT].measurement
87             = from->categories[LC_MEASUREMENT].measurement;
88         }
89
90       /* If there is still no definition issue an warning and create an
91          empty one.  */
92       if (measurement == NULL)
93         {
94           if (! be_quiet)
95             WITH_CUR_LOCALE (error (0, 0, _("\
96 No definition for %s category found"), "LC_MEASUREMENT"));
97           measurement_startup (NULL, locale, 0);
98           measurement = locale->categories[LC_MEASUREMENT].measurement;
99           nothing = 1;
100         }
101     }
102
103   if (measurement->measurement == 0)
104     {
105       if (! nothing)
106         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
107                                 "LC_MEASUREMENT", "measurement"));
108       /* Use as the default value the value of the i18n locale.  */
109       measurement->measurement = 1;
110     }
111   else
112     {
113       if (measurement->measurement > 3)
114         WITH_CUR_LOCALE (error (0, 0, _("%s: invalid value for field `%s'"),
115                                 "LC_MEASUREMENT", "measurement"));
116     }
117 }
118
119
120 void
121 measurement_output (struct localedef_t *locale,
122                     const struct charmap_t *charmap, const char *output_path)
123 {
124   struct locale_measurement_t *measurement =
125     locale->categories[LC_MEASUREMENT].measurement;
126   struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
127   struct locale_file data;
128   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
129   size_t cnt = 0;
130
131   data.magic = LIMAGIC (LC_MEASUREMENT);
132   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT);
133   iov[cnt].iov_base = (void *) &data;
134   iov[cnt].iov_len = sizeof (data);
135   ++cnt;
136
137   iov[cnt].iov_base = (void *) idx;
138   iov[cnt].iov_len = sizeof (idx);
139   ++cnt;
140
141   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
142   iov[cnt].iov_base = &measurement->measurement;
143   iov[cnt].iov_len = 1;
144   ++cnt;
145
146   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
147   iov[cnt].iov_base = (void *) charmap->code_set_name;
148   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
149   ++cnt;
150
151   assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT));
152
153   write_locale_data (output_path, LC_MEASUREMENT, "LC_MEASUREMENT",
154                      2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT), iov);
155 }
156
157
158 /* The parser for the LC_MEASUREMENT section of the locale definition.  */
159 void
160 measurement_read (struct linereader *ldfile, struct localedef_t *result,
161                   const struct charmap_t *charmap, const char *repertoire_name,
162                   int ignore_content)
163 {
164   struct locale_measurement_t *measurement;
165   struct token *now;
166   struct token *arg;
167   enum token_t nowtok;
168
169   /* The rest of the line containing `LC_MEASUREMENT' must be free.  */
170   lr_ignore_rest (ldfile, 1);
171
172   do
173     {
174       now = lr_token (ldfile, charmap, result, NULL, verbose);
175       nowtok = now->tok;
176     }
177   while (nowtok == tok_eol);
178
179   /* If we see `copy' now we are almost done.  */
180   if (nowtok == tok_copy)
181     {
182       handle_copy (ldfile, charmap, repertoire_name, result,
183                    tok_lc_measurement, LC_MEASUREMENT, "LC_MEASUREMENT",
184                    ignore_content);
185       return;
186     }
187
188   /* Prepare the data structures.  */
189   measurement_startup (ldfile, result, ignore_content);
190   measurement = result->categories[LC_MEASUREMENT].measurement;
191
192   while (1)
193     {
194       /* Of course we don't proceed beyond the end of file.  */
195       if (nowtok == tok_eof)
196         break;
197
198       /* Ingore empty lines.  */
199       if (nowtok == tok_eol)
200         {
201           now = lr_token (ldfile, charmap, result, NULL, verbose);
202           nowtok = now->tok;
203           continue;
204         }
205
206       switch (nowtok)
207         {
208 #define INT_ELEM(cat) \
209         case tok_##cat:                                                       \
210           /* Ignore the rest of the line if we don't need the input of        \
211              this line.  */                                                   \
212           if (ignore_content)                                                 \
213             {                                                                 \
214               lr_ignore_rest (ldfile, 0);                                     \
215               break;                                                          \
216             }                                                                 \
217                                                                               \
218           arg = lr_token (ldfile, charmap, result, NULL, verbose);            \
219           if (arg->tok != tok_number)                                         \
220             goto err_label;                                                   \
221           else if (measurement->cat != 0)                                     \
222             lr_error (ldfile, _("%s: field `%s' declared more than once"),    \
223                       "LC_MEASUREMENT", #cat);                                \
224           else if (!ignore_content)                                           \
225             measurement->cat = arg->val.num;                                  \
226           break
227
228           INT_ELEM (measurement);
229
230         case tok_end:
231           /* Next we assume `LC_MEASUREMENT'.  */
232           arg = lr_token (ldfile, charmap, result, NULL, verbose);
233           if (arg->tok == tok_eof)
234             break;
235           if (arg->tok == tok_eol)
236             lr_error (ldfile, _("%s: incomplete `END' line"),
237                       "LC_MEASUREMENT");
238           else if (arg->tok != tok_lc_measurement)
239             lr_error (ldfile, _("\
240 %1$s: definition does not end with `END %1$s'"), "LC_MEASUREMENT");
241           lr_ignore_rest (ldfile, arg->tok == tok_lc_measurement);
242           return;
243
244         default:
245         err_label:
246           SYNTAX_ERROR (_("%s: syntax error"), "LC_MEASUREMENT");
247         }
248
249       /* Prepare for the next round.  */
250       now = lr_token (ldfile, charmap, result, NULL, verbose);
251       nowtok = now->tok;
252     }
253
254   /* When we come here we reached the end of the file.  */
255   lr_error (ldfile, _("%s: premature end of file"),
256             "LC_MEASUREMENT");
257 }