powerpc: Update ulps
[glibc.git] / locale / programs / ld-paper.c
blob95b3d1074afba147b2cb6b2024c013347bd5a8b6
1 /* Copyright (C) 1998-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <langinfo.h>
22 #include <string.h>
23 #include <stdint.h>
24 #include <sys/uio.h>
26 #include <assert.h>
28 #include "localedef.h"
29 #include "localeinfo.h"
30 #include "locfile.h"
33 /* The real definition of the struct for the LC_PAPER locale. */
34 struct locale_paper_t
36 uint32_t height;
37 uint32_t width;
41 static void
42 paper_startup (struct linereader *lr, struct localedef_t *locale,
43 int ignore_content)
45 if (!ignore_content)
46 locale->categories[LC_PAPER].paper =
47 (struct locale_paper_t *) xcalloc (1, sizeof (struct locale_paper_t));
49 if (lr != NULL)
51 lr->translate_strings = 1;
52 lr->return_widestr = 0;
57 void
58 paper_finish (struct localedef_t *locale, const struct charmap_t *charmap)
60 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
61 int nothing = 0;
63 /* Now resolve copying and also handle completely missing definitions. */
64 if (paper == NULL)
66 /* First see whether we were supposed to copy. If yes, find the
67 actual definition. */
68 if (locale->copy_name[LC_PAPER] != NULL)
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;
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);
80 paper = locale->categories[LC_PAPER].paper
81 = from->categories[LC_PAPER].paper;
84 /* If there is still no definition issue an warning and create an
85 empty one. */
86 if (paper == NULL)
88 record_warning (_("\
89 No definition for %s category found"), "LC_PAPER");
90 paper_startup (NULL, locale, 0);
91 paper = locale->categories[LC_PAPER].paper;
92 nothing = 1;
96 if (paper->height == 0)
98 if (! nothing)
99 record_error (0, 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;
105 if (paper->width == 0)
107 if (! nothing)
108 record_error (0, 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;
116 void
117 paper_output (struct localedef_t *locale, const struct charmap_t *charmap,
118 const char *output_path)
120 struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
121 struct locale_file file;
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);
131 /* The parser for the LC_PAPER section of the locale definition. */
132 void
133 paper_read (struct linereader *ldfile, struct localedef_t *result,
134 const struct charmap_t *charmap, const char *repertoire_name,
135 int ignore_content)
137 struct locale_paper_t *paper;
138 struct token *now;
139 struct token *arg;
140 enum token_t nowtok;
142 /* The rest of the line containing `LC_PAPER' must be empty. */
143 lr_ignore_rest (ldfile, 1);
147 now = lr_token (ldfile, charmap, result, NULL, verbose);
148 nowtok = now->tok;
150 while (nowtok == tok_eol);
152 /* If we see `copy' now we are almost done. */
153 if (nowtok == tok_copy)
155 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_paper,
156 LC_PAPER, "LC_PAPER", ignore_content);
157 return;
160 /* Prepare the data structures. */
161 paper_startup (ldfile, result, ignore_content);
162 paper = result->categories[LC_PAPER].paper;
164 while (1)
166 /* Of course we don't proceed beyond the end of file. */
167 if (nowtok == tok_eof)
168 break;
170 /* Ingore empty lines. */
171 if (nowtok == tok_eol)
173 now = lr_token (ldfile, charmap, result, NULL, verbose);
174 nowtok = now->tok;
175 continue;
178 switch (nowtok)
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) \
186 lr_ignore_rest (ldfile, 0); \
187 break; \
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
200 INT_ELEM (height);
201 INT_ELEM (width);
203 case tok_end:
204 /* Next we assume `LC_PAPER'. */
205 arg = lr_token (ldfile, charmap, result, NULL, verbose);
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;
216 default:
217 err_label:
218 SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
221 /* Prepare for the next round. */
222 now = lr_token (ldfile, charmap, result, NULL, verbose);
223 nowtok = now->tok;
226 /* When we come here we reached the end of the file. */
227 lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");