Update.
[platform/upstream/glibc.git] / locale / programs / ld-paper.c
1 /* Copyright (C) 1998, 1999 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    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library 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 GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <error.h>
25 #include <langinfo.h>
26 #include <string.h>
27 #include <sys/uio.h>
28
29 #include <assert.h>
30
31 #include "localeinfo.h"
32 #include "locfile.h"
33
34
35 /* The real definition of the struct for the LC_PAPER locale.  */
36 struct locale_paper_t
37 {
38   uint32_t height;
39   uint32_t width;
40 };
41
42
43 static void
44 paper_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
59 void
60 paper_finish (struct localedef_t *locale, 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           error (0, 0, _("No 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         error (0, 0, _("%s: field `%s' not defined"), "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         error (0, 0, _("%s: field `%s' not defined"), "LC_PAPER", "width");
109       /* Use as default values the values from the i18n locale.  */
110       paper->width = 210;
111     }
112 }
113
114
115 void
116 paper_output (struct localedef_t *locale, struct charmap_t *charmap,
117               const char *output_path)
118 {
119   struct locale_paper_t *paper = locale->categories[LC_PAPER].paper;
120   struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER)];
121   struct locale_file data;
122   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_PAPER)];
123   size_t cnt = 0;
124
125   data.magic = LIMAGIC (LC_PAPER);
126   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_PAPER);
127   iov[cnt].iov_base = (void *) &data;
128   iov[cnt].iov_len = sizeof (data);
129   ++cnt;
130
131   iov[cnt].iov_base = (void *) idx;
132   iov[cnt].iov_len = sizeof (idx);
133   ++cnt;
134
135   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
136   iov[cnt].iov_base = &paper->height;
137   iov[cnt].iov_len = 4;
138   ++cnt;
139
140   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
141   iov[cnt].iov_base = &paper->width;
142   iov[cnt].iov_len = 4;
143   ++cnt;
144
145   assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER));
146
147   write_locale_data (output_path, "LC_PAPER",
148                      2 + _NL_ITEM_INDEX (_NL_NUM_LC_PAPER), iov);
149 }
150
151
152 /* The parser for the LC_PAPER section of the locale definition.  */
153 void
154 paper_read (struct linereader *ldfile, struct localedef_t *result,
155             struct charmap_t *charmap, const char *repertoire_name,
156             int ignore_content)
157 {
158   struct repertoire_t *repertoire = NULL;
159   struct locale_paper_t *paper;
160   struct token *now;
161   struct token *arg;
162   enum token_t nowtok;
163
164   /* Get the repertoire we have to use.  */
165   if (repertoire_name != NULL)
166     repertoire = repertoire_read (repertoire_name);
167
168   /* The rest of the line containing `LC_PAPER' must be empty.  */
169   lr_ignore_rest (ldfile, 1);
170
171   do
172     {
173       now = lr_token (ldfile, charmap, NULL);
174       nowtok = now->tok;
175     }
176   while (nowtok == tok_eol);
177
178   /* If we see `copy' now we are almost done.  */
179   if (nowtok == tok_copy)
180     {
181       handle_copy (ldfile, charmap, repertoire, result, tok_lc_paper,
182                    LC_PAPER, "LC_PAPER", ignore_content);
183       return;
184     }
185
186   /* Prepare the data structures.  */
187   paper_startup (ldfile, result, ignore_content);
188   paper = result->categories[LC_PAPER].paper;
189
190   while (1)
191     {
192       /* Of course we don't proceed beyond the end of file.  */
193       if (nowtok == tok_eof)
194         break;
195
196       /* Ingore empty lines.  */
197       if (nowtok == tok_eol)
198         {
199           now = lr_token (ldfile, charmap, NULL);
200           nowtok = now->tok;
201           continue;
202         }
203
204       switch (nowtok)
205         {
206 #define INT_ELEM(cat) \
207         case tok_##cat:                                                       \
208           /* Ignore the rest of the line if we don't need the input of        \
209              this line.  */                                                   \
210           if (ignore_content)                                                 \
211             {                                                                 \
212               lr_ignore_rest (ldfile, 0);                                     \
213               break;                                                          \
214             }                                                                 \
215                                                                               \
216           arg = lr_token (ldfile, charmap, NULL);                             \
217           if (arg->tok != tok_number)                                         \
218             goto err_label;                                                   \
219           else if (paper->cat != 0)                                           \
220             lr_error (ldfile, _("%s: field `%s' declared more than once"),    \
221                       "LC_PAPER", #cat);                                      \
222           else if (!ignore_content)                                           \
223             paper->cat = arg->val.num;                                        \
224           break
225
226           INT_ELEM (height);
227           INT_ELEM (width);
228
229         case tok_end:
230           /* Next we assume `LC_PAPER'.  */
231           arg = lr_token (ldfile, charmap, NULL);
232           if (arg->tok == tok_eof)
233             break;
234           if (arg->tok == tok_eol)
235             lr_error (ldfile, _("%s: incomplete `END' line"), "LC_PAPER");
236           else if (arg->tok != tok_lc_paper)
237             lr_error (ldfile, _("\
238 %1$s: definition does not end with `END %1$s'"), "LC_PAPER");
239           lr_ignore_rest (ldfile, arg->tok == tok_lc_paper);
240           return;
241
242         default:
243         err_label:
244           SYNTAX_ERROR (_("%s: syntax error"), "LC_PAPER");
245         }
246
247       /* Prepare for the next round.  */
248       now = lr_token (ldfile, charmap, NULL);
249       nowtok = now->tok;
250     }
251
252   /* When we come here we reached the end of the file.  */
253   lr_error (ldfile, _("%s: premature end of file"), "LC_PAPER");
254 }