1/* Copyright (C) 1998-2016 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, see <http://www.gnu.org/licenses/>. */
17
18#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
22#include <error.h>
23#include <langinfo.h>
24#include <string.h>
25#include <stdint.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. */
36struct locale_paper_t
37{
38 uint32_t height;
39 uint32_t width;
40};
41
42
43static void
44paper_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
59void
60paper_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, _("\
92No 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
119void
120paper_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 locale_file file;
125
126 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
127 add_locale_uint32 (&file, paper->height);
128 add_locale_uint32 (&file, paper->width);
129 add_locale_string (&file, charmap->code_set_name);
130 write_locale_data (output_path, LC_PAPER, "LC_PAPER", &file);
131}
132
133
134/* The parser for the LC_PAPER section of the locale definition. */
135void
136paper_read (struct linereader *ldfile, struct localedef_t *result,
137 const struct charmap_t *charmap, const char *repertoire_name,
138 int ignore_content)
139{
140 struct locale_paper_t *paper;
141 struct token *now;
142 struct token *arg;
143 enum token_t nowtok;
144
145 /* The rest of the line containing `LC_PAPER' must be empty. */
146 lr_ignore_rest (ldfile, 1);
147
148 do
149 {
150 now = lr_token (ldfile, charmap, result, NULL, verbose);
151 nowtok = now->tok;
152 }
153 while (nowtok == tok_eol);
154
155 /* If we see `copy' now we are almost done. */
156 if (nowtok == tok_copy)
157 {
158 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_paper,
159 LC_PAPER, "LC_PAPER", ignore_content);
160 return;
161 }
162
163 /* Prepare the data structures. */
164 paper_startup (ldfile, result, ignore_content);
165 paper = result->categories[LC_PAPER].paper;
166
167 while (1)
168 {
169 /* Of course we don't proceed beyond the end of file. */
170 if (nowtok == tok_eof)
171 break;
172
173 /* Ingore empty lines. */
174 if (nowtok == tok_eol)
175 {
176 now = lr_token (ldfile, charmap, result, NULL, verbose);
177 nowtok = now->tok;
178 continue;
179 }
180
181 switch (nowtok)
182 {
183#define INT_ELEM(cat) \
184 case tok_##cat: \
185 /* Ignore the rest of the line if we don't need the input of \
186 this line. */ \
187 if (ignore_content) \
188 { \
189 lr_ignore_rest (ldfile, 0); \
190 break; \
191 } \
192 \
193 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
194 if (arg->tok != tok_number) \
195 goto err_label; \
196 else if (paper->cat != 0) \
197 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
198 "LC_PAPER", #cat); \
199 else if (!ignore_content) \
200 paper->cat = arg->val.num; \
201 break
202
203 INT_ELEM (height);
204 INT_ELEM (width);
205
206 case tok_end:
207 /* Next we assume `LC_PAPER'. */
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"), "LC_PAPER");
213 else if (arg->tok != tok_lc_paper)
214 lr_error (ldfile, _("\
215%1$s: definition does not end with `END %1$s'"), "LC_PAPER");
216 lr_ignore_rest (ldfile, arg->tok == tok_lc_paper);
217 return;
218
219 default:
220 err_label:
221 SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
222 }
223
224 /* Prepare for the next round. */
225 now = lr_token (ldfile, charmap, result, NULL, verbose);
226 nowtok = now->tok;
227 }
228
229 /* When we come here we reached the end of the file. */
230 lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");
231}
232