1/* Copyright (C) 1998-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
16
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20
21#include <langinfo.h>
22#include <string.h>
23#include <stdint.h>
24#include <sys/uio.h>
25
26#include <assert.h>
27
28#include "localedef.h"
29#include "localeinfo.h"
30#include "locfile.h"
31
32
33/* The real definition of the struct for the LC_PAPER locale. */
34struct locale_paper_t
35{
36 uint32_t height;
37 uint32_t width;
38};
39
40
41static void
42paper_startup (struct linereader *lr, struct localedef_t *locale,
43 int ignore_content)
44{
45 if (!ignore_content)
46 locale->categories[LC_PAPER].paper =
47 (struct locale_paper_t *) xcalloc (n: 1, s: sizeof (struct locale_paper_t));
48
49 if (lr != NULL)
50 {
51 lr->translate_strings = 1;
52 lr->return_widestr = 0;
53 }
54}
55
56
57void
58paper_finish (struct localedef_t *locale, const struct charmap_t *charmap)
59{
60 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
61 int nothing = 0;
62
63 /* Now resolve copying and also handle completely missing definitions. */
64 if (paper == NULL)
65 {
66 /* First see whether we were supposed to copy. If yes, find the
67 actual definition. */
68 if (locale->copy_name[LC_PAPER] != NULL)
69 {
70 /* Find the copying locale. This has to happen transitively since
71 the locale we are copying from might also copying another one. */
72 struct localedef_t *from = locale;
73
74 do
75 from = find_locale (LC_PAPER, name: from->copy_name[LC_PAPER],
76 repertoire_name: from->repertoire_name, charmap);
77 while (from->categories[LC_PAPER].paper == NULL
78 && from->copy_name[LC_PAPER] != NULL);
79
80 paper = locale->categories[LC_PAPER].paper
81 = from->categories[LC_PAPER].paper;
82 }
83
84 /* If there is still no definition issue an warning and create an
85 empty one. */
86 if (paper == NULL)
87 {
88 record_warning (_("\
89No definition for %s category found"), "LC_PAPER");
90 paper_startup (NULL, locale, ignore_content: 0);
91 paper = locale->categories[LC_PAPER].paper;
92 nothing = 1;
93 }
94 }
95
96 if (paper->height == 0)
97 {
98 if (! nothing)
99 record_error (status: 0, errnum: 0, _("%s: field `%s' not defined"),
100 "LC_PAPER", "height");
101 /* Use as default values the values from the i18n locale. */
102 paper->height = 297;
103 }
104
105 if (paper->width == 0)
106 {
107 if (! nothing)
108 record_error (status: 0, errnum: 0, _("%s: field `%s' not defined"),
109 "LC_PAPER", "width");
110 /* Use as default values the values from the i18n locale. */
111 paper->width = 210;
112 }
113}
114
115
116void
117paper_output (struct localedef_t *locale, const struct charmap_t *charmap,
118 const char *output_path)
119{
120 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
121 struct locale_file file;
122
123 init_locale_data (file: &file, _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
124 add_locale_uint32 (file: &file, value: paper->height);
125 add_locale_uint32 (file: &file, value: paper->width);
126 add_locale_string (file: &file, string: charmap->code_set_name);
127 write_locale_data (output_path, LC_PAPER, category: "LC_PAPER", file: &file);
128}
129
130
131/* The parser for the LC_PAPER section of the locale definition. */
132void
133paper_read (struct linereader *ldfile, struct localedef_t *result,
134 const struct charmap_t *charmap, const char *repertoire_name,
135 int ignore_content)
136{
137 struct locale_paper_t *paper;
138 struct token *now;
139 struct token *arg;
140 enum token_t nowtok;
141
142 /* The rest of the line containing `LC_PAPER' must be empty. */
143 lr_ignore_rest (lr: ldfile, verbose: 1);
144
145 do
146 {
147 now = lr_token (lr: ldfile, charmap, locale: result, NULL, verbose);
148 nowtok = now->tok;
149 }
150 while (nowtok == tok_eol);
151
152 /* If we see `copy' now we are almost done. */
153 if (nowtok == tok_copy)
154 {
155 handle_copy (ldfile, charmap, repertoire_name, result, token: tok_lc_paper,
156 LC_PAPER, locale_name: "LC_PAPER", ignore_content);
157 return;
158 }
159
160 /* Prepare the data structures. */
161 paper_startup (lr: ldfile, locale: result, ignore_content);
162 paper = result->categories[LC_PAPER].paper;
163
164 while (1)
165 {
166 /* Of course we don't proceed beyond the end of file. */
167 if (nowtok == tok_eof)
168 break;
169
170 /* Ingore empty lines. */
171 if (nowtok == tok_eol)
172 {
173 now = lr_token (lr: ldfile, charmap, locale: result, NULL, verbose);
174 nowtok = now->tok;
175 continue;
176 }
177
178 switch (nowtok)
179 {
180#define INT_ELEM(cat) \
181 case tok_##cat: \
182 /* Ignore the rest of the line if we don't need the input of \
183 this line. */ \
184 if (ignore_content) \
185 { \
186 lr_ignore_rest (ldfile, 0); \
187 break; \
188 } \
189 \
190 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
191 if (arg->tok != tok_number) \
192 goto err_label; \
193 else if (paper->cat != 0) \
194 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
195 "LC_PAPER", #cat); \
196 else if (!ignore_content) \
197 paper->cat = arg->val.num; \
198 break
199
200 INT_ELEM (height);
201 INT_ELEM (width);
202
203 case tok_end:
204 /* Next we assume `LC_PAPER'. */
205 arg = lr_token (lr: ldfile, charmap, locale: result, NULL, verbose);
206 if (arg->tok == tok_eof)
207 break;
208 if (arg->tok == tok_eol)
209 lr_error (lr: ldfile, _("%s: incomplete `END' line"), "LC_PAPER");
210 else if (arg->tok != tok_lc_paper)
211 lr_error (lr: ldfile, _("\
212%1$s: definition does not end with `END %1$s'"), "LC_PAPER");
213 lr_ignore_rest (lr: ldfile, verbose: arg->tok == tok_lc_paper);
214 return;
215
216 default:
217 err_label:
218 SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
219 }
220
221 /* Prepare for the next round. */
222 now = lr_token (lr: ldfile, charmap, locale: result, NULL, verbose);
223 nowtok = now->tok;
224 }
225
226 /* When we come here we reached the end of the file. */
227 lr_error (lr: ldfile, _("%s: premature end of file"), "LC_PAPER");
228}
229

source code of glibc/locale/programs/ld-paper.c