1 /* Copyright (C) 1996-1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 1996.
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. */
47 #include "catgetsinfo.h"
51 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
62 struct message_list
*next
;
70 struct message_list
*messages
;
77 struct set_list
*next
;
83 struct set_list
*all_sets
;
84 struct set_list
*current_set
;
85 size_t total_messages
;
89 struct obstack mem_pool
;
93 /* If non-zero force creation of new file, not using existing one. */
96 /* Name of output file. */
97 static const char *output_name
;
99 /* Name of generated C header file. */
100 static const char *header_name
;
102 /* Name and version of program. */
103 static void print_version (FILE *stream
, struct argp_state
*state
);
104 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
108 /* Definitions of arguments for argp functions. */
109 static const struct argp_option options
[] =
111 { "header", 'H', N_("NAME"), 0,
112 N_("Create C header file NAME containing symbol definitions") },
113 { "new", OPT_NEW
, NULL
, 0,
114 N_("Do not use existing catalog, force new output file") },
115 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
116 { NULL
, 0, NULL
, 0, NULL
}
119 /* Short description of program. */
120 static const char doc
[] = N_("Generate message catalog.\
121 \vIf INPUT-FILE is -, input is read from standard input. If OUTPUT-FILE\n\
122 is -, output is written to standard output.\n");
124 /* Strings for arguments in help texts. */
125 static const char args_doc
[] = N_("\
126 -o OUTPUT-FILE [INPUT-FILE]...\n[OUTPUT-FILE [INPUT-FILE]...]");
128 /* Prototype for option handler. */
129 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
131 /* Function to print some extra text in the help message. */
132 static char *more_help (int key
, const char *text
, void *input
);
134 /* Data structure to communicate with argp functions. */
135 static struct argp argp
=
137 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
141 /* Wrapper functions with error checking for standard functions. */
142 extern void *xmalloc (size_t n
);
143 extern void *xcalloc (size_t n
, size_t s
);
144 extern void *xrealloc (void *o
, size_t n
);
145 extern char *xstrdup (const char *);
147 /* Prototypes for local functions. */
148 static void error_print (void);
149 static struct catalog
*read_input_file (struct catalog
*current
,
151 static void write_out (struct catalog
*result
, const char *output_name
,
152 const char *header_name
);
153 static struct set_list
*find_set (struct catalog
*current
, int number
);
154 static void normalize_line (const char *fname
, size_t line
, iconv_t cd
,
155 wchar_t *string
, wchar_t quote_char
,
156 wchar_t escape_char
);
157 static void read_old (struct catalog
*catalog
, const char *file_name
);
158 static int open_conversion (const char *codesetp
, iconv_t
*cd_towcp
,
159 iconv_t
*cd_tombp
, wchar_t *escape_charp
);
163 main (int argc
, char *argv
[])
165 struct catalog
*result
;
168 /* Set program name for messages. */
169 error_print_progname
= error_print
;
171 /* Set locale via LC_ALL. */
172 setlocale (LC_ALL
, "");
174 /* Set the text message domain. */
175 textdomain (PACKAGE
);
177 /* Initialize local variables. */
180 /* Parse and process arguments. */
181 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
183 /* Determine output file. */
184 if (output_name
== NULL
)
185 output_name
= remaining
< argc
? argv
[remaining
++] : "-";
187 /* Process all input files. */
188 setlocale (LC_CTYPE
, "C");
189 if (remaining
< argc
)
191 result
= read_input_file (result
, argv
[remaining
]);
192 while (++remaining
< argc
);
194 result
= read_input_file (NULL
, "-");
196 /* Write out the result. */
198 write_out (result
, output_name
, header_name
);
204 /* Handle program arguments. */
206 parse_opt (int key
, char *arg
, struct argp_state
*state
)
220 return ARGP_ERR_UNKNOWN
;
227 more_help (int key
, const char *text
, void *input
)
231 case ARGP_KEY_HELP_EXTRA
:
232 /* We print some extra information. */
233 return strdup (gettext ("\
234 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
238 return (char *) text
;
241 /* Print the version information. */
243 print_version (FILE *stream
, struct argp_state
*state
)
245 fprintf (stream
, "gencat (GNU %s) %s\n", PACKAGE
, VERSION
);
246 fprintf (stream
, gettext ("\
247 Copyright (C) %s Free Software Foundation, Inc.\n\
248 This is free software; see the source for copying conditions. There is NO\n\
249 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
251 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
255 /* The address of this function will be assigned to the hook in the
260 /* We don't want the program name to be printed in messages. Emacs'
261 compile.el does not like this. */
265 static struct catalog
*
266 read_input_file (struct catalog
*current
, const char *fname
)
274 iconv_t cd_towc
= (iconv_t
) -1;
275 iconv_t cd_tomb
= (iconv_t
) -1;
276 wchar_t escape_char
= L
'\\';
277 char *codeset
= NULL
;
279 if (strcmp (fname
, "-") == 0 || strcmp (fname
, "/dev/stdin") == 0)
282 fname
= gettext ("*standard input*");
285 fp
= fopen (fname
, "r");
288 error (0, errno
, gettext ("cannot open input file `%s'"), fname
);
292 /* If we haven't seen anything yet, allocate result structure. */
295 current
= (struct catalog
*) xcalloc (1, sizeof (*current
));
297 #define obstack_chunk_alloc malloc
298 #define obstack_chunk_free free
299 obstack_init (¤t
->mem_pool
);
301 current
->current_set
= find_set (current
, NL_SETD
);
309 wbuf
= (wchar_t *) xmalloc (wbufsize
);
315 size_t start_line
= line_number
+ 1;
322 act_len
= getline (&buf
, &len
, fp
);
327 /* It the line continued? */
328 if (buf
[act_len
- 1] == '\n')
331 continued
= buf
[act_len
- 1] == '\\';
338 /* Append to currently selected line. */
339 obstack_grow (¤t
->mem_pool
, buf
, act_len
);
343 obstack_1grow (¤t
->mem_pool
, '\0');
344 this_line
= (char *) obstack_finish (¤t
->mem_pool
);
347 if (this_line
[0] == '$')
349 if (isblank (this_line
[1]))
352 while (isblank (this_line
[cnt
]))
354 if (strncmp (&this_line
[cnt
], "codeset=", 8) != 0)
355 /* This is a comment line. Do nothing. */;
356 else if (codeset
!= NULL
)
357 /* Ignore multiple codeset. */;
362 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
366 int len
= cnt
- start
;
367 codeset
= xmalloc (len
+ 1);
368 *((char *) mempcpy (codeset
, &this_line
[start
], len
))
373 else if (strncmp (&this_line
[1], "set", 3) == 0)
375 int cnt
= sizeof ("set");
377 const char *symbol
= NULL
;
378 while (isspace (this_line
[cnt
]))
381 if (isdigit (this_line
[cnt
]))
383 set_number
= atol (&this_line
[cnt
]);
385 /* If the given number for the character set is
386 higher than any we used for symbolic set names
387 avoid clashing by using only higher numbers for
388 the following symbolic definitions. */
389 if (set_number
> current
->last_set
)
390 current
->last_set
= set_number
;
394 /* See whether it is a reasonable identifier. */
396 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
401 /* No correct character found. */
402 error_at_line (0, 0, fname
, start_line
,
403 gettext ("illegal set number"));
408 /* We have found seomthing that looks like a
409 correct identifier. */
410 struct set_list
*runp
;
412 this_line
[cnt
] = '\0';
414 symbol
= &this_line
[start
];
416 /* Test whether the identifier was already used. */
417 runp
= current
->all_sets
;
419 if (runp
->symbol
!= NULL
420 && strcmp (runp
->symbol
, symbol
) == 0)
427 /* We cannot allow duplicate identifiers for
429 error_at_line (0, 0, fname
, start_line
,
430 gettext ("duplicate set definition"));
431 error_at_line (0, 0, runp
->fname
, runp
->line
,
433 this is the first definition"));
437 /* Allocate next free message set for identifier. */
438 set_number
= ++current
->last_set
;
444 /* We found a legal set number. */
445 current
->current_set
= find_set (current
, set_number
);
448 current
->current_set
->symbol
= symbol
;
449 current
->current_set
->fname
= fname
;
450 current
->current_set
->line
= start_line
;
453 else if (strncmp (&this_line
[1], "delset", 6) == 0)
455 int cnt
= sizeof ("delset");
457 while (isspace (this_line
[cnt
]))
460 if (isdigit (this_line
[cnt
]))
462 size_t set_number
= atol (&this_line
[cnt
]);
463 struct set_list
*set
;
465 /* Mark the message set with the given number as
467 set
= find_set (current
, set_number
);
472 /* See whether it is a reasonable identifier. */
474 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
479 error_at_line (0, 0, fname
, start_line
,
480 gettext ("illegal set number"));
486 struct set_list
*runp
;
488 this_line
[cnt
] = '\0';
490 symbol
= &this_line
[start
];
492 /* We have a symbolic set name. This name must
493 appear somewhere else in the catalogs read so
496 for (runp
= current
->all_sets
; runp
!= NULL
;
499 if (strcmp (runp
->symbol
, symbol
) == 0)
506 /* Name does not exist before. */
507 error_at_line (0, 0, fname
, start_line
,
508 gettext ("unknown set `%s'"), symbol
);
512 else if (strncmp (&this_line
[1], "quote", 5) == 0)
521 cnt
= sizeof ("quote");
522 while (isspace (this_line
[cnt
]))
525 /* We need the conversion. */
526 if (cd_towc
== (iconv_t
) -1
527 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
529 /* Something is wrong. */
532 /* Yes, the quote char can be '\0'; this means no quote
533 char. The function using the information works on
534 wide characters so we have to convert it here. */
535 buf
[0] = this_line
[cnt
];
540 wbufptr
= (char *) wbuf
;
543 /* Flush the state. */
544 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
546 iconv (cd_towc
, &bufptr
, &buflen
, &wbufptr
, &wbuflen
);
547 if (buflen
!= 0 || (wchar_t *) wbufptr
!= &wbuf
[2])
548 error_at_line (0, 0, fname
, start_line
,
549 gettext ("invalid quote character"));
551 /* Use the converted wide character. */
552 current
->quote_char
= wbuf
[0];
558 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
560 this_line
[cnt
] = '\0';
561 error_at_line (0, 0, fname
, start_line
,
562 gettext ("unknown directive `%s': line ignored"),
566 else if (isalnum (this_line
[0]) || this_line
[0] == '_')
568 const char *ident
= this_line
;
569 char *line
= this_line
;
574 while (line
[0] != '\0' && !isspace (line
[0]));
576 *line
++ = '\0'; /* Terminate the identifier. */
578 /* Now we found the beginning of the message itself. */
580 if (isdigit (ident
[0]))
582 struct message_list
*runp
;
583 struct message_list
*lastp
;
585 message_number
= atoi (ident
);
587 /* Find location to insert the new message. */
588 runp
= current
->current_set
->messages
;
591 if (runp
->number
== message_number
)
600 /* Oh, oh. There is already a message with this
601 number in the message set. */
602 error_at_line (0, 0, fname
, start_line
,
603 gettext ("duplicated message number"));
604 error_at_line (0, 0, runp
->fname
, runp
->line
,
605 gettext ("this is the first definition"));
608 ident
= NULL
; /* We don't have a symbol. */
610 if (message_number
!= 0
611 && message_number
> current
->current_set
->last_message
)
612 current
->current_set
->last_message
= message_number
;
614 else if (ident
[0] != '\0')
616 struct message_list
*runp
;
617 struct message_list
*lastp
;
619 /* Test whether the symbolic name was not used for
620 another message in this message set. */
621 runp
= current
->current_set
->messages
;
624 if (runp
->symbol
!= NULL
&& strcmp (ident
, runp
->symbol
) == 0)
630 /* The name is already used. */
631 error_at_line (0, 0, fname
, start_line
, gettext ("\
632 duplicated message identifier"));
633 error_at_line (0, 0, runp
->fname
, runp
->line
,
634 gettext ("this is the first definition"));
638 /* Give the message the next unused number. */
639 message_number
= ++current
->current_set
->last_message
;
644 if (message_number
!= 0)
650 struct message_list
*newp
;
651 size_t line_len
= strlen (line
) + 1;
653 /* We need the conversion. */
654 if (cd_towc
== (iconv_t
) -1
655 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
657 /* Something is wrong. */
660 /* Convert to a wide character string. We have to
661 interpret escape sequences which will be impossible
662 without doing the conversion if the codeset of the
663 message is stateful. */
668 outbuf
= (char *) wbuf
;
671 /* Flush the state. */
672 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
674 iconv (cd_towc
, &inbuf
, &inlen
, &outbuf
, &outlen
);
677 /* The string is converted. */
678 assert (outlen
< wbufsize
);
679 assert (wbuf
[(wbufsize
- outlen
) / sizeof (wchar_t) - 1]
686 /* Something is wrong with this string, we ignore it. */
687 error_at_line (0, 0, fname
, start_line
, gettext ("\
688 invalid character: message ignored"));
692 /* The output buffer is too small. */
694 wbuf
= (wchar_t *) xrealloc (wbuf
, wbufsize
);
697 /* Strip quote characters, change escape sequences into
698 correct characters etc. */
699 normalize_line (fname
, start_line
, cd_towc
, wbuf
,
700 current
->quote_char
, escape_char
);
702 /* Now the string is free of escape sequences. Convert it
703 back into a multibyte character string. First free the
704 memory allocated for the original string. */
705 obstack_free (¤t
->mem_pool
, this_line
);
707 used
= 1; /* Yes, we use the line. */
709 /* Now fill in the new string. It should never happen that
710 the replaced string is longer than the original. */
711 inbuf
= (char *) wbuf
;
712 inlen
= (wcslen (wbuf
) + 1) * sizeof (wchar_t);
714 outlen
= obstack_room (¤t
->mem_pool
);
715 obstack_blank (¤t
->mem_pool
, outlen
);
716 this_line
= (char *) obstack_base (¤t
->mem_pool
);
719 /* Flush the state. */
720 iconv (cd_tomb
, NULL
, NULL
, NULL
, NULL
);
722 iconv (cd_tomb
, &inbuf
, &inlen
, &outbuf
, &outlen
);
725 error_at_line (0, 0, fname
, start_line
,
726 gettext ("invalid line"));
729 assert (outbuf
[-1] == '\0');
731 /* Free the memory in the obstack we don't use. */
732 obstack_blank (¤t
->mem_pool
, -(int) outlen
);
733 line
= obstack_finish (¤t
->mem_pool
);
735 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
736 newp
->number
= message_number
;
737 newp
->message
= line
;
738 /* Remember symbolic name; is NULL if no is given. */
739 newp
->symbol
= ident
;
740 /* Remember where we found the character. */
742 newp
->line
= start_line
;
744 /* Find place to insert to message. We keep them in a
745 sorted single linked list. */
746 if (current
->current_set
->messages
== NULL
747 || current
->current_set
->messages
->number
> message_number
)
749 newp
->next
= current
->current_set
->messages
;
750 current
->current_set
->messages
= newp
;
754 struct message_list
*runp
;
755 runp
= current
->current_set
->messages
;
756 while (runp
->next
!= NULL
)
757 if (runp
->next
->number
> message_number
)
761 newp
->next
= runp
->next
;
765 ++current
->total_messages
;
772 /* See whether we have any non-white space character in this
774 while (this_line
[cnt
] != '\0' && isspace (this_line
[cnt
]))
777 if (this_line
[cnt
] != '\0')
778 /* Yes, some unknown characters found. */
779 error_at_line (0, 0, fname
, start_line
,
780 gettext ("malformed line ignored"));
784 /* We can save the memory for the line if it was not used. */
786 obstack_free (¤t
->mem_pool
, this_line
);
789 /* Close the conversion modules. */
790 iconv_close (cd_towc
);
791 iconv_close (cd_tomb
);
804 write_out (struct catalog
*catalog
, const char *output_name
,
805 const char *header_name
)
807 /* Computing the "optimal" size. */
808 struct set_list
*set_run
;
809 size_t best_total
, best_size
, best_depth
;
810 size_t act_size
, act_depth
;
811 struct catalog_obj obj
;
812 struct obstack string_pool
;
815 uint32_t *array1
, *array2
;
819 /* If not otherwise told try to read file with existing
822 read_old (catalog
, output_name
);
824 /* Initialize best_size with a very high value. */
825 best_total
= best_size
= best_depth
= UINT_MAX
;
827 /* We need some start size for testing. Let's start with
828 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
830 act_size
= 1 + catalog
->total_messages
/ 5;
832 /* We determine the size of a hash table here. Because the message
833 numbers can be chosen arbitrary by the programmer we cannot use
834 the simple method of accessing the array using the message
835 number. The algorithm is based on the trivial hash function
836 NUMBER % TABLE_SIZE, where collisions are stored in a second
837 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
838 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
839 while (act_size
<= best_total
)
841 size_t deep
[act_size
];
844 memset (deep
, '\0', act_size
* sizeof (size_t));
845 set_run
= catalog
->all_sets
;
846 while (set_run
!= NULL
)
848 struct message_list
*message_run
;
850 message_run
= set_run
->messages
;
851 while (message_run
!= NULL
)
853 size_t idx
= (message_run
->number
* set_run
->number
) % act_size
;
856 if (deep
[idx
] > act_depth
)
858 act_depth
= deep
[idx
];
859 if (act_depth
* act_size
> best_total
)
862 message_run
= message_run
->next
;
864 set_run
= set_run
->next
;
867 if (act_depth
* act_size
<= best_total
)
869 /* We have found a better solution. */
870 best_total
= act_depth
* act_size
;
871 best_size
= act_size
;
872 best_depth
= act_depth
;
878 /* let's be prepared for an empty message file. */
879 if (best_size
== UINT_MAX
)
885 /* OK, now we have the size we will use. Fill in the header, build
886 the table and the second one with swapped byte order. */
887 obj
.magic
= CATGETS_MAGIC
;
888 obj
.plane_size
= best_size
;
889 obj
.plane_depth
= best_depth
;
891 /* Allocate room for all needed arrays. */
893 (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
894 memset (array1
, '\0', best_size
* best_depth
* sizeof (uint32_t) * 3);
896 = (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
897 obstack_init (&string_pool
);
899 set_run
= catalog
->all_sets
;
900 while (set_run
!= NULL
)
902 struct message_list
*message_run
;
904 message_run
= set_run
->messages
;
905 while (message_run
!= NULL
)
907 size_t idx
= (((message_run
->number
* set_run
->number
) % best_size
)
909 /* Determine collision depth. */
910 while (array1
[idx
] != 0)
911 idx
+= best_size
* 3;
913 /* Store set number, message number and pointer into string
914 space, relative to the first string. */
915 array1
[idx
+ 0] = set_run
->number
;
916 array1
[idx
+ 1] = message_run
->number
;
917 array1
[idx
+ 2] = obstack_object_size (&string_pool
);
919 /* Add current string to the continuous space containing all
921 obstack_grow0 (&string_pool
, message_run
->message
,
922 strlen (message_run
->message
));
924 message_run
= message_run
->next
;
927 set_run
= set_run
->next
;
929 strings_size
= obstack_object_size (&string_pool
);
930 strings
= obstack_finish (&string_pool
);
932 /* Compute ARRAY2 by changing the byte order. */
933 for (cnt
= 0; cnt
< best_size
* best_depth
* 3; ++cnt
)
934 array2
[cnt
] = SWAPU32 (array1
[cnt
]);
936 /* Now we can write out the whole data. */
937 if (strcmp (output_name
, "-") == 0
938 || strcmp (output_name
, "/dev/stdout") == 0)
942 fd
= creat (output_name
, 0666);
944 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
948 /* Write out header. */
949 write (fd
, &obj
, sizeof (obj
));
951 /* We always write out the little endian version of the index
953 #if __BYTE_ORDER == __LITTLE_ENDIAN
954 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
955 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
956 #elif __BYTE_ORDER == __BIG_ENDIAN
957 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
958 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
960 # error Cannot handle __BYTE_ORDER byte order
963 /* Finally write the strings. */
964 write (fd
, strings
, strings_size
);
966 if (fd
!= STDOUT_FILENO
)
969 /* If requested now write out the header file. */
970 if (header_name
!= NULL
)
975 /* Open output file. "-" or "/dev/stdout" means write to
977 if (strcmp (header_name
, "-") == 0
978 || strcmp (header_name
, "/dev/stdout") == 0)
982 fp
= fopen (header_name
, "w");
984 error (EXIT_FAILURE
, errno
,
985 gettext ("cannot open output file `%s'"), header_name
);
988 /* Iterate over all sets and all messages. */
989 set_run
= catalog
->all_sets
;
990 while (set_run
!= NULL
)
992 struct message_list
*message_run
;
994 /* If the current message set has a symbolic name write this
996 if (set_run
->symbol
!= NULL
)
997 fprintf (fp
, "%s#define %sSet %#x\t/* %s:%Zu */\n",
998 first
? "" : "\n", set_run
->symbol
, set_run
->number
- 1,
999 set_run
->fname
, set_run
->line
);
1002 message_run
= set_run
->messages
;
1003 while (message_run
!= NULL
)
1005 /* If the current message has a symbolic name write
1006 #define out. But we have to take care for the set
1007 not having a symbolic name. */
1008 if (message_run
->symbol
!= NULL
)
1010 if (set_run
->symbol
== NULL
)
1011 fprintf (fp
, "#define AutomaticSet%d%s %#x\t/* %s:%Zu */\n",
1012 set_run
->number
, message_run
->symbol
,
1013 message_run
->number
, message_run
->fname
,
1016 fprintf (fp
, "#define %s%s %#x\t/* %s:%Zu */\n",
1017 set_run
->symbol
, message_run
->symbol
,
1018 message_run
->number
, message_run
->fname
,
1022 message_run
= message_run
->next
;
1025 set_run
= set_run
->next
;
1034 static struct set_list
*
1035 find_set (struct catalog
*current
, int number
)
1037 struct set_list
*result
= current
->all_sets
;
1039 /* We must avoid set number 0 because a set of this number signals
1040 in the tables that the entry is not occupied. */
1043 while (result
!= NULL
)
1044 if (result
->number
== number
)
1047 result
= result
->next
;
1049 /* Prepare new message set. */
1050 result
= (struct set_list
*) xcalloc (1, sizeof (*result
));
1051 result
->number
= number
;
1052 result
->next
= current
->all_sets
;
1053 current
->all_sets
= result
;
1059 /* Normalize given string *in*place* by processing escape sequences
1060 and quote characters. */
1062 normalize_line (const char *fname
, size_t line
, iconv_t cd
, wchar_t *string
,
1063 wchar_t quote_char
, wchar_t escape_char
)
1066 wchar_t *rp
= string
;
1067 wchar_t *wp
= string
;
1069 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1077 while (*rp
!= L
'\0')
1078 if (*rp
== quote_char
)
1079 /* We simply end the string when we find the first time an
1080 not-escaped quote character. */
1082 else if (*rp
== escape_char
)
1085 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1086 /* This is an extension to XPG. */
1089 /* Recognize escape sequences. */
1126 number
= *rp
++ - L
'0';
1127 while (number
<= (255 / 8) && *rp
>= L
'0' && *rp
<= L
'7')
1130 number
+= *rp
++ - L
'0';
1133 cbuf
[0] = (char) number
;
1138 wcbufptr
= (char *) wcbuf
;
1139 wcbufin
= sizeof (wcbuf
);
1141 /* Flush the state. */
1142 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
1144 iconv (cd
, &cbufptr
, &cbufin
, &wcbufptr
, &wcbufin
);
1145 if (cbufptr
!= &cbuf
[2] || (wchar_t *) wcbufptr
!= &wcbuf
[2])
1146 error_at_line (0, 0, fname
, line
,
1147 gettext ("invalid escape sequence"));
1153 if (*rp
== escape_char
)
1155 *wp
++ = escape_char
;
1159 /* Simply ignore the backslash character. */;
1166 /* If we saw a quote character at the beginning we expect another
1168 if (is_quoted
&& *rp
!= quote_char
)
1169 error_at_line (0, 0, fname
, line
, gettext ("unterminated message"));
1171 /* Terminate string. */
1178 read_old (struct catalog
*catalog
, const char *file_name
)
1180 struct catalog_info old_cat_obj
;
1181 struct set_list
*set
= NULL
;
1185 old_cat_obj
.status
= closed
;
1186 old_cat_obj
.cat_name
= file_name
;
1187 old_cat_obj
.nlspath
= NULL
;
1188 __libc_lock_init (old_cat_obj
.lock
);
1190 /* Try to open catalog, but don't look through the NLSPATH. */
1191 __open_catalog (&old_cat_obj
);
1193 if (old_cat_obj
.status
!= mmapped
&& old_cat_obj
.status
!= malloced
)
1195 if (errno
== ENOENT
)
1196 /* No problem, the catalog simply does not exist. */
1199 error (EXIT_FAILURE
, errno
, gettext ("while opening old catalog file"));
1202 /* OK, we have the catalog loaded. Now read all messages and merge
1203 them. When set and message number clash for any message the new
1204 one is used. If the new one is empty it indicates that the
1205 message should be deleted. */
1206 for (cnt
= 0; cnt
< old_cat_obj
.plane_size
* old_cat_obj
.plane_depth
; ++cnt
)
1208 struct message_list
*message
, *last
;
1210 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] == 0)
1211 /* No message in this slot. */
1214 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1 != (uint32_t) last_set
)
1216 last_set
= old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1;
1217 set
= find_set (catalog
, old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1);
1221 message
= set
->messages
;
1222 while (message
!= NULL
)
1224 if ((uint32_t) message
->number
>= old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1227 message
= message
->next
;
1231 || (uint32_t) message
->number
> old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1233 /* We have found a message which is not yet in the catalog.
1234 Insert it at the right position. */
1235 struct message_list
*newp
;
1237 newp
= (struct message_list
*) xmalloc (sizeof(*newp
));
1238 newp
->number
= old_cat_obj
.name_ptr
[cnt
* 3 + 1];
1240 &old_cat_obj
.strings
[old_cat_obj
.name_ptr
[cnt
* 3 + 2]];
1243 newp
->symbol
= NULL
;
1244 newp
->next
= message
;
1247 set
->messages
= newp
;
1251 ++catalog
->total_messages
;
1253 else if (*message
->message
== '\0')
1255 /* The new empty message has overridden the old one thus
1256 "deleting" it as required. Now remove the empty remains. */
1258 set
->messages
= message
->next
;
1260 last
->next
= message
->next
;
1267 open_conversion (const char *codeset
, iconv_t
*cd_towcp
, iconv_t
*cd_tombp
,
1268 wchar_t *escape_charp
)
1277 /* If the input file does not specify the codeset use the locale's. */
1278 if (codeset
== NULL
)
1280 setlocale (LC_ALL
, "");
1281 codeset
= nl_langinfo (CODESET
);
1282 setlocale (LC_ALL
, "C");
1285 /* Get the conversion modules. */
1286 *cd_towcp
= iconv_open ("WCHAR_T", codeset
);
1287 *cd_tombp
= iconv_open (codeset
, "WCHAR_T");
1288 if (*cd_towcp
== (iconv_t
) -1 || *cd_tombp
== (iconv_t
) -1)
1290 error (0, 0, gettext ("conversion modules not available"));
1291 if (*cd_towcp
!= (iconv_t
) -1)
1292 iconv_close (*cd_towcp
);
1297 /* One special case for historical reasons is the backslash
1298 character. In some codesets the byte value 0x5c is not mapped to
1299 U005c in Unicode. These charsets then don't have a backslash
1300 character at all. Therefore we have to live with whatever the
1301 codeset provides and recognize, instead of the U005c, the character
1302 the byte value 0x5c is mapped to. */
1308 wbufptr
= (char *) wbuf
;
1309 wbufsize
= sizeof (wbuf
);
1311 iconv (*cd_towcp
, &bufptr
, &bufsize
, &wbufptr
, &wbufsize
);
1312 if (bufsize
!= 0 || wbufsize
!= 0)
1314 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1315 on with using U005c. */
1316 error (0, 0, gettext ("cannot determine escape character"));
1317 *escape_charp
= L
'\\';
1320 *escape_charp
= wbuf
[0];