]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/programs/ld-paper.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / locale / programs / ld-paper.c
CommitLineData
6d7e8eda 1/* Copyright (C) 1998-2023 Free Software Foundation, Inc.
4b10dd6c 2 This file is part of the GNU C Library.
4b10dd6c 3
43bc8ac6 4 This program is free software; you can redistribute it and/or modify
2e2efe65
RM
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.
4b10dd6c 8
43bc8ac6 9 This program is distributed in the hope that it will be useful,
4b10dd6c 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
43bc8ac6
UD
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
4b10dd6c 13
43bc8ac6 14 You should have received a copy of the GNU General Public License
5a82c748 15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
4b10dd6c
UD
16
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20
4b10dd6c
UD
21#include <langinfo.h>
22#include <string.h>
e054f494 23#include <stdint.h>
4b10dd6c
UD
24#include <sys/uio.h>
25
26#include <assert.h>
27
f2b98f97 28#include "localedef.h"
4b10dd6c
UD
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;
4b10dd6c 37 uint32_t width;
4b10dd6c
UD
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 (1, sizeof (struct locale_paper_t));
48
b9eb05d6
UD
49 if (lr != NULL)
50 {
51 lr->translate_strings = 1;
52 lr->return_widestr = 0;
53 }
4b10dd6c
UD
54}
55
56
57void
47e8b443 58paper_finish (struct localedef_t *locale, const struct charmap_t *charmap)
4b10dd6c
UD
59{
60 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
b9eb05d6
UD
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, from->copy_name[LC_PAPER],
76 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 {
f16491eb
CD
88 record_warning (_("\
89No definition for %s category found"), "LC_PAPER");
b9eb05d6
UD
90 paper_startup (NULL, locale, 0);
91 paper = locale->categories[LC_PAPER].paper;
92 nothing = 1;
93 }
94 }
4b10dd6c
UD
95
96 if (paper->height == 0)
97 {
b9eb05d6 98 if (! nothing)
f16491eb
CD
99 record_error (0, 0, _("%s: field `%s' not defined"),
100 "LC_PAPER", "height");
4b10dd6c
UD
101 /* Use as default values the values from the i18n locale. */
102 paper->height = 297;
103 }
4b10dd6c
UD
104
105 if (paper->width == 0)
106 {
b9eb05d6 107 if (! nothing)
f16491eb
CD
108 record_error (0, 0, _("%s: field `%s' not defined"),
109 "LC_PAPER", "width");
4b10dd6c
UD
110 /* Use as default values the values from the i18n locale. */
111 paper->width = 210;
112 }
4b10dd6c
UD
113}
114
115
116void
47e8b443 117paper_output (struct localedef_t *locale, const struct charmap_t *charmap,
4b10dd6c
UD
118 const char *output_path)
119{
120 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
1ecbb381
RS
121 struct locale_file file;
122
123 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
124 add_locale_uint32 (&file, paper->height);
125 add_locale_uint32 (&file, paper->width);
126 add_locale_string (&file, charmap->code_set_name);
127 write_locale_data (output_path, LC_PAPER, "LC_PAPER", &file);
4b10dd6c
UD
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,
47e8b443 134 const struct charmap_t *charmap, const char *repertoire_name,
4b10dd6c
UD
135 int ignore_content)
136{
4b10dd6c
UD
137 struct locale_paper_t *paper;
138 struct token *now;
139 struct token *arg;
140 enum token_t nowtok;
141
4b10dd6c
UD
142 /* The rest of the line containing `LC_PAPER' must be empty. */
143 lr_ignore_rest (ldfile, 1);
144
145 do
146 {
47e8b443 147 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
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 {
01ff9d0b 155 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_paper,
b9eb05d6 156 LC_PAPER, "LC_PAPER", ignore_content);
4b10dd6c
UD
157 return;
158 }
159
160 /* Prepare the data structures. */
161 paper_startup (ldfile, 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 {
47e8b443 173 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
174 nowtok = now->tok;
175 continue;
176 }
177
178 switch (nowtok)
179 {
180#define INT_ELEM(cat) \
181 case tok_##cat: \
b9eb05d6
UD
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 \
47e8b443 190 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
4b10dd6c
UD
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'. */
47e8b443 205 arg = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
206 if (arg->tok == tok_eof)
207 break;
208 if (arg->tok == tok_eol)
209 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_PAPER");
210 else if (arg->tok != tok_lc_paper)
211 lr_error (ldfile, _("\
212%1$s: definition does not end with `END %1$s'"), "LC_PAPER");
213 lr_ignore_rest (ldfile, 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. */
47e8b443 222 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
223 nowtok = now->tok;
224 }
225
226 /* When we come here we reached the end of the file. */
227 lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");
228}