1 /* Copyright (C) 1995 Free Software Foundation, Inc.
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with the GNU C Library; see the file COPYING.LIB. If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
32 #include "localedef.h"
33 #include "localeinfo.h"
36 /* We don't have these constants defined because we don't use them. Give
38 #define CTYPE_MB_CUR_MIN 0
39 #define CTYPE_MB_CUR_MAX 0
40 #define CTYPE_HASH_SIZE 0
41 #define CTYPE_HASH_LAYERS 0
43 #define CTYPE_TOUPPER_EB 0
44 #define CTYPE_TOLOWER_EB 0
45 #define CTYPE_TOUPPER_EL 0
46 #define CTYPE_TOLOWER_EL 0
49 /* We have all categories defined in `categories.def'. Now construct
50 the description and data structure used for all categories. */
51 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out) \
52 struct cat_item category##_desc[] = \
57 char *category##_values[NELEMS (category##_desc) - 1] = { NULL, };
58 #include "categories.def"
59 #undef DEFINE_CATEGORY
61 struct category category
[] =
63 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out) \
64 [category] = { _NL_NUM_##category, name, NELEMS (category##_desc) - 1, \
65 category##_desc, category##_values, in, check, out },
66 #include "categories.def"
67 #undef DEFINE_CATEGORY
69 #define NCATEGORIES NELEMS (category)
72 #define SYNTAX_ERROR \
73 error (0, 0, gettext ("%s:%Zd: syntax error in locale definition file"), \
74 locfile_data.filename, locfile_data.line_no)
77 /* Prototypes for local functions. */
78 static int get_byte (char *byte_ptr
);
79 static char *is_locale_name (int cat_no
, const char *str
, int len
);
82 /* Read a locale definition file FILE. The format is defined in
85 locfile_read (const char *fname
)
87 /* Pointer to text of last token. */
89 /* Length of last token (or if NUMBER the value itself). */
91 /* The last returned token. */
93 /* For error correction we remember whether the last token was correct. */
94 int correct_token
= 1;
96 /* Open the desired input file on stdin. */
99 while ((token
= locfile_lex (&ptr
, &len
)) != 0)
103 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
104 if (token
== category
[cat_no
].cat_id
)
107 if (cat_no
>= NCATEGORIES
)
108 /* A syntax error occured. No valid category defintion starts. */
110 if (correct_token
!= 0)
111 error (0, 0, gettext ("%s:%Zd: locale category start expected"),
112 locfile_data
.filename
, locfile_data
.line_no
);
114 /* To prevent following errors mark as error case. */
117 /* Synchronization point is the beginning of a new category.
118 Overread all line upto this silently. */
119 ignore_to_eol (0, 0);
123 /* Rest of the line should be empty. */
124 ignore_to_eol (0, 1);
126 /* Perhaps these category is already specified. We simply give a
127 warning and overwrite the values. */
128 if (category
[cat_no
].filled
!= 0)
129 error (0, 0, gettext ("%s:%Zd: multiple definition of locale "
130 "category %s"), locfile_data
.filename
,
131 locfile_data
.line_no
, category
[cat_no
].name
);
133 /* We read the first token because this could be the copy statement. */
134 token
= xlocfile_lex (&ptr
, &len
);
136 if (token
== TOK_COPY
)
137 /* Copying the definitions from an existing locale is requested. */
141 /* Get the name of the locale to copy from. */
142 token
= xlocfile_lex (&ptr
, &len
);
143 if (token
!= TOK_IDENT
&& token
!= TOK_STRING
)
144 /* No name, then mark error and ignore everything upto next
145 start of an category section. */
147 /* To prevent following errors mark as error case. */
150 /* Synchronization point is the beginning of a new category.
151 Overread all line upto this silently. */
152 ignore_to_eol (0, 0);
154 else if ((str
= is_locale_name (cat_no
, ptr
, len
)) != NULL
)
155 /* Yes the name really names an existing locale file. We are
156 returned the complete file name. Store it so that we can
157 copy it in the output phase. */
159 category
[cat_no
].copy_locale
= str
;
160 category
[cat_no
].filled
= 1;
162 ignore_to_eol (0, 1);
165 /* No, the name does not address a valid locale file. Mark
166 error case and ignore rest of category. */
169 memcpy (tmp
, ptr
, len
);
171 error (0, 0, gettext ("%s:%Zd: invalid locale `%s' in copy "
172 "statement"), locfile_data
.filename
,
173 locfile_data
.line_no
, tmp
);
175 ignore_to_eol (0, 0);
178 /* This should END as the next token. */
179 token
= xlocfile_lex (&ptr
, &len
);
181 if (token
== TOK_END
)
182 /* This is the end of the category. */
184 token
= xlocfile_lex (&ptr
, &len
);
186 if (token
!= category
[cat_no
].cat_id
)
187 /* Wrong category name after END. */
189 error (0, 0, gettext ("%s:%Zd: category `%s' does not "
190 "end with `END %s'"),
191 locfile_data
.filename
, locfile_data
.line_no
,
192 category
[cat_no
].name
, category
[cat_no
].name
);
193 ignore_to_eol (0, 0);
196 ignore_to_eol (0, 1);
201 /* No END following copy. Give error while not in error case. */
203 if (correct_token
!= 0)
204 error (0, 0, gettext ("%s:%Zd: `copy' must be sole rule"),
205 locfile_data
.filename
, locfile_data
.line_no
);
207 ignore_to_eol (0, 0);
213 /* Now it's time to mark as mentioned in the locale file. */
214 category
[cat_no
].filled
= 1;
216 if (category
[cat_no
].infct
!= NULL
)
217 /* The category needs a special input handling. */
219 category
[cat_no
].infct(token
);
223 /* Now process the given items. */
228 if (token
== TOK_END
)
229 /* This is the end of the category. */
231 token
= xlocfile_lex (&ptr
, &len
);
233 if (token
!= category
[cat_no
].cat_id
)
235 error (0, 0, gettext ("%s:%Zd: category `%s' does not end "
237 locfile_data
.filename
, locfile_data
.line_no
,
238 category
[cat_no
].name
, category
[cat_no
].name
);
239 ignore_to_eol (0, 0);
242 ignore_to_eol (0, 1);
244 /* Start next category. */
248 /* All other lines should describe valid items of the category. */
249 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
250 if (category
[cat_no
].item_desc
[item_no
].item_id
== token
)
253 if (item_no
>= category
[cat_no
].number
)
254 /* This is not a valid item of the category. */
257 ignore_to_eol (0, 0);
259 token
= xlocfile_lex (&ptr
, &len
);
261 /* And process next item. */
265 /* Test whether already a value is defined. */
266 if (category
[cat_no
].item_value
[item_no
] != NULL
)
267 error (0, 0, gettext ("%s:%Zd: category item `%s' already "
269 locfile_data
.filename
, locfile_data
.line_no
,
270 category
[cat_no
].item_desc
[item_no
].name
);
272 switch (category
[cat_no
].item_desc
[item_no
].value_type
)
275 /* Get next token. This is the argument to the item. */
276 token
= xlocfile_lex (&ptr
, &len
);
278 if (token
!= TOK_STRING
)
281 category
[cat_no
].item_value
[item_no
] = strdup (ptr
);
282 ignore_to_eol (0, ptr
!= NULL
);
285 /* This is a difficult case. The number of strings in
286 the array may vary. But for now its only necessary
287 with ALT_DIGITS from LC_TIME. This item is the last
288 so be can solve it by storing the number of string in
289 the first place and the string indeces following
294 if (category
[cat_no
].item_value
[item_no
] != NULL
)
295 buffer
= (char **) category
[cat_no
].item_value
[item_no
];
297 buffer
= (char **) xmalloc (
298 sizeof (char *) * category
[cat_no
].item_desc
[item_no
].max
);
300 category
[cat_no
].item_value
[item_no
] = (char *) buffer
;
302 /* As explained we may need a place to store the real number
304 if (category
[cat_no
].item_desc
[item_no
].min
305 != category
[cat_no
].item_desc
[item_no
].max
)
311 token
= xlocfile_lex (&ptr
, &len
);
312 if (token
!= TOK_STRING
)
318 if (cnt
>= category
[cat_no
].item_desc
[item_no
].max
)
320 error (0, 0, gettext ("%s:%Zd: too many elements "
322 locfile_data
.filename
, locfile_data
.line_no
,
323 category
[cat_no
].item_desc
[item_no
].name
);
327 buffer
[cnt
++] = strdup (ptr
);
329 token
= locfile_lex (&ptr
, &len
);
331 while (token
== TOK_CHAR
&& len
== ';');
333 ignore_to_eol (token
, ptr
!= NULL
);
335 if (cnt
< category
[cat_no
].item_desc
[item_no
].min
)
336 error (0, 0, gettext ("%s:%Zd: too few elements for item "
338 locfile_data
.filename
, locfile_data
.line_no
,
339 category
[cat_no
].item_desc
[item_no
].name
);
341 if (category
[cat_no
].item_desc
[item_no
].min
342 != category
[cat_no
].item_desc
[item_no
].max
)
343 *(int *) category
[cat_no
].item_value
[item_no
] = cnt
;
349 category
[cat_no
].item_value
[item_no
] = (char *) xmalloc (
351 ok
= get_byte (category
[cat_no
].item_value
[item_no
]);
352 ignore_to_eol (0, ok
);
363 buffer
= (char *) xmalloc ((maxsize
= 30));
366 while ((ok
= get_byte (&byte
)))
369 buffer
= (char *) xmalloc ((maxsize
*= 2));
371 buffer
[cnt
++] = byte
;
373 token
= locfile_lex (&ptr
, &len
);
374 if (token
!= TOK_CHAR
|| len
!= ';')
379 category
[cat_no
].item_value
[item_no
] = buffer
;
380 ignore_to_eol (token
, ok
);
384 error (5, 0, gettext ("internal error in %s, line %u"),
385 __FUNCTION__
, __LINE__
);
389 /* Get next token. */
390 token
= xlocfile_lex (&ptr
, &len
);
396 /* Check given values for categories for consistency. */
398 categories_check (void)
402 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
403 if (category
[cat_no
].copy_locale
== NULL
)
404 if (category
[cat_no
].filled
!= 0)
405 if (category
[cat_no
].checkfct
)
406 category
[cat_no
].checkfct();
411 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
412 if (category
[cat_no
].item_value
[item_no
] == NULL
)
416 /* If the item is defined in the standard is it an error to
417 have it not defined. */
418 errcode
= category
[cat_no
].item_desc
[item_no
].status
== std
421 error (errcode
, 0, gettext ("item `%s' of category `%s' "
423 category
[cat_no
].item_desc
[item_no
].name
,
424 category
[cat_no
].name
);
428 error (0, 0, gettext ("category `%s' not defined"),
429 category
[cat_no
].name
);
433 /* Write out the binary representation of the category data which can be
434 loaded by setlocale(1). */
436 categories_write (void)
444 struct obstack obstk
;
447 #define obstack_chunk_alloc xmalloc
448 #define obstack_chunk_free free
449 obstack_init (&obstk
);
451 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
455 if (category
[cat_no
].copy_locale
!= NULL
)
456 /* Simply copy the addressed locale file of the specified
457 category. Please note that this is tried before the distinction
458 between categories which need special handling is made. */
462 /* Open source file. */
463 source
= open (category
[cat_no
].copy_locale
, O_RDONLY
);
465 error (0, 0, gettext ("cannot copy locale definition file `%s'"),
466 category
[cat_no
].copy_locale
);
469 /* Construct file name of output file and open for writing. */
470 char path
[strlen (output_path
)
471 + strlen(category
[cat_no
].name
) + 1];
475 t
= stpcpy (path
, output_path
);
476 strcpy (t
, category
[cat_no
].name
);
478 dest
= creat (path
, 0666);
480 error (0, 0, gettext ("cannot open output file `%s': %m"),
487 /* Copy the files. */
490 size
= read (source
, buffer
, BUFSIZ
);
491 write (dest
, buffer
, size
);
498 puts (category
[cat_no
].name
);
507 if (category
[cat_no
].outfct
)
508 result
= category
[cat_no
].outfct();
514 int item_no
, len
, slen
, cnt
;
517 /* Count number of elements. */
518 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
520 switch (category
[cat_no
].item_desc
[item_no
].value_type
)
528 elems
+= category
[cat_no
].item_desc
[item_no
].max
;
531 error (5, 0, gettext ("internal error in %s, line %u"),
532 __FUNCTION__
, __LINE__
);
537 /* We now have the number of elements. We build the structure
538 and a helper structure for writing all out. */
539 len
= sizeof (struct locale_file
) + elems
* sizeof (int);
540 data
= obstack_alloc (&obstk
, len
);
541 iov
= obstack_alloc (&obstk
, (elems
+ 1) * sizeof (struct iovec
));
543 data
->magic
= LIMAGIC (cat_no
);
545 iov
[0].iov_base
= data
;
546 iov
[0].iov_len
= len
;
549 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
550 if (category
[cat_no
].item_value
[item_no
] == NULL
)
552 switch (category
[cat_no
].item_desc
[item_no
].value_type
)
557 data
->idx
[cnt
] = len
;
558 ++len
; /* We reserve one single byte for this entry. */
559 iov
[1 + cnt
].iov_base
= (char *) "";
560 iov
[1 + cnt
].iov_len
= 1;
568 max
= category
[cat_no
].item_desc
[item_no
].max
;
570 for (nstr
= 0; nstr
< max
; ++nstr
)
572 data
->idx
[cnt
] = len
;
574 iov
[1 + cnt
].iov_base
= "";
575 iov
[1 + cnt
].iov_len
= 1;
582 switch (category
[cat_no
].item_desc
[item_no
].value_type
)
586 data
->idx
[cnt
] = len
;
587 slen
= strlen (category
[cat_no
].item_value
[item_no
]) + 1;
589 iov
[1 + cnt
].iov_base
= category
[cat_no
].item_value
[item_no
];
590 iov
[1 + cnt
].iov_len
= slen
;
594 data
->idx
[cnt
] = len
;
597 iov
[1 + cnt
].iov_base
= category
[cat_no
].item_value
[item_no
];
598 iov
[1 + cnt
].iov_len
= slen
;
606 if (category
[cat_no
].item_desc
[item_no
].min
607 == category
[cat_no
].item_desc
[item_no
].max
)
609 nstr
= category
[cat_no
].item_desc
[item_no
].min
;
610 first
= (char **) category
[cat_no
].item_value
[item_no
];
614 nstr
= *(int *) category
[cat_no
].item_value
[item_no
];
616 ((char **) category
[cat_no
].item_value
[item_no
]) + 1;
621 data
->idx
[cnt
] = len
;
624 slen
= strlen (*first
) + 1;
625 iov
[1 + cnt
].iov_base
= *first
;
630 iov
[1 + cnt
].iov_base
= (char *) "";
633 iov
[1 + cnt
].iov_len
= slen
;
638 while (nact
< category
[cat_no
].item_desc
[item_no
].max
)
640 data
->idx
[cnt
] = len
;
642 iov
[1 + cnt
].iov_base
= (char *) "";
643 iov
[1 + cnt
].iov_len
= 1;
653 assert (cnt
<= elems
);
655 /* Construct the output filename from the argument given to
656 localedef on the command line. */
657 path
= (char *) obstack_alloc (&obstk
, strlen (output_path
) +
658 2 * strlen (category
[cat_no
].name
) + 5);
659 t
= stpcpy (path
, output_path
);
660 strcpy (t
, category
[cat_no
].name
);
662 fd
= creat (path
, 0666);
666 /* Check whether it failed because the named file is a directory.
667 In that case we use the file .../LC_xxx/SYS_LC_xxx, as the
668 loading functions of the C Library do. */
671 if (stat (path
, &st
) == 0 && S_ISDIR (st
.st_mode
))
673 stpcpy (stpcpy (strchr (path
, '\0'), "/SYS_"),
674 category
[cat_no
].name
);
675 fd
= creat (path
, 0666);
681 error (0, 0, gettext ("cannot open output file `%s': %m"),
687 if (writev (fd
, iov
, cnt
+ 1) == -1)
689 error (0, 0, gettext ("cannot write output file `%s': %m"),
694 if (elems
==0) write(fd
, &elems
, 10);
698 /* The old data is not needed anymore, but keep the obstack
700 obstack_free (&obstk
, data
);
704 puts (category
[cat_no
].name
);
706 /* Now the whole obstack can be removed. */
707 obstack_free (&obstk
, NULL
);
711 /* Get the representation of a number. This is a positive integer or
712 the number -1 which is handled as a special symbol by the scanner. */
714 get_byte (char *byte_ptr
)
720 token
= locfile_lex (&ptr
, &len
);
721 if (token
!= TOK_NUMBER
&& token
!= TOK_MINUS1
)
722 /* None of the valid number format. */
724 error (0, 0, gettext ("%s:%Zd: number expected"),
725 locfile_data
.filename
, locfile_data
.line_no
);
730 if (token
== TOK_MINUS1
)
732 *byte_ptr
= CHAR_MAX
;
737 /* The value of the numbers has to be less than CHAR_MAX. This is
738 ok for the information they have to express. */
740 error (0, 0, gettext ("%s:%Zd: invalid number"),
741 locfile_data
.filename
, locfile_data
.line_no
);
751 /* Test whether the string STR with length LEN is the name of an existing
752 locale and whether a file for category CAT_NO is found in this directory.
753 This categories are looked for in the system locale definition file
755 Return the complete file name for the category file. */
757 is_locale_name (int cat_no
, const char *str
, int len
)
759 static char **locale_names
= NULL
;
760 static int max_count
= 0;
761 static int locale_count
= 0;
766 if (locale_names
== NULL
)
767 /* Read in the list of all available locales. */
770 struct dirent
*dirent
;
772 /* LOCALE_NAMES is not NULL anymore, but LOCALE_COUNT == 0. */
775 dir
= opendir (LOCALE_PATH
);
778 error (1, errno
, gettext ("cannot read locale directory `%s'"),
784 /* Now we can look for all files in the directory. */
785 while ((dirent
= readdir (dir
)) != NULL
)
786 if (strcmp (dirent
->d_name
, ".") != 0
787 && strcmp (dirent
->d_name
, "..") != 0)
790 locale_names
= (char **) xmalloc ((max_count
= 10)
792 else if (locale_count
>= max_count
)
793 locale_names
= (char **) xrealloc (locale_names
,
796 locale_names
[locale_count
++] = strdup (dirent
->d_name
);
801 for (cnt
= 0; cnt
< locale_count
; ++cnt
)
802 if (strncmp (str
, locale_names
[cnt
], len
) == 0
803 && locale_names
[cnt
][len
] == '\0')
806 if (cnt
>= locale_count
)
809 /* Now search for this specific locale file. */
810 asprintf (&fname
, "%s/%s/%s", LOCALE_PATH
, locale_names
[cnt
],
811 category
[cat_no
].name
);
813 fd
= open (fname
, O_RDONLY
);
820 exist
= fstat (fd
, &st
);