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