.
[glibc/history.git] / locale / programs / ld-paper.c
blobeda94f63049e98486c688a48f80da321506922b4
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_PAPER locale. */
36 struct locale_paper_t
38 uint32_t height;
39 uint32_t width;
43 static void
44 paper_startup (struct linereader *lr, struct localedef_t *locale,
45 int ignore_content)
47 if (!ignore_content)
48 locale->categories[LC_PAPER].paper =
49 (struct locale_paper_t *) xcalloc (1, sizeof (struct locale_paper_t));
51 if (lr != NULL)
53 lr->translate_strings = 1;
54 lr->return_widestr = 0;
59 void
60 paper_finish (struct localedef_t *locale, const struct charmap_t *charmap)
62 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
63 int nothing = 0;
65 /* Now resolve copying and also handle completely missing definitions. */
66 if (paper == NULL)
68 /* First see whether we were supposed to copy. If yes, find the
69 actual definition. */
70 if (locale->copy_name[LC_PAPER] != NULL)
72 /* Find the copying locale. This has to happen transitively since
73 the locale we are copying from might also copying another one. */
74 struct localedef_t *from = locale;
77 from = find_locale (LC_PAPER, from->copy_name[LC_PAPER],
78 from->repertoire_name, charmap);
79 while (from->categories[LC_PAPER].paper == NULL
80 && from->copy_name[LC_PAPER] != NULL);
82 paper = locale->categories[LC_PAPER].paper
83 = from->categories[LC_PAPER].paper;
86 /* If there is still no definition issue an warning and create an
87 empty one. */
88 if (paper == NULL)
90 if (! be_quiet)
91 WITH_CUR_LOCALE (error (0, 0, _("\
92 No definition for %s category found"), "LC_PAPER"));
93 paper_startup (NULL, locale, 0);
94 paper = locale->categories[LC_PAPER].paper;
95 nothing = 1;
99 if (paper->height == 0)
101 if (! nothing)
102 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
103 "LC_PAPER", "height"));
104 /* Use as default values the values from the i18n locale. */
105 paper->height = 297;
108 if (paper->width == 0)
110 if (! nothing)
111 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
112 "LC_PAPER", "width"));
113 /* Use as default values the values from the i18n locale. */
114 paper->width = 210;
119 void
120 paper_output (struct localedef_t *locale, const struct charmap_t *charmap,
121 const char *output_path)
123 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
124 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER)];
125 struct locale_file data;
126 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_PAPER)];
127 size_t cnt = 0;
129 data.magic = LIMAGIC (LC_PAPER);
130 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_PAPER);
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[cnt - 2].iov_len + iov[cnt - 1].iov_len;
140 iov[cnt].iov_base = &paper->height;
141 iov[cnt].iov_len = 4;
142 ++cnt;
144 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
145 iov[cnt].iov_base = &paper->width;
146 iov[cnt].iov_len = 4;
147 ++cnt;
149 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
150 iov[cnt].iov_base = (void *) charmap->code_set_name;
151 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
152 ++cnt;
154 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
156 write_locale_data (output_path, LC_PAPER, "LC_PAPER",
157 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER), iov);
161 /* The parser for the LC_PAPER section of the locale definition. */
162 void
163 paper_read (struct linereader *ldfile, struct localedef_t *result,
164 const struct charmap_t *charmap, const char *repertoire_name,
165 int ignore_content)
167 struct locale_paper_t *paper;
168 struct token *now;
169 struct token *arg;
170 enum token_t nowtok;
172 /* The rest of the line containing `LC_PAPER' must be empty. */
173 lr_ignore_rest (ldfile, 1);
177 now = lr_token (ldfile, charmap, result, NULL, verbose);
178 nowtok = now->tok;
180 while (nowtok == tok_eol);
182 /* If we see `copy' now we are almost done. */
183 if (nowtok == tok_copy)
185 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_paper,
186 LC_PAPER, "LC_PAPER", ignore_content);
187 return;
190 /* Prepare the data structures. */
191 paper_startup (ldfile, result, ignore_content);
192 paper = result->categories[LC_PAPER].paper;
194 while (1)
196 /* Of course we don't proceed beyond the end of file. */
197 if (nowtok == tok_eof)
198 break;
200 /* Ingore empty lines. */
201 if (nowtok == tok_eol)
203 now = lr_token (ldfile, charmap, result, NULL, verbose);
204 nowtok = now->tok;
205 continue;
208 switch (nowtok)
210 #define INT_ELEM(cat) \
211 case tok_##cat: \
212 /* Ignore the rest of the line if we don't need the input of \
213 this line. */ \
214 if (ignore_content) \
216 lr_ignore_rest (ldfile, 0); \
217 break; \
220 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
221 if (arg->tok != tok_number) \
222 goto err_label; \
223 else if (paper->cat != 0) \
224 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
225 "LC_PAPER", #cat); \
226 else if (!ignore_content) \
227 paper->cat = arg->val.num; \
228 break
230 INT_ELEM (height);
231 INT_ELEM (width);
233 case tok_end:
234 /* Next we assume `LC_PAPER'. */
235 arg = lr_token (ldfile, charmap, result, NULL, verbose);
236 if (arg->tok == tok_eof)
237 break;
238 if (arg->tok == tok_eol)
239 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_PAPER");
240 else if (arg->tok != tok_lc_paper)
241 lr_error (ldfile, _("\
242 %1$s: definition does not end with `END %1$s'"), "LC_PAPER");
243 lr_ignore_rest (ldfile, arg->tok == tok_lc_paper);
244 return;
246 default:
247 err_label:
248 SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
251 /* Prepare for the next round. */
252 now = lr_token (ldfile, charmap, result, NULL, verbose);
253 nowtok = now->tok;
256 /* When we come here we reached the end of the file. */
257 lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");