Add systemd unit file for nscd
[glibc.git] / locale / programs / ld-measurement.c
blob5be54e739d901083128ef0e737891bb9c90b1b04
1 /* Copyright (C) 1998-2013 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, see <http://www.gnu.org/licenses/>. */
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 <stdint.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 locale_file file;
127 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT));
128 add_locale_char (&file, measurement->measurement);
129 add_locale_string (&file, charmap->code_set_name);
130 write_locale_data (output_path, LC_MEASUREMENT, "LC_MEASUREMENT", &file);
134 /* The parser for the LC_MEASUREMENT section of the locale definition. */
135 void
136 measurement_read (struct linereader *ldfile, struct localedef_t *result,
137 const struct charmap_t *charmap, const char *repertoire_name,
138 int ignore_content)
140 struct locale_measurement_t *measurement;
141 struct token *now;
142 struct token *arg;
143 enum token_t nowtok;
145 /* The rest of the line containing `LC_MEASUREMENT' must be free. */
146 lr_ignore_rest (ldfile, 1);
150 now = lr_token (ldfile, charmap, result, NULL, verbose);
151 nowtok = now->tok;
153 while (nowtok == tok_eol);
155 /* If we see `copy' now we are almost done. */
156 if (nowtok == tok_copy)
158 handle_copy (ldfile, charmap, repertoire_name, result,
159 tok_lc_measurement, LC_MEASUREMENT, "LC_MEASUREMENT",
160 ignore_content);
161 return;
164 /* Prepare the data structures. */
165 measurement_startup (ldfile, result, ignore_content);
166 measurement = result->categories[LC_MEASUREMENT].measurement;
168 while (1)
170 /* Of course we don't proceed beyond the end of file. */
171 if (nowtok == tok_eof)
172 break;
174 /* Ingore empty lines. */
175 if (nowtok == tok_eol)
177 now = lr_token (ldfile, charmap, result, NULL, verbose);
178 nowtok = now->tok;
179 continue;
182 switch (nowtok)
184 #define INT_ELEM(cat) \
185 case tok_##cat: \
186 /* Ignore the rest of the line if we don't need the input of \
187 this line. */ \
188 if (ignore_content) \
190 lr_ignore_rest (ldfile, 0); \
191 break; \
194 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
195 if (arg->tok != tok_number) \
196 goto err_label; \
197 else if (measurement->cat != 0) \
198 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
199 "LC_MEASUREMENT", #cat); \
200 else if (!ignore_content) \
201 measurement->cat = arg->val.num; \
202 break
204 INT_ELEM (measurement);
206 case tok_end:
207 /* Next we assume `LC_MEASUREMENT'. */
208 arg = lr_token (ldfile, charmap, result, NULL, verbose);
209 if (arg->tok == tok_eof)
210 break;
211 if (arg->tok == tok_eol)
212 lr_error (ldfile, _("%s: incomplete `END' line"),
213 "LC_MEASUREMENT");
214 else if (arg->tok != tok_lc_measurement)
215 lr_error (ldfile, _("\
216 %1$s: definition does not end with `END %1$s'"), "LC_MEASUREMENT");
217 lr_ignore_rest (ldfile, arg->tok == tok_lc_measurement);
218 return;
220 default:
221 err_label:
222 SYNTAX_ERROR (_("%s: syntax error"), "LC_MEASUREMENT");
225 /* Prepare for the next round. */
226 now = lr_token (ldfile, charmap, result, NULL, verbose);
227 nowtok = now->tok;
230 /* When we come here we reached the end of the file. */
231 lr_error (ldfile, _("%s: premature end of file"),
232 "LC_MEASUREMENT");