Update.
[glibc.git] / locale / programs / ld-measurement.c
blob610eb5e09c32f84395335cb41af4cd6367675abc
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <error.h>
25 #include <langinfo.h>
26 #include <string.h>
27 #include <sys/uio.h>
29 #include <assert.h>
31 #include "localeinfo.h"
32 #include "locfile.h"
35 /* The real definition of the struct for the LC_MEASUREMENT locale. */
36 struct locale_measurement_t
38 unsigned char measurement;
42 static void
43 measurement_startup (struct linereader *lr, struct localedef_t *locale,
44 int ignore_content)
46 if (!ignore_content)
47 locale->categories[LC_MEASUREMENT].measurement =
48 (struct locale_measurement_t *)
49 xcalloc (1, sizeof (struct locale_measurement_t));
51 if (lr != NULL)
53 lr->translate_strings = 1;
54 lr->return_widestr = 0;
59 void
60 measurement_finish (struct localedef_t *locale, 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 error (0, 0, _("No definition for %s category found"),
93 "LC_MEASUREMENT");
94 measurement_startup (NULL, locale, 0);
95 measurement = locale->categories[LC_MEASUREMENT].measurement;
96 nothing = 1;
100 if (measurement->measurement == 0)
102 if (! nothing)
103 error (0, 0, _("%s: field `%s' not defined"),
104 "LC_MEASUREMENT", "measurement");
105 /* Use as the default value the value of the i18n locale. */
106 measurement->measurement = 1;
108 else
110 if (measurement->measurement > 3)
111 error (0, 0, _("%s: invalid value for field `%s'"),
112 "LC_MEASUREMENT", "meassurement");
117 void
118 measurement_output (struct localedef_t *locale, struct charmap_t *charmap,
119 const char *output_path)
121 struct locale_measurement_t *measurement =
122 locale->categories[LC_MEASUREMENT].measurement;
123 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
124 struct locale_file data;
125 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
126 size_t cnt = 0;
128 data.magic = LIMAGIC (LC_MEASUREMENT);
129 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT);
130 iov[cnt].iov_base = (void *) &data;
131 iov[cnt].iov_len = sizeof (data);
132 ++cnt;
134 iov[cnt].iov_base = (void *) idx;
135 iov[cnt].iov_len = sizeof (idx);
136 ++cnt;
138 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
139 iov[cnt].iov_base = &measurement->measurement;
140 iov[cnt].iov_len = 1;
141 ++cnt;
143 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT));
145 write_locale_data (output_path, "LC_MEASUREMENT",
146 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT), iov);
150 /* The parser for the LC_MEASUREMENT section of the locale definition. */
151 void
152 measurement_read (struct linereader *ldfile, struct localedef_t *result,
153 struct charmap_t *charmap, const char *repertoire_name,
154 int ignore_content)
156 struct repertoire_t *repertoire = NULL;
157 struct locale_measurement_t *measurement;
158 struct token *now;
159 struct token *arg;
160 enum token_t nowtok;
162 /* Get the repertoire we have to use. */
163 if (repertoire_name != NULL)
164 repertoire = repertoire_read (repertoire_name);
166 /* The rest of the line containing `LC_MEASUREMENT' must be free. */
167 lr_ignore_rest (ldfile, 1);
171 now = lr_token (ldfile, charmap, NULL);
172 nowtok = now->tok;
174 while (nowtok == tok_eol);
176 /* If we see `copy' now we are almost done. */
177 if (nowtok == tok_copy)
179 handle_copy (ldfile, charmap, repertoire, result, tok_lc_measurement,
180 LC_MEASUREMENT, "LC_MEASUREMENT", ignore_content);
181 return;
184 /* Prepare the data structures. */
185 measurement_startup (ldfile, result, ignore_content);
186 measurement = result->categories[LC_MEASUREMENT].measurement;
188 while (1)
190 /* Of course we don't proceed beyond the end of file. */
191 if (nowtok == tok_eof)
192 break;
194 /* Ingore empty lines. */
195 if (nowtok == tok_eol)
197 now = lr_token (ldfile, charmap, NULL);
198 nowtok = now->tok;
199 continue;
202 switch (nowtok)
204 #define INT_ELEM(cat) \
205 case tok_##cat: \
206 /* Ignore the rest of the line if we don't need the input of \
207 this line. */ \
208 if (ignore_content) \
210 lr_ignore_rest (ldfile, 0); \
211 break; \
214 arg = lr_token (ldfile, charmap, NULL); \
215 if (arg->tok != tok_number) \
216 goto err_label; \
217 else if (measurement->cat != 0) \
218 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
219 "LC_MEASUREMENT", #cat); \
220 else if (!ignore_content) \
221 measurement->cat = arg->val.num; \
222 break
224 INT_ELEM (measurement);
226 case tok_end:
227 /* Next we assume `LC_MEASUREMENT'. */
228 arg = lr_token (ldfile, charmap, NULL);
229 if (arg->tok == tok_eof)
230 break;
231 if (arg->tok == tok_eol)
232 lr_error (ldfile, _("%s: incomplete `END' line"),
233 "LC_MEASUREMENT");
234 else if (arg->tok != tok_lc_measurement)
235 lr_error (ldfile, _("\
236 %1$s: definition does not end with `END %1$s'"), "LC_MEASUREMENT");
237 lr_ignore_rest (ldfile, arg->tok == tok_lc_measurement);
238 return;
240 default:
241 err_label:
242 SYNTAX_ERROR (_("%s: syntax error"), "LC_MEASUREMENT");
245 /* Prepare for the next round. */
246 now = lr_token (ldfile, charmap, NULL);
247 nowtok = now->tok;
250 /* When we come here we reached the end of the file. */
251 lr_error (ldfile, _("%s: premature end of file"),
252 "LC_MEASUREMENT");