(hsearch_r): Add back ensurance that hval is not zero.
[glibc/history.git] / locale / programs / ld-measurement.c
blob8abe38e135ccff0b989274fa0d77ebdf2dc8092c
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 as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <error.h>
24 #include <langinfo.h>
25 #include <string.h>
26 #include <sys/uio.h>
28 #include <assert.h>
30 #include "localedef.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,
61 const struct charmap_t *charmap)
63 struct locale_measurement_t *measurement =
64 locale->categories[LC_MEASUREMENT].measurement;
65 int nothing = 0;
67 /* Now resolve copying and also handle completely missing definitions. */
68 if (measurement == NULL)
70 /* First see whether we were supposed to copy. If yes, find the
71 actual definition. */
72 if (locale->copy_name[LC_MEASUREMENT] != NULL)
74 /* Find the copying locale. This has to happen transitively since
75 the locale we are copying from might also copying another one. */
76 struct localedef_t *from = locale;
79 from = find_locale (LC_MEASUREMENT,
80 from->copy_name[LC_MEASUREMENT],
81 from->repertoire_name, charmap);
82 while (from->categories[LC_MEASUREMENT].measurement == NULL
83 && from->copy_name[LC_MEASUREMENT] != NULL);
85 measurement = locale->categories[LC_MEASUREMENT].measurement
86 = from->categories[LC_MEASUREMENT].measurement;
89 /* If there is still no definition issue an warning and create an
90 empty one. */
91 if (measurement == NULL)
93 if (! be_quiet)
94 WITH_CUR_LOCALE (error (0, 0, _("\
95 No definition for %s category found"), "LC_MEASUREMENT"));
96 measurement_startup (NULL, locale, 0);
97 measurement = locale->categories[LC_MEASUREMENT].measurement;
98 nothing = 1;
102 if (measurement->measurement == 0)
104 if (! nothing)
105 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
106 "LC_MEASUREMENT", "measurement"));
107 /* Use as the default value the value of the i18n locale. */
108 measurement->measurement = 1;
110 else
112 if (measurement->measurement > 3)
113 WITH_CUR_LOCALE (error (0, 0, _("%s: invalid value for field `%s'"),
114 "LC_MEASUREMENT", "measurement"));
119 void
120 measurement_output (struct localedef_t *locale,
121 const struct charmap_t *charmap, const char *output_path)
123 struct locale_measurement_t *measurement =
124 locale->categories[LC_MEASUREMENT].measurement;
125 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
126 struct locale_file data;
127 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
128 size_t cnt = 0;
130 data.magic = LIMAGIC (LC_MEASUREMENT);
131 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT);
132 iov[cnt].iov_base = (void *) &data;
133 iov[cnt].iov_len = sizeof (data);
134 ++cnt;
136 iov[cnt].iov_base = (void *) idx;
137 iov[cnt].iov_len = sizeof (idx);
138 ++cnt;
140 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
141 iov[cnt].iov_base = &measurement->measurement;
142 iov[cnt].iov_len = 1;
143 ++cnt;
145 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
146 iov[cnt].iov_base = (void *) charmap->code_set_name;
147 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
148 ++cnt;
150 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT));
152 write_locale_data (output_path, LC_MEASUREMENT, "LC_MEASUREMENT",
153 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT), iov);
157 /* The parser for the LC_MEASUREMENT section of the locale definition. */
158 void
159 measurement_read (struct linereader *ldfile, struct localedef_t *result,
160 const struct charmap_t *charmap, const char *repertoire_name,
161 int ignore_content)
163 struct locale_measurement_t *measurement;
164 struct token *now;
165 struct token *arg;
166 enum token_t nowtok;
168 /* The rest of the line containing `LC_MEASUREMENT' must be free. */
169 lr_ignore_rest (ldfile, 1);
173 now = lr_token (ldfile, charmap, result, NULL, verbose);
174 nowtok = now->tok;
176 while (nowtok == tok_eol);
178 /* If we see `copy' now we are almost done. */
179 if (nowtok == tok_copy)
181 handle_copy (ldfile, charmap, repertoire_name, result,
182 tok_lc_measurement, LC_MEASUREMENT, "LC_MEASUREMENT",
183 ignore_content);
184 return;
187 /* Prepare the data structures. */
188 measurement_startup (ldfile, result, ignore_content);
189 measurement = result->categories[LC_MEASUREMENT].measurement;
191 while (1)
193 /* Of course we don't proceed beyond the end of file. */
194 if (nowtok == tok_eof)
195 break;
197 /* Ingore empty lines. */
198 if (nowtok == tok_eol)
200 now = lr_token (ldfile, charmap, result, NULL, verbose);
201 nowtok = now->tok;
202 continue;
205 switch (nowtok)
207 #define INT_ELEM(cat) \
208 case tok_##cat: \
209 /* Ignore the rest of the line if we don't need the input of \
210 this line. */ \
211 if (ignore_content) \
213 lr_ignore_rest (ldfile, 0); \
214 break; \
217 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
218 if (arg->tok != tok_number) \
219 goto err_label; \
220 else if (measurement->cat != 0) \
221 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
222 "LC_MEASUREMENT", #cat); \
223 else if (!ignore_content) \
224 measurement->cat = arg->val.num; \
225 break
227 INT_ELEM (measurement);
229 case tok_end:
230 /* Next we assume `LC_MEASUREMENT'. */
231 arg = lr_token (ldfile, charmap, result, NULL, verbose);
232 if (arg->tok == tok_eof)
233 break;
234 if (arg->tok == tok_eol)
235 lr_error (ldfile, _("%s: incomplete `END' line"),
236 "LC_MEASUREMENT");
237 else if (arg->tok != tok_lc_measurement)
238 lr_error (ldfile, _("\
239 %1$s: definition does not end with `END %1$s'"), "LC_MEASUREMENT");
240 lr_ignore_rest (ldfile, arg->tok == tok_lc_measurement);
241 return;
243 default:
244 err_label:
245 SYNTAX_ERROR (_("%s: syntax error"), "LC_MEASUREMENT");
248 /* Prepare for the next round. */
249 now = lr_token (ldfile, charmap, result, NULL, verbose);
250 nowtok = now->tok;
253 /* When we come here we reached the end of the file. */
254 lr_error (ldfile, _("%s: premature end of file"),
255 "LC_MEASUREMENT");