1 /* Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
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.
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.
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. */
28 #include <sys/param.h>
31 #include "localeinfo.h"
32 #include "linereader.h"
34 #include "localedef.h"
35 #include "elem-hash.h"
37 /* Uncomment the following line in the production version. */
38 /* #define NDEBUG 1 */
41 #define obstack_chunk_alloc malloc
42 #define obstack_chunk_free free
45 obstack_int32_grow (struct obstack
*obstack
, int32_t data
)
47 if (sizeof (int32_t) == sizeof (int))
48 obstack_int_grow (obstack
, data
);
50 obstack_grow (obstack
, &data
, sizeof (int32_t));
54 obstack_int32_grow_fast (struct obstack
*obstack
, int32_t data
)
56 if (sizeof (int32_t) == sizeof (int))
57 obstack_int_grow_fast (obstack
, data
);
59 obstack_grow (obstack
, &data
, sizeof (int32_t));
62 /* Forward declaration. */
65 /* Data type for list of strings. */
68 struct section_list
*def_next
;
69 struct section_list
*next
;
70 /* Name of the section. */
72 /* First element of this section. */
73 struct element_t
*first
;
74 /* Last element of this section. */
75 struct element_t
*last
;
76 /* These are the rules for this section. */
77 enum coll_sort_rule
*rules
;
78 /* Index of the rule set in the appropriate section of the output file. */
86 /* Number of elements. */
92 /* Data type for collating element. */
104 /* The following is a bit mask which bits are set if this element is
105 used in the appropriate level. Interesting for the singlebyte
108 XXX The type here restricts the number of levels to 32. It could
109 be changed if necessary but I doubt this is necessary. */
110 unsigned int used_in_level
;
112 struct element_list_t
*weights
;
114 /* Nonzero if this is a real character definition. */
117 /* Order of the character in the sequence. This information will
118 be used in range expressions. */
122 /* Where does the definition come from. */
126 /* Which section does this belong to. */
127 struct section_list
*section
;
129 /* Predecessor and successor in the order list. */
130 struct element_t
*last
;
131 struct element_t
*next
;
133 /* Next element in multibyte output list. */
134 struct element_t
*mbnext
;
135 struct element_t
*mblast
;
137 /* Next element in wide character output list. */
138 struct element_t
*wcnext
;
139 struct element_t
*wclast
;
142 /* Special element value. */
143 #define ELEMENT_ELLIPSIS2 ((struct element_t *) 1)
144 #define ELEMENT_ELLIPSIS3 ((struct element_t *) 2)
145 #define ELEMENT_ELLIPSIS4 ((struct element_t *) 3)
147 /* Data type for collating symbol. */
152 /* Point to place in the order list. */
153 struct element_t
*order
;
155 /* Where does the definition come from. */
160 /* Sparse table of struct element_t *. */
161 #define TABLE wchead_table
162 #define ELEMENT struct element_t *
168 /* Sparse table of int32_t. */
169 #define TABLE collidx_table
170 #define ELEMENT int32_t
174 /* Sparse table of uint32_t. */
175 #define TABLE collseq_table
176 #define ELEMENT uint32_t
177 #define DEFAULT ~((uint32_t) 0)
181 /* The real definition of the struct for the LC_COLLATE locale. */
182 struct locale_collate_t
187 /* List of known scripts. */
188 struct section_list
*known_sections
;
189 /* List of used sections. */
190 struct section_list
*sections
;
191 /* Current section using definition. */
192 struct section_list
*current_section
;
193 /* There always can be an unnamed section. */
194 struct section_list unnamed_section
;
195 /* To make handling of errors easier we have another section. */
196 struct section_list error_section
;
197 /* Sometimes we are defining the values for collating symbols before
198 the first actual section. */
199 struct section_list symbol_section
;
201 /* Start of the order list. */
202 struct element_t
*start
;
204 /* The undefined element. */
205 struct element_t undefined
;
207 /* This is the cursor for `reorder_after' insertions. */
208 struct element_t
*cursor
;
210 /* This value is used when handling ellipsis. */
211 struct element_t ellipsis_weight
;
213 /* Known collating elements. */
214 hash_table elem_table
;
216 /* Known collating symbols. */
217 hash_table sym_table
;
219 /* Known collation sequences. */
220 hash_table seq_table
;
222 struct obstack mempool
;
224 /* The LC_COLLATE category is a bit special as it is sometimes possible
225 that the definitions from more than one input file contains information.
226 Therefore we keep all relevant input in a list. */
227 struct locale_collate_t
*next
;
229 /* Arrays with heads of the list for each of the leading bytes in
230 the multibyte sequences. */
231 struct element_t
*mbheads
[256];
233 /* Arrays with heads of the list for each of the leading bytes in
234 the multibyte sequences. */
235 struct wchead_table wcheads
;
237 /* The arrays with the collation sequence order. */
238 unsigned char mbseqorder
[256];
239 struct collseq_table wcseqorder
;
243 /* We have a few global variables which are used for reading all
244 LC_COLLATE category descriptions in all files. */
245 static uint32_t nrules
;
248 /* We need UTF-8 encoding of numbers. */
250 utf8_encode (char *buf
, int val
)
263 for (step
= 2; step
< 6; ++step
)
264 if ((val
& (~(uint32_t)0 << (5 * step
+ 1))) == 0)
268 *buf
= (unsigned char) (~0xff >> step
);
272 buf
[step
] = 0x80 | (val
& 0x3f);
283 static struct section_list
*
284 make_seclist_elem (struct locale_collate_t
*collate
, const char *string
,
285 struct section_list
*next
)
287 struct section_list
*newp
;
289 newp
= (struct section_list
*) obstack_alloc (&collate
->mempool
,
299 static struct element_t
*
300 new_element (struct locale_collate_t
*collate
, const char *mbs
, size_t mbslen
,
301 const uint32_t *wcs
, const char *name
, size_t namelen
,
304 struct element_t
*newp
;
306 newp
= (struct element_t
*) obstack_alloc (&collate
->mempool
,
308 newp
->name
= name
== NULL
? NULL
: obstack_copy0 (&collate
->mempool
,
312 newp
->mbs
= obstack_copy0 (&collate
->mempool
, mbs
, mbslen
);
322 size_t nwcs
= wcslen ((wchar_t *) wcs
);
324 obstack_grow (&collate
->mempool
, wcs
, nwcs
* sizeof (uint32_t));
325 obstack_grow (&collate
->mempool
, &zero
, sizeof (uint32_t));
326 newp
->wcs
= (uint32_t *) obstack_finish (&collate
->mempool
);
334 newp
->mborder
= NULL
;
336 newp
->used_in_level
= 0;
337 newp
->is_character
= is_character
;
339 /* Will be allocated later. */
340 newp
->weights
= NULL
;
345 newp
->section
= collate
->current_section
;
357 static struct symbol_t
*
358 new_symbol (struct locale_collate_t
*collate
, const char *name
, size_t len
)
360 struct symbol_t
*newp
;
362 newp
= (struct symbol_t
*) obstack_alloc (&collate
->mempool
, sizeof (*newp
));
364 newp
->name
= obstack_copy0 (&collate
->mempool
, name
, len
);
374 /* Test whether this name is already defined somewhere. */
376 check_duplicate (struct linereader
*ldfile
, struct locale_collate_t
*collate
,
377 struct charmap_t
*charmap
, struct repertoire_t
*repertoire
,
378 const char *symbol
, size_t symbol_len
)
382 if (find_entry (&charmap
->char_table
, symbol
, symbol_len
, &ignore
) == 0)
384 lr_error (ldfile
, _("`%.*s' already defined in charmap"),
385 (int) symbol_len
, symbol
);
389 if (repertoire
!= NULL
390 && (find_entry (&repertoire
->char_table
, symbol
, symbol_len
, &ignore
)
393 lr_error (ldfile
, _("`%.*s' already defined in repertoire"),
394 (int) symbol_len
, symbol
);
398 if (find_entry (&collate
->sym_table
, symbol
, symbol_len
, &ignore
) == 0)
400 lr_error (ldfile
, _("`%.*s' already defined as collating symbol"),
401 (int) symbol_len
, symbol
);
405 if (find_entry (&collate
->elem_table
, symbol
, symbol_len
, &ignore
) == 0)
407 lr_error (ldfile
, _("`%.*s' already defined as collating element"),
408 (int) symbol_len
, symbol
);
416 /* Read the direction specification. */
418 read_directions (struct linereader
*ldfile
, struct token
*arg
,
419 struct charmap_t
*charmap
, struct repertoire_t
*repertoire
,
420 struct locale_collate_t
*collate
)
423 int max
= nrules
?: 10;
424 enum coll_sort_rule
*rules
= calloc (max
, sizeof (*rules
));
431 if (arg
->tok
== tok_forward
)
433 if (rules
[cnt
] & sort_backward
)
437 lr_error (ldfile
, _("\
438 %s: `forward' and `backward' are mutually excluding each other"),
443 else if (rules
[cnt
] & sort_forward
)
447 lr_error (ldfile
, _("\
448 %s: `%s' mentioned more than once in definition of weight %d"),
449 "LC_COLLATE", "forward", cnt
+ 1);
453 rules
[cnt
] |= sort_forward
;
457 else if (arg
->tok
== tok_backward
)
459 if (rules
[cnt
] & sort_forward
)
463 lr_error (ldfile
, _("\
464 %s: `forward' and `backward' are mutually excluding each other"),
469 else if (rules
[cnt
] & sort_backward
)
473 lr_error (ldfile
, _("\
474 %s: `%s' mentioned more than once in definition of weight %d"),
475 "LC_COLLATE", "backward", cnt
+ 1);
479 rules
[cnt
] |= sort_backward
;
483 else if (arg
->tok
== tok_position
)
485 if (rules
[cnt
] & sort_position
)
489 lr_error (ldfile
, _("\
490 %s: `%s' mentioned more than once in definition of weight %d"),
491 "LC_COLLATE", "position", cnt
+ 1);
495 rules
[cnt
] |= sort_position
;
501 arg
= lr_token (ldfile
, charmap
, repertoire
);
503 if (arg
->tok
== tok_eof
|| arg
->tok
== tok_eol
|| arg
->tok
== tok_comma
504 || arg
->tok
== tok_semicolon
)
506 if (! valid
&& ! warned
)
508 lr_error (ldfile
, _("%s: syntax error"), "LC_COLLATE");
512 /* See whether we have to increment the counter. */
513 if (arg
->tok
!= tok_comma
&& rules
[cnt
] != 0)
515 /* Add the default `forward' if we have seen only `position'. */
516 if (rules
[cnt
] == sort_position
)
517 rules
[cnt
] = sort_position
| sort_forward
;
522 if (arg
->tok
== tok_eof
|| arg
->tok
== tok_eol
)
523 /* End of line or file, so we exit the loop. */
528 /* See whether we have enough room in the array. */
532 rules
= (enum coll_sort_rule
*) xrealloc (rules
,
535 memset (&rules
[cnt
], '\0', (max
- cnt
) * sizeof (*rules
));
542 /* There must not be any more rule. */
545 lr_error (ldfile
, _("\
546 %s: too many rules; first entry only had %d"),
547 "LC_COLLATE", nrules
);
551 lr_ignore_rest (ldfile
, 0);
560 lr_error (ldfile
, _("%s: syntax error"), "LC_COLLATE");
565 arg
= lr_token (ldfile
, charmap
, repertoire
);
570 /* Now we know how many rules we have. */
572 rules
= (enum coll_sort_rule
*) xrealloc (rules
,
573 nrules
* sizeof (*rules
));
579 /* Not enough rules in this specification. */
581 lr_error (ldfile
, _("%s: not enough sorting rules"), "LC_COLLATE");
584 rules
[cnt
] = sort_forward
;
585 while (++cnt
< nrules
);
589 collate
->current_section
->rules
= rules
;
593 static struct element_t
*
594 find_element (struct linereader
*ldfile
, struct locale_collate_t
*collate
,
595 const char *str
, size_t len
)
597 struct element_t
*result
= NULL
;
599 /* Search for the entries among the collation sequences already define. */
600 if (find_entry (&collate
->seq_table
, str
, len
, (void **) &result
) != 0)
602 /* Nope, not define yet. So we see whether it is a
606 if (find_entry (&collate
->sym_table
, str
, len
, &ptr
) == 0)
608 /* It's a collation symbol. */
609 struct symbol_t
*sym
= (struct symbol_t
*) ptr
;
613 result
= sym
->order
= new_element (collate
, NULL
, 0, NULL
,
616 else if (find_entry (&collate
->elem_table
, str
, len
,
617 (void **) &result
) != 0)
619 /* It's also no collation element. So it is a character
620 element defined later. */
621 result
= new_element (collate
, NULL
, 0, NULL
, str
, len
, 1);
623 /* Insert it into the sequence table. */
624 insert_entry (&collate
->seq_table
, str
, len
, result
);
633 unlink_element (struct locale_collate_t
*collate
)
635 if (collate
->cursor
== collate
->start
)
637 assert (collate
->cursor
->next
== NULL
);
638 assert (collate
->cursor
->last
== NULL
);
639 collate
->cursor
= NULL
;
643 if (collate
->cursor
->next
!= NULL
)
644 collate
->cursor
->next
->last
= collate
->cursor
->last
;
645 if (collate
->cursor
->last
!= NULL
)
646 collate
->cursor
->last
->next
= collate
->cursor
->next
;
647 collate
->cursor
= collate
->cursor
->last
;
653 insert_weights (struct linereader
*ldfile
, struct element_t
*elem
,
654 struct charmap_t
*charmap
, struct repertoire_t
*repertoire
,
655 struct locale_collate_t
*collate
, enum token_t ellipsis
)
660 /* Initialize all the fields. */
661 elem
->file
= ldfile
->fname
;
662 elem
->line
= ldfile
->lineno
;
663 elem
->last
= collate
->cursor
;
664 elem
->next
= collate
->cursor
? collate
->cursor
->next
: NULL
;
665 if (collate
->cursor
!= NULL
&& collate
->cursor
->next
!= NULL
)
666 collate
->cursor
->next
->last
= elem
;
667 elem
->section
= collate
->current_section
;
668 if (collate
->cursor
!= NULL
)
669 collate
->cursor
->next
= elem
;
670 if (collate
->start
== NULL
)
672 assert (collate
->cursor
== NULL
);
673 collate
->start
= elem
;
675 elem
->weights
= (struct element_list_t
*)
676 obstack_alloc (&collate
->mempool
, nrules
* sizeof (struct element_list_t
));
677 memset (elem
->weights
, '\0', nrules
* sizeof (struct element_list_t
));
679 if (collate
->current_section
->first
== NULL
)
680 collate
->current_section
->first
= elem
;
681 if (collate
->current_section
->last
== collate
->cursor
)
682 collate
->current_section
->last
= elem
;
684 collate
->cursor
= elem
;
688 arg
= lr_token (ldfile
, charmap
, repertoire
);
691 if (arg
->tok
== tok_eof
|| arg
->tok
== tok_eol
)
694 if (arg
->tok
== tok_ignore
)
696 /* The weight for this level has to be ignored. We use the
697 null pointer to indicate this. */
698 elem
->weights
[weight_cnt
].w
= (struct element_t
**)
699 obstack_alloc (&collate
->mempool
, sizeof (struct element_t
*));
700 elem
->weights
[weight_cnt
].w
[0] = NULL
;
701 elem
->weights
[weight_cnt
].cnt
= 1;
703 else if (arg
->tok
== tok_bsymbol
|| arg
->tok
== tok_ucs4
)
706 struct element_t
*val
;
710 if (arg
->tok
== tok_bsymbol
)
712 symstr
= arg
->val
.str
.startmb
;
713 symlen
= arg
->val
.str
.lenmb
;
717 snprintf (ucs4str
, sizeof (ucs4str
), "U%08X", arg
->val
.ucs4
);
722 val
= find_element (ldfile
, collate
, symstr
, symlen
);
726 elem
->weights
[weight_cnt
].w
= (struct element_t
**)
727 obstack_alloc (&collate
->mempool
, sizeof (struct element_t
*));
728 elem
->weights
[weight_cnt
].w
[0] = val
;
729 elem
->weights
[weight_cnt
].cnt
= 1;
731 else if (arg
->tok
== tok_string
)
733 /* Split the string up in the individual characters and put
734 the element definitions in the list. */
735 const char *cp
= arg
->val
.str
.startmb
;
737 struct element_t
*charelem
;
738 struct element_t
**weights
= NULL
;
743 lr_error (ldfile
, _("%s: empty weight string not allowed"),
745 lr_ignore_rest (ldfile
, 0);
753 /* Ahh, it's a bsymbol or an UCS4 value. If it's
754 the latter we have to unify the name. */
755 const char *startp
= ++cp
;
760 if (*cp
== ldfile
->escape_char
)
763 /* It's a syntax error. */
769 if (cp
- startp
== 5 && startp
[0] == 'U'
770 && isxdigit (startp
[1]) && isxdigit (startp
[2])
771 && isxdigit (startp
[3]) && isxdigit (startp
[4]))
773 unsigned int ucs4
= strtoul (startp
+ 1, NULL
, 16);
776 newstr
= (char *) xmalloc (10);
777 snprintf (newstr
, 10, "U%08X", ucs4
);
785 charelem
= find_element (ldfile
, collate
, startp
, len
);
790 /* People really shouldn't use characters directly in
791 the string. Especially since it's not really clear
792 what this means. We interpret all characters in the
793 string as if that would be bsymbols. Otherwise we
794 would have to match back to bsymbols somehow and this
795 is normally not what people normally expect. */
796 charelem
= find_element (ldfile
, collate
, cp
++, 1);
799 if (charelem
== NULL
)
801 /* We ignore the rest of the line. */
802 lr_ignore_rest (ldfile
, 0);
806 /* Add the pointer. */
809 struct element_t
**newp
;
811 newp
= (struct element_t
**)
812 alloca (max
* sizeof (struct element_t
*));
813 memcpy (newp
, weights
, cnt
* sizeof (struct element_t
*));
816 weights
[cnt
++] = charelem
;
820 /* Now store the information. */
821 elem
->weights
[weight_cnt
].w
= (struct element_t
**)
822 obstack_alloc (&collate
->mempool
,
823 cnt
* sizeof (struct element_t
*));
824 memcpy (elem
->weights
[weight_cnt
].w
, weights
,
825 cnt
* sizeof (struct element_t
*));
826 elem
->weights
[weight_cnt
].cnt
= cnt
;
828 /* We don't need the string anymore. */
829 free (arg
->val
.str
.startmb
);
831 else if (ellipsis
!= tok_none
832 && (arg
->tok
== tok_ellipsis2
833 || arg
->tok
== tok_ellipsis3
834 || arg
->tok
== tok_ellipsis4
))
836 /* It must be the same ellipsis as used in the initial column. */
837 if (arg
->tok
!= ellipsis
)
838 lr_error (ldfile
, _("\
839 %s: weights must use the same ellipsis symbol as the name"),
842 /* The weight for this level has to be ignored. We use the
843 null pointer to indicate this. */
844 elem
->weights
[weight_cnt
].w
= (struct element_t
**)
845 obstack_alloc (&collate
->mempool
, sizeof (struct element_t
*));
846 elem
->weights
[weight_cnt
].w
[0] = ELEMENT_ELLIPSIS2
;
847 elem
->weights
[weight_cnt
].cnt
= 1;
852 /* It's a syntax error. */
853 lr_error (ldfile
, _("%s: syntax error"), "LC_COLLATE");
854 lr_ignore_rest (ldfile
, 0);
858 arg
= lr_token (ldfile
, charmap
, repertoire
);
859 /* This better should be the end of the line or a semicolon. */
860 if (arg
->tok
== tok_semicolon
)
861 /* OK, ignore this and read the next token. */
862 arg
= lr_token (ldfile
, charmap
, repertoire
);
863 else if (arg
->tok
!= tok_eof
&& arg
->tok
!= tok_eol
)
865 /* It's a syntax error. */
866 lr_error (ldfile
, _("%s: syntax error"), "LC_COLLATE");
867 lr_ignore_rest (ldfile
, 0);
871 while (++weight_cnt
< nrules
);
873 if (weight_cnt
< nrules
)
875 /* This means the rest of the line uses the current element as
879 elem
->weights
[weight_cnt
].w
= (struct element_t
**)
880 obstack_alloc (&collate
->mempool
, sizeof (struct element_t
*));
881 if (ellipsis
== tok_none
)
882 elem
->weights
[weight_cnt
].w
[0] = elem
;
884 elem
->weights
[weight_cnt
].w
[0] = ELEMENT_ELLIPSIS2
;
885 elem
->weights
[weight_cnt
].cnt
= 1;
887 while (++weight_cnt
< nrules
);
891 if (arg
->tok
== tok_ignore
|| arg
->tok
== tok_bsymbol
)
893 /* Too many rule values. */
894 lr_error (ldfile
, _("%s: too many values"), "LC_COLLATE");
895 lr_ignore_rest (ldfile
, 0);
898 lr_ignore_rest (ldfile
, arg
->tok
!= tok_eol
&& arg
->tok
!= tok_eof
);
904 insert_value (struct linereader
*ldfile
, const char *symstr
, size_t symlen
,
905 struct charmap_t
*charmap
, struct repertoire_t
*repertoire
,
906 struct locale_collate_t
*collate
)
908 /* First find out what kind of symbol this is. */
911 struct element_t
*elem
= NULL
;
913 /* Try to find the character in the charmap. */
914 seq
= charmap_find_value (charmap
, symstr
, symlen
);
916 /* Determine the wide character. */
917 if (seq
== NULL
|| seq
->ucs4
== UNINITIALIZED_CHAR_VALUE
)
919 wc
= repertoire_find_value (repertoire
, symstr
, symlen
);
926 if (wc
== ILLEGAL_CHAR_VALUE
&& seq
== NULL
)
928 /* It's no character, so look through the collation elements and
932 if (find_entry (&collate
->sym_table
, symstr
, symlen
, &result
) == 0)
934 /* It's a collation symbol. */
935 struct symbol_t
*sym
= (struct symbol_t
*) result
;
939 elem
= sym
->order
= new_element (collate
, NULL
, 0, NULL
,
940 sym
->name
, strlen (sym
->name
), 0);
942 else if (find_entry (&collate
->elem_table
, symstr
, symlen
,
943 (void **) &elem
) != 0)
945 /* It's also no collation element. Therefore ignore it. */
946 lr_ignore_rest (ldfile
, 0);
952 /* Otherwise the symbols stands for a character. */
953 if (find_entry (&collate
->seq_table
, symstr
, symlen
,
954 (void **) &elem
) != 0)
956 uint32_t wcs
[2] = { wc
, 0 };
958 /* We have to allocate an entry. */
959 elem
= new_element (collate
, seq
!= NULL
? seq
->bytes
: NULL
,
960 seq
!= NULL
? seq
->nbytes
: 0,
961 wc
== ILLEGAL_CHAR_VALUE
? NULL
: wcs
,
964 /* And add it to the table. */
965 if (insert_entry (&collate
->seq_table
, symstr
, symlen
, elem
) != 0)
966 /* This cannot happen. */
967 assert (! "Internal error");
971 /* Maybe the character was used before the definition. In this case
972 we have to insert the byte sequences now. */
973 if (elem
->mbs
== NULL
&& seq
!= NULL
)
975 elem
->mbs
= obstack_copy0 (&collate
->mempool
,
976 seq
->bytes
, seq
->nbytes
);
977 elem
->nmbs
= seq
->nbytes
;
980 if (elem
->wcs
== NULL
&& wc
!= ILLEGAL_CHAR_VALUE
)
982 uint32_t wcs
[2] = { wc
, 0 };
984 elem
->wcs
= obstack_copy (&collate
->mempool
, wcs
, sizeof (wcs
));
990 /* Test whether this element is not already in the list. */
991 if (elem
->next
!= NULL
|| (collate
->cursor
!= NULL
992 && elem
->next
== collate
->cursor
))
994 lr_error (ldfile
, _("order for `%.*s' already defined at %s:%Zu"),
995 (int) symlen
, symstr
, elem
->file
, elem
->line
);
996 lr_ignore_rest (ldfile
, 0);
1000 insert_weights (ldfile
, elem
, charmap
, repertoire
, collate
, tok_none
);
1007 handle_ellipsis (struct linereader
*ldfile
, const char *symstr
, size_t symlen
,
1008 enum token_t ellipsis
, struct charmap_t
*charmap
,
1009 struct repertoire_t
*repertoire
,
1010 struct locale_collate_t
*collate
)
1012 struct element_t
*startp
;
1013 struct element_t
*endp
;
1015 /* Unlink the entry added for the ellipsis. */
1016 unlink_element (collate
);
1017 startp
= collate
->cursor
;
1019 /* Process and add the end-entry. */
1021 && insert_value (ldfile
, symstr
, symlen
, charmap
, repertoire
, collate
))
1022 /* Something went wrong with inserting the to-value. This means
1023 we cannot process the ellipsis. */
1026 /* Reset the cursor. */
1027 collate
->cursor
= startp
;
1029 /* Now we have to handle many different situations:
1030 - we have to distinguish between the three different ellipsis forms
1031 - the is the ellipsis at the beginning, in the middle, or at the end.
1033 endp
= collate
->cursor
->next
;
1034 assert (symstr
== NULL
|| endp
!= NULL
);
1036 /* XXX The following is probably very wrong since also collating symbols
1037 can appear in ranges. But do we want/can refine the test for that? */
1039 /* Both, the start and the end symbol, must stand for characters. */
1040 if ((startp
!= NULL
&& (startp
->name
== NULL
|| ! startp
->is_character
))
1041 || (endp
!= NULL
&& (endp
->name
== NULL
|| ! endp
->is_character
)))
1043 lr_error (ldfile
, _("\
1044 %s: the start and the end symbol of a range must stand for characters"),
1050 if (ellipsis
== tok_ellipsis3
)
1052 /* One requirement we make here: the length of the byte
1053 sequences for the first and end character must be the same.
1054 This is mainly to prevent unwanted effects and this is often
1055 not what is wanted. */
1056 size_t len
= (startp
->mbs
!= NULL
? startp
->nmbs
1057 : (endp
->mbs
!= NULL
? endp
->nmbs
: 0));
1058 char mbcnt
[len
+ 1];
1059 char mbend
[len
+ 1];
1061 /* Well, this should be caught somewhere else already. Just to
1063 assert (startp
== NULL
|| startp
->wcs
== NULL
|| startp
->wcs
[1] == 0);
1064 assert (endp
== NULL
|| endp
->wcs
== NULL
|| endp
->wcs
[1] == 0);
1066 if (startp
!= NULL
&& endp
!= NULL
1067 && startp
->mbs
!= NULL
&& endp
->mbs
!= NULL
1068 && startp
->nmbs
!= endp
->nmbs
)
1070 lr_error (ldfile
, _("\
1071 %s: byte sequences of first and last character must have the same length"),
1076 /* Determine whether we have to generate multibyte sequences. */
1077 if ((startp
== NULL
|| startp
->mbs
!= NULL
)
1078 && (endp
== NULL
|| endp
->mbs
!= NULL
))
1083 /* Prepare the beginning byte sequence. This is either from the
1084 beginning byte sequence or it is all nulls if it was an
1085 initial ellipsis. */
1086 if (startp
== NULL
|| startp
->mbs
== NULL
)
1087 memset (mbcnt
, '\0', len
);
1090 memcpy (mbcnt
, startp
->mbs
, len
);
1092 /* And increment it so that the value is the first one we will
1094 for (cnt
= len
- 1; cnt
>= 0; --cnt
)
1095 if (++mbcnt
[cnt
] != '\0')
1100 /* And the end sequence. */
1101 if (endp
== NULL
|| endp
->mbs
== NULL
)
1102 memset (mbend
, '\0', len
);
1104 memcpy (mbend
, endp
->mbs
, len
);
1107 /* Test whether we have a correct range. */
1108 ret
= memcmp (mbcnt
, mbend
, len
);
1112 lr_error (ldfile
, _("%s: byte sequence of first character of \
1113 sequence is not lower than that of the last character"), "LC_COLLATE");
1117 /* Generate the byte sequences data. */
1120 struct charseq
*seq
;
1122 /* Quite a bit of work ahead. We have to find the character
1123 definition for the byte sequence and then determine the
1124 wide character belonging to it. */
1125 seq
= charmap_find_symbol (charmap
, mbcnt
, len
);
1128 struct element_t
*elem
;
1131 /* I don't this this can ever happen. */
1132 assert (seq
->name
!= NULL
);
1133 namelen
= strlen (seq
->name
);
1135 if (seq
->ucs4
== UNINITIALIZED_CHAR_VALUE
)
1136 seq
->ucs4
= repertoire_find_value (repertoire
, seq
->name
,
1139 /* Now we are ready to insert the new value in the
1140 sequence. Find out whether the element is
1142 if (find_entry (&collate
->seq_table
, seq
->name
, namelen
,
1143 (void **) &elem
) != 0)
1145 uint32_t wcs
[2] = { seq
->ucs4
, 0 };
1147 /* We have to allocate an entry. */
1148 elem
= new_element (collate
, mbcnt
, len
,
1149 seq
->ucs4
== ILLEGAL_CHAR_VALUE
1150 ? NULL
: wcs
, seq
->name
,
1153 /* And add it to the table. */
1154 if (insert_entry (&collate
->seq_table
, seq
->name
,
1155 namelen
, elem
) != 0)
1156 /* This cannot happen. */
1157 assert (! "Internal error");
1160 /* Test whether this element is not already in the list. */
1161 if (elem
->next
!= NULL
|| (collate
->cursor
!= NULL
1162 && elem
->next
== collate
->cursor
))
1164 lr_error (ldfile
, _("\
1165 order for `%.*s' already defined at %s:%Zu"),
1166 (int) namelen
, seq
->name
,
1167 elem
->file
, elem
->line
);
1171 /* Enqueue the new element. */
1172 elem
->last
= collate
->cursor
;
1173 if (collate
->cursor
== NULL
)
1177 elem
->next
= collate
->cursor
->next
;
1178 elem
->last
->next
= elem
;
1179 if (elem
->next
!= NULL
)
1180 elem
->next
->last
= elem
;
1182 if (collate
->start
== NULL
)
1184 assert (collate
->cursor
== NULL
);
1185 collate
->start
= elem
;
1187 collate
->cursor
= elem
;
1189 /* Add the weight value. We take them from the
1190 `ellipsis_weights' member of `collate'. */
1191 elem
->weights
= (struct element_list_t
*)
1192 obstack_alloc (&collate
->mempool
,
1193 nrules
* sizeof (struct element_list_t
));
1194 for (cnt
= 0; cnt
< nrules
; ++cnt
)
1195 if (collate
->ellipsis_weight
.weights
[cnt
].cnt
== 1
1196 && (collate
->ellipsis_weight
.weights
[cnt
].w
[0]
1197 == ELEMENT_ELLIPSIS2
))
1199 elem
->weights
[cnt
].w
= (struct element_t
**)
1200 obstack_alloc (&collate
->mempool
,
1201 sizeof (struct element_t
*));
1202 elem
->weights
[cnt
].w
[0] = elem
;
1203 elem
->weights
[cnt
].cnt
= 1;
1207 /* Simply use the weight from `ellipsis_weight'. */
1208 elem
->weights
[cnt
].w
=
1209 collate
->ellipsis_weight
.weights
[cnt
].w
;
1210 elem
->weights
[cnt
].cnt
=
1211 collate
->ellipsis_weight
.weights
[cnt
].cnt
;
1215 /* Increment for the next round. */
1217 for (cnt
= len
- 1; cnt
>= 0; --cnt
)
1218 if (++mbcnt
[cnt
] != '\0')
1221 /* Find out whether this was all. */
1222 if (cnt
< 0 || memcmp (mbcnt
, mbend
, len
) >= 0)
1223 /* Yep, that's all. */
1230 /* For symbolic range we naturally must have a beginning and an
1231 end specified by the user. */
1233 lr_error (ldfile
, _("\
1234 %s: symbolic range ellipsis must not directly follow `order_start'"),
1236 else if (endp
== NULL
)
1237 lr_error (ldfile
, _("\
1238 %s: symbolic range ellipsis must not be direct followed by `order_end'"),
1242 /* Determine the range. To do so we have to determine the
1243 common prefix of the both names and then the numeric
1244 values of both ends. */
1245 size_t lenfrom
= strlen (startp
->name
);
1246 size_t lento
= strlen (endp
->name
);
1247 char buf
[lento
+ 1];
1252 int base
= ellipsis
== tok_ellipsis2
? 16 : 10;
1254 if (lenfrom
!= lento
)
1257 lr_error (ldfile
, _("\
1258 `%s' and `%.*s' are no valid names for symbolic range"),
1259 startp
->name
, (int) lento
, endp
->name
);
1263 while (startp
->name
[preflen
] == endp
->name
[preflen
])
1264 if (startp
->name
[preflen
] == '\0')
1265 /* Nothing to be done. The start and end point are identical
1266 and while inserting the end point we have already given
1267 the user an error message. */
1273 from
= strtol (startp
->name
+ preflen
, &cp
, base
);
1274 if ((from
== UINT_MAX
&& errno
== ERANGE
) || *cp
!= '\0')
1278 to
= strtol (endp
->name
+ preflen
, &cp
, base
);
1279 if ((to
== UINT_MAX
&& errno
== ERANGE
) || *cp
!= '\0')
1282 /* Copy the prefix. */
1283 memcpy (buf
, startp
->name
, preflen
);
1285 /* Loop over all values. */
1286 for (++from
; from
< to
; ++from
)
1288 struct element_t
*elem
= NULL
;
1289 struct charseq
*seq
;
1293 /* Generate the the name. */
1294 sprintf (buf
+ preflen
, base
== 10 ? "%d" : "%x", from
);
1296 /* Look whether this name is already defined. */
1297 if (find_entry (&collate
->seq_table
, buf
, symlen
,
1298 (void **) &elem
) == 0)
1300 if (elem
->next
!= NULL
|| (collate
->cursor
!= NULL
1301 && elem
->next
== collate
->cursor
))
1303 lr_error (ldfile
, _("\
1304 %s: order for `%.*s' already defined at %s:%Zu"),
1305 "LC_COLLATE", (int) lenfrom
, buf
,
1306 elem
->file
, elem
->line
);
1310 if (elem
->name
== NULL
)
1312 lr_error (ldfile
, _("%s: `%s' must be a charater"),
1318 if (elem
== NULL
|| (elem
->mbs
== NULL
&& elem
->wcs
== NULL
))
1320 /* Search for a character of this name. */
1321 seq
= charmap_find_value (charmap
, buf
, lenfrom
);
1322 if (seq
== NULL
|| seq
->ucs4
== UNINITIALIZED_CHAR_VALUE
)
1324 wc
= repertoire_find_value (repertoire
, buf
, lenfrom
);
1332 if (wc
== ILLEGAL_CHAR_VALUE
&& seq
== NULL
)
1333 /* We don't know anything about a character with this
1334 name. XXX Should we warn? */
1339 uint32_t wcs
[2] = { wc
, 0 };
1341 /* We have to allocate an entry. */
1342 elem
= new_element (collate
,
1343 seq
!= NULL
? seq
->bytes
: NULL
,
1344 seq
!= NULL
? seq
->nbytes
: 0,
1345 wc
== ILLEGAL_CHAR_VALUE
1346 ? NULL
: wcs
, buf
, lenfrom
, 1);
1350 /* Update the element. */
1353 elem
->mbs
= obstack_copy0 (&collate
->mempool
,
1354 seq
->bytes
, seq
->nbytes
);
1355 elem
->nmbs
= seq
->nbytes
;
1358 if (wc
!= ILLEGAL_CHAR_VALUE
)
1362 obstack_grow (&collate
->mempool
,
1363 &wc
, sizeof (uint32_t));
1364 obstack_grow (&collate
->mempool
,
1365 &zero
, sizeof (uint32_t));
1366 elem
->wcs
= obstack_finish (&collate
->mempool
);
1371 elem
->file
= ldfile
->fname
;
1372 elem
->line
= ldfile
->lineno
;
1373 elem
->section
= collate
->current_section
;
1376 /* Enqueue the new element. */
1377 elem
->last
= collate
->cursor
;
1378 elem
->next
= collate
->cursor
->next
;
1379 elem
->last
->next
= elem
;
1380 if (elem
->next
!= NULL
)
1381 elem
->next
->last
= elem
;
1382 collate
->cursor
= elem
;
1384 /* Now add the weights. They come from the `ellipsis_weights'
1385 member of `collate'. */
1386 elem
->weights
= (struct element_list_t
*)
1387 obstack_alloc (&collate
->mempool
,
1388 nrules
* sizeof (struct element_list_t
));
1389 for (cnt
= 0; cnt
< nrules
; ++cnt
)
1390 if (collate
->ellipsis_weight
.weights
[cnt
].cnt
== 1
1391 && (collate
->ellipsis_weight
.weights
[cnt
].w
[0]
1392 == ELEMENT_ELLIPSIS2
))
1394 elem
->weights
[cnt
].w
= (struct element_t
**)
1395 obstack_alloc (&collate
->mempool
,
1396 sizeof (struct element_t
*));
1397 elem
->weights
[cnt
].w
[0] = elem
;
1398 elem
->weights
[cnt
].cnt
= 1;
1402 /* Simly use the weight from `ellipsis_weight'. */
1403 elem
->weights
[cnt
].w
=
1404 collate
->ellipsis_weight
.weights
[cnt
].w
;
1405 elem
->weights
[cnt
].cnt
=
1406 collate
->ellipsis_weight
.weights
[cnt
].cnt
;
1415 collate_startup (struct linereader
*ldfile
, struct localedef_t
*locale
,
1416 struct localedef_t
*copy_locale
, int ignore_content
)
1418 if (!ignore_content
&& locale
->categories
[LC_COLLATE
].collate
== NULL
)
1420 struct locale_collate_t
*collate
;
1422 if (copy_locale
== NULL
)
1424 collate
= locale
->categories
[LC_COLLATE
].collate
=
1425 (struct locale_collate_t
*)
1426 xcalloc (1, sizeof (struct locale_collate_t
));
1428 /* Init the various data structures. */
1429 init_hash (&collate
->elem_table
, 100);
1430 init_hash (&collate
->sym_table
, 100);
1431 init_hash (&collate
->seq_table
, 500);
1432 obstack_init (&collate
->mempool
);
1434 collate
->col_weight_max
= -1;
1437 collate
= locale
->categories
[LC_COLLATE
].collate
=
1438 copy_locale
->categories
[LC_COLLATE
].collate
;
1441 ldfile
->translate_strings
= 0;
1442 ldfile
->return_widestr
= 0;
1447 collate_finish (struct localedef_t
*locale
, struct charmap_t
*charmap
)
1449 /* Now is the time when we can assign the individual collation
1450 values for all the symbols. We have possibly different values
1451 for the wide- and the multibyte-character symbols. This is done
1452 since it might make a difference in the encoding if there is in
1453 some cases no multibyte-character but there are wide-characters.
1454 (The other way around it is not important since theencoded
1455 collation value in the wide-character case is 32 bits wide and
1456 therefore requires no encoding).
1458 The lowest collation value assigned is 2. Zero is reserved for
1459 the NUL byte terminating the strings in the `strxfrm'/`wcsxfrm'
1460 functions and 1 is used to separate the individual passes for the
1463 We also have to construct is list with all the bytes/words which
1464 can come first in a sequence, followed by all the elements which
1465 also start with this byte/word. The order is reverse which has
1466 among others the important effect that longer strings are located
1467 first in the list. This is required for the output data since
1468 the algorithm used in `strcoll' etc depends on this.
1470 The multibyte case is easy. We simply sort into an array with
1472 struct locale_collate_t
*collate
= locale
->categories
[LC_COLLATE
].collate
;
1477 struct element_t
*runp
;
1479 int need_undefined
= 0;
1480 struct section_list
*sect
;
1482 int nr_wide_elems
= 0;
1484 if (collate
== NULL
)
1486 /* No data, no check. */
1488 error (0, 0, _("No definition for %s category found"), "LC_COLLATE");
1492 /* If this assertion is hit change the type in `element_t'. */
1493 assert (nrules
<= sizeof (runp
->used_in_level
) * 8);
1495 /* Make sure that the `position' rule is used either in all sections
1497 for (i
= 0; i
< nrules
; ++i
)
1498 for (sect
= collate
->sections
; sect
!= NULL
; sect
= sect
->next
)
1499 if (sect
->rules
!= NULL
1500 && ((sect
->rules
[i
] & sort_position
)
1501 != (collate
->sections
->rules
[i
] & sort_position
)))
1504 %s: `position' must be used for a specific level in all sections or none"),
1509 /* Find out which elements are used at which level. At the same
1510 time we find out whether we have any undefined symbols. */
1511 runp
= collate
->start
;
1512 while (runp
!= NULL
)
1514 if (runp
->mbs
!= NULL
)
1516 for (i
= 0; i
< nrules
; ++i
)
1520 for (j
= 0; j
< runp
->weights
[i
].cnt
; ++j
)
1521 /* A NULL pointer as the weight means IGNORE. */
1522 if (runp
->weights
[i
].w
[j
] != NULL
)
1524 if (runp
->weights
[i
].w
[j
]->weights
== NULL
)
1526 error_at_line (0, 0, runp
->file
, runp
->line
,
1527 _("symbol `%s' not defined"),
1528 runp
->weights
[i
].w
[j
]->name
);
1531 runp
->weights
[i
].w
[j
] = &collate
->undefined
;
1534 /* Set the bit for the level. */
1535 runp
->weights
[i
].w
[j
]->used_in_level
|= 1 << i
;
1540 /* Up to the next entry. */
1544 /* Walk through the list of defined sequences and assign weights. Also
1545 create the data structure which will allow generating the single byte
1546 character based tables.
1548 Since at each time only the weights for each of the rules are
1549 only compared to other weights for this rule it is possible to
1550 assign more compact weight values than simply counting all
1551 weights in sequence. We can assign weights from 3, one for each
1552 rule individually and only for those elements, which are actually
1555 Why is this important? It is not for the wide char table. But
1556 it is for the singlebyte output since here larger numbers have to
1557 be encoded to make it possible to emit the value as a byte
1559 for (i
= 0; i
< nrules
; ++i
)
1564 runp
= collate
->start
;
1565 while (runp
!= NULL
)
1567 /* Determine the order. */
1568 if (runp
->used_in_level
!= 0)
1570 runp
->mborder
= (int *) obstack_alloc (&collate
->mempool
,
1571 nrules
* sizeof (int));
1573 for (i
= 0; i
< nrules
; ++i
)
1574 if ((runp
->used_in_level
& (1 << i
)) != 0)
1575 runp
->mborder
[i
] = mbact
[i
]++;
1577 runp
->mborder
[i
] = 0;
1580 if (runp
->mbs
!= NULL
)
1582 struct element_t
**eptr
;
1583 struct element_t
*lastp
= NULL
;
1585 /* Find the point where to insert in the list. */
1586 eptr
= &collate
->mbheads
[((unsigned char *) runp
->mbs
)[0]];
1587 while (*eptr
!= NULL
)
1589 if ((*eptr
)->nmbs
< runp
->nmbs
)
1592 if ((*eptr
)->nmbs
== runp
->nmbs
)
1594 int c
= memcmp ((*eptr
)->mbs
, runp
->mbs
, runp
->nmbs
);
1598 /* This should not happen. It means that we have
1599 to symbols with the same byte sequence. It is
1600 of course an error. */
1601 error_at_line (0, 0, (*eptr
)->file
, (*eptr
)->line
,
1602 _("symbol `%s' has the same encoding as"),
1604 error_at_line (0, 0, runp
->file
, runp
->line
,
1605 _("symbol `%s'"), runp
->name
);
1609 /* Insert it here. */
1613 /* To the next entry. */
1615 eptr
= &(*eptr
)->mbnext
;
1618 /* Set the pointers. */
1619 runp
->mbnext
= *eptr
;
1620 runp
->mblast
= lastp
;
1622 (*eptr
)->mblast
= runp
;
1627 if (runp
->used_in_level
)
1629 runp
->wcorder
= wcact
++;
1631 /* We take the opportunity to count the elements which have
1636 if (runp
->is_character
)
1638 if (runp
->nmbs
== 1)
1639 collate
->mbseqorder
[((unsigned char *) runp
->mbs
)[0]] = mbseqact
++;
1641 runp
->wcseqorder
= wcseqact
++;
1644 /* Up to the next entry. */
1648 /* Find out whether any of the `mbheads' entries is unset. In this
1649 case we use the UNDEFINED entry. */
1650 for (i
= 1; i
< 256; ++i
)
1651 if (collate
->mbheads
[i
] == NULL
)
1654 collate
->mbheads
[i
] = &collate
->undefined
;
1657 /* Now to the wide character case. */
1658 collate
->wcheads
.p
= 6;
1659 collate
->wcheads
.q
= 10;
1660 wchead_table_init (&collate
->wcheads
);
1662 collate
->wcseqorder
.p
= 6;
1663 collate
->wcseqorder
.q
= 10;
1664 collseq_table_init (&collate
->wcseqorder
);
1667 runp
= collate
->start
;
1668 while (runp
!= NULL
)
1670 if (runp
->wcs
!= NULL
)
1672 struct element_t
*e
;
1673 struct element_t
**eptr
;
1674 struct element_t
*lastp
;
1676 /* Insert the collation sequence value. */
1677 collseq_table_add (&collate
->wcseqorder
, runp
->wcs
[0],
1680 /* Find the point where to insert in the list. */
1681 e
= wchead_table_get (&collate
->wcheads
, runp
->wcs
[0]);
1684 while (*eptr
!= NULL
)
1686 if ((*eptr
)->nwcs
< runp
->nwcs
)
1689 if ((*eptr
)->nwcs
== runp
->nwcs
)
1691 int c
= wmemcmp ((wchar_t *) (*eptr
)->wcs
,
1692 (wchar_t *) runp
->wcs
, runp
->nwcs
);
1696 /* This should not happen. It means that we have
1697 two symbols with the same byte sequence. It is
1698 of course an error. */
1699 error_at_line (0, 0, (*eptr
)->file
, (*eptr
)->line
,
1700 _("symbol `%s' has the same encoding as"),
1702 error_at_line (0, 0, runp
->file
, runp
->line
,
1703 _("symbol `%s'"), runp
->name
);
1707 /* Insert it here. */
1711 /* To the next entry. */
1713 eptr
= &(*eptr
)->wcnext
;
1716 /* Set the pointers. */
1717 runp
->wcnext
= *eptr
;
1718 runp
->wclast
= lastp
;
1720 (*eptr
)->wclast
= runp
;
1723 wchead_table_add (&collate
->wcheads
, runp
->wcs
[0], e
);
1727 /* Up to the next entry. */
1731 collseq_table_finalize (&collate
->wcseqorder
);
1733 /* Now determine whether the UNDEFINED entry is needed and if yes,
1734 whether it was defined. */
1735 collate
->undefined
.used_in_level
= need_undefined
? ~0ul : 0;
1736 if (collate
->undefined
.file
== NULL
)
1740 /* This seems not to be enforced by recent standards. Don't
1741 emit an error, simply append UNDEFINED at the end. */
1743 error (0, 0, _("no definition of `UNDEFINED'"));
1745 /* Add UNDEFINED at the end. */
1746 collate
->undefined
.mborder
=
1747 (int *) obstack_alloc (&collate
->mempool
, nrules
* sizeof (int));
1749 for (i
= 0; i
< nrules
; ++i
)
1750 collate
->undefined
.mborder
[i
] = mbact
[i
]++;
1753 /* In any case we will need the definition for the wide character
1754 case. But we will not complain that it is missing since the
1755 specification strangely enough does not seem to account for
1757 collate
->undefined
.wcorder
= wcact
++;
1760 /* Finally, try to unify the rules for the sections. Whenever the rules
1761 for a section are the same as those for another section give the
1762 ruleset the same index. Since there are never many section we can
1763 use an O(n^2) algorithm here. */
1764 sect
= collate
->sections
;
1765 while (sect
!= NULL
&& sect
->rules
== NULL
)
1767 assert (sect
!= NULL
);
1771 struct section_list
*osect
= collate
->sections
;
1773 while (osect
!= sect
)
1774 if (osect
->rules
!= NULL
1775 && memcmp (osect
->rules
, sect
->rules
, nrules
) == 0)
1778 osect
= osect
->next
;
1781 sect
->ruleidx
= ruleidx
++;
1783 sect
->ruleidx
= osect
->ruleidx
;
1788 while (sect
!= NULL
&& sect
->rules
== NULL
);
1790 while (sect
!= NULL
);
1791 /* We are currently not prepared for more than 256 rulesets. But this
1792 should never really be a problem. */
1793 assert (ruleidx
<= 256);
1798 output_weight (struct obstack
*pool
, struct locale_collate_t
*collate
,
1799 struct element_t
*elem
)
1804 /* Optimize the use of UNDEFINED. */
1805 if (elem
== &collate
->undefined
)
1806 /* The weights are already inserted. */
1809 /* This byte can start exactly one collation element and this is
1810 a single byte. We can directly give the index to the weights. */
1811 retval
= obstack_object_size (pool
);
1813 /* Construct the weight. */
1814 for (cnt
= 0; cnt
< nrules
; ++cnt
)
1816 char buf
[elem
->weights
[cnt
].cnt
* 7];
1820 for (i
= 0; i
< elem
->weights
[cnt
].cnt
; ++i
)
1821 /* Encode the weight value. We do nothing for IGNORE entries. */
1822 if (elem
->weights
[cnt
].w
[i
] != NULL
)
1823 len
+= utf8_encode (&buf
[len
],
1824 elem
->weights
[cnt
].w
[i
]->mborder
[cnt
]);
1826 /* And add the buffer content. */
1827 obstack_1grow (pool
, len
);
1828 obstack_grow (pool
, buf
, len
);
1831 return retval
| ((elem
->section
->ruleidx
& 0x7f) << 24);
1836 output_weightwc (struct obstack
*pool
, struct locale_collate_t
*collate
,
1837 struct element_t
*elem
)
1842 /* Optimize the use of UNDEFINED. */
1843 if (elem
== &collate
->undefined
)
1844 /* The weights are already inserted. */
1847 /* This byte can start exactly one collation element and this is
1848 a single byte. We can directly give the index to the weights. */
1849 retval
= obstack_object_size (pool
) / sizeof (int32_t);
1851 /* Construct the weight. */
1852 for (cnt
= 0; cnt
< nrules
; ++cnt
)
1854 int32_t buf
[elem
->weights
[cnt
].cnt
];
1858 for (i
= 0, j
= 0; i
< elem
->weights
[cnt
].cnt
; ++i
)
1859 if (elem
->weights
[cnt
].w
[i
] != NULL
)
1860 buf
[j
++] = elem
->weights
[cnt
].w
[i
]->wcorder
;
1862 /* And add the buffer content. */
1863 obstack_int32_grow (pool
, j
);
1865 obstack_grow (pool
, buf
, j
* sizeof (int32_t));
1868 return retval
| ((elem
->section
->ruleidx
& 0x7f) << 24);
1873 collate_output (struct localedef_t
*locale
, struct charmap_t
*charmap
,
1874 const char *output_path
)
1876 struct locale_collate_t
*collate
= locale
->categories
[LC_COLLATE
].collate
;
1877 const size_t nelems
= _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE
);
1878 struct iovec iov
[2 + nelems
];
1879 struct locale_file data
;
1880 uint32_t idx
[nelems
];
1883 int32_t tablemb
[256];
1884 struct obstack weightpool
;
1885 struct obstack extrapool
;
1886 struct obstack indirectpool
;
1887 struct section_list
*sect
;
1888 struct collidx_table tablewc
;
1890 uint32_t *elem_table
;
1892 struct element_t
*runp
;
1894 data
.magic
= LIMAGIC (LC_COLLATE
);
1896 iov
[0].iov_base
= (void *) &data
;
1897 iov
[0].iov_len
= sizeof (data
);
1899 iov
[1].iov_base
= (void *) idx
;
1900 iov
[1].iov_len
= sizeof (idx
);
1902 idx
[0] = iov
[0].iov_len
+ iov
[1].iov_len
;
1905 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_NRULES
));
1906 iov
[2 + cnt
].iov_base
= &nrules
;
1907 iov
[2 + cnt
].iov_len
= sizeof (uint32_t);
1908 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
1911 /* If we have no LC_COLLATE data emit only the number of rules as zero. */
1912 if (collate
== NULL
)
1916 while (cnt
< _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE
))
1918 /* The words have to be handled specially. */
1919 if (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_SYMB_HASH_SIZEMB
))
1921 iov
[2 + cnt
].iov_base
= &dummy
;
1922 iov
[2 + cnt
].iov_len
= sizeof (int32_t);
1926 iov
[2 + cnt
].iov_base
= NULL
;
1927 iov
[2 + cnt
].iov_len
= 0;
1930 if (cnt
+ 1 < _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE
))
1931 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
1935 assert (cnt
== _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE
));
1937 write_locale_data (output_path
, "LC_COLLATE", 2 + cnt
, iov
);
1942 obstack_init (&weightpool
);
1943 obstack_init (&extrapool
);
1944 obstack_init (&indirectpool
);
1946 /* Since we are using the sign of an integer to mark indirection the
1947 offsets in the arrays we are indirectly referring to must not be
1948 zero since -0 == 0. Therefore we add a bit of dummy content. */
1949 obstack_int32_grow (&extrapool
, 0);
1950 obstack_int32_grow (&indirectpool
, 0);
1952 /* Prepare the ruleset table. */
1953 for (sect
= collate
->sections
, i
= 0; sect
!= NULL
; sect
= sect
->next
)
1954 if (sect
->rules
!= NULL
&& sect
->ruleidx
== i
)
1958 obstack_make_room (&weightpool
, nrules
);
1960 for (j
= 0; j
< nrules
; ++j
)
1961 obstack_1grow_fast (&weightpool
, sect
->rules
[j
]);
1964 /* And align the output. */
1965 i
= (nrules
* i
) % __alignof__ (int32_t);
1968 obstack_1grow (&weightpool
, '\0');
1969 while (++i
< __alignof__ (int32_t));
1971 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_RULESETS
));
1972 iov
[2 + cnt
].iov_len
= obstack_object_size (&weightpool
);
1973 iov
[2 + cnt
].iov_base
= obstack_finish (&weightpool
);
1974 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
1977 /* Generate the 8-bit table. Walk through the lists of sequences
1978 starting with the same byte and add them one after the other to
1979 the table. In case we have more than one sequence starting with
1980 the same byte we have to use extra indirection.
1982 First add a record for the NUL byte. This entry will never be used
1983 so it does not matter. */
1986 /* Now insert the `UNDEFINED' value if it is used. Since this value
1987 will probably be used more than once it is good to store the
1988 weights only once. */
1989 if (collate
->undefined
.used_in_level
!= 0)
1990 output_weight (&weightpool
, collate
, &collate
->undefined
);
1992 for (ch
= 1; ch
< 256; ++ch
)
1993 if (collate
->mbheads
[ch
]->mbnext
== NULL
1994 && collate
->mbheads
[ch
]->nmbs
<= 1)
1996 tablemb
[ch
] = output_weight (&weightpool
, collate
,
1997 collate
->mbheads
[ch
]);
2001 /* The entries in the list are sorted by length and then
2002 alphabetically. This is the order in which we will add the
2003 elements to the collation table. This allows simply walking
2004 the table in sequence and stopping at the first matching
2005 entry. Since the longer sequences are coming first in the
2006 list they have the possibility to match first, just as it
2007 has to be. In the worst case we are walking to the end of
2008 the list where we put, if no singlebyte sequence is defined
2009 in the locale definition, the weights for UNDEFINED.
2011 To reduce the length of the search list we compress them a bit.
2012 This happens by collecting sequences of consecutive byte
2013 sequences in one entry (having and begin and end byte sequence)
2014 and add only one index into the weight table. We can find the
2015 consecutive entries since they are also consecutive in the list. */
2016 struct element_t
*runp
= collate
->mbheads
[ch
];
2017 struct element_t
*lastp
;
2019 assert ((obstack_object_size (&extrapool
)
2020 & (__alignof__ (int32_t) - 1)) == 0);
2022 tablemb
[ch
] = -obstack_object_size (&extrapool
);
2026 /* Store the current index in the weight table. We know that
2027 the current position in the `extrapool' is aligned on a
2032 /* Find out wether this is a single entry or we have more than
2033 one consecutive entry. */
2034 if (runp
->mbnext
!= NULL
2035 && runp
->nmbs
== runp
->mbnext
->nmbs
2036 && memcmp (runp
->mbs
, runp
->mbnext
->mbs
, runp
->nmbs
- 1) == 0
2037 && (runp
->mbs
[runp
->nmbs
- 1]
2038 == runp
->mbnext
->mbs
[runp
->nmbs
- 1] + 1))
2041 struct element_t
*series_startp
= runp
;
2042 struct element_t
*curp
;
2044 /* Compute how much space we will need. */
2045 added
= ((sizeof (int32_t) + 1 + 2 * (runp
->nmbs
- 1)
2046 + __alignof__ (int32_t) - 1)
2047 & ~(__alignof__ (int32_t) - 1));
2048 assert ((obstack_object_size (&extrapool
)
2049 & (__alignof__ (int32_t) - 1)) == 0);
2050 obstack_make_room (&extrapool
, added
);
2052 /* More than one consecutive entry. We mark this by having
2053 a negative index into the indirect table. */
2054 obstack_int32_grow_fast (&extrapool
,
2055 -(obstack_object_size (&indirectpool
)
2056 / sizeof (int32_t)));
2058 /* Now search first the end of the series. */
2060 runp
= runp
->mbnext
;
2061 while (runp
->mbnext
!= NULL
2062 && runp
->nmbs
== runp
->mbnext
->nmbs
2063 && memcmp (runp
->mbs
, runp
->mbnext
->mbs
,
2064 runp
->nmbs
- 1) == 0
2065 && (runp
->mbs
[runp
->nmbs
- 1]
2066 == runp
->mbnext
->mbs
[runp
->nmbs
- 1] + 1));
2068 /* Now walk backward from here to the beginning. */
2071 assert (runp
->nmbs
<= 256);
2072 obstack_1grow_fast (&extrapool
, curp
->nmbs
- 1);
2073 for (i
= 1; i
< curp
->nmbs
; ++i
)
2074 obstack_1grow_fast (&extrapool
, curp
->mbs
[i
]);
2076 /* Now find the end of the consecutive sequence and
2077 add all the indeces in the indirect pool. */
2080 weightidx
= output_weight (&weightpool
, collate
, curp
);
2081 obstack_int32_grow (&indirectpool
, weightidx
);
2083 curp
= curp
->mblast
;
2085 while (curp
!= series_startp
);
2087 /* Add the final weight. */
2088 weightidx
= output_weight (&weightpool
, collate
, curp
);
2089 obstack_int32_grow (&indirectpool
, weightidx
);
2091 /* And add the end byte sequence. Without length this
2093 for (i
= 1; i
< curp
->nmbs
; ++i
)
2094 obstack_1grow_fast (&extrapool
, curp
->mbs
[i
]);
2098 /* A single entry. Simply add the index and the length and
2099 string (except for the first character which is already
2103 /* Output the weight info. */
2104 weightidx
= output_weight (&weightpool
, collate
, runp
);
2106 added
= ((sizeof (int32_t) + 1 + runp
->nmbs
- 1
2107 + __alignof__ (int32_t) - 1)
2108 & ~(__alignof__ (int32_t) - 1));
2109 assert ((obstack_object_size (&extrapool
)
2110 & (__alignof__ (int32_t) - 1)) == 0);
2111 obstack_make_room (&extrapool
, added
);
2113 obstack_int32_grow_fast (&extrapool
, weightidx
);
2114 assert (runp
->nmbs
<= 256);
2115 obstack_1grow_fast (&extrapool
, runp
->nmbs
- 1);
2117 for (i
= 1; i
< runp
->nmbs
; ++i
)
2118 obstack_1grow_fast (&extrapool
, runp
->mbs
[i
]);
2121 /* Add alignment bytes if necessary. */
2122 while ((obstack_object_size (&extrapool
)
2123 & (__alignof__ (int32_t) - 1)) != 0)
2124 obstack_1grow_fast (&extrapool
, '\0');
2128 runp
= runp
->mbnext
;
2130 while (runp
!= NULL
);
2132 assert ((obstack_object_size (&extrapool
)
2133 & (__alignof__ (int32_t) - 1)) == 0);
2135 /* If the final entry in the list is not a single character we
2136 add an UNDEFINED entry here. */
2137 if (lastp
->nmbs
!= 1)
2139 int added
= ((sizeof (int32_t) + 1 + 1 + __alignof__ (int32_t) - 1)
2140 & ~(__alignof__ (int32_t) - 1));
2141 obstack_make_room (&extrapool
, added
);
2143 obstack_int32_grow_fast (&extrapool
, 0);
2144 /* XXX What rule? We just pick the first. */
2145 obstack_1grow_fast (&extrapool
, 0);
2146 /* Length is zero. */
2147 obstack_1grow_fast (&extrapool
, 0);
2149 /* Add alignment bytes if necessary. */
2150 while ((obstack_object_size (&extrapool
)
2151 & (__alignof__ (int32_t) - 1)) != 0)
2152 obstack_1grow_fast (&extrapool
, '\0');
2156 /* Add padding to the tables if necessary. */
2157 while ((obstack_object_size (&weightpool
) & (__alignof__ (int32_t) - 1))
2159 obstack_1grow (&weightpool
, 0);
2161 /* Now add the four tables. */
2162 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_TABLEMB
));
2163 iov
[2 + cnt
].iov_base
= tablemb
;
2164 iov
[2 + cnt
].iov_len
= sizeof (tablemb
);
2165 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2166 assert ((iov
[2 + cnt
].iov_len
& (__alignof__ (int32_t) - 1)) == 0);
2169 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_WEIGHTMB
));
2170 iov
[2 + cnt
].iov_len
= obstack_object_size (&weightpool
);
2171 iov
[2 + cnt
].iov_base
= obstack_finish (&weightpool
);
2172 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2175 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_EXTRAMB
));
2176 iov
[2 + cnt
].iov_len
= obstack_object_size (&extrapool
);
2177 iov
[2 + cnt
].iov_base
= obstack_finish (&extrapool
);
2178 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2181 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_INDIRECTMB
));
2182 iov
[2 + cnt
].iov_len
= obstack_object_size (&indirectpool
);
2183 iov
[2 + cnt
].iov_base
= obstack_finish (&indirectpool
);
2184 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2185 assert ((iov
[2 + cnt
].iov_len
& (__alignof__ (int32_t) - 1)) == 0);
2189 /* Now the same for the wide character table. We need to store some
2190 more information here. */
2191 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_GAP1
));
2192 iov
[2 + cnt
].iov_base
= NULL
;
2193 iov
[2 + cnt
].iov_len
= 0;
2194 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2195 assert (idx
[cnt
] % 4 == 0);
2198 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_GAP2
));
2199 iov
[2 + cnt
].iov_base
= NULL
;
2200 iov
[2 + cnt
].iov_len
= 0;
2201 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2202 assert (idx
[cnt
] % 4 == 0);
2205 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_GAP3
));
2206 iov
[2 + cnt
].iov_base
= NULL
;
2207 iov
[2 + cnt
].iov_len
= 0;
2208 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2209 assert (idx
[cnt
] % 4 == 0);
2212 /* Since we are using the sign of an integer to mark indirection the
2213 offsets in the arrays we are indirectly referring to must not be
2214 zero since -0 == 0. Therefore we add a bit of dummy content. */
2215 obstack_int32_grow (&extrapool
, 0);
2216 obstack_int32_grow (&indirectpool
, 0);
2218 /* Now insert the `UNDEFINED' value if it is used. Since this value
2219 will probably be used more than once it is good to store the
2220 weights only once. */
2221 if (output_weightwc (&weightpool
, collate
, &collate
->undefined
) != 0)
2224 /* Generate the table. Walk through the lists of sequences starting
2225 with the same wide character and add them one after the other to
2226 the table. In case we have more than one sequence starting with
2227 the same byte we have to use extra indirection. */
2229 void add_to_tablewc (uint32_t ch
, struct element_t
*runp
)
2231 if (runp
->wcnext
== NULL
&& runp
->nwcs
== 1)
2233 int32_t weigthidx
= output_weightwc (&weightpool
, collate
, runp
);
2234 collidx_table_add (&tablewc
, ch
, weigthidx
);
2238 /* As for the singlebyte table, we recognize sequences and
2240 struct element_t
*lastp
;
2242 collidx_table_add (&tablewc
, ch
,
2243 -(obstack_object_size (&extrapool
) / sizeof (uint32_t)));
2247 /* Store the current index in the weight table. We know that
2248 the current position in the `extrapool' is aligned on a
2253 /* Find out wether this is a single entry or we have more than
2254 one consecutive entry. */
2255 if (runp
->wcnext
!= NULL
2256 && runp
->nwcs
== runp
->wcnext
->nwcs
2257 && wmemcmp ((wchar_t *) runp
->wcs
,
2258 (wchar_t *)runp
->wcnext
->wcs
,
2259 runp
->nwcs
- 1) == 0
2260 && (runp
->wcs
[runp
->nwcs
- 1]
2261 == runp
->wcnext
->wcs
[runp
->nwcs
- 1] + 1))
2264 struct element_t
*series_startp
= runp
;
2265 struct element_t
*curp
;
2267 /* Now add first the initial byte sequence. */
2268 added
= (1 + 1 + 2 * (runp
->nwcs
- 1)) * sizeof (int32_t);
2269 if (sizeof (int32_t) == sizeof (int))
2270 obstack_make_room (&extrapool
, added
);
2272 /* More than one consecutive entry. We mark this by having
2273 a negative index into the indirect table. */
2274 obstack_int32_grow_fast (&extrapool
,
2275 -(obstack_object_size (&indirectpool
)
2276 / sizeof (int32_t)));
2277 obstack_int32_grow_fast (&extrapool
, runp
->nwcs
- 1);
2280 runp
= runp
->wcnext
;
2281 while (runp
->wcnext
!= NULL
2282 && runp
->nwcs
== runp
->wcnext
->nwcs
2283 && wmemcmp ((wchar_t *) runp
->wcs
,
2284 (wchar_t *)runp
->wcnext
->wcs
,
2285 runp
->nwcs
- 1) == 0
2286 && (runp
->wcs
[runp
->nwcs
- 1]
2287 == runp
->wcnext
->wcs
[runp
->nwcs
- 1] + 1));
2289 /* Now walk backward from here to the beginning. */
2292 for (i
= 1; i
< runp
->nwcs
; ++i
)
2293 obstack_int32_grow_fast (&extrapool
, curp
->wcs
[i
]);
2295 /* Now find the end of the consecutive sequence and
2296 add all the indeces in the indirect pool. */
2299 weightidx
= output_weightwc (&weightpool
, collate
,
2301 obstack_int32_grow (&indirectpool
, weightidx
);
2303 curp
= curp
->wclast
;
2305 while (curp
!= series_startp
);
2307 /* Add the final weight. */
2308 weightidx
= output_weightwc (&weightpool
, collate
, curp
);
2309 obstack_int32_grow (&indirectpool
, weightidx
);
2311 /* And add the end byte sequence. Without length this
2313 for (i
= 1; i
< curp
->nwcs
; ++i
)
2314 obstack_int32_grow (&extrapool
, curp
->wcs
[i
]);
2318 /* A single entry. Simply add the index and the length and
2319 string (except for the first character which is already
2323 /* Output the weight info. */
2324 weightidx
= output_weightwc (&weightpool
, collate
, runp
);
2326 added
= (1 + 1 + runp
->nwcs
- 1) * sizeof (int32_t);
2327 if (sizeof (int) == sizeof (int32_t))
2328 obstack_make_room (&extrapool
, added
);
2330 obstack_int32_grow_fast (&extrapool
, weightidx
);
2331 obstack_int32_grow_fast (&extrapool
, runp
->nwcs
- 1);
2332 for (i
= 1; i
< runp
->nwcs
; ++i
)
2333 obstack_int32_grow_fast (&extrapool
, runp
->wcs
[i
]);
2338 runp
= runp
->wcnext
;
2340 while (runp
!= NULL
);
2346 collidx_table_init (&tablewc
);
2348 wchead_table_iterate (&collate
->wcheads
, add_to_tablewc
);
2350 collidx_table_finalize (&tablewc
);
2353 /* Now add the four tables. */
2354 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_TABLEWC
));
2355 iov
[2 + cnt
].iov_base
= tablewc
.result
;
2356 iov
[2 + cnt
].iov_len
= tablewc
.result_size
;
2357 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2358 assert (iov
[2 + cnt
].iov_len
% sizeof (int32_t) == 0);
2359 assert (idx
[cnt
] % 4 == 0);
2362 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_WEIGHTWC
));
2363 iov
[2 + cnt
].iov_len
= obstack_object_size (&weightpool
);
2364 iov
[2 + cnt
].iov_base
= obstack_finish (&weightpool
);
2365 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2366 assert (iov
[2 + cnt
].iov_len
% sizeof (int32_t) == 0);
2367 assert (idx
[cnt
] % 4 == 0);
2370 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_EXTRAWC
));
2371 iov
[2 + cnt
].iov_len
= obstack_object_size (&extrapool
);
2372 iov
[2 + cnt
].iov_base
= obstack_finish (&extrapool
);
2373 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2374 assert (iov
[2 + cnt
].iov_len
% sizeof (int32_t) == 0);
2375 assert (idx
[cnt
] % 4 == 0);
2378 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_INDIRECTWC
));
2379 iov
[2 + cnt
].iov_len
= obstack_object_size (&indirectpool
);
2380 iov
[2 + cnt
].iov_base
= obstack_finish (&indirectpool
);
2381 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2382 assert (iov
[2 + cnt
].iov_len
% sizeof (int32_t) == 0);
2383 assert (idx
[cnt
] % 4 == 0);
2387 /* Finally write the table with collation element names out. It is
2388 a hash table with a simple function which gets the name of the
2389 character as the input. One character might have many names. The
2390 value associated with the name is an index into the weight table
2391 where we are then interested in the first-level weight value.
2393 To determine how large the table should be we are counting the
2394 elements have to put in. Since we are using internal chaining
2395 using a secondary hash function we have to make the table a bit
2396 larger to avoid extremely long search times. We can achieve
2397 good results with a 40% larger table than there are entries. */
2399 runp
= collate
->start
;
2400 while (runp
!= NULL
)
2402 if (runp
->mbs
!= NULL
&& runp
->weights
!= NULL
)
2403 /* Yep, the element really counts. */
2408 /* Add 40% and find the next prime number. */
2409 elem_size
= MIN (next_prime (elem_size
* 1.4), 257);
2411 /* Allocate the table. Each entry consists of two words: the hash
2412 value and an index in a secondary table which provides the index
2413 into the weight table and the string itself (so that a match can
2415 elem_table
= (uint32_t *) obstack_alloc (&extrapool
,
2416 elem_size
* 2 * sizeof (uint32_t));
2417 memset (elem_table
, '\0', elem_size
* 2 * sizeof (uint32_t));
2419 /* Now add the elements. */
2420 runp
= collate
->start
;
2421 while (runp
!= NULL
)
2423 if (runp
->mbs
!= NULL
&& runp
->weights
!= NULL
)
2425 /* Compute the hash value of the name. */
2426 uint32_t namelen
= strlen (runp
->name
);
2427 uint32_t hash
= elem_hash (runp
->name
, namelen
);
2428 size_t idx
= hash
% elem_size
;
2430 if (elem_table
[idx
* 2] != 0)
2432 /* The spot is already take. Try iterating using the value
2433 from the secondary hashing function. */
2434 size_t iter
= hash
% (elem_size
- 2);
2439 if (idx
>= elem_size
)
2442 while (elem_table
[idx
* 2] != 0);
2444 /* This is the spot where we will insert the value. */
2445 elem_table
[idx
* 2] = hash
;
2446 elem_table
[idx
* 2 + 1] = obstack_object_size (&extrapool
);
2448 /* The the string itself including length. */
2449 obstack_1grow (&extrapool
, namelen
);
2450 obstack_grow (&extrapool
, runp
->name
, namelen
);
2452 /* And the multibyte representation. */
2453 obstack_1grow (&extrapool
, runp
->nmbs
);
2454 obstack_grow (&extrapool
, runp
->mbs
, runp
->nmbs
);
2456 /* And align again to 32 bits. */
2457 if ((1 + namelen
+ 1 + runp
->nmbs
) % sizeof (int32_t) != 0)
2458 obstack_grow (&extrapool
, "\0\0",
2460 - ((1 + namelen
+ 1 + runp
->nmbs
)
2461 % sizeof (int32_t))));
2463 /* Now some 32-bit values: multibyte collation sequence,
2464 wide char string (including length), and wide char
2465 collation sequence. */
2466 obstack_int32_grow (&extrapool
, runp
->mbseqorder
);
2468 obstack_int32_grow (&extrapool
, runp
->nwcs
);
2469 obstack_grow (&extrapool
, runp
->wcs
,
2470 runp
->nwcs
* sizeof (uint32_t));
2472 obstack_int32_grow (&extrapool
, runp
->wcseqorder
);
2479 /* Prepare to write out this data. */
2480 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_SYMB_HASH_SIZEMB
));
2481 iov
[2 + cnt
].iov_base
= &elem_size
;
2482 iov
[2 + cnt
].iov_len
= sizeof (int32_t);
2483 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2484 assert (idx
[cnt
] % 4 == 0);
2487 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_SYMB_TABLEMB
));
2488 iov
[2 + cnt
].iov_base
= elem_table
;
2489 iov
[2 + cnt
].iov_len
= elem_size
* 2 * sizeof (int32_t);
2490 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2491 assert (idx
[cnt
] % 4 == 0);
2494 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_SYMB_EXTRAMB
));
2495 iov
[2 + cnt
].iov_len
= obstack_object_size (&extrapool
);
2496 iov
[2 + cnt
].iov_base
= obstack_finish (&extrapool
);
2497 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2500 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_COLLSEQMB
));
2501 iov
[2 + cnt
].iov_base
= collate
->mbseqorder
;
2502 iov
[2 + cnt
].iov_len
= 256;
2503 idx
[1 + cnt
] = idx
[cnt
] + iov
[2 + cnt
].iov_len
;
2506 assert (cnt
== _NL_ITEM_INDEX (_NL_COLLATE_COLLSEQWC
));
2507 iov
[2 + cnt
].iov_base
= collate
->wcseqorder
.result
;
2508 iov
[2 + cnt
].iov_len
= collate
->wcseqorder
.result_size
;
2509 assert (idx
[cnt
] % 4 == 0);
2512 assert (cnt
== _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE
));
2514 write_locale_data (output_path
, "LC_COLLATE", 2 + cnt
, iov
);
2516 obstack_free (&weightpool
, NULL
);
2517 obstack_free (&extrapool
, NULL
);
2518 obstack_free (&indirectpool
, NULL
);
2523 collate_read (struct linereader
*ldfile
, struct localedef_t
*result
,
2524 struct charmap_t
*charmap
, const char *repertoire_name
,
2527 struct repertoire_t
*repertoire
= NULL
;
2528 struct locale_collate_t
*collate
;
2530 struct token
*arg
= NULL
;
2531 enum token_t nowtok
;
2533 enum token_t was_ellipsis
= tok_none
;
2534 struct localedef_t
*copy_locale
= NULL
;
2536 /* Get the repertoire we have to use. */
2537 if (repertoire_name
!= NULL
)
2538 repertoire
= repertoire_read (repertoire_name
);
2540 /* The rest of the line containing `LC_COLLATE' must be free. */
2541 lr_ignore_rest (ldfile
, 1);
2545 now
= lr_token (ldfile
, charmap
, NULL
);
2548 while (nowtok
== tok_eol
);
2550 if (nowtok
== tok_copy
)
2553 now
= lr_token (ldfile
, charmap
, NULL
);
2554 if (now
->tok
!= tok_string
)
2556 SYNTAX_ERROR (_("%s: syntax error"), "LC_COLLATE");
2560 now
= lr_token (ldfile
, charmap
, NULL
);
2561 while (now
->tok
!= tok_eof
&& now
->tok
!= tok_end
);
2563 if (now
->tok
!= tok_eof
2564 || (now
= lr_token (ldfile
, charmap
, NULL
), now
->tok
== tok_eof
))
2565 lr_error (ldfile
, _("%s: premature end of file"), "LC_COLLATE");
2566 else if (now
->tok
!= tok_lc_collate
)
2568 lr_error (ldfile
, _("\
2569 %1$s: definition does not end with `END %1$s'"), "LC_COLLATE");
2570 lr_ignore_rest (ldfile
, 0);
2573 lr_ignore_rest (ldfile
, 1);
2578 if (! ignore_content
)
2580 /* Get the locale definition. */
2581 copy_locale
= load_locale (LC_COLLATE
, now
->val
.str
.startmb
,
2582 repertoire_name
, charmap
, NULL
);
2583 if ((copy_locale
->avail
& COLLATE_LOCALE
) == 0)
2585 /* Not yet loaded. So do it now. */
2586 if (locfile_read (copy_locale
, charmap
) != 0)
2591 lr_ignore_rest (ldfile
, 1);
2593 now
= lr_token (ldfile
, charmap
, NULL
);
2597 /* Prepare the data structures. */
2598 collate_startup (ldfile
, result
, copy_locale
, ignore_content
);
2599 collate
= result
->categories
[LC_COLLATE
].collate
;
2607 /* Of course we don't proceed beyond the end of file. */
2608 if (nowtok
== tok_eof
)
2611 /* Ingore empty lines. */
2612 if (nowtok
== tok_eol
)
2614 now
= lr_token (ldfile
, charmap
, NULL
);
2622 /* Allow copying other locales. */
2623 now
= lr_token (ldfile
, charmap
, NULL
);
2624 if (now
->tok
!= tok_string
)
2627 if (! ignore_content
)
2628 load_locale (LC_COLLATE
, now
->val
.str
.startmb
, repertoire_name
,
2631 lr_ignore_rest (ldfile
, 1);
2634 case tok_coll_weight_max
:
2635 /* Ignore the rest of the line if we don't need the input of
2639 lr_ignore_rest (ldfile
, 0);
2646 arg
= lr_token (ldfile
, charmap
, NULL
);
2647 if (arg
->tok
!= tok_number
)
2649 if (collate
->col_weight_max
!= -1)
2650 lr_error (ldfile
, _("%s: duplicate definition of `%s'"),
2651 "LC_COLLATE", "col_weight_max");
2653 collate
->col_weight_max
= arg
->val
.num
;
2654 lr_ignore_rest (ldfile
, 1);
2657 case tok_section_symbol
:
2658 /* Ignore the rest of the line if we don't need the input of
2662 lr_ignore_rest (ldfile
, 0);
2669 arg
= lr_token (ldfile
, charmap
, repertoire
);
2670 if (arg
->tok
!= tok_bsymbol
)
2672 else if (!ignore_content
)
2674 /* Check whether this section is already known. */
2675 struct section_list
*known
= collate
->sections
;
2676 while (known
!= NULL
)
2678 if (strcmp (known
->name
, arg
->val
.str
.startmb
) == 0)
2680 known
= known
->next
;
2686 _("%s: duplicate declaration of section `%s'"),
2687 "LC_COLLATE", arg
->val
.str
.startmb
);
2688 free (arg
->val
.str
.startmb
);
2691 collate
->sections
= make_seclist_elem (collate
,
2692 arg
->val
.str
.startmb
,
2695 lr_ignore_rest (ldfile
, known
== NULL
);
2699 free (arg
->val
.str
.startmb
);
2700 lr_ignore_rest (ldfile
, 0);
2704 case tok_collating_element
:
2705 /* Ignore the rest of the line if we don't need the input of
2709 lr_ignore_rest (ldfile
, 0);
2716 arg
= lr_token (ldfile
, charmap
, repertoire
);
2717 if (arg
->tok
!= tok_bsymbol
)
2721 const char *symbol
= arg
->val
.str
.startmb
;
2722 size_t symbol_len
= arg
->val
.str
.lenmb
;
2724 /* Next the `from' keyword. */
2725 arg
= lr_token (ldfile
, charmap
, repertoire
);
2726 if (arg
->tok
!= tok_from
)
2728 free ((char *) symbol
);
2732 ldfile
->return_widestr
= 1;
2733 ldfile
->translate_strings
= 1;
2735 /* Finally the string with the replacement. */
2736 arg
= lr_token (ldfile
, charmap
, repertoire
);
2738 ldfile
->return_widestr
= 0;
2739 ldfile
->translate_strings
= 0;
2741 if (arg
->tok
!= tok_string
)
2744 if (!ignore_content
&& symbol
!= NULL
)
2746 /* The name is already defined. */
2747 if (check_duplicate (ldfile
, collate
, charmap
,
2748 repertoire
, symbol
, symbol_len
))
2751 if (arg
->val
.str
.startmb
!= NULL
)
2752 insert_entry (&collate
->elem_table
, symbol
, symbol_len
,
2753 new_element (collate
,
2754 arg
->val
.str
.startmb
,
2755 arg
->val
.str
.lenmb
- 1,
2756 arg
->val
.str
.startwc
,
2757 symbol
, symbol_len
, 0));
2763 free ((char *) symbol
);
2764 if (arg
->val
.str
.startmb
!= NULL
)
2765 free (arg
->val
.str
.startmb
);
2766 if (arg
->val
.str
.startwc
!= NULL
)
2767 free (arg
->val
.str
.startwc
);
2769 lr_ignore_rest (ldfile
, 1);
2773 case tok_collating_symbol
:
2774 /* Ignore the rest of the line if we don't need the input of
2778 lr_ignore_rest (ldfile
, 0);
2782 if (state
!= 0 && state
!= 2)
2785 arg
= lr_token (ldfile
, charmap
, repertoire
);
2786 if (arg
->tok
!= tok_bsymbol
)
2790 char *symbol
= arg
->val
.str
.startmb
;
2791 size_t symbol_len
= arg
->val
.str
.lenmb
;
2792 char *endsymbol
= NULL
;
2793 size_t endsymbol_len
= 0;
2794 enum token_t ellipsis
= tok_none
;
2796 arg
= lr_token (ldfile
, charmap
, repertoire
);
2797 if (arg
->tok
== tok_ellipsis2
|| arg
->tok
== tok_ellipsis4
)
2799 ellipsis
= arg
->tok
;
2801 arg
= lr_token (ldfile
, charmap
, repertoire
);
2802 if (arg
->tok
!= tok_bsymbol
)
2808 endsymbol
= arg
->val
.str
.startmb
;
2809 endsymbol_len
= arg
->val
.str
.lenmb
;
2811 lr_ignore_rest (ldfile
, 1);
2813 else if (arg
->tok
!= tok_eol
)
2819 if (!ignore_content
)
2822 || (ellipsis
!= tok_none
&& endsymbol
== NULL
))
2824 lr_error (ldfile
, _("\
2825 %s: unknown character in collating symbol name"),
2829 else if (ellipsis
== tok_none
)
2831 /* The name is already defined. */
2832 if (check_duplicate (ldfile
, collate
, charmap
,
2833 repertoire
, symbol
, symbol_len
))
2836 insert_entry (&collate
->sym_table
, symbol
, symbol_len
,
2837 new_symbol (collate
, symbol
, symbol_len
));
2839 else if (symbol_len
!= endsymbol_len
)
2843 _("invalid names for character range"));
2848 /* Oh my, we have to handle an ellipsis. First, as
2849 usual, determine the common prefix and then
2850 convert the rest into a range. */
2852 unsigned long int from
;
2853 unsigned long int to
;
2856 for (prefixlen
= 0; prefixlen
< symbol_len
; ++prefixlen
)
2857 if (symbol
[prefixlen
] != endsymbol
[prefixlen
])
2860 /* Convert the rest into numbers. */
2861 symbol
[symbol_len
] = '\0';
2862 from
= strtoul (&symbol
[prefixlen
], &endp
,
2863 ellipsis
== tok_ellipsis2
? 16 : 10);
2865 goto col_sym_inv_range
;
2867 endsymbol
[symbol_len
] = '\0';
2868 to
= strtoul (&endsymbol
[prefixlen
], &endp
,
2869 ellipsis
== tok_ellipsis2
? 16 : 10);
2871 goto col_sym_inv_range
;
2874 goto col_sym_inv_range
;
2876 /* Now loop over all entries. */
2881 symbuf
= (char *) obstack_alloc (&collate
->mempool
,
2884 /* Create the name. */
2886 ellipsis
== tok_ellipsis2
2887 ? "%.*s%.*lX" : "%.*s%.*lX",
2888 (int) prefixlen
, symbol
,
2889 (int) (symbol_len
- prefixlen
), from
);
2891 /* The name is already defined. */
2892 if (check_duplicate (ldfile
, collate
, charmap
,
2893 repertoire
, symbuf
, symbol_len
))
2896 insert_entry (&collate
->sym_table
, symbuf
,
2898 new_symbol (collate
, symbuf
,
2901 /* Increment the counter. */
2913 if (endsymbol
!= NULL
)
2919 case tok_symbol_equivalence
:
2920 /* Ignore the rest of the line if we don't need the input of
2924 lr_ignore_rest (ldfile
, 0);
2931 arg
= lr_token (ldfile
, charmap
, repertoire
);
2932 if (arg
->tok
!= tok_bsymbol
)
2936 const char *newname
= arg
->val
.str
.startmb
;
2937 size_t newname_len
= arg
->val
.str
.lenmb
;
2938 const char *symname
;
2940 struct symbol_t
*symval
;
2942 arg
= lr_token (ldfile
, charmap
, repertoire
);
2943 if (arg
->tok
!= tok_bsymbol
)
2945 if (newname
!= NULL
)
2946 free ((char *) newname
);
2950 symname
= arg
->val
.str
.startmb
;
2951 symname_len
= arg
->val
.str
.lenmb
;
2953 if (newname
== NULL
)
2955 lr_error (ldfile
, _("\
2956 %s: unknown character in equivalent definition name"),
2960 if (newname
!= NULL
)
2961 free ((char *) newname
);
2962 if (symname
!= NULL
)
2963 free ((char *) symname
);
2966 if (symname
== NULL
)
2968 lr_error (ldfile
, _("\
2969 %s: unknown character in equivalent definition value"),
2971 goto sym_equiv_free
;
2974 /* See whether the symbol name is already defined. */
2975 if (find_entry (&collate
->sym_table
, symname
, symname_len
,
2976 (void **) &symval
) != 0)
2978 lr_error (ldfile
, _("\
2979 %s: unknown symbol `%s' in equivalent definition"),
2980 "LC_COLLATE", symname
);
2984 if (insert_entry (&collate
->sym_table
,
2985 newname
, newname_len
, symval
) < 0)
2987 lr_error (ldfile
, _("\
2988 error while adding equivalent collating symbol"));
2989 goto sym_equiv_free
;
2992 free ((char *) symname
);
2994 lr_ignore_rest (ldfile
, 1);
2998 /* We get told about the scripts we know. */
2999 arg
= lr_token (ldfile
, charmap
, repertoire
);
3000 if (arg
->tok
!= tok_bsymbol
)
3004 struct section_list
*runp
= collate
->known_sections
;
3007 while (runp
!= NULL
)
3008 if (strncmp (runp
->name
, arg
->val
.str
.startmb
,
3009 arg
->val
.str
.lenmb
) == 0
3010 && runp
->name
[arg
->val
.str
.lenmb
] == '\0')
3013 runp
= runp
->def_next
;
3017 lr_error (ldfile
, _("duplicate definition of script `%s'"),
3019 lr_ignore_rest (ldfile
, 0);
3023 runp
= (struct section_list
*) xcalloc (1, sizeof (*runp
));
3024 name
= strncpy (xmalloc (arg
->val
.str
.lenmb
+ 1),
3025 arg
->val
.str
.startmb
, arg
->val
.str
.lenmb
);
3026 name
[arg
->val
.str
.lenmb
] = '\0';
3029 runp
->def_next
= collate
->known_sections
;
3030 collate
->known_sections
= runp
;
3032 lr_ignore_rest (ldfile
, 1);
3035 case tok_order_start
:
3036 /* Ignore the rest of the line if we don't need the input of
3040 lr_ignore_rest (ldfile
, 0);
3044 if (state
!= 0 && state
!= 1)
3048 /* The 14652 draft does not specify whether all `order_start' lines
3049 must contain the same number of sort-rules, but 14651 does. So
3050 we require this here as well. */
3051 arg
= lr_token (ldfile
, charmap
, repertoire
);
3052 if (arg
->tok
== tok_bsymbol
)
3054 /* This better should be a section name. */
3055 struct section_list
*sp
= collate
->known_sections
;
3057 && (sp
->name
== NULL
3058 || strncmp (sp
->name
, arg
->val
.str
.startmb
,
3059 arg
->val
.str
.lenmb
) != 0
3060 || sp
->name
[arg
->val
.str
.lenmb
] != '\0'))
3065 lr_error (ldfile
, _("\
3066 %s: unknown section name `%s'"),
3067 "LC_COLLATE", arg
->val
.str
.startmb
);
3068 /* We use the error section. */
3069 collate
->current_section
= &collate
->error_section
;
3071 if (collate
->error_section
.first
== NULL
)
3073 if (collate
->sections
== NULL
)
3074 collate
->sections
= &collate
->error_section
;
3077 sp
= collate
->sections
;
3078 while (sp
->next
!= NULL
)
3081 collate
->error_section
.next
= NULL
;
3082 sp
->next
= &collate
->error_section
;
3088 /* One should not be allowed to open the same
3090 if (sp
->first
!= NULL
)
3091 lr_error (ldfile
, _("\
3092 %s: multiple order definitions for section `%s'"),
3093 "LC_COLLATE", sp
->name
);
3096 if (collate
->current_section
== NULL
)
3097 collate
->current_section
= sp
;
3100 sp
->next
= collate
->current_section
->next
;
3101 collate
->current_section
->next
= sp
;
3105 /* Next should come the end of the line or a semicolon. */
3106 arg
= lr_token (ldfile
, charmap
, repertoire
);
3107 if (arg
->tok
== tok_eol
)
3111 /* This means we have exactly one rule: `forward'. */
3113 lr_error (ldfile
, _("\
3114 %s: invalid number of sorting rules"),
3118 sp
->rules
= obstack_alloc (&collate
->mempool
,
3119 (sizeof (enum coll_sort_rule
)
3121 for (cnt
= 0; cnt
< nrules
; ++cnt
)
3122 sp
->rules
[cnt
] = sort_forward
;
3128 /* Get the next token. */
3129 arg
= lr_token (ldfile
, charmap
, repertoire
);
3134 /* There is no section symbol. Therefore we use the unnamed
3136 collate
->current_section
= &collate
->unnamed_section
;
3138 if (collate
->unnamed_section
.first
!= NULL
)
3139 lr_error (ldfile
, _("\
3140 %s: multiple order definitions for unnamed section"),
3144 collate
->unnamed_section
.next
= collate
->sections
;
3145 collate
->sections
= &collate
->unnamed_section
;
3149 /* Now read the direction names. */
3150 read_directions (ldfile
, arg
, charmap
, repertoire
, collate
);
3152 /* From now be need the strings untranslated. */
3153 ldfile
->translate_strings
= 0;
3157 /* Ignore the rest of the line if we don't need the input of
3161 lr_ignore_rest (ldfile
, 0);
3168 /* Handle ellipsis at end of list. */
3169 if (was_ellipsis
!= tok_none
)
3171 handle_ellipsis (ldfile
, NULL
, 0, was_ellipsis
, charmap
,
3172 repertoire
, collate
);
3173 was_ellipsis
= tok_none
;
3177 lr_ignore_rest (ldfile
, 1);
3180 case tok_reorder_after
:
3181 /* Ignore the rest of the line if we don't need the input of
3185 lr_ignore_rest (ldfile
, 0);
3191 lr_error (ldfile
, _("%s: missing `order_end' keyword"),
3195 /* Handle ellipsis at end of list. */
3196 if (was_ellipsis
!= tok_none
)
3198 handle_ellipsis (ldfile
, arg
->val
.str
.startmb
,
3199 arg
->val
.str
.lenmb
, was_ellipsis
, charmap
,
3200 repertoire
, collate
);
3201 was_ellipsis
= tok_none
;
3204 else if (state
!= 2 && state
!= 3)
3208 arg
= lr_token (ldfile
, charmap
, repertoire
);
3209 if (arg
->tok
== tok_bsymbol
|| arg
->tok
== tok_ucs4
)
3211 /* Find this symbol in the sequence table. */
3215 struct element_t
*insp
;
3218 if (arg
->tok
== tok_bsymbol
)
3220 startmb
= arg
->val
.str
.startmb
;
3221 lenmb
= arg
->val
.str
.lenmb
;
3225 sprintf (ucsbuf
, "U%08X", arg
->val
.ucs4
);
3230 if (find_entry (&collate
->seq_table
, startmb
, lenmb
,
3231 (void **) &insp
) == 0)
3232 /* Yes, the symbol exists. Simply point the cursor
3234 collate
->cursor
= insp
;
3237 struct symbol_t
*symbp
;
3239 if (find_entry (&collate
->sym_table
, startmb
, lenmb
,
3240 (void **) &symbp
) == 0)
3242 if (symbp
->order
->last
!= NULL
3243 || symbp
->order
->next
!= NULL
)
3244 collate
->cursor
= symbp
->order
;
3247 /* This is a collating symbol but its position
3248 is not yet defined. */
3249 lr_error (ldfile
, _("\
3250 %s: order for collating symbol %.*s not yet defined"),
3251 "LC_COLLATE", (int) lenmb
, startmb
);
3252 collate
->cursor
= NULL
;
3256 else if (find_entry (&collate
->elem_table
, startmb
, lenmb
,
3257 (void **) &insp
) == 0)
3259 if (insp
->last
!= NULL
|| insp
->next
!= NULL
)
3260 collate
->cursor
= insp
;
3263 /* This is a collating element but its position
3264 is not yet defined. */
3265 lr_error (ldfile
, _("\
3266 %s: order for collating element %.*s not yet defined"),
3267 "LC_COLLATE", (int) lenmb
, startmb
);
3268 collate
->cursor
= NULL
;
3274 /* This is bad. The symbol after which we have to
3275 insert does not exist. */
3276 lr_error (ldfile
, _("\
3277 %s: cannot reorder after %.*s: symbol not known"),
3278 "LC_COLLATE", (int) lenmb
, startmb
);
3279 collate
->cursor
= NULL
;
3284 lr_ignore_rest (ldfile
, no_error
);
3287 /* This must not happen. */
3291 case tok_reorder_end
:
3292 /* Ignore the rest of the line if we don't need the input of
3300 lr_ignore_rest (ldfile
, 1);
3303 case tok_reorder_sections_after
:
3304 /* Ignore the rest of the line if we don't need the input of
3308 lr_ignore_rest (ldfile
, 0);
3314 lr_error (ldfile
, _("%s: missing `order_end' keyword"),
3318 /* Handle ellipsis at end of list. */
3319 if (was_ellipsis
!= tok_none
)
3321 handle_ellipsis (ldfile
, NULL
, 0, was_ellipsis
, charmap
,
3322 repertoire
, collate
);
3323 was_ellipsis
= tok_none
;
3326 else if (state
== 3)
3328 error (0, 0, _("%s: missing `reorder-end' keyword"),
3332 else if (state
!= 2 && state
!= 4)
3336 /* Get the name of the sections we are adding after. */
3337 arg
= lr_token (ldfile
, charmap
, repertoire
);
3338 if (arg
->tok
== tok_bsymbol
)
3340 /* Now find a section with this name. */
3341 struct section_list
*runp
= collate
->sections
;
3343 while (runp
!= NULL
)
3345 if (runp
->name
!= NULL
3346 && strlen (runp
->name
) == arg
->val
.str
.lenmb
3347 && memcmp (runp
->name
, arg
->val
.str
.startmb
,
3348 arg
->val
.str
.lenmb
) == 0)
3355 collate
->current_section
= runp
;
3358 /* This is bad. The section after which we have to
3359 reorder does not exist. Therefore we cannot
3360 process the whole rest of this reorder
3362 lr_error (ldfile
, _("%s: section `%.*s' not known"),
3363 "LC_COLLATE", (int) arg
->val
.str
.lenmb
,
3364 arg
->val
.str
.startmb
);
3368 lr_ignore_rest (ldfile
, 0);
3370 now
= lr_token (ldfile
, charmap
, NULL
);
3372 while (now
->tok
== tok_reorder_sections_after
3373 || now
->tok
== tok_reorder_sections_end
3374 || now
->tok
== tok_end
);
3376 /* Process the token we just saw. */
3382 /* This must not happen. */
3386 case tok_reorder_sections_end
:
3387 /* Ignore the rest of the line if we don't need the input of
3395 lr_ignore_rest (ldfile
, 1);
3400 /* Ignore the rest of the line if we don't need the input of
3404 lr_ignore_rest (ldfile
, 0);
3408 if (state
!= 0 && state
!= 1 && state
!= 3 && state
!= 5)
3411 if ((state
== 0 || state
== 5) && nowtok
== tok_ucs4
)
3414 if (nowtok
== tok_ucs4
)
3416 snprintf (ucs4buf
, sizeof (ucs4buf
), "U%08X", now
->val
.ucs4
);
3422 symstr
= arg
->val
.str
.startmb
;
3423 symlen
= arg
->val
.str
.lenmb
;
3428 /* We are outside an `order_start' region. This means
3429 we must only accept definitions of values for
3430 collation symbols since these are purely abstract
3431 values and don't need dorections associated. */
3432 struct element_t
*seqp
;
3434 if (find_entry (&collate
->seq_table
, symstr
, symlen
,
3435 (void **) &seqp
) == 0)
3437 /* It's already defined. First check whether this
3438 is really a collating symbol. */
3439 if (seqp
->is_character
)
3448 if (find_entry (&collate
->sym_table
, symstr
, symlen
,
3450 /* No collating symbol, it's an error. */
3453 /* Maybe this is the first time we define a symbol
3454 value and it is before the first actual section. */
3455 if (collate
->sections
== NULL
)
3456 collate
->sections
= collate
->current_section
=
3457 &collate
->symbol_section
;
3460 if (was_ellipsis
!= tok_none
)
3463 handle_ellipsis (ldfile
, symstr
, symlen
, was_ellipsis
,
3464 charmap
, repertoire
, collate
);
3466 /* Remember that we processed the ellipsis. */
3467 was_ellipsis
= tok_none
;
3469 /* And don't add the value a second time. */
3473 else if (state
== 3)
3475 /* It is possible that we already have this collation sequence.
3476 In this case we move the entry. */
3477 struct element_t
*seqp
;
3480 /* If the symbol after which we have to insert was not found
3481 ignore all entries. */
3482 if (collate
->cursor
== NULL
)
3484 lr_ignore_rest (ldfile
, 0);
3488 if (find_entry (&collate
->seq_table
, symstr
, symlen
,
3489 (void **) &seqp
) == 0)
3492 if (find_entry (&collate
->sym_table
, symstr
, symlen
, &sym
) == 0
3493 && (seqp
= ((struct symbol_t
*) sym
)->order
) != NULL
)
3496 if (find_entry (&collate
->elem_table
, symstr
, symlen
,
3497 (void **) &seqp
) == 0)
3500 /* Remove the entry from the old position. */
3501 if (seqp
->last
== NULL
)
3502 collate
->start
= seqp
->next
;
3504 seqp
->last
->next
= seqp
->next
;
3505 if (seqp
->next
!= NULL
)
3506 seqp
->next
->last
= seqp
->last
;
3508 /* We also have to check whether this entry is the
3509 first or last of a section. */
3510 if (seqp
->section
->first
== seqp
)
3512 if (seqp
->section
->first
== seqp
->section
->last
)
3513 /* This setion has no content anymore. */
3514 seqp
->section
->first
= seqp
->section
->last
= NULL
;
3516 seqp
->section
->first
= seqp
->next
;
3518 else if (seqp
->section
->last
== seqp
)
3519 seqp
->section
->last
= seqp
->last
;
3521 /* Now insert it in the new place. */
3522 insert_weights (ldfile
, seqp
, charmap
, repertoire
, collate
,
3527 /* Otherwise we just add a new entry. */
3529 else if (state
== 5)
3531 /* We are reordering sections. Find the named section. */
3532 struct section_list
*runp
= collate
->sections
;
3533 struct section_list
*prevp
= NULL
;
3535 while (runp
!= NULL
)
3537 if (runp
->name
!= NULL
3538 && strlen (runp
->name
) == symlen
3539 && memcmp (runp
->name
, symstr
, symlen
) == 0)
3548 lr_error (ldfile
, _("%s: section `%.*s' not known"),
3549 "LC_COLLATE", (int) symlen
, symstr
);
3550 lr_ignore_rest (ldfile
, 0);
3554 if (runp
!= collate
->current_section
)
3556 /* Remove the named section from the old place and
3557 insert it in the new one. */
3558 prevp
->next
= runp
->next
;
3560 runp
->next
= collate
->current_section
->next
;
3561 collate
->current_section
->next
= runp
;
3562 collate
->current_section
= runp
;
3565 /* Process the rest of the line which might change
3566 the collation rules. */
3567 arg
= lr_token (ldfile
, charmap
, repertoire
);
3568 if (arg
->tok
!= tok_eof
&& arg
->tok
!= tok_eol
)
3569 read_directions (ldfile
, arg
, charmap
, repertoire
,
3574 else if (was_ellipsis
!= tok_none
)
3576 /* Using the information in the `ellipsis_weight'
3577 element and this and the last value we have to handle
3578 the ellipsis now. */
3579 assert (state
== 1);
3581 handle_ellipsis (ldfile
, symstr
, symlen
, was_ellipsis
, charmap
,
3582 repertoire
, collate
);
3584 /* Remember that we processed the ellipsis. */
3585 was_ellipsis
= tok_none
;
3587 /* And don't add the value a second time. */
3591 /* Now insert in the new place. */
3592 insert_value (ldfile
, symstr
, symlen
, charmap
, repertoire
, collate
);
3596 /* Ignore the rest of the line if we don't need the input of
3600 lr_ignore_rest (ldfile
, 0);
3607 if (was_ellipsis
!= tok_none
)
3610 _("%s: cannot have `%s' as end of ellipsis range"),
3611 "LC_COLLATE", "UNDEFINED");
3613 unlink_element (collate
);
3614 was_ellipsis
= tok_none
;
3617 /* See whether UNDEFINED already appeared somewhere. */
3618 if (collate
->undefined
.next
!= NULL
3619 || (collate
->cursor
!= NULL
3620 && collate
->undefined
.next
== collate
->cursor
))
3623 _("%s: order for `%.*s' already defined at %s:%Zu"),
3624 "LC_COLLATE", 9, "UNDEFINED",
3625 collate
->undefined
.file
,
3626 collate
->undefined
.line
);
3627 lr_ignore_rest (ldfile
, 0);
3630 /* Parse the weights. */
3631 insert_weights (ldfile
, &collate
->undefined
, charmap
,
3632 repertoire
, collate
, tok_none
);
3638 /* This is the symbolic (decimal or hexadecimal) or absolute
3640 if (was_ellipsis
!= tok_none
)
3643 if (state
!= 0 && state
!= 1 && state
!= 3)
3646 was_ellipsis
= nowtok
;
3648 insert_weights (ldfile
, &collate
->ellipsis_weight
, charmap
,
3649 repertoire
, collate
, nowtok
);
3653 /* Next we assume `LC_COLLATE'. */
3654 if (!ignore_content
)
3657 /* We must either see a copy statement or have
3660 _("%s: empty category description not allowed"),
3662 else if (state
== 1)
3664 lr_error (ldfile
, _("%s: missing `order_end' keyword"),
3667 /* Handle ellipsis at end of list. */
3668 if (was_ellipsis
!= tok_none
)
3670 handle_ellipsis (ldfile
, NULL
, 0, was_ellipsis
, charmap
,
3671 repertoire
, collate
);
3672 was_ellipsis
= tok_none
;
3675 else if (state
== 3)
3676 error (0, 0, _("%s: missing `reorder-end' keyword"),
3678 else if (state
== 5)
3679 error (0, 0, _("%s: missing `reorder-sections-end' keyword"),
3682 arg
= lr_token (ldfile
, charmap
, NULL
);
3683 if (arg
->tok
== tok_eof
)
3685 if (arg
->tok
== tok_eol
)
3686 lr_error (ldfile
, _("%s: incomplete `END' line"), "LC_COLLATE");
3687 else if (arg
->tok
!= tok_lc_collate
)
3688 lr_error (ldfile
, _("\
3689 %1$s: definition does not end with `END %1$s'"), "LC_COLLATE");
3690 lr_ignore_rest (ldfile
, arg
->tok
== tok_lc_collate
);
3695 SYNTAX_ERROR (_("%s: syntax error"), "LC_COLLATE");
3698 /* Prepare for the next round. */
3699 now
= lr_token (ldfile
, charmap
, NULL
);
3703 /* When we come here we reached the end of the file. */
3704 lr_error (ldfile
, _("%s: premature end of file"), "LC_COLLATE");