2.5-18.1
[glibc.git] / locale / programs / ld-measurement.c
blob6a9b847a1a3cb08163629f009026e4f8af02d026
1 /* Copyright (C) 1998,1999,2000,2001,2002,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
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.
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. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <error.h>
23 #include <langinfo.h>
24 #include <string.h>
25 #include <sys/uio.h>
27 #include <assert.h>
29 #include "localedef.h"
30 #include "localeinfo.h"
31 #include "locfile.h"
34 /* The real definition of the struct for the LC_MEASUREMENT locale. */
35 struct locale_measurement_t
37 unsigned char measurement;
41 static void
42 measurement_startup (struct linereader *lr, struct localedef_t *locale,
43 int ignore_content)
45 if (!ignore_content)
46 locale->categories[LC_MEASUREMENT].measurement =
47 (struct locale_measurement_t *)
48 xcalloc (1, sizeof (struct locale_measurement_t));
50 if (lr != NULL)
52 lr->translate_strings = 1;
53 lr->return_widestr = 0;
58 void
59 measurement_finish (struct localedef_t *locale,
60 const struct charmap_t *charmap)
62 struct locale_measurement_t *measurement =
63 locale->categories[LC_MEASUREMENT].measurement;
64 int nothing = 0;
66 /* Now resolve copying and also handle completely missing definitions. */
67 if (measurement == NULL)
69 /* First see whether we were supposed to copy. If yes, find the
70 actual definition. */
71 if (locale->copy_name[LC_MEASUREMENT] != NULL)
73 /* Find the copying locale. This has to happen transitively since
74 the locale we are copying from might also copying another one. */
75 struct localedef_t *from = locale;
78 from = find_locale (LC_MEASUREMENT,
79 from->copy_name[LC_MEASUREMENT],
80 from->repertoire_name, charmap);
81 while (from->categories[LC_MEASUREMENT].measurement == NULL
82 && from->copy_name[LC_MEASUREMENT] != NULL);
84 measurement = locale->categories[LC_MEASUREMENT].measurement
85 = from->categories[LC_MEASUREMENT].measurement;
88 /* If there is still no definition issue an warning and create an
89 empty one. */
90 if (measurement == NULL)
92 if (! be_quiet)
93 WITH_CUR_LOCALE (error (0, 0, _("\
94 No definition for %s category found"), "LC_MEASUREMENT"));
95 measurement_startup (NULL, locale, 0);
96 measurement = locale->categories[LC_MEASUREMENT].measurement;
97 nothing = 1;
101 if (measurement->measurement == 0)
103 if (! nothing)
104 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
105 "LC_MEASUREMENT", "measurement"));
106 /* Use as the default value the value of the i18n locale. */
107 measurement->measurement = 1;
109 else
111 if (measurement->measurement > 3)
112 WITH_CUR_LOCALE (error (0, 0, _("%s: invalid value for field `%s'"),
113 "LC_MEASUREMENT", "measurement"));
118 void
119 measurement_output (struct localedef_t *locale,
120 const struct charmap_t *charmap, const char *output_path)
122 struct locale_measurement_t *measurement =
123 locale->categories[LC_MEASUREMENT].measurement;
124 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
125 struct locale_file data;
126 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
127 size_t cnt = 0;
129 data.magic = LIMAGIC (LC_MEASUREMENT);
130 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT);
131 iov[cnt].iov_base = (void *) &data;
132 iov[cnt].iov_len = sizeof (data);
133 ++cnt;
135 iov[cnt].iov_base = (void *) idx;
136 iov[cnt].iov_len = sizeof (idx);
137 ++cnt;
139 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
140 iov[cnt].iov_base = &measurement->measurement;
141 iov[cnt].iov_len = 1;
142 ++cnt;
144 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
145 iov[cnt].iov_base = (void *) charmap->code_set_name;
146 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
147 ++cnt;
149 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT));
151 write_locale_data (output_path, LC_MEASUREMENT, "LC_MEASUREMENT",
152 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT), iov);
156 /* The parser for the LC_MEASUREMENT section of the locale definition. */
157 void
158 measurement_read (struct linereader *ldfile, struct localedef_t *result,
159 const struct charmap_t *charmap, const char *repertoire_name,
160 int ignore_content)
162 struct locale_measurement_t *measurement;
163 struct token *now;
164 struct token *arg;
165 enum token_t nowtok;
167 /* The rest of the line containing `LC_MEASUREMENT' must be free. */
168 lr_ignore_rest (ldfile, 1);
172 now = lr_token (ldfile, charmap, result, NULL, verbose);
173 nowtok = now->tok;
175 while (nowtok == tok_eol);
177 /* If we see `copy' now we are almost done. */
178 if (nowtok == tok_copy)
180 handle_copy (ldfile, charmap, repertoire_name, result,
181 tok_lc_measurement, LC_MEASUREMENT, "LC_MEASUREMENT",
182 ignore_content);
183 return;
186 /* Prepare the data structures. */
187 measurement_startup (ldfile, result, ignore_content);
188 measurement = result->categories[LC_MEASUREMENT].measurement;
190 while (1)
192 /* Of course we don't proceed beyond the end of file. */
193 if (nowtok == tok_eof)
194 break;
196 /* Ingore empty lines. */
197 if (nowtok == tok_eol)
199 now = lr_token (ldfile, charmap, result, NULL, verbose);
200 nowtok = now->tok;
201 continue;
204 switch (nowtok)
206 #define INT_ELEM(cat) \
207 case tok_##cat: \
208 /* Ignore the rest of the line if we don't need the input of \
209 this line. */ \
210 if (ignore_content) \
212 lr_ignore_rest (ldfile, 0); \
213 break; \
216 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
217 if (arg->tok != tok_number) \
218 goto err_label; \
219 else if (measurement->cat != 0) \
220 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
221 "LC_MEASUREMENT", #cat); \
222 else if (!ignore_content) \
223 measurement->cat = arg->val.num; \
224 break
226 INT_ELEM (measurement);
228 case tok_end:
229 /* Next we assume `LC_MEASUREMENT'. */
230 arg = lr_token (ldfile, charmap, result, NULL, verbose);
231 if (arg->tok == tok_eof)
232 break;
233 if (arg->tok == tok_eol)
234 lr_error (ldfile, _("%s: incomplete `END' line"),
235 "LC_MEASUREMENT");
236 else if (arg->tok != tok_lc_measurement)
237 lr_error (ldfile, _("\
238 %1$s: definition does not end with `END %1$s'"), "LC_MEASUREMENT");
239 lr_ignore_rest (ldfile, arg->tok == tok_lc_measurement);
240 return;
242 default:
243 err_label:
244 SYNTAX_ERROR (_("%s: syntax error"), "LC_MEASUREMENT");
247 /* Prepare for the next round. */
248 now = lr_token (ldfile, charmap, result, NULL, verbose);
249 nowtok = now->tok;
252 /* When we come here we reached the end of the file. */
253 lr_error (ldfile, _("%s: premature end of file"),
254 "LC_MEASUREMENT");