Cast to __intptr_t before casting pointer to int64
[glibc.git] / catgets / gencat.c
blob5078e3c8282cfa006f53eee499154baa62adc71f
1 /* Copyright (C) 1996-2011, 2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 1996.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include <argp.h>
23 #include <assert.h>
24 #include <ctype.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <error.h>
28 #include <fcntl.h>
29 #include <iconv.h>
30 #include <langinfo.h>
31 #include <locale.h>
32 #include <libintl.h>
33 #include <limits.h>
34 #include <nl_types.h>
35 #include <obstack.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <wchar.h>
43 #include "version.h"
45 #include "catgetsinfo.h"
48 #define SWAPU32(w) \
49 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
51 struct message_list
53 int number;
54 const char *message;
56 const char *fname;
57 size_t line;
58 const char *symbol;
60 struct message_list *next;
64 struct set_list
66 int number;
67 int deleted;
68 struct message_list *messages;
69 int last_message;
71 const char *fname;
72 size_t line;
73 const char *symbol;
75 struct set_list *next;
79 struct catalog
81 struct set_list *all_sets;
82 struct set_list *current_set;
83 size_t total_messages;
84 wint_t quote_char;
85 int last_set;
87 struct obstack mem_pool;
91 /* If non-zero force creation of new file, not using existing one. */
92 static int force_new;
94 /* Name of output file. */
95 static const char *output_name;
97 /* Name of generated C header file. */
98 static const char *header_name;
100 /* Name and version of program. */
101 static void print_version (FILE *stream, struct argp_state *state);
102 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
104 #define OPT_NEW 1
106 /* Definitions of arguments for argp functions. */
107 static const struct argp_option options[] =
109 { "header", 'H', N_("NAME"), 0,
110 N_("Create C header file NAME containing symbol definitions") },
111 { "new", OPT_NEW, NULL, 0,
112 N_("Do not use existing catalog, force new output file") },
113 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
114 { NULL, 0, NULL, 0, NULL }
117 /* Short description of program. */
118 static const char doc[] = N_("Generate message catalog.\
119 \vIf INPUT-FILE is -, input is read from standard input. If OUTPUT-FILE\n\
120 is -, output is written to standard output.\n");
122 /* Strings for arguments in help texts. */
123 static const char args_doc[] = N_("\
124 -o OUTPUT-FILE [INPUT-FILE]...\n[OUTPUT-FILE [INPUT-FILE]...]");
126 /* Prototype for option handler. */
127 static error_t parse_opt (int key, char *arg, struct argp_state *state);
129 /* Function to print some extra text in the help message. */
130 static char *more_help (int key, const char *text, void *input);
132 /* Data structure to communicate with argp functions. */
133 static struct argp argp =
135 options, parse_opt, args_doc, doc, NULL, more_help
139 /* Wrapper functions with error checking for standard functions. */
140 extern void *xmalloc (size_t n)
141 __attribute_malloc__ __attribute_alloc_size (1);
142 extern void *xcalloc (size_t n, size_t s)
143 __attribute_malloc__ __attribute_alloc_size (1, 2);
144 extern void *xrealloc (void *o, size_t n)
145 __attribute_malloc__ __attribute_alloc_size (2);
146 extern char *xstrdup (const char *) __attribute_malloc__;
148 /* Prototypes for local functions. */
149 static void error_print (void);
150 static struct catalog *read_input_file (struct catalog *current,
151 const char *fname);
152 static void write_out (struct catalog *result, const char *output_name,
153 const char *header_name);
154 static struct set_list *find_set (struct catalog *current, int number);
155 static void normalize_line (const char *fname, size_t line, iconv_t cd,
156 wchar_t *string, wchar_t quote_char,
157 wchar_t escape_char);
158 static void read_old (struct catalog *catalog, const char *file_name);
159 static int open_conversion (const char *codesetp, iconv_t *cd_towcp,
160 iconv_t *cd_tombp, wchar_t *escape_charp);
164 main (int argc, char *argv[])
166 struct catalog *result;
167 int remaining;
169 /* Set program name for messages. */
170 error_print_progname = error_print;
172 /* Set locale via LC_ALL. */
173 setlocale (LC_ALL, "");
175 /* Set the text message domain. */
176 textdomain (PACKAGE);
178 /* Initialize local variables. */
179 result = NULL;
181 /* Parse and process arguments. */
182 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
184 /* Determine output file. */
185 if (output_name == NULL)
186 output_name = remaining < argc ? argv[remaining++] : "-";
188 /* Process all input files. */
189 setlocale (LC_CTYPE, "C");
190 if (remaining < argc)
192 result = read_input_file (result, argv[remaining]);
193 while (++remaining < argc);
194 else
195 result = read_input_file (NULL, "-");
197 /* Write out the result. */
198 if (result != NULL)
199 write_out (result, output_name, header_name);
201 return error_message_count != 0;
205 /* Handle program arguments. */
206 static error_t
207 parse_opt (int key, char *arg, struct argp_state *state)
209 switch (key)
211 case 'H':
212 header_name = arg;
213 break;
214 case OPT_NEW:
215 force_new = 1;
216 break;
217 case 'o':
218 output_name = arg;
219 break;
220 default:
221 return ARGP_ERR_UNKNOWN;
223 return 0;
227 static char *
228 more_help (int key, const char *text, void *input)
230 char *tp = NULL;
231 switch (key)
233 case ARGP_KEY_HELP_EXTRA:
234 /* We print some extra information. */
235 if (asprintf (&tp, gettext ("\
236 For bug reporting instructions, please see:\n\
237 %s.\n"), REPORT_BUGS_TO) < 0)
238 return NULL;
239 return tp;
240 default:
241 break;
243 return (char *) text;
246 /* Print the version information. */
247 static void
248 print_version (FILE *stream, struct argp_state *state)
250 fprintf (stream, "gencat %s%s\n", PKGVERSION, VERSION);
251 fprintf (stream, gettext ("\
252 Copyright (C) %s Free Software Foundation, Inc.\n\
253 This is free software; see the source for copying conditions. There is NO\n\
254 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
255 "), "2012");
256 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
260 /* The address of this function will be assigned to the hook in the
261 error functions. */
262 static void
263 error_print ()
265 /* We don't want the program name to be printed in messages. Emacs'
266 compile.el does not like this. */
270 static struct catalog *
271 read_input_file (struct catalog *current, const char *fname)
273 FILE *fp;
274 char *buf;
275 size_t len;
276 size_t line_number;
277 wchar_t *wbuf;
278 size_t wbufsize;
279 iconv_t cd_towc = (iconv_t) -1;
280 iconv_t cd_tomb = (iconv_t) -1;
281 wchar_t escape_char = L'\\';
282 char *codeset = NULL;
284 if (strcmp (fname, "-") == 0 || strcmp (fname, "/dev/stdin") == 0)
286 fp = stdin;
287 fname = gettext ("*standard input*");
289 else
290 fp = fopen (fname, "r");
291 if (fp == NULL)
293 error (0, errno, gettext ("cannot open input file `%s'"), fname);
294 return current;
297 /* If we haven't seen anything yet, allocate result structure. */
298 if (current == NULL)
300 current = (struct catalog *) xcalloc (1, sizeof (*current));
302 #define obstack_chunk_alloc malloc
303 #define obstack_chunk_free free
304 obstack_init (&current->mem_pool);
306 current->current_set = find_set (current, NL_SETD);
309 buf = NULL;
310 len = 0;
311 line_number = 0;
313 wbufsize = 1024;
314 wbuf = (wchar_t *) xmalloc (wbufsize);
316 while (!feof (fp))
318 int continued;
319 int used;
320 size_t start_line = line_number + 1;
321 char *this_line;
325 int act_len;
327 act_len = getline (&buf, &len, fp);
328 if (act_len <= 0)
329 break;
330 ++line_number;
332 /* It the line continued? */
333 continued = 0;
334 if (buf[act_len - 1] == '\n')
336 --act_len;
338 /* There might be more than one backslash at the end of
339 the line. Only if there is an odd number of them is
340 the line continued. */
341 if (act_len > 0 && buf[act_len - 1] == '\\')
343 int temp_act_len = act_len;
347 --temp_act_len;
348 continued = !continued;
350 while (temp_act_len > 0 && buf[temp_act_len - 1] == '\\');
352 if (continued)
353 --act_len;
357 /* Append to currently selected line. */
358 obstack_grow (&current->mem_pool, buf, act_len);
360 while (continued);
362 obstack_1grow (&current->mem_pool, '\0');
363 this_line = (char *) obstack_finish (&current->mem_pool);
365 used = 0;
366 if (this_line[0] == '$')
368 if (isblank (this_line[1]))
370 int cnt = 1;
371 while (isblank (this_line[cnt]))
372 ++cnt;
373 if (strncmp (&this_line[cnt], "codeset=", 8) != 0)
374 /* This is a comment line. Do nothing. */;
375 else if (codeset != NULL)
376 /* Ignore multiple codeset. */;
377 else
379 int start = cnt + 8;
380 cnt = start;
381 while (this_line[cnt] != '\0' && !isspace (this_line[cnt]))
382 ++cnt;
383 if (cnt != start)
385 int len = cnt - start;
386 codeset = xmalloc (len + 1);
387 *((char *) mempcpy (codeset, &this_line[start], len))
388 = '\0';
392 else if (strncmp (&this_line[1], "set", 3) == 0)
394 int cnt = sizeof ("set");
395 int set_number;
396 const char *symbol = NULL;
397 while (isspace (this_line[cnt]))
398 ++cnt;
400 if (isdigit (this_line[cnt]))
402 set_number = atol (&this_line[cnt]);
404 /* If the given number for the character set is
405 higher than any we used for symbolic set names
406 avoid clashing by using only higher numbers for
407 the following symbolic definitions. */
408 if (set_number > current->last_set)
409 current->last_set = set_number;
411 else
413 /* See whether it is a reasonable identifier. */
414 int start = cnt;
415 while (isalnum (this_line[cnt]) || this_line[cnt] == '_')
416 ++cnt;
418 if (cnt == start)
420 /* No correct character found. */
421 error_at_line (0, 0, fname, start_line,
422 gettext ("illegal set number"));
423 set_number = 0;
425 else
427 /* We have found seomthing that looks like a
428 correct identifier. */
429 struct set_list *runp;
431 this_line[cnt] = '\0';
432 used = 1;
433 symbol = &this_line[start];
435 /* Test whether the identifier was already used. */
436 runp = current->all_sets;
437 while (runp != 0)
438 if (runp->symbol != NULL
439 && strcmp (runp->symbol, symbol) == 0)
440 break;
441 else
442 runp = runp->next;
444 if (runp != NULL)
446 /* We cannot allow duplicate identifiers for
447 message sets. */
448 error_at_line (0, 0, fname, start_line,
449 gettext ("duplicate set definition"));
450 error_at_line (0, 0, runp->fname, runp->line,
451 gettext ("\
452 this is the first definition"));
453 set_number = 0;
455 else
456 /* Allocate next free message set for identifier. */
457 set_number = ++current->last_set;
461 if (set_number != 0)
463 /* We found a legal set number. */
464 current->current_set = find_set (current, set_number);
465 if (symbol != NULL)
466 used = 1;
467 current->current_set->symbol = symbol;
468 current->current_set->fname = fname;
469 current->current_set->line = start_line;
472 else if (strncmp (&this_line[1], "delset", 6) == 0)
474 int cnt = sizeof ("delset");
475 while (isspace (this_line[cnt]))
476 ++cnt;
478 if (isdigit (this_line[cnt]))
480 size_t set_number = atol (&this_line[cnt]);
481 struct set_list *set;
483 /* Mark the message set with the given number as
484 deleted. */
485 set = find_set (current, set_number);
486 set->deleted = 1;
488 else
490 /* See whether it is a reasonable identifier. */
491 int start = cnt;
492 while (isalnum (this_line[cnt]) || this_line[cnt] == '_')
493 ++cnt;
495 if (cnt == start)
496 error_at_line (0, 0, fname, start_line,
497 gettext ("illegal set number"));
498 else
500 const char *symbol;
501 struct set_list *runp;
503 this_line[cnt] = '\0';
504 used = 1;
505 symbol = &this_line[start];
507 /* We have a symbolic set name. This name must
508 appear somewhere else in the catalogs read so
509 far. */
510 for (runp = current->all_sets; runp != NULL;
511 runp = runp->next)
513 if (strcmp (runp->symbol, symbol) == 0)
515 runp->deleted = 1;
516 break;
519 if (runp == NULL)
520 /* Name does not exist before. */
521 error_at_line (0, 0, fname, start_line,
522 gettext ("unknown set `%s'"), symbol);
526 else if (strncmp (&this_line[1], "quote", 5) == 0)
528 char buf[2];
529 char *bufptr;
530 size_t buflen;
531 char *wbufptr;
532 size_t wbuflen;
533 int cnt;
535 cnt = sizeof ("quote");
536 while (isspace (this_line[cnt]))
537 ++cnt;
539 /* We need the conversion. */
540 if (cd_towc == (iconv_t) -1
541 && open_conversion (codeset, &cd_towc, &cd_tomb,
542 &escape_char) != 0)
543 /* Something is wrong. */
544 goto out;
546 /* Yes, the quote char can be '\0'; this means no quote
547 char. The function using the information works on
548 wide characters so we have to convert it here. */
549 buf[0] = this_line[cnt];
550 buf[1] = '\0';
551 bufptr = buf;
552 buflen = 2;
554 wbufptr = (char *) wbuf;
555 wbuflen = wbufsize;
557 /* Flush the state. */
558 iconv (cd_towc, NULL, NULL, NULL, NULL);
560 iconv (cd_towc, &bufptr, &buflen, &wbufptr, &wbuflen);
561 if (buflen != 0 || (wchar_t *) wbufptr != &wbuf[2])
562 error_at_line (0, 0, fname, start_line,
563 gettext ("invalid quote character"));
564 else
565 /* Use the converted wide character. */
566 current->quote_char = wbuf[0];
568 else
570 int cnt;
571 cnt = 2;
572 while (this_line[cnt] != '\0' && !isspace (this_line[cnt]))
573 ++cnt;
574 this_line[cnt] = '\0';
575 error_at_line (0, 0, fname, start_line,
576 gettext ("unknown directive `%s': line ignored"),
577 &this_line[1]);
580 else if (isalnum (this_line[0]) || this_line[0] == '_')
582 const char *ident = this_line;
583 char *line = this_line;
584 int message_number;
587 ++line;
588 while (line[0] != '\0' && !isspace (line[0]));
589 if (line[0] != '\0')
590 *line++ = '\0'; /* Terminate the identifier. */
592 /* Now we found the beginning of the message itself. */
594 if (isdigit (ident[0]))
596 struct message_list *runp;
597 struct message_list *lastp;
599 message_number = atoi (ident);
601 /* Find location to insert the new message. */
602 runp = current->current_set->messages;
603 lastp = NULL;
604 while (runp != NULL)
605 if (runp->number == message_number)
606 break;
607 else
609 lastp = runp;
610 runp = runp->next;
612 if (runp != NULL)
614 /* Oh, oh. There is already a message with this
615 number in the message set. */
616 if (runp->symbol == NULL)
618 /* The existing message had its number specified
619 by the user. Fatal collision type uh, oh. */
620 error_at_line (0, 0, fname, start_line,
621 gettext ("duplicated message number"));
622 error_at_line (0, 0, runp->fname, runp->line,
623 gettext ("this is the first definition"));
624 message_number = 0;
626 else
628 /* Collision was with number auto-assigned to a
629 symbolic. Change existing symbolic number
630 and move to end the list (if not already there). */
631 runp->number = ++current->current_set->last_message;
633 if (runp->next != NULL)
635 struct message_list *endp;
637 if (lastp == NULL)
638 current->current_set->messages=runp->next;
639 else
640 lastp->next=runp->next;
642 endp = runp->next;
643 while (endp->next != NULL)
644 endp = endp->next;
646 endp->next = runp;
647 runp->next = NULL;
651 ident = NULL; /* We don't have a symbol. */
653 if (message_number != 0
654 && message_number > current->current_set->last_message)
655 current->current_set->last_message = message_number;
657 else if (ident[0] != '\0')
659 struct message_list *runp;
661 /* Test whether the symbolic name was not used for
662 another message in this message set. */
663 runp = current->current_set->messages;
664 while (runp != NULL)
665 if (runp->symbol != NULL && strcmp (ident, runp->symbol) == 0)
666 break;
667 else
668 runp = runp->next;
669 if (runp != NULL)
671 /* The name is already used. */
672 error_at_line (0, 0, fname, start_line, gettext ("\
673 duplicated message identifier"));
674 error_at_line (0, 0, runp->fname, runp->line,
675 gettext ("this is the first definition"));
676 message_number = 0;
678 else
679 /* Give the message the next unused number. */
680 message_number = ++current->current_set->last_message;
682 else
683 message_number = 0;
685 if (message_number != 0)
687 char *inbuf;
688 size_t inlen;
689 char *outbuf;
690 size_t outlen;
691 struct message_list *newp;
692 size_t line_len = strlen (line) + 1;
693 size_t ident_len = 0;
695 /* We need the conversion. */
696 if (cd_towc == (iconv_t) -1
697 && open_conversion (codeset, &cd_towc, &cd_tomb,
698 &escape_char) != 0)
699 /* Something is wrong. */
700 goto out;
702 /* Convert to a wide character string. We have to
703 interpret escape sequences which will be impossible
704 without doing the conversion if the codeset of the
705 message is stateful. */
706 while (1)
708 inbuf = line;
709 inlen = line_len;
710 outbuf = (char *) wbuf;
711 outlen = wbufsize;
713 /* Flush the state. */
714 iconv (cd_towc, NULL, NULL, NULL, NULL);
716 iconv (cd_towc, &inbuf, &inlen, &outbuf, &outlen);
717 if (inlen == 0)
719 /* The string is converted. */
720 assert (outlen < wbufsize);
721 assert (wbuf[(wbufsize - outlen) / sizeof (wchar_t) - 1]
722 == L'\0');
723 break;
726 if (outlen != 0)
728 /* Something is wrong with this string, we ignore it. */
729 error_at_line (0, 0, fname, start_line, gettext ("\
730 invalid character: message ignored"));
731 goto ignore;
734 /* The output buffer is too small. */
735 wbufsize *= 2;
736 wbuf = (wchar_t *) xrealloc (wbuf, wbufsize);
739 /* Strip quote characters, change escape sequences into
740 correct characters etc. */
741 normalize_line (fname, start_line, cd_towc, wbuf,
742 current->quote_char, escape_char);
744 if (ident)
745 ident_len = line - this_line;
747 /* Now the string is free of escape sequences. Convert it
748 back into a multibyte character string. First free the
749 memory allocated for the original string. */
750 obstack_free (&current->mem_pool, this_line);
752 used = 1; /* Yes, we use the line. */
754 /* Now fill in the new string. It should never happen that
755 the replaced string is longer than the original. */
756 inbuf = (char *) wbuf;
757 inlen = (wcslen (wbuf) + 1) * sizeof (wchar_t);
759 outlen = obstack_room (&current->mem_pool);
760 obstack_blank (&current->mem_pool, outlen);
761 this_line = (char *) obstack_base (&current->mem_pool);
762 outbuf = this_line + ident_len;
763 outlen -= ident_len;
765 /* Flush the state. */
766 iconv (cd_tomb, NULL, NULL, NULL, NULL);
768 iconv (cd_tomb, &inbuf, &inlen, &outbuf, &outlen);
769 if (inlen != 0)
771 error_at_line (0, 0, fname, start_line,
772 gettext ("invalid line"));
773 goto ignore;
775 assert (outbuf[-1] == '\0');
777 /* Free the memory in the obstack we don't use. */
778 obstack_blank (&current->mem_pool, -(int) outlen);
779 line = obstack_finish (&current->mem_pool);
781 newp = (struct message_list *) xmalloc (sizeof (*newp));
782 newp->number = message_number;
783 newp->message = line + ident_len;
784 /* Remember symbolic name; is NULL if no is given. */
785 newp->symbol = ident ? line : NULL;
786 /* Remember where we found the character. */
787 newp->fname = fname;
788 newp->line = start_line;
790 /* Find place to insert to message. We keep them in a
791 sorted single linked list. */
792 if (current->current_set->messages == NULL
793 || current->current_set->messages->number > message_number)
795 newp->next = current->current_set->messages;
796 current->current_set->messages = newp;
798 else
800 struct message_list *runp;
801 runp = current->current_set->messages;
802 while (runp->next != NULL)
803 if (runp->next->number > message_number)
804 break;
805 else
806 runp = runp->next;
807 newp->next = runp->next;
808 runp->next = newp;
811 ++current->total_messages;
813 else
815 size_t cnt;
817 cnt = 0;
818 /* See whether we have any non-white space character in this
819 line. */
820 while (this_line[cnt] != '\0' && isspace (this_line[cnt]))
821 ++cnt;
823 if (this_line[cnt] != '\0')
824 /* Yes, some unknown characters found. */
825 error_at_line (0, 0, fname, start_line,
826 gettext ("malformed line ignored"));
829 ignore:
830 /* We can save the memory for the line if it was not used. */
831 if (!used)
832 obstack_free (&current->mem_pool, this_line);
835 /* Close the conversion modules. */
836 iconv_close (cd_towc);
837 iconv_close (cd_tomb);
838 free (codeset);
840 out:
841 free (wbuf);
843 if (fp != stdin)
844 fclose (fp);
845 return current;
849 static void
850 write_out (struct catalog *catalog, const char *output_name,
851 const char *header_name)
853 /* Computing the "optimal" size. */
854 struct set_list *set_run;
855 size_t best_total, best_size, best_depth;
856 size_t act_size, act_depth;
857 struct catalog_obj obj;
858 struct obstack string_pool;
859 const char *strings;
860 size_t strings_size;
861 uint32_t *array1, *array2;
862 size_t cnt;
863 int fd;
865 /* If not otherwise told try to read file with existing
866 translations. */
867 if (!force_new)
868 read_old (catalog, output_name);
870 /* Initialize best_size with a very high value. */
871 best_total = best_size = best_depth = UINT_MAX;
873 /* We need some start size for testing. Let's start with
874 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
875 5. */
876 act_size = 1 + catalog->total_messages / 5;
878 /* We determine the size of a hash table here. Because the message
879 numbers can be chosen arbitrary by the programmer we cannot use
880 the simple method of accessing the array using the message
881 number. The algorithm is based on the trivial hash function
882 NUMBER % TABLE_SIZE, where collisions are stored in a second
883 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
884 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
885 while (act_size <= best_total)
887 size_t deep[act_size];
889 act_depth = 1;
890 memset (deep, '\0', act_size * sizeof (size_t));
891 set_run = catalog->all_sets;
892 while (set_run != NULL)
894 struct message_list *message_run;
896 message_run = set_run->messages;
897 while (message_run != NULL)
899 size_t idx = (message_run->number * set_run->number) % act_size;
901 ++deep[idx];
902 if (deep[idx] > act_depth)
904 act_depth = deep[idx];
905 if (act_depth * act_size > best_total)
906 break;
908 message_run = message_run->next;
910 set_run = set_run->next;
913 if (act_depth * act_size <= best_total)
915 /* We have found a better solution. */
916 best_total = act_depth * act_size;
917 best_size = act_size;
918 best_depth = act_depth;
921 ++act_size;
924 /* let's be prepared for an empty message file. */
925 if (best_size == UINT_MAX)
927 best_size = 1;
928 best_depth = 1;
931 /* OK, now we have the size we will use. Fill in the header, build
932 the table and the second one with swapped byte order. */
933 obj.magic = CATGETS_MAGIC;
934 obj.plane_size = best_size;
935 obj.plane_depth = best_depth;
937 /* Allocate room for all needed arrays. */
938 array1 =
939 (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
940 memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
941 array2
942 = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
943 obstack_init (&string_pool);
945 set_run = catalog->all_sets;
946 while (set_run != NULL)
948 struct message_list *message_run;
950 message_run = set_run->messages;
951 while (message_run != NULL)
953 size_t idx = (((message_run->number * set_run->number) % best_size)
954 * 3);
955 /* Determine collision depth. */
956 while (array1[idx] != 0)
957 idx += best_size * 3;
959 /* Store set number, message number and pointer into string
960 space, relative to the first string. */
961 array1[idx + 0] = set_run->number;
962 array1[idx + 1] = message_run->number;
963 array1[idx + 2] = obstack_object_size (&string_pool);
965 /* Add current string to the continuous space containing all
966 strings. */
967 obstack_grow0 (&string_pool, message_run->message,
968 strlen (message_run->message));
970 message_run = message_run->next;
973 set_run = set_run->next;
975 strings_size = obstack_object_size (&string_pool);
976 strings = obstack_finish (&string_pool);
978 /* Compute ARRAY2 by changing the byte order. */
979 for (cnt = 0; cnt < best_size * best_depth * 3; ++cnt)
980 array2[cnt] = SWAPU32 (array1[cnt]);
982 /* Now we can write out the whole data. */
983 if (strcmp (output_name, "-") == 0
984 || strcmp (output_name, "/dev/stdout") == 0)
985 fd = STDOUT_FILENO;
986 else
988 fd = creat (output_name, 0666);
989 if (fd < 0)
990 error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"),
991 output_name);
994 /* Write out header. */
995 write (fd, &obj, sizeof (obj));
997 /* We always write out the little endian version of the index
998 arrays. */
999 #if __BYTE_ORDER == __LITTLE_ENDIAN
1000 write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
1001 write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
1002 #elif __BYTE_ORDER == __BIG_ENDIAN
1003 write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
1004 write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
1005 #else
1006 # error Cannot handle __BYTE_ORDER byte order
1007 #endif
1009 /* Finally write the strings. */
1010 write (fd, strings, strings_size);
1012 if (fd != STDOUT_FILENO)
1013 close (fd);
1015 /* If requested now write out the header file. */
1016 if (header_name != NULL)
1018 int first = 1;
1019 FILE *fp;
1021 /* Open output file. "-" or "/dev/stdout" means write to
1022 standard output. */
1023 if (strcmp (header_name, "-") == 0
1024 || strcmp (header_name, "/dev/stdout") == 0)
1025 fp = stdout;
1026 else
1028 fp = fopen (header_name, "w");
1029 if (fp == NULL)
1030 error (EXIT_FAILURE, errno,
1031 gettext ("cannot open output file `%s'"), header_name);
1034 /* Iterate over all sets and all messages. */
1035 set_run = catalog->all_sets;
1036 while (set_run != NULL)
1038 struct message_list *message_run;
1040 /* If the current message set has a symbolic name write this
1041 out first. */
1042 if (set_run->symbol != NULL)
1043 fprintf (fp, "%s#define %sSet %#x\t/* %s:%Zu */\n",
1044 first ? "" : "\n", set_run->symbol, set_run->number - 1,
1045 set_run->fname, set_run->line);
1046 first = 0;
1048 message_run = set_run->messages;
1049 while (message_run != NULL)
1051 /* If the current message has a symbolic name write
1052 #define out. But we have to take care for the set
1053 not having a symbolic name. */
1054 if (message_run->symbol != NULL)
1056 if (set_run->symbol == NULL)
1057 fprintf (fp, "#define AutomaticSet%d%s %#x\t/* %s:%Zu */\n",
1058 set_run->number, message_run->symbol,
1059 message_run->number, message_run->fname,
1060 message_run->line);
1061 else
1062 fprintf (fp, "#define %s%s %#x\t/* %s:%Zu */\n",
1063 set_run->symbol, message_run->symbol,
1064 message_run->number, message_run->fname,
1065 message_run->line);
1068 message_run = message_run->next;
1071 set_run = set_run->next;
1074 if (fp != stdout)
1075 fclose (fp);
1080 static struct set_list *
1081 find_set (struct catalog *current, int number)
1083 struct set_list *result = current->all_sets;
1085 /* We must avoid set number 0 because a set of this number signals
1086 in the tables that the entry is not occupied. */
1087 ++number;
1089 while (result != NULL)
1090 if (result->number == number)
1091 return result;
1092 else
1093 result = result->next;
1095 /* Prepare new message set. */
1096 result = (struct set_list *) xcalloc (1, sizeof (*result));
1097 result->number = number;
1098 result->next = current->all_sets;
1099 current->all_sets = result;
1101 return result;
1105 /* Normalize given string *in*place* by processing escape sequences
1106 and quote characters. */
1107 static void
1108 normalize_line (const char *fname, size_t line, iconv_t cd, wchar_t *string,
1109 wchar_t quote_char, wchar_t escape_char)
1111 int is_quoted;
1112 wchar_t *rp = string;
1113 wchar_t *wp = string;
1115 if (quote_char != L'\0' && *rp == quote_char)
1117 is_quoted = 1;
1118 ++rp;
1120 else
1121 is_quoted = 0;
1123 while (*rp != L'\0')
1124 if (*rp == quote_char)
1125 /* We simply end the string when we find the first time an
1126 not-escaped quote character. */
1127 break;
1128 else if (*rp == escape_char)
1130 ++rp;
1131 if (quote_char != L'\0' && *rp == quote_char)
1132 /* This is an extension to XPG. */
1133 *wp++ = *rp++;
1134 else
1135 /* Recognize escape sequences. */
1136 switch (*rp)
1138 case L'n':
1139 *wp++ = L'\n';
1140 ++rp;
1141 break;
1142 case L't':
1143 *wp++ = L'\t';
1144 ++rp;
1145 break;
1146 case L'v':
1147 *wp++ = L'\v';
1148 ++rp;
1149 break;
1150 case L'b':
1151 *wp++ = L'\b';
1152 ++rp;
1153 break;
1154 case L'r':
1155 *wp++ = L'\r';
1156 ++rp;
1157 break;
1158 case L'f':
1159 *wp++ = L'\f';
1160 ++rp;
1161 break;
1162 case L'0' ... L'7':
1164 int number;
1165 char cbuf[2];
1166 char *cbufptr;
1167 size_t cbufin;
1168 wchar_t wcbuf[2];
1169 char *wcbufptr;
1170 size_t wcbufin;
1172 number = *rp++ - L'0';
1173 while (number <= (255 / 8) && *rp >= L'0' && *rp <= L'7')
1175 number *= 8;
1176 number += *rp++ - L'0';
1179 cbuf[0] = (char) number;
1180 cbuf[1] = '\0';
1181 cbufptr = cbuf;
1182 cbufin = 2;
1184 wcbufptr = (char *) wcbuf;
1185 wcbufin = sizeof (wcbuf);
1187 /* Flush the state. */
1188 iconv (cd, NULL, NULL, NULL, NULL);
1190 iconv (cd, &cbufptr, &cbufin, &wcbufptr, &wcbufin);
1191 if (cbufptr != &cbuf[2] || (wchar_t *) wcbufptr != &wcbuf[2])
1192 error_at_line (0, 0, fname, line,
1193 gettext ("invalid escape sequence"));
1194 else
1195 *wp++ = wcbuf[0];
1197 break;
1198 default:
1199 if (*rp == escape_char)
1201 *wp++ = escape_char;
1202 ++rp;
1204 else
1205 /* Simply ignore the backslash character. */;
1206 break;
1209 else
1210 *wp++ = *rp++;
1212 /* If we saw a quote character at the beginning we expect another
1213 one at the end. */
1214 if (is_quoted && *rp != quote_char)
1215 error_at_line (0, 0, fname, line, gettext ("unterminated message"));
1217 /* Terminate string. */
1218 *wp = L'\0';
1219 return;
1223 static void
1224 read_old (struct catalog *catalog, const char *file_name)
1226 struct catalog_info old_cat_obj;
1227 struct set_list *set = NULL;
1228 int last_set = -1;
1229 size_t cnt;
1231 /* Try to open catalog, but don't look through the NLSPATH. */
1232 if (__open_catalog (file_name, NULL, NULL, &old_cat_obj) != 0)
1234 if (errno == ENOENT)
1235 /* No problem, the catalog simply does not exist. */
1236 return;
1237 else
1238 error (EXIT_FAILURE, errno,
1239 gettext ("while opening old catalog file"));
1242 /* OK, we have the catalog loaded. Now read all messages and merge
1243 them. When set and message number clash for any message the new
1244 one is used. If the new one is empty it indicates that the
1245 message should be deleted. */
1246 for (cnt = 0; cnt < old_cat_obj.plane_size * old_cat_obj.plane_depth; ++cnt)
1248 struct message_list *message, *last;
1250 if (old_cat_obj.name_ptr[cnt * 3 + 0] == 0)
1251 /* No message in this slot. */
1252 continue;
1254 if (old_cat_obj.name_ptr[cnt * 3 + 0] - 1 != (uint32_t) last_set)
1256 last_set = old_cat_obj.name_ptr[cnt * 3 + 0] - 1;
1257 set = find_set (catalog, old_cat_obj.name_ptr[cnt * 3 + 0] - 1);
1260 last = NULL;
1261 message = set->messages;
1262 while (message != NULL)
1264 if ((uint32_t) message->number >= old_cat_obj.name_ptr[cnt * 3 + 1])
1265 break;
1266 last = message;
1267 message = message->next;
1270 if (message == NULL
1271 || (uint32_t) message->number > old_cat_obj.name_ptr[cnt * 3 + 1])
1273 /* We have found a message which is not yet in the catalog.
1274 Insert it at the right position. */
1275 struct message_list *newp;
1277 newp = (struct message_list *) xmalloc (sizeof(*newp));
1278 newp->number = old_cat_obj.name_ptr[cnt * 3 + 1];
1279 newp->message =
1280 &old_cat_obj.strings[old_cat_obj.name_ptr[cnt * 3 + 2]];
1281 newp->fname = NULL;
1282 newp->line = 0;
1283 newp->symbol = NULL;
1284 newp->next = message;
1286 if (last == NULL)
1287 set->messages = newp;
1288 else
1289 last->next = newp;
1291 ++catalog->total_messages;
1293 else if (*message->message == '\0')
1295 /* The new empty message has overridden the old one thus
1296 "deleting" it as required. Now remove the empty remains. */
1297 if (last == NULL)
1298 set->messages = message->next;
1299 else
1300 last->next = message->next;
1306 static int
1307 open_conversion (const char *codeset, iconv_t *cd_towcp, iconv_t *cd_tombp,
1308 wchar_t *escape_charp)
1310 char buf[2];
1311 char *bufptr;
1312 size_t bufsize;
1313 wchar_t wbuf[2];
1314 char *wbufptr;
1315 size_t wbufsize;
1317 /* If the input file does not specify the codeset use the locale's. */
1318 if (codeset == NULL)
1320 setlocale (LC_ALL, "");
1321 codeset = nl_langinfo (CODESET);
1322 setlocale (LC_ALL, "C");
1325 /* Get the conversion modules. */
1326 *cd_towcp = iconv_open ("WCHAR_T", codeset);
1327 *cd_tombp = iconv_open (codeset, "WCHAR_T");
1328 if (*cd_towcp == (iconv_t) -1 || *cd_tombp == (iconv_t) -1)
1330 error (0, 0, gettext ("conversion modules not available"));
1331 if (*cd_towcp != (iconv_t) -1)
1332 iconv_close (*cd_towcp);
1334 return 1;
1337 /* One special case for historical reasons is the backslash
1338 character. In some codesets the byte value 0x5c is not mapped to
1339 U005c in Unicode. These charsets then don't have a backslash
1340 character at all. Therefore we have to live with whatever the
1341 codeset provides and recognize, instead of the U005c, the character
1342 the byte value 0x5c is mapped to. */
1343 buf[0] = '\\';
1344 buf[1] = '\0';
1345 bufptr = buf;
1346 bufsize = 2;
1348 wbufptr = (char *) wbuf;
1349 wbufsize = sizeof (wbuf);
1351 iconv (*cd_towcp, &bufptr, &bufsize, &wbufptr, &wbufsize);
1352 if (bufsize != 0 || wbufsize != 0)
1354 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1355 on with using U005c. */
1356 error (0, 0, gettext ("cannot determine escape character"));
1357 *escape_charp = L'\\';
1359 else
1360 *escape_charp = wbuf[0];
1362 return 0;