]> git.ipfire.org Git - thirdparty/glibc.git/blame - catgets/gencat.c
Update copyright year
[thirdparty/glibc.git] / catgets / gencat.c
CommitLineData
81fb02b0 1/* Copyright (C) 1996-2011, 2012 Free Software Foundation, Inc.
df4ef2ab 2 This file is part of the GNU C Library.
b6aa34eb 3 Contributed by Ulrich Drepper <drepper@redhat.com>, 1996.
a641835a 4
43bc8ac6 5 This program is free software; you can redistribute it and/or modify
2e2efe65
RM
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.
a641835a 9
43bc8ac6 10 This program is distributed in the hope that it will be useful,
df4ef2ab 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
43bc8ac6
UD
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
a641835a 14
43bc8ac6
UD
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
a641835a
RM
18
19#ifdef HAVE_CONFIG_H
d1dc39a4 20# include "config.h"
a641835a
RM
21#endif
22
5a97622d 23#include <argp.h>
d2defdc4 24#include <assert.h>
a641835a
RM
25#include <ctype.h>
26#include <endian.h>
27#include <errno.h>
28#include <error.h>
29#include <fcntl.h>
d2defdc4
UD
30#include <iconv.h>
31#include <langinfo.h>
e75154a6 32#include <locale.h>
a641835a
RM
33#include <libintl.h>
34#include <limits.h>
35#include <nl_types.h>
36#include <obstack.h>
b6aa34eb 37#include <stdint.h>
a641835a
RM
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
d2defdc4 42#include <wchar.h>
a641835a
RM
43
44#include "version.h"
45
46#include "catgetsinfo.h"
47
48
49#define SWAPU32(w) \
50 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
51
52struct message_list
53{
54 int number;
55 const char *message;
56
57 const char *fname;
58 size_t line;
59 const char *symbol;
60
61 struct message_list *next;
62};
63
64
65struct set_list
66{
67 int number;
68 int deleted;
69 struct message_list *messages;
70 int last_message;
71
72 const char *fname;
73 size_t line;
74 const char *symbol;
75
76 struct set_list *next;
77};
78
79
80struct catalog
81{
82 struct set_list *all_sets;
83 struct set_list *current_set;
84 size_t total_messages;
d2defdc4 85 wint_t quote_char;
a641835a
RM
86 int last_set;
87
88 struct obstack mem_pool;
89};
90
91
92/* If non-zero force creation of new file, not using existing one. */
93static int force_new;
94
5a97622d
UD
95/* Name of output file. */
96static const char *output_name;
97
98/* Name of generated C header file. */
99static const char *header_name;
100
101/* Name and version of program. */
102static void print_version (FILE *stream, struct argp_state *state);
103void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
104
105#define OPT_NEW 1
106
107/* Definitions of arguments for argp functions. */
108static const struct argp_option options[] =
109{
110 { "header", 'H', N_("NAME"), 0,
111 N_("Create C header file NAME containing symbol definitions") },
112 { "new", OPT_NEW, NULL, 0,
113 N_("Do not use existing catalog, force new output file") },
114 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
115 { NULL, 0, NULL, 0, NULL }
116};
117
118/* Short description of program. */
119static const char doc[] = N_("Generate message catalog.\
120\vIf INPUT-FILE is -, input is read from standard input. If OUTPUT-FILE\n\
121is -, output is written to standard output.\n");
122
123/* Strings for arguments in help texts. */
124static const char args_doc[] = N_("\
125-o OUTPUT-FILE [INPUT-FILE]...\n[OUTPUT-FILE [INPUT-FILE]...]");
126
127/* Prototype for option handler. */
128static error_t parse_opt (int key, char *arg, struct argp_state *state);
129
130/* Function to print some extra text in the help message. */
131static char *more_help (int key, const char *text, void *input);
132
133/* Data structure to communicate with argp functions. */
134static struct argp argp =
a641835a 135{
5a97622d 136 options, parse_opt, args_doc, doc, NULL, more_help
a641835a
RM
137};
138
5a97622d 139
a641835a
RM
140/* Wrapper functions with error checking for standard functions. */
141extern void *xmalloc (size_t n);
a3b2008a 142extern void *xcalloc (size_t n, size_t s);
d2defdc4
UD
143extern void *xrealloc (void *o, size_t n);
144extern char *xstrdup (const char *);
a641835a
RM
145
146/* Prototypes for local functions. */
a641835a
RM
147static void error_print (void);
148static struct catalog *read_input_file (struct catalog *current,
149 const char *fname);
150static void write_out (struct catalog *result, const char *output_name,
151 const char *header_name);
152static struct set_list *find_set (struct catalog *current, int number);
d2defdc4 153static void normalize_line (const char *fname, size_t line, iconv_t cd,
ee25ee65
UD
154 wchar_t *string, wchar_t quote_char,
155 wchar_t escape_char);
a641835a 156static void read_old (struct catalog *catalog, const char *file_name);
d2defdc4 157static int open_conversion (const char *codesetp, iconv_t *cd_towcp,
ee25ee65 158 iconv_t *cd_tombp, wchar_t *escape_charp);
a641835a
RM
159
160
161int
162main (int argc, char *argv[])
163{
164 struct catalog *result;
2f6d1f1b 165 int remaining;
a641835a
RM
166
167 /* Set program name for messages. */
168 error_print_progname = error_print;
169
170 /* Set locale via LC_ALL. */
171 setlocale (LC_ALL, "");
172
173 /* Set the text message domain. */
174 textdomain (PACKAGE);
175
176 /* Initialize local variables. */
a641835a
RM
177 result = NULL;
178
5a97622d 179 /* Parse and process arguments. */
2f6d1f1b 180 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
a641835a
RM
181
182 /* Determine output file. */
183 if (output_name == NULL)
2f6d1f1b 184 output_name = remaining < argc ? argv[remaining++] : "-";
a641835a
RM
185
186 /* Process all input files. */
187 setlocale (LC_CTYPE, "C");
2f6d1f1b 188 if (remaining < argc)
a641835a 189 do
2f6d1f1b
UD
190 result = read_input_file (result, argv[remaining]);
191 while (++remaining < argc);
a641835a
RM
192 else
193 result = read_input_file (NULL, "-");
194
195 /* Write out the result. */
196 if (result != NULL)
197 write_out (result, output_name, header_name);
198
819c56e7 199 return error_message_count != 0;
a641835a
RM
200}
201
202
5a97622d
UD
203/* Handle program arguments. */
204static error_t
205parse_opt (int key, char *arg, struct argp_state *state)
a641835a 206{
5a97622d 207 switch (key)
fafaa44e 208 {
5a97622d
UD
209 case 'H':
210 header_name = arg;
211 break;
212 case OPT_NEW:
213 force_new = 1;
214 break;
215 case 'o':
216 output_name = arg;
217 break;
218 default:
219 return ARGP_ERR_UNKNOWN;
fafaa44e 220 }
5a97622d
UD
221 return 0;
222}
a641835a 223
5a97622d
UD
224
225static char *
226more_help (int key, const char *text, void *input)
227{
228 switch (key)
229 {
230 case ARGP_KEY_HELP_EXTRA:
231 /* We print some extra information. */
232 return strdup (gettext ("\
d40eb37a
UD
233For bug reporting instructions, please see:\n\
234<http://www.gnu.org/software/libc/bugs.html>.\n"));
5a97622d
UD
235 default:
236 break;
237 }
238 return (char *) text;
239}
240
241/* Print the version information. */
242static void
243print_version (FILE *stream, struct argp_state *state)
244{
245 fprintf (stream, "gencat (GNU %s) %s\n", PACKAGE, VERSION);
246 fprintf (stream, gettext ("\
247Copyright (C) %s Free Software Foundation, Inc.\n\
248This is free software; see the source for copying conditions. There is NO\n\
249warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
81fb02b0 250"), "2012");
5a97622d 251 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
a641835a
RM
252}
253
254
255/* The address of this function will be assigned to the hook in the
256 error functions. */
257static void
258error_print ()
259{
260 /* We don't want the program name to be printed in messages. Emacs'
261 compile.el does not like this. */
262}
263
264
265static struct catalog *
266read_input_file (struct catalog *current, const char *fname)
267{
268 FILE *fp;
269 char *buf;
270 size_t len;
271 size_t line_number;
d2defdc4
UD
272 wchar_t *wbuf;
273 size_t wbufsize;
274 iconv_t cd_towc = (iconv_t) -1;
275 iconv_t cd_tomb = (iconv_t) -1;
ee25ee65 276 wchar_t escape_char = L'\\';
d2defdc4 277 char *codeset = NULL;
a641835a
RM
278
279 if (strcmp (fname, "-") == 0 || strcmp (fname, "/dev/stdin") == 0)
280 {
281 fp = stdin;
282 fname = gettext ("*standard input*");
283 }
284 else
285 fp = fopen (fname, "r");
286 if (fp == NULL)
287 {
288 error (0, errno, gettext ("cannot open input file `%s'"), fname);
289 return current;
290 }
291
292 /* If we haven't seen anything yet, allocate result structure. */
293 if (current == NULL)
294 {
a3b2008a 295 current = (struct catalog *) xcalloc (1, sizeof (*current));
a641835a 296
df4ef2ab 297#define obstack_chunk_alloc malloc
a641835a
RM
298#define obstack_chunk_free free
299 obstack_init (&current->mem_pool);
a3b2008a
UD
300
301 current->current_set = find_set (current, NL_SETD);
a641835a
RM
302 }
303
304 buf = NULL;
305 len = 0;
306 line_number = 0;
d2defdc4
UD
307
308 wbufsize = 1024;
309 wbuf = (wchar_t *) xmalloc (wbufsize);
310
a641835a
RM
311 while (!feof (fp))
312 {
313 int continued;
314 int used;
315 size_t start_line = line_number + 1;
316 char *this_line;
317
318 do
319 {
320 int act_len;
321
322 act_len = getline (&buf, &len, fp);
323 if (act_len <= 0)
324 break;
325 ++line_number;
326
327 /* It the line continued? */
eabea972 328 continued = 0;
a641835a
RM
329 if (buf[act_len - 1] == '\n')
330 {
331 --act_len;
eabea972
UD
332
333 /* There might be more than one backslash at the end of
334 the line. Only if there is an odd number of them is
335 the line continued. */
a5392bed 336 if (act_len > 0 && buf[act_len - 1] == '\\')
eabea972
UD
337 {
338 int temp_act_len = act_len;
339
340 do
341 {
342 --temp_act_len;
343 continued = !continued;
344 }
345 while (temp_act_len > 0 && buf[temp_act_len - 1] == '\\');
eabea972 346
c70ad7d7
UD
347 if (continued)
348 --act_len;
349 }
a641835a 350 }
a641835a
RM
351
352 /* Append to currently selected line. */
353 obstack_grow (&current->mem_pool, buf, act_len);
354 }
355 while (continued);
356
357 obstack_1grow (&current->mem_pool, '\0');
358 this_line = (char *) obstack_finish (&current->mem_pool);
359
360 used = 0;
361 if (this_line[0] == '$')
362 {
c3880fbd 363 if (isblank (this_line[1]))
d2defdc4
UD
364 {
365 int cnt = 1;
366 while (isblank (this_line[cnt]))
367 ++cnt;
368 if (strncmp (&this_line[cnt], "codeset=", 8) != 0)
369 /* This is a comment line. Do nothing. */;
370 else if (codeset != NULL)
371 /* Ignore multiple codeset. */;
372 else
373 {
374 int start = cnt + 8;
375 cnt = start;
376 while (this_line[cnt] != '\0' && !isspace (this_line[cnt]))
377 ++cnt;
378 if (cnt != start)
379 {
380 int len = cnt - start;
381 codeset = xmalloc (len + 1);
382 *((char *) mempcpy (codeset, &this_line[start], len))
383 = '\0';
384 }
385 }
386 }
a641835a
RM
387 else if (strncmp (&this_line[1], "set", 3) == 0)
388 {
40a55d20 389 int cnt = sizeof ("set");
6dbe2837 390 int set_number;
a641835a
RM
391 const char *symbol = NULL;
392 while (isspace (this_line[cnt]))
393 ++cnt;
394
395 if (isdigit (this_line[cnt]))
396 {
397 set_number = atol (&this_line[cnt]);
398
399 /* If the given number for the character set is
400 higher than any we used for symbolic set names
401 avoid clashing by using only higher numbers for
402 the following symbolic definitions. */
403 if (set_number > current->last_set)
404 current->last_set = set_number;
405 }
406 else
407 {
408 /* See whether it is a reasonable identifier. */
409 int start = cnt;
410 while (isalnum (this_line[cnt]) || this_line[cnt] == '_')
411 ++cnt;
412
413 if (cnt == start)
414 {
415 /* No correct character found. */
416 error_at_line (0, 0, fname, start_line,
417 gettext ("illegal set number"));
418 set_number = 0;
419 }
420 else
421 {
6d52618b 422 /* We have found seomthing that looks like a
a641835a
RM
423 correct identifier. */
424 struct set_list *runp;
425
426 this_line[cnt] = '\0';
427 used = 1;
428 symbol = &this_line[start];
429
430 /* Test whether the identifier was already used. */
431 runp = current->all_sets;
432 while (runp != 0)
433 if (runp->symbol != NULL
434 && strcmp (runp->symbol, symbol) == 0)
435 break;
436 else
437 runp = runp->next;
438
439 if (runp != NULL)
440 {
441 /* We cannot allow duplicate identifiers for
442 message sets. */
443 error_at_line (0, 0, fname, start_line,
444 gettext ("duplicate set definition"));
445 error_at_line (0, 0, runp->fname, runp->line,
446 gettext ("\
447this is the first definition"));
448 set_number = 0;
449 }
450 else
451 /* Allocate next free message set for identifier. */
452 set_number = ++current->last_set;
453 }
454 }
455
456 if (set_number != 0)
457 {
458 /* We found a legal set number. */
459 current->current_set = find_set (current, set_number);
460 if (symbol != NULL)
461 used = 1;
462 current->current_set->symbol = symbol;
463 current->current_set->fname = fname;
464 current->current_set->line = start_line;
465 }
466 }
467 else if (strncmp (&this_line[1], "delset", 6) == 0)
468 {
469 int cnt = sizeof ("delset");
a641835a
RM
470 while (isspace (this_line[cnt]))
471 ++cnt;
472
473 if (isdigit (this_line[cnt]))
474 {
475 size_t set_number = atol (&this_line[cnt]);
476 struct set_list *set;
477
478 /* Mark the message set with the given number as
479 deleted. */
480 set = find_set (current, set_number);
481 set->deleted = 1;
482 }
483 else
484 {
485 /* See whether it is a reasonable identifier. */
486 int start = cnt;
487 while (isalnum (this_line[cnt]) || this_line[cnt] == '_')
488 ++cnt;
489
490 if (cnt == start)
5615eaf2
RM
491 error_at_line (0, 0, fname, start_line,
492 gettext ("illegal set number"));
a641835a
RM
493 else
494 {
495 const char *symbol;
496 struct set_list *runp;
497
498 this_line[cnt] = '\0';
499 used = 1;
500 symbol = &this_line[start];
501
502 /* We have a symbolic set name. This name must
503 appear somewhere else in the catalogs read so
504 far. */
a641835a
RM
505 for (runp = current->all_sets; runp != NULL;
506 runp = runp->next)
507 {
508 if (strcmp (runp->symbol, symbol) == 0)
509 {
510 runp->deleted = 1;
511 break;
512 }
513 }
514 if (runp == NULL)
515 /* Name does not exist before. */
516 error_at_line (0, 0, fname, start_line,
517 gettext ("unknown set `%s'"), symbol);
518 }
519 }
520 }
521 else if (strncmp (&this_line[1], "quote", 5) == 0)
522 {
d2defdc4
UD
523 char buf[2];
524 char *bufptr;
525 size_t buflen;
526 char *wbufptr;
527 size_t wbuflen;
528 int cnt;
529
530 cnt = sizeof ("quote");
a641835a
RM
531 while (isspace (this_line[cnt]))
532 ++cnt;
d2defdc4
UD
533
534 /* We need the conversion. */
535 if (cd_towc == (iconv_t) -1
ee25ee65
UD
536 && open_conversion (codeset, &cd_towc, &cd_tomb,
537 &escape_char) != 0)
d2defdc4
UD
538 /* Something is wrong. */
539 goto out;
540
a641835a 541 /* Yes, the quote char can be '\0'; this means no quote
d2defdc4
UD
542 char. The function using the information works on
543 wide characters so we have to convert it here. */
544 buf[0] = this_line[cnt];
545 buf[1] = '\0';
546 bufptr = buf;
547 buflen = 2;
548
549 wbufptr = (char *) wbuf;
550 wbuflen = wbufsize;
551
552 /* Flush the state. */
553 iconv (cd_towc, NULL, NULL, NULL, NULL);
554
555 iconv (cd_towc, &bufptr, &buflen, &wbufptr, &wbuflen);
556 if (buflen != 0 || (wchar_t *) wbufptr != &wbuf[2])
557 error_at_line (0, 0, fname, start_line,
558 gettext ("invalid quote character"));
559 else
560 /* Use the converted wide character. */
561 current->quote_char = wbuf[0];
a641835a
RM
562 }
563 else
564 {
565 int cnt;
566 cnt = 2;
567 while (this_line[cnt] != '\0' && !isspace (this_line[cnt]))
568 ++cnt;
569 this_line[cnt] = '\0';
570 error_at_line (0, 0, fname, start_line,
571 gettext ("unknown directive `%s': line ignored"),
572 &this_line[1]);
573 }
574 }
575 else if (isalnum (this_line[0]) || this_line[0] == '_')
576 {
577 const char *ident = this_line;
0a70515e 578 char *line = this_line;
a641835a
RM
579 int message_number;
580
581 do
0a70515e
UD
582 ++line;
583 while (line[0] != '\0' && !isspace (line[0]));
584 if (line[0] != '\0')
585 *line++ = '\0'; /* Terminate the identifier. */
a641835a 586
a641835a
RM
587 /* Now we found the beginning of the message itself. */
588
589 if (isdigit (ident[0]))
590 {
591 struct message_list *runp;
96eaef36 592 struct message_list *lastp;
a641835a
RM
593
594 message_number = atoi (ident);
595
596 /* Find location to insert the new message. */
597 runp = current->current_set->messages;
96eaef36 598 lastp = NULL;
a641835a
RM
599 while (runp != NULL)
600 if (runp->number == message_number)
601 break;
602 else
96eaef36
UD
603 {
604 lastp = runp;
605 runp = runp->next;
606 }
a641835a
RM
607 if (runp != NULL)
608 {
b6aa34eb
UD
609 /* Oh, oh. There is already a message with this
610 number in the message set. */
819c56e7
UD
611 if (runp->symbol == NULL)
612 {
613 /* The existing message had its number specified
614 by the user. Fatal collision type uh, oh. */
615 error_at_line (0, 0, fname, start_line,
616 gettext ("duplicated message number"));
617 error_at_line (0, 0, runp->fname, runp->line,
618 gettext ("this is the first definition"));
619 message_number = 0;
620 }
621 else
622 {
623 /* Collision was with number auto-assigned to a
624 symbolic. Change existing symbolic number
625 and move to end the list (if not already there). */
626 runp->number = ++current->current_set->last_message;
627
628 if (runp->next != NULL)
629 {
630 struct message_list *endp;
631
632 if (lastp == NULL)
633 current->current_set->messages=runp->next;
634 else
635 lastp->next=runp->next;
636
637 endp = runp->next;
638 while (endp->next != NULL)
639 endp = endp->next;
640
641 endp->next = runp;
642 runp->next = NULL;
643 }
644 }
a641835a
RM
645 }
646 ident = NULL; /* We don't have a symbol. */
647
648 if (message_number != 0
649 && message_number > current->current_set->last_message)
650 current->current_set->last_message = message_number;
651 }
652 else if (ident[0] != '\0')
653 {
654 struct message_list *runp;
a641835a
RM
655
656 /* Test whether the symbolic name was not used for
657 another message in this message set. */
96eaef36 658 runp = current->current_set->messages;
a641835a
RM
659 while (runp != NULL)
660 if (runp->symbol != NULL && strcmp (ident, runp->symbol) == 0)
661 break;
662 else
663 runp = runp->next;
664 if (runp != NULL)
665 {
b6aa34eb
UD
666 /* The name is already used. */
667 error_at_line (0, 0, fname, start_line, gettext ("\
96eaef36 668duplicated message identifier"));
b6aa34eb
UD
669 error_at_line (0, 0, runp->fname, runp->line,
670 gettext ("this is the first definition"));
a641835a
RM
671 message_number = 0;
672 }
673 else
674 /* Give the message the next unused number. */
675 message_number = ++current->current_set->last_message;
676 }
677 else
678 message_number = 0;
679
680 if (message_number != 0)
681 {
d2defdc4
UD
682 char *inbuf;
683 size_t inlen;
684 char *outbuf;
685 size_t outlen;
a641835a 686 struct message_list *newp;
0a70515e 687 size_t line_len = strlen (line) + 1;
9d37acc4 688 size_t ident_len = 0;
d2defdc4
UD
689
690 /* We need the conversion. */
691 if (cd_towc == (iconv_t) -1
ee25ee65
UD
692 && open_conversion (codeset, &cd_towc, &cd_tomb,
693 &escape_char) != 0)
d2defdc4
UD
694 /* Something is wrong. */
695 goto out;
696
697 /* Convert to a wide character string. We have to
698 interpret escape sequences which will be impossible
699 without doing the conversion if the codeset of the
700 message is stateful. */
701 while (1)
702 {
0a70515e
UD
703 inbuf = line;
704 inlen = line_len;
d2defdc4
UD
705 outbuf = (char *) wbuf;
706 outlen = wbufsize;
707
708 /* Flush the state. */
709 iconv (cd_towc, NULL, NULL, NULL, NULL);
710
711 iconv (cd_towc, &inbuf, &inlen, &outbuf, &outlen);
712 if (inlen == 0)
713 {
714 /* The string is converted. */
715 assert (outlen < wbufsize);
716 assert (wbuf[(wbufsize - outlen) / sizeof (wchar_t) - 1]
717 == L'\0');
718 break;
719 }
720
721 if (outlen != 0)
722 {
723 /* Something is wrong with this string, we ignore it. */
724 error_at_line (0, 0, fname, start_line, gettext ("\
725invalid character: message ignored"));
726 goto ignore;
727 }
728
729 /* The output buffer is too small. */
730 wbufsize *= 2;
731 wbuf = (wchar_t *) xrealloc (wbuf, wbufsize);
732 }
a641835a 733
a641835a
RM
734 /* Strip quote characters, change escape sequences into
735 correct characters etc. */
d2defdc4 736 normalize_line (fname, start_line, cd_towc, wbuf,
ee25ee65 737 current->quote_char, escape_char);
a641835a 738
9d37acc4
UD
739 if (ident)
740 ident_len = line - this_line;
741
d2defdc4
UD
742 /* Now the string is free of escape sequences. Convert it
743 back into a multibyte character string. First free the
744 memory allocated for the original string. */
745 obstack_free (&current->mem_pool, this_line);
746
0a70515e
UD
747 used = 1; /* Yes, we use the line. */
748
d2defdc4
UD
749 /* Now fill in the new string. It should never happen that
750 the replaced string is longer than the original. */
751 inbuf = (char *) wbuf;
752 inlen = (wcslen (wbuf) + 1) * sizeof (wchar_t);
753
754 outlen = obstack_room (&current->mem_pool);
0a70515e
UD
755 obstack_blank (&current->mem_pool, outlen);
756 this_line = (char *) obstack_base (&current->mem_pool);
9d37acc4
UD
757 outbuf = this_line + ident_len;
758 outlen -= ident_len;
d2defdc4
UD
759
760 /* Flush the state. */
761 iconv (cd_tomb, NULL, NULL, NULL, NULL);
762
763 iconv (cd_tomb, &inbuf, &inlen, &outbuf, &outlen);
764 if (inlen != 0)
765 {
766 error_at_line (0, 0, fname, start_line,
767 gettext ("invalid line"));
768 goto ignore;
769 }
770 assert (outbuf[-1] == '\0');
771
772 /* Free the memory in the obstack we don't use. */
0a70515e
UD
773 obstack_blank (&current->mem_pool, -(int) outlen);
774 line = obstack_finish (&current->mem_pool);
d2defdc4 775
a641835a
RM
776 newp = (struct message_list *) xmalloc (sizeof (*newp));
777 newp->number = message_number;
9d37acc4 778 newp->message = line + ident_len;
a641835a 779 /* Remember symbolic name; is NULL if no is given. */
9d37acc4 780 newp->symbol = ident ? line : NULL;
a641835a
RM
781 /* Remember where we found the character. */
782 newp->fname = fname;
783 newp->line = start_line;
784
785 /* Find place to insert to message. We keep them in a
786 sorted single linked list. */
787 if (current->current_set->messages == NULL
788 || current->current_set->messages->number > message_number)
789 {
790 newp->next = current->current_set->messages;
791 current->current_set->messages = newp;
792 }
793 else
794 {
795 struct message_list *runp;
796 runp = current->current_set->messages;
797 while (runp->next != NULL)
798 if (runp->next->number > message_number)
799 break;
800 else
801 runp = runp->next;
802 newp->next = runp->next;
803 runp->next = newp;
804 }
805 }
806 ++current->total_messages;
807 }
808 else
809 {
810 size_t cnt;
811
812 cnt = 0;
813 /* See whether we have any non-white space character in this
814 line. */
815 while (this_line[cnt] != '\0' && isspace (this_line[cnt]))
816 ++cnt;
817
818 if (this_line[cnt] != '\0')
819 /* Yes, some unknown characters found. */
820 error_at_line (0, 0, fname, start_line,
821 gettext ("malformed line ignored"));
822 }
823
d2defdc4 824 ignore:
a641835a
RM
825 /* We can save the memory for the line if it was not used. */
826 if (!used)
827 obstack_free (&current->mem_pool, this_line);
828 }
829
d2defdc4
UD
830 /* Close the conversion modules. */
831 iconv_close (cd_towc);
832 iconv_close (cd_tomb);
833 free (codeset);
834
835 out:
836 free (wbuf);
837
a641835a
RM
838 if (fp != stdin)
839 fclose (fp);
840 return current;
841}
842
843
844static void
845write_out (struct catalog *catalog, const char *output_name,
846 const char *header_name)
847{
848 /* Computing the "optimal" size. */
849 struct set_list *set_run;
850 size_t best_total, best_size, best_depth;
851 size_t act_size, act_depth;
852 struct catalog_obj obj;
853 struct obstack string_pool;
854 const char *strings;
855 size_t strings_size;
b6aa34eb 856 uint32_t *array1, *array2;
a641835a
RM
857 size_t cnt;
858 int fd;
859
860 /* If not otherwise told try to read file with existing
861 translations. */
862 if (!force_new)
863 read_old (catalog, output_name);
864
865 /* Initialize best_size with a very high value. */
866 best_total = best_size = best_depth = UINT_MAX;
867
868 /* We need some start size for testing. Let's start with
869 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
870 5. */
871 act_size = 1 + catalog->total_messages / 5;
872
873 /* We determine the size of a hash table here. Because the message
874 numbers can be chosen arbitrary by the programmer we cannot use
875 the simple method of accessing the array using the message
876 number. The algorithm is based on the trivial hash function
877 NUMBER % TABLE_SIZE, where collisions are stored in a second
878 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
879 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
880 while (act_size <= best_total)
881 {
882 size_t deep[act_size];
883
884 act_depth = 1;
885 memset (deep, '\0', act_size * sizeof (size_t));
886 set_run = catalog->all_sets;
887 while (set_run != NULL)
888 {
889 struct message_list *message_run;
890
891 message_run = set_run->messages;
892 while (message_run != NULL)
893 {
894 size_t idx = (message_run->number * set_run->number) % act_size;
895
896 ++deep[idx];
897 if (deep[idx] > act_depth)
898 {
899 act_depth = deep[idx];
900 if (act_depth * act_size > best_total)
901 break;
902 }
903 message_run = message_run->next;
904 }
a641835a
RM
905 set_run = set_run->next;
906 }
907
adc6ff7f
RM
908 if (act_depth * act_size <= best_total)
909 {
910 /* We have found a better solution. */
911 best_total = act_depth * act_size;
912 best_size = act_size;
913 best_depth = act_depth;
914 }
915
a641835a
RM
916 ++act_size;
917 }
918
919 /* let's be prepared for an empty message file. */
920 if (best_size == UINT_MAX)
921 {
922 best_size = 1;
923 best_depth = 1;
924 }
925
926 /* OK, now we have the size we will use. Fill in the header, build
927 the table and the second one with swapped byte order. */
928 obj.magic = CATGETS_MAGIC;
929 obj.plane_size = best_size;
930 obj.plane_depth = best_depth;
931
932 /* Allocate room for all needed arrays. */
933 array1 =
b6aa34eb
UD
934 (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
935 memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
a641835a 936 array2
b6aa34eb 937 = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
a641835a
RM
938 obstack_init (&string_pool);
939
940 set_run = catalog->all_sets;
941 while (set_run != NULL)
942 {
943 struct message_list *message_run;
944
945 message_run = set_run->messages;
946 while (message_run != NULL)
947 {
948 size_t idx = (((message_run->number * set_run->number) % best_size)
949 * 3);
950 /* Determine collision depth. */
951 while (array1[idx] != 0)
952 idx += best_size * 3;
953
954 /* Store set number, message number and pointer into string
955 space, relative to the first string. */
956 array1[idx + 0] = set_run->number;
957 array1[idx + 1] = message_run->number;
958 array1[idx + 2] = obstack_object_size (&string_pool);
959
960 /* Add current string to the continuous space containing all
961 strings. */
962 obstack_grow0 (&string_pool, message_run->message,
963 strlen (message_run->message));
964
965 message_run = message_run->next;
966 }
967
968 set_run = set_run->next;
969 }
970 strings_size = obstack_object_size (&string_pool);
971 strings = obstack_finish (&string_pool);
972
973 /* Compute ARRAY2 by changing the byte order. */
974 for (cnt = 0; cnt < best_size * best_depth * 3; ++cnt)
975 array2[cnt] = SWAPU32 (array1[cnt]);
976
977 /* Now we can write out the whole data. */
978 if (strcmp (output_name, "-") == 0
979 || strcmp (output_name, "/dev/stdout") == 0)
980 fd = STDOUT_FILENO;
981 else
982 {
983 fd = creat (output_name, 0666);
984 if (fd < 0)
985 error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"),
986 output_name);
987 }
988
989 /* Write out header. */
990 write (fd, &obj, sizeof (obj));
991
992 /* We always write out the little endian version of the index
993 arrays. */
994#if __BYTE_ORDER == __LITTLE_ENDIAN
b6aa34eb
UD
995 write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
996 write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
a641835a 997#elif __BYTE_ORDER == __BIG_ENDIAN
b6aa34eb
UD
998 write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
999 write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
a641835a
RM
1000#else
1001# error Cannot handle __BYTE_ORDER byte order
1002#endif
1003
1004 /* Finally write the strings. */
1005 write (fd, strings, strings_size);
1006
1007 if (fd != STDOUT_FILENO)
1008 close (fd);
1009
1010 /* If requested now write out the header file. */
1011 if (header_name != NULL)
1012 {
1013 int first = 1;
1014 FILE *fp;
1015
1016 /* Open output file. "-" or "/dev/stdout" means write to
1017 standard output. */
1018 if (strcmp (header_name, "-") == 0
1019 || strcmp (header_name, "/dev/stdout") == 0)
1020 fp = stdout;
1021 else
1022 {
1023 fp = fopen (header_name, "w");
1024 if (fp == NULL)
1025 error (EXIT_FAILURE, errno,
1026 gettext ("cannot open output file `%s'"), header_name);
1027 }
1028
1029 /* Iterate over all sets and all messages. */
1030 set_run = catalog->all_sets;
1031 while (set_run != NULL)
1032 {
1033 struct message_list *message_run;
1034
1035 /* If the current message set has a symbolic name write this
1036 out first. */
1037 if (set_run->symbol != NULL)
a4242e25 1038 fprintf (fp, "%s#define %sSet %#x\t/* %s:%Zu */\n",
a641835a
RM
1039 first ? "" : "\n", set_run->symbol, set_run->number - 1,
1040 set_run->fname, set_run->line);
1041 first = 0;
1042
1043 message_run = set_run->messages;
1044 while (message_run != NULL)
1045 {
1046 /* If the current message has a symbolic name write
1047 #define out. But we have to take care for the set
1048 not having a symbolic name. */
1049 if (message_run->symbol != NULL)
6e4c40ba
UD
1050 {
1051 if (set_run->symbol == NULL)
1052 fprintf (fp, "#define AutomaticSet%d%s %#x\t/* %s:%Zu */\n",
1053 set_run->number, message_run->symbol,
1054 message_run->number, message_run->fname,
1055 message_run->line);
1056 else
1057 fprintf (fp, "#define %s%s %#x\t/* %s:%Zu */\n",
1058 set_run->symbol, message_run->symbol,
1059 message_run->number, message_run->fname,
1060 message_run->line);
1061 }
a641835a
RM
1062
1063 message_run = message_run->next;
1064 }
1065
1066 set_run = set_run->next;
1067 }
1068
1069 if (fp != stdout)
1070 fclose (fp);
1071 }
1072}
1073
1074
1075static struct set_list *
1076find_set (struct catalog *current, int number)
1077{
1078 struct set_list *result = current->all_sets;
1079
1080 /* We must avoid set number 0 because a set of this number signals
1081 in the tables that the entry is not occupied. */
1082 ++number;
1083
1084 while (result != NULL)
1085 if (result->number == number)
1086 return result;
1087 else
1088 result = result->next;
1089
1090 /* Prepare new message set. */
a3b2008a 1091 result = (struct set_list *) xcalloc (1, sizeof (*result));
a641835a 1092 result->number = number;
a641835a
RM
1093 result->next = current->all_sets;
1094 current->all_sets = result;
1095
1096 return result;
1097}
1098
1099
1100/* Normalize given string *in*place* by processing escape sequences
1101 and quote characters. */
1102static void
d2defdc4 1103normalize_line (const char *fname, size_t line, iconv_t cd, wchar_t *string,
ee25ee65 1104 wchar_t quote_char, wchar_t escape_char)
a641835a
RM
1105{
1106 int is_quoted;
d2defdc4
UD
1107 wchar_t *rp = string;
1108 wchar_t *wp = string;
a641835a 1109
d2defdc4 1110 if (quote_char != L'\0' && *rp == quote_char)
a641835a
RM
1111 {
1112 is_quoted = 1;
1113 ++rp;
1114 }
1115 else
1116 is_quoted = 0;
1117
d2defdc4 1118 while (*rp != L'\0')
a641835a
RM
1119 if (*rp == quote_char)
1120 /* We simply end the string when we find the first time an
1121 not-escaped quote character. */
1122 break;
ee25ee65 1123 else if (*rp == escape_char)
a641835a
RM
1124 {
1125 ++rp;
d2defdc4 1126 if (quote_char != L'\0' && *rp == quote_char)
a641835a
RM
1127 /* This is an extension to XPG. */
1128 *wp++ = *rp++;
1129 else
1130 /* Recognize escape sequences. */
1131 switch (*rp)
1132 {
d2defdc4
UD
1133 case L'n':
1134 *wp++ = L'\n';
a641835a
RM
1135 ++rp;
1136 break;
d2defdc4
UD
1137 case L't':
1138 *wp++ = L'\t';
a641835a
RM
1139 ++rp;
1140 break;
d2defdc4
UD
1141 case L'v':
1142 *wp++ = L'\v';
a641835a
RM
1143 ++rp;
1144 break;
d2defdc4
UD
1145 case L'b':
1146 *wp++ = L'\b';
a641835a
RM
1147 ++rp;
1148 break;
d2defdc4
UD
1149 case L'r':
1150 *wp++ = L'\r';
a641835a
RM
1151 ++rp;
1152 break;
d2defdc4
UD
1153 case L'f':
1154 *wp++ = L'\f';
a641835a
RM
1155 ++rp;
1156 break;
d2defdc4 1157 case L'0' ... L'7':
a641835a 1158 {
d2defdc4
UD
1159 int number;
1160 char cbuf[2];
1161 char *cbufptr;
1162 size_t cbufin;
1163 wchar_t wcbuf[2];
1164 char *wcbufptr;
1165 size_t wcbufin;
1166
1167 number = *rp++ - L'0';
1168 while (number <= (255 / 8) && *rp >= L'0' && *rp <= L'7')
a641835a
RM
1169 {
1170 number *= 8;
d2defdc4 1171 number += *rp++ - L'0';
a641835a 1172 }
d2defdc4
UD
1173
1174 cbuf[0] = (char) number;
1175 cbuf[1] = '\0';
1176 cbufptr = cbuf;
1177 cbufin = 2;
1178
1179 wcbufptr = (char *) wcbuf;
1180 wcbufin = sizeof (wcbuf);
1181
1182 /* Flush the state. */
1183 iconv (cd, NULL, NULL, NULL, NULL);
1184
1185 iconv (cd, &cbufptr, &cbufin, &wcbufptr, &wcbufin);
1186 if (cbufptr != &cbuf[2] || (wchar_t *) wcbufptr != &wcbuf[2])
1187 error_at_line (0, 0, fname, line,
1188 gettext ("invalid escape sequence"));
1189 else
1190 *wp++ = wcbuf[0];
a641835a
RM
1191 }
1192 break;
1193 default:
ee25ee65
UD
1194 if (*rp == escape_char)
1195 {
1196 *wp++ = escape_char;
1197 ++rp;
1198 }
1199 else
1200 /* Simply ignore the backslash character. */;
a641835a
RM
1201 break;
1202 }
1203 }
1204 else
1205 *wp++ = *rp++;
1206
1207 /* If we saw a quote character at the beginning we expect another
1208 one at the end. */
1209 if (is_quoted && *rp != quote_char)
d2defdc4 1210 error_at_line (0, 0, fname, line, gettext ("unterminated message"));
a641835a
RM
1211
1212 /* Terminate string. */
d2defdc4 1213 *wp = L'\0';
a641835a
RM
1214 return;
1215}
1216
1217
1218static void
1219read_old (struct catalog *catalog, const char *file_name)
1220{
1221 struct catalog_info old_cat_obj;
1222 struct set_list *set = NULL;
1223 int last_set = -1;
1224 size_t cnt;
1225
a641835a 1226 /* Try to open catalog, but don't look through the NLSPATH. */
ca130fe4 1227 if (__open_catalog (file_name, NULL, NULL, &old_cat_obj) != 0)
6e4c40ba
UD
1228 {
1229 if (errno == ENOENT)
1230 /* No problem, the catalog simply does not exist. */
1231 return;
1232 else
ca130fe4
UD
1233 error (EXIT_FAILURE, errno,
1234 gettext ("while opening old catalog file"));
6e4c40ba 1235 }
a641835a
RM
1236
1237 /* OK, we have the catalog loaded. Now read all messages and merge
1238 them. When set and message number clash for any message the new
b6aa34eb
UD
1239 one is used. If the new one is empty it indicates that the
1240 message should be deleted. */
a641835a
RM
1241 for (cnt = 0; cnt < old_cat_obj.plane_size * old_cat_obj.plane_depth; ++cnt)
1242 {
1243 struct message_list *message, *last;
1244
1245 if (old_cat_obj.name_ptr[cnt * 3 + 0] == 0)
1246 /* No message in this slot. */
1247 continue;
1248
b6aa34eb 1249 if (old_cat_obj.name_ptr[cnt * 3 + 0] - 1 != (uint32_t) last_set)
a641835a
RM
1250 {
1251 last_set = old_cat_obj.name_ptr[cnt * 3 + 0] - 1;
1252 set = find_set (catalog, old_cat_obj.name_ptr[cnt * 3 + 0] - 1);
1253 }
1254
1255 last = NULL;
1256 message = set->messages;
1257 while (message != NULL)
1258 {
b6aa34eb 1259 if ((uint32_t) message->number >= old_cat_obj.name_ptr[cnt * 3 + 1])
a641835a
RM
1260 break;
1261 last = message;
1262 message = message->next;
1263 }
1264
1265 if (message == NULL
b6aa34eb 1266 || (uint32_t) message->number > old_cat_obj.name_ptr[cnt * 3 + 1])
a641835a
RM
1267 {
1268 /* We have found a message which is not yet in the catalog.
1269 Insert it at the right position. */
1270 struct message_list *newp;
1271
1272 newp = (struct message_list *) xmalloc (sizeof(*newp));
1273 newp->number = old_cat_obj.name_ptr[cnt * 3 + 1];
1274 newp->message =
1275 &old_cat_obj.strings[old_cat_obj.name_ptr[cnt * 3 + 2]];
1276 newp->fname = NULL;
1277 newp->line = 0;
1278 newp->symbol = NULL;
1279 newp->next = message;
1280
1281 if (last == NULL)
1282 set->messages = newp;
1283 else
1284 last->next = newp;
1285
1286 ++catalog->total_messages;
1287 }
b6aa34eb
UD
1288 else if (*message->message == '\0')
1289 {
1290 /* The new empty message has overridden the old one thus
1291 "deleting" it as required. Now remove the empty remains. */
1292 if (last == NULL)
1293 set->messages = message->next;
1294 else
1295 last->next = message->next;
1296 }
a641835a
RM
1297 }
1298}
d2defdc4
UD
1299
1300
1301static int
ee25ee65
UD
1302open_conversion (const char *codeset, iconv_t *cd_towcp, iconv_t *cd_tombp,
1303 wchar_t *escape_charp)
d2defdc4 1304{
ee25ee65
UD
1305 char buf[2];
1306 char *bufptr;
1307 size_t bufsize;
1308 wchar_t wbuf[2];
1309 char *wbufptr;
1310 size_t wbufsize;
1311
d2defdc4
UD
1312 /* If the input file does not specify the codeset use the locale's. */
1313 if (codeset == NULL)
1314 {
1315 setlocale (LC_ALL, "");
1316 codeset = nl_langinfo (CODESET);
1317 setlocale (LC_ALL, "C");
1318 }
1319
1320 /* Get the conversion modules. */
1321 *cd_towcp = iconv_open ("WCHAR_T", codeset);
1322 *cd_tombp = iconv_open (codeset, "WCHAR_T");
1323 if (*cd_towcp == (iconv_t) -1 || *cd_tombp == (iconv_t) -1)
1324 {
1325 error (0, 0, gettext ("conversion modules not available"));
1326 if (*cd_towcp != (iconv_t) -1)
1327 iconv_close (*cd_towcp);
1328
1329 return 1;
1330 }
1331
ee25ee65
UD
1332 /* One special case for historical reasons is the backslash
1333 character. In some codesets the byte value 0x5c is not mapped to
1334 U005c in Unicode. These charsets then don't have a backslash
1335 character at all. Therefore we have to live with whatever the
1336 codeset provides and recognize, instead of the U005c, the character
1337 the byte value 0x5c is mapped to. */
1338 buf[0] = '\\';
1339 buf[1] = '\0';
1340 bufptr = buf;
1341 bufsize = 2;
1342
1343 wbufptr = (char *) wbuf;
1344 wbufsize = sizeof (wbuf);
1345
1346 iconv (*cd_towcp, &bufptr, &bufsize, &wbufptr, &wbufsize);
1347 if (bufsize != 0 || wbufsize != 0)
1348 {
1349 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1350 on with using U005c. */
1351 error (0, 0, gettext ("cannot determine escape character"));
1352 *escape_charp = L'\\';
1353 }
1354 else
1355 *escape_charp = wbuf[0];
1356
d2defdc4
UD
1357 return 0;
1358}