]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-paper.c
* elf/ldconfig.c: Allow GPLv2 or any later version.
[thirdparty/glibc.git] / locale / programs / ld-paper.c
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.
4
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.
9
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.
14
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. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <error.h>
24 #include <langinfo.h>
25 #include <string.h>
26 #include <sys/uio.h>
27
28 #include <assert.h>
29
30 #include "localedef.h"
31 #include "localeinfo.h"
32 #include "locfile.h"
33
34
35 /* The real definition of the struct for the LC_PAPER locale. */
36 struct locale_paper_t
37 {
38 uint32_t height;
39 uint32_t width;
40 };
41
42
43 static void
44 paper_startup (struct linereader *lr, struct localedef_t *locale,
45 int ignore_content)
46 {
47 if (!ignore_content)
48 locale->categories[LC_PAPER].paper =
49 (struct locale_paper_t *) xcalloc (1, sizeof (struct locale_paper_t));
50
51 if (lr != NULL)
52 {
53 lr->translate_strings = 1;
54 lr->return_widestr = 0;
55 }
56 }
57
58
59 void
60 paper_finish (struct localedef_t *locale, const struct charmap_t *charmap)
61 {
62 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
63 int nothing = 0;
64
65 /* Now resolve copying and also handle completely missing definitions. */
66 if (paper == NULL)
67 {
68 /* First see whether we were supposed to copy. If yes, find the
69 actual definition. */
70 if (locale->copy_name[LC_PAPER] != NULL)
71 {
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;
75
76 do
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);
81
82 paper = locale->categories[LC_PAPER].paper
83 = from->categories[LC_PAPER].paper;
84 }
85
86 /* If there is still no definition issue an warning and create an
87 empty one. */
88 if (paper == NULL)
89 {
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;
96 }
97 }
98
99 if (paper->height == 0)
100 {
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;
106 }
107
108 if (paper->width == 0)
109 {
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;
115 }
116 }
117
118
119 void
120 paper_output (struct localedef_t *locale, const struct charmap_t *charmap,
121 const char *output_path)
122 {
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;
128
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;
134
135 iov[cnt].iov_base = (void *) idx;
136 iov[cnt].iov_len = sizeof (idx);
137 ++cnt;
138
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;
143
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;
148
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;
153
154 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
155
156 write_locale_data (output_path, LC_PAPER, "LC_PAPER",
157 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER), iov);
158 }
159
160
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)
166 {
167 struct locale_paper_t *paper;
168 struct token *now;
169 struct token *arg;
170 enum token_t nowtok;
171
172 /* The rest of the line containing `LC_PAPER' must be empty. */
173 lr_ignore_rest (ldfile, 1);
174
175 do
176 {
177 now = lr_token (ldfile, charmap, result, NULL, verbose);
178 nowtok = now->tok;
179 }
180 while (nowtok == tok_eol);
181
182 /* If we see `copy' now we are almost done. */
183 if (nowtok == tok_copy)
184 {
185 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_paper,
186 LC_PAPER, "LC_PAPER", ignore_content);
187 return;
188 }
189
190 /* Prepare the data structures. */
191 paper_startup (ldfile, result, ignore_content);
192 paper = result->categories[LC_PAPER].paper;
193
194 while (1)
195 {
196 /* Of course we don't proceed beyond the end of file. */
197 if (nowtok == tok_eof)
198 break;
199
200 /* Ingore empty lines. */
201 if (nowtok == tok_eol)
202 {
203 now = lr_token (ldfile, charmap, result, NULL, verbose);
204 nowtok = now->tok;
205 continue;
206 }
207
208 switch (nowtok)
209 {
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) \
215 { \
216 lr_ignore_rest (ldfile, 0); \
217 break; \
218 } \
219 \
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
229
230 INT_ELEM (height);
231 INT_ELEM (width);
232
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;
245
246 default:
247 err_label:
248 SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
249 }
250
251 /* Prepare for the next round. */
252 now = lr_token (ldfile, charmap, result, NULL, verbose);
253 nowtok = now->tok;
254 }
255
256 /* When we come here we reached the end of the file. */
257 lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");
258 }