1/* Copyright (C) 1998-2019 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 <langinfo.h>
23#include <string.h>
24#include <stdint.h>
25#include <sys/uio.h>
26
27#include <assert.h>
28
29#include "localedef.h"
30#include "localeinfo.h"
31#include "locfile.h"
32
33
34/* The real definition of the struct for the LC_PAPER locale. */
35struct locale_paper_t
36{
37 uint32_t height;
38 uint32_t width;
39};
40
41
42static void
43paper_startup (struct linereader *lr, struct localedef_t *locale,
44 int ignore_content)
45{
46 if (!ignore_content)
47 locale->categories[LC_PAPER].paper =
48 (struct locale_paper_t *) xcalloc (1, sizeof (struct locale_paper_t));
49
50 if (lr != NULL)
51 {
52 lr->translate_strings = 1;
53 lr->return_widestr = 0;
54 }
55}
56
57
58void
59paper_finish (struct localedef_t *locale, const struct charmap_t *charmap)
60{
61 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
62 int nothing = 0;
63
64 /* Now resolve copying and also handle completely missing definitions. */
65 if (paper == NULL)
66 {
67 /* First see whether we were supposed to copy. If yes, find the
68 actual definition. */
69 if (locale->copy_name[LC_PAPER] != NULL)
70 {
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;
74
75 do
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);
80
81 paper = locale->categories[LC_PAPER].paper
82 = from->categories[LC_PAPER].paper;
83 }
84
85 /* If there is still no definition issue an warning and create an
86 empty one. */
87 if (paper == NULL)
88 {
89 record_warning (_("\
90No definition for %s category found"), "LC_PAPER");
91 paper_startup (NULL, locale, 0);
92 paper = locale->categories[LC_PAPER].paper;
93 nothing = 1;
94 }
95 }
96
97 if (paper->height == 0)
98 {
99 if (! nothing)
100 record_error (0, 0, _("%s: field `%s' not defined"),
101 "LC_PAPER", "height");
102 /* Use as default values the values from the i18n locale. */
103 paper->height = 297;
104 }
105
106 if (paper->width == 0)
107 {
108 if (! nothing)
109 record_error (0, 0, _("%s: field `%s' not defined"),
110 "LC_PAPER", "width");
111 /* Use as default values the values from the i18n locale. */
112 paper->width = 210;
113 }
114}
115
116
117void
118paper_output (struct localedef_t *locale, const struct charmap_t *charmap,
119 const char *output_path)
120{
121 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
122 struct locale_file file;
123
124 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
125 add_locale_uint32 (&file, paper->height);
126 add_locale_uint32 (&file, paper->width);
127 add_locale_string (&file, charmap->code_set_name);
128 write_locale_data (output_path, LC_PAPER, "LC_PAPER", &file);
129}
130
131
132/* The parser for the LC_PAPER section of the locale definition. */
133void
134paper_read (struct linereader *ldfile, struct localedef_t *result,
135 const struct charmap_t *charmap, const char *repertoire_name,
136 int ignore_content)
137{
138 struct locale_paper_t *paper;
139 struct token *now;
140 struct token *arg;
141 enum token_t nowtok;
142
143 /* The rest of the line containing `LC_PAPER' must be empty. */
144 lr_ignore_rest (ldfile, 1);
145
146 do
147 {
148 now = lr_token (ldfile, charmap, result, NULL, verbose);
149 nowtok = now->tok;
150 }
151 while (nowtok == tok_eol);
152
153 /* If we see `copy' now we are almost done. */
154 if (nowtok == tok_copy)
155 {
156 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_paper,
157 LC_PAPER, "LC_PAPER", ignore_content);
158 return;
159 }
160
161 /* Prepare the data structures. */
162 paper_startup (ldfile, result, ignore_content);
163 paper = result->categories[LC_PAPER].paper;
164
165 while (1)
166 {
167 /* Of course we don't proceed beyond the end of file. */
168 if (nowtok == tok_eof)
169 break;
170
171 /* Ingore empty lines. */
172 if (nowtok == tok_eol)
173 {
174 now = lr_token (ldfile, charmap, result, NULL, verbose);
175 nowtok = now->tok;
176 continue;
177 }
178
179 switch (nowtok)
180 {
181#define INT_ELEM(cat) \
182 case tok_##cat: \
183 /* Ignore the rest of the line if we don't need the input of \
184 this line. */ \
185 if (ignore_content) \
186 { \
187 lr_ignore_rest (ldfile, 0); \
188 break; \
189 } \
190 \
191 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
192 if (arg->tok != tok_number) \
193 goto err_label; \
194 else if (paper->cat != 0) \
195 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
196 "LC_PAPER", #cat); \
197 else if (!ignore_content) \
198 paper->cat = arg->val.num; \
199 break
200
201 INT_ELEM (height);
202 INT_ELEM (width);
203
204 case tok_end:
205 /* Next we assume `LC_PAPER'. */
206 arg = lr_token (ldfile, charmap, result, NULL, verbose);
207 if (arg->tok == tok_eof)
208 break;
209 if (arg->tok == tok_eol)
210 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_PAPER");
211 else if (arg->tok != tok_lc_paper)
212 lr_error (ldfile, _("\
213%1$s: definition does not end with `END %1$s'"), "LC_PAPER");
214 lr_ignore_rest (ldfile, arg->tok == tok_lc_paper);
215 return;
216
217 default:
218 err_label:
219 SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
220 }
221
222 /* Prepare for the next round. */
223 now = lr_token (ldfile, charmap, result, NULL, verbose);
224 nowtok = now->tok;
225 }
226
227 /* When we come here we reached the end of the file. */
228 lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");
229}
230