Update.
[glibc.git] / locale / programs / ld-paper.c
blob2910954f04d34dd5f94894b9ed92cc8f054f426a
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.
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.
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.
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. */
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 "localedef.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
36 /* The real definition of the struct for the LC_PAPER locale. */
37 struct locale_paper_t
39 uint32_t height;
40 uint32_t width;
44 static void
45 paper_startup (struct linereader *lr, struct localedef_t *locale,
46 int ignore_content)
48 if (!ignore_content)
49 locale->categories[LC_PAPER].paper =
50 (struct locale_paper_t *) xcalloc (1, sizeof (struct locale_paper_t));
52 if (lr != NULL)
54 lr->translate_strings = 1;
55 lr->return_widestr = 0;
60 void
61 paper_finish (struct localedef_t *locale, const struct charmap_t *charmap)
63 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
64 int nothing = 0;
66 /* Now resolve copying and also handle completely missing definitions. */
67 if (paper == NULL)
69 /* First see whether we were supposed to copy. If yes, find the
70 actual definition. */
71 if (locale->copy_name[LC_PAPER] != 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_PAPER, from->copy_name[LC_PAPER],
79 from->repertoire_name, charmap);
80 while (from->categories[LC_PAPER].paper == NULL
81 && from->copy_name[LC_PAPER] != NULL);
83 paper = locale->categories[LC_PAPER].paper
84 = from->categories[LC_PAPER].paper;
87 /* If there is still no definition issue an warning and create an
88 empty one. */
89 if (paper == NULL)
91 if (! be_quiet)
92 WITH_CUR_LOCALE (error (0, 0, _("\
93 No definition for %s category found"), "LC_PAPER"));
94 paper_startup (NULL, locale, 0);
95 paper = locale->categories[LC_PAPER].paper;
96 nothing = 1;
100 if (paper->height == 0)
102 if (! nothing)
103 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
104 "LC_PAPER", "height"));
105 /* Use as default values the values from the i18n locale. */
106 paper->height = 297;
109 if (paper->width == 0)
111 if (! nothing)
112 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
113 "LC_PAPER", "width"));
114 /* Use as default values the values from the i18n locale. */
115 paper->width = 210;
120 void
121 paper_output (struct localedef_t *locale, const struct charmap_t *charmap,
122 const char *output_path)
124 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
125 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER)];
126 struct locale_file data;
127 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_PAPER)];
128 size_t cnt = 0;
130 data.magic = LIMAGIC (LC_PAPER);
131 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_PAPER);
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[cnt - 2].iov_len + iov[cnt - 1].iov_len;
141 iov[cnt].iov_base = &paper->height;
142 iov[cnt].iov_len = 4;
143 ++cnt;
145 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
146 iov[cnt].iov_base = &paper->width;
147 iov[cnt].iov_len = 4;
148 ++cnt;
150 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
151 iov[cnt].iov_base = (void *) charmap->code_set_name;
152 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
153 ++cnt;
155 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
157 write_locale_data (output_path, LC_PAPER, "LC_PAPER",
158 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER), iov);
162 /* The parser for the LC_PAPER section of the locale definition. */
163 void
164 paper_read (struct linereader *ldfile, struct localedef_t *result,
165 const struct charmap_t *charmap, const char *repertoire_name,
166 int ignore_content)
168 struct locale_paper_t *paper;
169 struct token *now;
170 struct token *arg;
171 enum token_t nowtok;
173 /* The rest of the line containing `LC_PAPER' must be empty. */
174 lr_ignore_rest (ldfile, 1);
178 now = lr_token (ldfile, charmap, result, NULL, verbose);
179 nowtok = now->tok;
181 while (nowtok == tok_eol);
183 /* If we see `copy' now we are almost done. */
184 if (nowtok == tok_copy)
186 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_paper,
187 LC_PAPER, "LC_PAPER", ignore_content);
188 return;
191 /* Prepare the data structures. */
192 paper_startup (ldfile, result, ignore_content);
193 paper = result->categories[LC_PAPER].paper;
195 while (1)
197 /* Of course we don't proceed beyond the end of file. */
198 if (nowtok == tok_eof)
199 break;
201 /* Ingore empty lines. */
202 if (nowtok == tok_eol)
204 now = lr_token (ldfile, charmap, result, NULL, verbose);
205 nowtok = now->tok;
206 continue;
209 switch (nowtok)
211 #define INT_ELEM(cat) \
212 case tok_##cat: \
213 /* Ignore the rest of the line if we don't need the input of \
214 this line. */ \
215 if (ignore_content) \
217 lr_ignore_rest (ldfile, 0); \
218 break; \
221 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
222 if (arg->tok != tok_number) \
223 goto err_label; \
224 else if (paper->cat != 0) \
225 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
226 "LC_PAPER", #cat); \
227 else if (!ignore_content) \
228 paper->cat = arg->val.num; \
229 break
231 INT_ELEM (height);
232 INT_ELEM (width);
234 case tok_end:
235 /* Next we assume `LC_PAPER'. */
236 arg = lr_token (ldfile, charmap, result, NULL, verbose);
237 if (arg->tok == tok_eof)
238 break;
239 if (arg->tok == tok_eol)
240 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_PAPER");
241 else if (arg->tok != tok_lc_paper)
242 lr_error (ldfile, _("\
243 %1$s: definition does not end with `END %1$s'"), "LC_PAPER");
244 lr_ignore_rest (ldfile, arg->tok == tok_lc_paper);
245 return;
247 default:
248 err_label:
249 SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
252 /* Prepare for the next round. */
253 now = lr_token (ldfile, charmap, result, NULL, verbose);
254 nowtok = now->tok;
257 /* When we come here we reached the end of the file. */
258 lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");