Update copyright notices with scripts/update-copyrights.
[glibc.git] / locale / programs / ld-paper.c
blob32285ded83f83f6e13da8b38b5c3320a07eee31a
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 <sys/uio.h>
27 #include <assert.h>
29 #include "localedef.h"
30 #include "localeinfo.h"
31 #include "locfile.h"
34 /* The real definition of the struct for the LC_PAPER locale. */
35 struct locale_paper_t
37 uint32_t height;
38 uint32_t width;
42 static void
43 paper_startup (struct linereader *lr, struct localedef_t *locale,
44 int ignore_content)
46 if (!ignore_content)
47 locale->categories[LC_PAPER].paper =
48 (struct locale_paper_t *) xcalloc (1, sizeof (struct locale_paper_t));
50 if (lr != NULL)
52 lr->translate_strings = 1;
53 lr->return_widestr = 0;
58 void
59 paper_finish (struct localedef_t *locale, const struct charmap_t *charmap)
61 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
62 int nothing = 0;
64 /* Now resolve copying and also handle completely missing definitions. */
65 if (paper == NULL)
67 /* First see whether we were supposed to copy. If yes, find the
68 actual definition. */
69 if (locale->copy_name[LC_PAPER] != NULL)
71 /* Find the copying locale. This has to happen transitively since
72 the locale we are copying from might also copying another one. */
73 struct localedef_t *from = locale;
76 from = find_locale (LC_PAPER, from->copy_name[LC_PAPER],
77 from->repertoire_name, charmap);
78 while (from->categories[LC_PAPER].paper == NULL
79 && from->copy_name[LC_PAPER] != NULL);
81 paper = locale->categories[LC_PAPER].paper
82 = from->categories[LC_PAPER].paper;
85 /* If there is still no definition issue an warning and create an
86 empty one. */
87 if (paper == NULL)
89 if (! be_quiet)
90 WITH_CUR_LOCALE (error (0, 0, _("\
91 No definition for %s category found"), "LC_PAPER"));
92 paper_startup (NULL, locale, 0);
93 paper = locale->categories[LC_PAPER].paper;
94 nothing = 1;
98 if (paper->height == 0)
100 if (! nothing)
101 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
102 "LC_PAPER", "height"));
103 /* Use as default values the values from the i18n locale. */
104 paper->height = 297;
107 if (paper->width == 0)
109 if (! nothing)
110 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
111 "LC_PAPER", "width"));
112 /* Use as default values the values from the i18n locale. */
113 paper->width = 210;
118 void
119 paper_output (struct localedef_t *locale, const struct charmap_t *charmap,
120 const char *output_path)
122 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
123 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER)];
124 struct locale_file data;
125 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_PAPER)];
126 size_t cnt = 0;
128 data.magic = LIMAGIC (LC_PAPER);
129 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_PAPER);
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[cnt - 2].iov_len + iov[cnt - 1].iov_len;
139 iov[cnt].iov_base = &paper->height;
140 iov[cnt].iov_len = 4;
141 ++cnt;
143 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
144 iov[cnt].iov_base = &paper->width;
145 iov[cnt].iov_len = 4;
146 ++cnt;
148 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
149 iov[cnt].iov_base = (void *) charmap->code_set_name;
150 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
151 ++cnt;
153 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
155 write_locale_data (output_path, LC_PAPER, "LC_PAPER",
156 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER), iov);
160 /* The parser for the LC_PAPER section of the locale definition. */
161 void
162 paper_read (struct linereader *ldfile, struct localedef_t *result,
163 const struct charmap_t *charmap, const char *repertoire_name,
164 int ignore_content)
166 struct locale_paper_t *paper;
167 struct token *now;
168 struct token *arg;
169 enum token_t nowtok;
171 /* The rest of the line containing `LC_PAPER' must be empty. */
172 lr_ignore_rest (ldfile, 1);
176 now = lr_token (ldfile, charmap, result, NULL, verbose);
177 nowtok = now->tok;
179 while (nowtok == tok_eol);
181 /* If we see `copy' now we are almost done. */
182 if (nowtok == tok_copy)
184 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_paper,
185 LC_PAPER, "LC_PAPER", ignore_content);
186 return;
189 /* Prepare the data structures. */
190 paper_startup (ldfile, result, ignore_content);
191 paper = result->categories[LC_PAPER].paper;
193 while (1)
195 /* Of course we don't proceed beyond the end of file. */
196 if (nowtok == tok_eof)
197 break;
199 /* Ingore empty lines. */
200 if (nowtok == tok_eol)
202 now = lr_token (ldfile, charmap, result, NULL, verbose);
203 nowtok = now->tok;
204 continue;
207 switch (nowtok)
209 #define INT_ELEM(cat) \
210 case tok_##cat: \
211 /* Ignore the rest of the line if we don't need the input of \
212 this line. */ \
213 if (ignore_content) \
215 lr_ignore_rest (ldfile, 0); \
216 break; \
219 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
220 if (arg->tok != tok_number) \
221 goto err_label; \
222 else if (paper->cat != 0) \
223 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
224 "LC_PAPER", #cat); \
225 else if (!ignore_content) \
226 paper->cat = arg->val.num; \
227 break
229 INT_ELEM (height);
230 INT_ELEM (width);
232 case tok_end:
233 /* Next we assume `LC_PAPER'. */
234 arg = lr_token (ldfile, charmap, result, NULL, verbose);
235 if (arg->tok == tok_eof)
236 break;
237 if (arg->tok == tok_eol)
238 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_PAPER");
239 else if (arg->tok != tok_lc_paper)
240 lr_error (ldfile, _("\
241 %1$s: definition does not end with `END %1$s'"), "LC_PAPER");
242 lr_ignore_rest (ldfile, arg->tok == tok_lc_paper);
243 return;
245 default:
246 err_label:
247 SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
250 /* Prepare for the next round. */
251 now = lr_token (ldfile, charmap, result, NULL, verbose);
252 nowtok = now->tok;
255 /* When we come here we reached the end of the file. */
256 lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");