1 /* Copyright (C) 1996-2005, 2006 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 version 2 as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
45 #include "catgetsinfo.h"
49 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
60 struct message_list
*next
;
68 struct message_list
*messages
;
75 struct set_list
*next
;
81 struct set_list
*all_sets
;
82 struct set_list
*current_set
;
83 size_t total_messages
;
87 struct obstack mem_pool
;
91 /* If non-zero force creation of new file, not using existing one. */
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
;
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 extern void *xcalloc (size_t n
, size_t s
);
142 extern void *xrealloc (void *o
, size_t n
);
143 extern char *xstrdup (const char *);
145 /* Prototypes for local functions. */
146 static void error_print (void);
147 static struct catalog
*read_input_file (struct catalog
*current
,
149 static void write_out (struct catalog
*result
, const char *output_name
,
150 const char *header_name
);
151 static struct set_list
*find_set (struct catalog
*current
, int number
);
152 static void normalize_line (const char *fname
, size_t line
, iconv_t cd
,
153 wchar_t *string
, wchar_t quote_char
,
154 wchar_t escape_char
);
155 static void read_old (struct catalog
*catalog
, const char *file_name
);
156 static int open_conversion (const char *codesetp
, iconv_t
*cd_towcp
,
157 iconv_t
*cd_tombp
, wchar_t *escape_charp
);
161 main (int argc
, char *argv
[])
163 struct catalog
*result
;
166 /* Set program name for messages. */
167 error_print_progname
= error_print
;
169 /* Set locale via LC_ALL. */
170 setlocale (LC_ALL
, "");
172 /* Set the text message domain. */
173 textdomain (PACKAGE
);
175 /* Initialize local variables. */
178 /* Parse and process arguments. */
179 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
181 /* Determine output file. */
182 if (output_name
== NULL
)
183 output_name
= remaining
< argc
? argv
[remaining
++] : "-";
185 /* Process all input files. */
186 setlocale (LC_CTYPE
, "C");
187 if (remaining
< argc
)
189 result
= read_input_file (result
, argv
[remaining
]);
190 while (++remaining
< argc
);
192 result
= read_input_file (NULL
, "-");
194 /* Write out the result. */
196 write_out (result
, output_name
, header_name
);
198 return error_message_count
!= 0;
202 /* Handle program arguments. */
204 parse_opt (int key
, char *arg
, struct argp_state
*state
)
218 return ARGP_ERR_UNKNOWN
;
225 more_help (int key
, const char *text
, void *input
)
229 case ARGP_KEY_HELP_EXTRA
:
230 /* We print some extra information. */
231 return strdup (gettext ("\
232 For bug reporting instructions, please see:\n\
233 <http://www.gnu.org/software/libc/bugs.html>.\n"));
237 return (char *) text
;
240 /* Print the version information. */
242 print_version (FILE *stream
, struct argp_state
*state
)
244 fprintf (stream
, "gencat (GNU %s) %s\n", PACKAGE
, VERSION
);
245 fprintf (stream
, gettext ("\
246 Copyright (C) %s Free Software Foundation, Inc.\n\
247 This is free software; see the source for copying conditions. There is NO\n\
248 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
250 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
254 /* The address of this function will be assigned to the hook in the
259 /* We don't want the program name to be printed in messages. Emacs'
260 compile.el does not like this. */
264 static struct catalog
*
265 read_input_file (struct catalog
*current
, const char *fname
)
273 iconv_t cd_towc
= (iconv_t
) -1;
274 iconv_t cd_tomb
= (iconv_t
) -1;
275 wchar_t escape_char
= L
'\\';
276 char *codeset
= NULL
;
278 if (strcmp (fname
, "-") == 0 || strcmp (fname
, "/dev/stdin") == 0)
281 fname
= gettext ("*standard input*");
284 fp
= fopen (fname
, "r");
287 error (0, errno
, gettext ("cannot open input file `%s'"), fname
);
291 /* If we haven't seen anything yet, allocate result structure. */
294 current
= (struct catalog
*) xcalloc (1, sizeof (*current
));
296 #define obstack_chunk_alloc malloc
297 #define obstack_chunk_free free
298 obstack_init (¤t
->mem_pool
);
300 current
->current_set
= find_set (current
, NL_SETD
);
308 wbuf
= (wchar_t *) xmalloc (wbufsize
);
314 size_t start_line
= line_number
+ 1;
321 act_len
= getline (&buf
, &len
, fp
);
326 /* It the line continued? */
328 if (buf
[act_len
- 1] == '\n')
332 /* There might be more than one backslash at the end of
333 the line. Only if there is an odd number of them is
334 the line continued. */
335 if (act_len
> 0 && buf
[act_len
- 1] == '\\')
337 int temp_act_len
= act_len
;
342 continued
= !continued
;
344 while (temp_act_len
> 0 && buf
[temp_act_len
- 1] == '\\');
351 /* Append to currently selected line. */
352 obstack_grow (¤t
->mem_pool
, buf
, act_len
);
356 obstack_1grow (¤t
->mem_pool
, '\0');
357 this_line
= (char *) obstack_finish (¤t
->mem_pool
);
360 if (this_line
[0] == '$')
362 if (isblank (this_line
[1]))
365 while (isblank (this_line
[cnt
]))
367 if (strncmp (&this_line
[cnt
], "codeset=", 8) != 0)
368 /* This is a comment line. Do nothing. */;
369 else if (codeset
!= NULL
)
370 /* Ignore multiple codeset. */;
375 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
379 int len
= cnt
- start
;
380 codeset
= xmalloc (len
+ 1);
381 *((char *) mempcpy (codeset
, &this_line
[start
], len
))
386 else if (strncmp (&this_line
[1], "set", 3) == 0)
388 int cnt
= sizeof ("set");
390 const char *symbol
= NULL
;
391 while (isspace (this_line
[cnt
]))
394 if (isdigit (this_line
[cnt
]))
396 set_number
= atol (&this_line
[cnt
]);
398 /* If the given number for the character set is
399 higher than any we used for symbolic set names
400 avoid clashing by using only higher numbers for
401 the following symbolic definitions. */
402 if (set_number
> current
->last_set
)
403 current
->last_set
= set_number
;
407 /* See whether it is a reasonable identifier. */
409 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
414 /* No correct character found. */
415 error_at_line (0, 0, fname
, start_line
,
416 gettext ("illegal set number"));
421 /* We have found seomthing that looks like a
422 correct identifier. */
423 struct set_list
*runp
;
425 this_line
[cnt
] = '\0';
427 symbol
= &this_line
[start
];
429 /* Test whether the identifier was already used. */
430 runp
= current
->all_sets
;
432 if (runp
->symbol
!= NULL
433 && strcmp (runp
->symbol
, symbol
) == 0)
440 /* We cannot allow duplicate identifiers for
442 error_at_line (0, 0, fname
, start_line
,
443 gettext ("duplicate set definition"));
444 error_at_line (0, 0, runp
->fname
, runp
->line
,
446 this is the first definition"));
450 /* Allocate next free message set for identifier. */
451 set_number
= ++current
->last_set
;
457 /* We found a legal set number. */
458 current
->current_set
= find_set (current
, set_number
);
461 current
->current_set
->symbol
= symbol
;
462 current
->current_set
->fname
= fname
;
463 current
->current_set
->line
= start_line
;
466 else if (strncmp (&this_line
[1], "delset", 6) == 0)
468 int cnt
= sizeof ("delset");
470 while (isspace (this_line
[cnt
]))
473 if (isdigit (this_line
[cnt
]))
475 size_t set_number
= atol (&this_line
[cnt
]);
476 struct set_list
*set
;
478 /* Mark the message set with the given number as
480 set
= find_set (current
, set_number
);
485 /* See whether it is a reasonable identifier. */
487 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
492 error_at_line (0, 0, fname
, start_line
,
493 gettext ("illegal set number"));
499 struct set_list
*runp
;
501 this_line
[cnt
] = '\0';
503 symbol
= &this_line
[start
];
505 /* We have a symbolic set name. This name must
506 appear somewhere else in the catalogs read so
509 for (runp
= current
->all_sets
; runp
!= NULL
;
512 if (strcmp (runp
->symbol
, symbol
) == 0)
519 /* Name does not exist before. */
520 error_at_line (0, 0, fname
, start_line
,
521 gettext ("unknown set `%s'"), symbol
);
525 else if (strncmp (&this_line
[1], "quote", 5) == 0)
534 cnt
= sizeof ("quote");
535 while (isspace (this_line
[cnt
]))
538 /* We need the conversion. */
539 if (cd_towc
== (iconv_t
) -1
540 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
542 /* Something is wrong. */
545 /* Yes, the quote char can be '\0'; this means no quote
546 char. The function using the information works on
547 wide characters so we have to convert it here. */
548 buf
[0] = this_line
[cnt
];
553 wbufptr
= (char *) wbuf
;
556 /* Flush the state. */
557 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
559 iconv (cd_towc
, &bufptr
, &buflen
, &wbufptr
, &wbuflen
);
560 if (buflen
!= 0 || (wchar_t *) wbufptr
!= &wbuf
[2])
561 error_at_line (0, 0, fname
, start_line
,
562 gettext ("invalid quote character"));
564 /* Use the converted wide character. */
565 current
->quote_char
= wbuf
[0];
571 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
573 this_line
[cnt
] = '\0';
574 error_at_line (0, 0, fname
, start_line
,
575 gettext ("unknown directive `%s': line ignored"),
579 else if (isalnum (this_line
[0]) || this_line
[0] == '_')
581 const char *ident
= this_line
;
582 char *line
= this_line
;
587 while (line
[0] != '\0' && !isspace (line
[0]));
589 *line
++ = '\0'; /* Terminate the identifier. */
591 /* Now we found the beginning of the message itself. */
593 if (isdigit (ident
[0]))
595 struct message_list
*runp
;
596 struct message_list
*lastp
;
598 message_number
= atoi (ident
);
600 /* Find location to insert the new message. */
601 runp
= current
->current_set
->messages
;
604 if (runp
->number
== message_number
)
613 /* Oh, oh. There is already a message with this
614 number in the message set. */
615 if (runp
->symbol
== NULL
)
617 /* The existing message had its number specified
618 by the user. Fatal collision type uh, oh. */
619 error_at_line (0, 0, fname
, start_line
,
620 gettext ("duplicated message number"));
621 error_at_line (0, 0, runp
->fname
, runp
->line
,
622 gettext ("this is the first definition"));
627 /* Collision was with number auto-assigned to a
628 symbolic. Change existing symbolic number
629 and move to end the list (if not already there). */
630 runp
->number
= ++current
->current_set
->last_message
;
632 if (runp
->next
!= NULL
)
634 struct message_list
*endp
;
637 current
->current_set
->messages
=runp
->next
;
639 lastp
->next
=runp
->next
;
642 while (endp
->next
!= NULL
)
650 ident
= NULL
; /* We don't have a symbol. */
652 if (message_number
!= 0
653 && message_number
> current
->current_set
->last_message
)
654 current
->current_set
->last_message
= message_number
;
656 else if (ident
[0] != '\0')
658 struct message_list
*runp
;
659 struct message_list
*lastp
;
661 /* Test whether the symbolic name was not used for
662 another message in this message set. */
663 runp
= current
->current_set
->messages
;
666 if (runp
->symbol
!= NULL
&& strcmp (ident
, runp
->symbol
) == 0)
672 /* The name is already used. */
673 error_at_line (0, 0, fname
, start_line
, gettext ("\
674 duplicated message identifier"));
675 error_at_line (0, 0, runp
->fname
, runp
->line
,
676 gettext ("this is the first definition"));
680 /* Give the message the next unused number. */
681 message_number
= ++current
->current_set
->last_message
;
686 if (message_number
!= 0)
692 struct message_list
*newp
;
693 size_t line_len
= strlen (line
) + 1;
694 size_t ident_len
= 0;
696 /* We need the conversion. */
697 if (cd_towc
== (iconv_t
) -1
698 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
700 /* Something is wrong. */
703 /* Convert to a wide character string. We have to
704 interpret escape sequences which will be impossible
705 without doing the conversion if the codeset of the
706 message is stateful. */
711 outbuf
= (char *) wbuf
;
714 /* Flush the state. */
715 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
717 iconv (cd_towc
, &inbuf
, &inlen
, &outbuf
, &outlen
);
720 /* The string is converted. */
721 assert (outlen
< wbufsize
);
722 assert (wbuf
[(wbufsize
- outlen
) / sizeof (wchar_t) - 1]
729 /* Something is wrong with this string, we ignore it. */
730 error_at_line (0, 0, fname
, start_line
, gettext ("\
731 invalid character: message ignored"));
735 /* The output buffer is too small. */
737 wbuf
= (wchar_t *) xrealloc (wbuf
, wbufsize
);
740 /* Strip quote characters, change escape sequences into
741 correct characters etc. */
742 normalize_line (fname
, start_line
, cd_towc
, wbuf
,
743 current
->quote_char
, escape_char
);
746 ident_len
= line
- this_line
;
748 /* Now the string is free of escape sequences. Convert it
749 back into a multibyte character string. First free the
750 memory allocated for the original string. */
751 obstack_free (¤t
->mem_pool
, this_line
);
753 used
= 1; /* Yes, we use the line. */
755 /* Now fill in the new string. It should never happen that
756 the replaced string is longer than the original. */
757 inbuf
= (char *) wbuf
;
758 inlen
= (wcslen (wbuf
) + 1) * sizeof (wchar_t);
760 outlen
= obstack_room (¤t
->mem_pool
);
761 obstack_blank (¤t
->mem_pool
, outlen
);
762 this_line
= (char *) obstack_base (¤t
->mem_pool
);
763 outbuf
= this_line
+ ident_len
;
766 /* Flush the state. */
767 iconv (cd_tomb
, NULL
, NULL
, NULL
, NULL
);
769 iconv (cd_tomb
, &inbuf
, &inlen
, &outbuf
, &outlen
);
772 error_at_line (0, 0, fname
, start_line
,
773 gettext ("invalid line"));
776 assert (outbuf
[-1] == '\0');
778 /* Free the memory in the obstack we don't use. */
779 obstack_blank (¤t
->mem_pool
, -(int) outlen
);
780 line
= obstack_finish (¤t
->mem_pool
);
782 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
783 newp
->number
= message_number
;
784 newp
->message
= line
+ ident_len
;
785 /* Remember symbolic name; is NULL if no is given. */
786 newp
->symbol
= ident
? line
: NULL
;
787 /* Remember where we found the character. */
789 newp
->line
= start_line
;
791 /* Find place to insert to message. We keep them in a
792 sorted single linked list. */
793 if (current
->current_set
->messages
== NULL
794 || current
->current_set
->messages
->number
> message_number
)
796 newp
->next
= current
->current_set
->messages
;
797 current
->current_set
->messages
= newp
;
801 struct message_list
*runp
;
802 runp
= current
->current_set
->messages
;
803 while (runp
->next
!= NULL
)
804 if (runp
->next
->number
> message_number
)
808 newp
->next
= runp
->next
;
812 ++current
->total_messages
;
819 /* See whether we have any non-white space character in this
821 while (this_line
[cnt
] != '\0' && isspace (this_line
[cnt
]))
824 if (this_line
[cnt
] != '\0')
825 /* Yes, some unknown characters found. */
826 error_at_line (0, 0, fname
, start_line
,
827 gettext ("malformed line ignored"));
831 /* We can save the memory for the line if it was not used. */
833 obstack_free (¤t
->mem_pool
, this_line
);
836 /* Close the conversion modules. */
837 iconv_close (cd_towc
);
838 iconv_close (cd_tomb
);
851 write_out (struct catalog
*catalog
, const char *output_name
,
852 const char *header_name
)
854 /* Computing the "optimal" size. */
855 struct set_list
*set_run
;
856 size_t best_total
, best_size
, best_depth
;
857 size_t act_size
, act_depth
;
858 struct catalog_obj obj
;
859 struct obstack string_pool
;
862 uint32_t *array1
, *array2
;
866 /* If not otherwise told try to read file with existing
869 read_old (catalog
, output_name
);
871 /* Initialize best_size with a very high value. */
872 best_total
= best_size
= best_depth
= UINT_MAX
;
874 /* We need some start size for testing. Let's start with
875 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
877 act_size
= 1 + catalog
->total_messages
/ 5;
879 /* We determine the size of a hash table here. Because the message
880 numbers can be chosen arbitrary by the programmer we cannot use
881 the simple method of accessing the array using the message
882 number. The algorithm is based on the trivial hash function
883 NUMBER % TABLE_SIZE, where collisions are stored in a second
884 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
885 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
886 while (act_size
<= best_total
)
888 size_t deep
[act_size
];
891 memset (deep
, '\0', act_size
* sizeof (size_t));
892 set_run
= catalog
->all_sets
;
893 while (set_run
!= NULL
)
895 struct message_list
*message_run
;
897 message_run
= set_run
->messages
;
898 while (message_run
!= NULL
)
900 size_t idx
= (message_run
->number
* set_run
->number
) % act_size
;
903 if (deep
[idx
] > act_depth
)
905 act_depth
= deep
[idx
];
906 if (act_depth
* act_size
> best_total
)
909 message_run
= message_run
->next
;
911 set_run
= set_run
->next
;
914 if (act_depth
* act_size
<= best_total
)
916 /* We have found a better solution. */
917 best_total
= act_depth
* act_size
;
918 best_size
= act_size
;
919 best_depth
= act_depth
;
925 /* let's be prepared for an empty message file. */
926 if (best_size
== UINT_MAX
)
932 /* OK, now we have the size we will use. Fill in the header, build
933 the table and the second one with swapped byte order. */
934 obj
.magic
= CATGETS_MAGIC
;
935 obj
.plane_size
= best_size
;
936 obj
.plane_depth
= best_depth
;
938 /* Allocate room for all needed arrays. */
940 (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
941 memset (array1
, '\0', best_size
* best_depth
* sizeof (uint32_t) * 3);
943 = (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
944 obstack_init (&string_pool
);
946 set_run
= catalog
->all_sets
;
947 while (set_run
!= NULL
)
949 struct message_list
*message_run
;
951 message_run
= set_run
->messages
;
952 while (message_run
!= NULL
)
954 size_t idx
= (((message_run
->number
* set_run
->number
) % best_size
)
956 /* Determine collision depth. */
957 while (array1
[idx
] != 0)
958 idx
+= best_size
* 3;
960 /* Store set number, message number and pointer into string
961 space, relative to the first string. */
962 array1
[idx
+ 0] = set_run
->number
;
963 array1
[idx
+ 1] = message_run
->number
;
964 array1
[idx
+ 2] = obstack_object_size (&string_pool
);
966 /* Add current string to the continuous space containing all
968 obstack_grow0 (&string_pool
, message_run
->message
,
969 strlen (message_run
->message
));
971 message_run
= message_run
->next
;
974 set_run
= set_run
->next
;
976 strings_size
= obstack_object_size (&string_pool
);
977 strings
= obstack_finish (&string_pool
);
979 /* Compute ARRAY2 by changing the byte order. */
980 for (cnt
= 0; cnt
< best_size
* best_depth
* 3; ++cnt
)
981 array2
[cnt
] = SWAPU32 (array1
[cnt
]);
983 /* Now we can write out the whole data. */
984 if (strcmp (output_name
, "-") == 0
985 || strcmp (output_name
, "/dev/stdout") == 0)
989 fd
= creat (output_name
, 0666);
991 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
995 /* Write out header. */
996 write (fd
, &obj
, sizeof (obj
));
998 /* We always write out the little endian version of the index
1000 #if __BYTE_ORDER == __LITTLE_ENDIAN
1001 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1002 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1003 #elif __BYTE_ORDER == __BIG_ENDIAN
1004 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1005 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1007 # error Cannot handle __BYTE_ORDER byte order
1010 /* Finally write the strings. */
1011 write (fd
, strings
, strings_size
);
1013 if (fd
!= STDOUT_FILENO
)
1016 /* If requested now write out the header file. */
1017 if (header_name
!= NULL
)
1022 /* Open output file. "-" or "/dev/stdout" means write to
1024 if (strcmp (header_name
, "-") == 0
1025 || strcmp (header_name
, "/dev/stdout") == 0)
1029 fp
= fopen (header_name
, "w");
1031 error (EXIT_FAILURE
, errno
,
1032 gettext ("cannot open output file `%s'"), header_name
);
1035 /* Iterate over all sets and all messages. */
1036 set_run
= catalog
->all_sets
;
1037 while (set_run
!= NULL
)
1039 struct message_list
*message_run
;
1041 /* If the current message set has a symbolic name write this
1043 if (set_run
->symbol
!= NULL
)
1044 fprintf (fp
, "%s#define %sSet %#x\t/* %s:%Zu */\n",
1045 first
? "" : "\n", set_run
->symbol
, set_run
->number
- 1,
1046 set_run
->fname
, set_run
->line
);
1049 message_run
= set_run
->messages
;
1050 while (message_run
!= NULL
)
1052 /* If the current message has a symbolic name write
1053 #define out. But we have to take care for the set
1054 not having a symbolic name. */
1055 if (message_run
->symbol
!= NULL
)
1057 if (set_run
->symbol
== NULL
)
1058 fprintf (fp
, "#define AutomaticSet%d%s %#x\t/* %s:%Zu */\n",
1059 set_run
->number
, message_run
->symbol
,
1060 message_run
->number
, message_run
->fname
,
1063 fprintf (fp
, "#define %s%s %#x\t/* %s:%Zu */\n",
1064 set_run
->symbol
, message_run
->symbol
,
1065 message_run
->number
, message_run
->fname
,
1069 message_run
= message_run
->next
;
1072 set_run
= set_run
->next
;
1081 static struct set_list
*
1082 find_set (struct catalog
*current
, int number
)
1084 struct set_list
*result
= current
->all_sets
;
1086 /* We must avoid set number 0 because a set of this number signals
1087 in the tables that the entry is not occupied. */
1090 while (result
!= NULL
)
1091 if (result
->number
== number
)
1094 result
= result
->next
;
1096 /* Prepare new message set. */
1097 result
= (struct set_list
*) xcalloc (1, sizeof (*result
));
1098 result
->number
= number
;
1099 result
->next
= current
->all_sets
;
1100 current
->all_sets
= result
;
1106 /* Normalize given string *in*place* by processing escape sequences
1107 and quote characters. */
1109 normalize_line (const char *fname
, size_t line
, iconv_t cd
, wchar_t *string
,
1110 wchar_t quote_char
, wchar_t escape_char
)
1113 wchar_t *rp
= string
;
1114 wchar_t *wp
= string
;
1116 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1124 while (*rp
!= L
'\0')
1125 if (*rp
== quote_char
)
1126 /* We simply end the string when we find the first time an
1127 not-escaped quote character. */
1129 else if (*rp
== escape_char
)
1132 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1133 /* This is an extension to XPG. */
1136 /* Recognize escape sequences. */
1173 number
= *rp
++ - L
'0';
1174 while (number
<= (255 / 8) && *rp
>= L
'0' && *rp
<= L
'7')
1177 number
+= *rp
++ - L
'0';
1180 cbuf
[0] = (char) number
;
1185 wcbufptr
= (char *) wcbuf
;
1186 wcbufin
= sizeof (wcbuf
);
1188 /* Flush the state. */
1189 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
1191 iconv (cd
, &cbufptr
, &cbufin
, &wcbufptr
, &wcbufin
);
1192 if (cbufptr
!= &cbuf
[2] || (wchar_t *) wcbufptr
!= &wcbuf
[2])
1193 error_at_line (0, 0, fname
, line
,
1194 gettext ("invalid escape sequence"));
1200 if (*rp
== escape_char
)
1202 *wp
++ = escape_char
;
1206 /* Simply ignore the backslash character. */;
1213 /* If we saw a quote character at the beginning we expect another
1215 if (is_quoted
&& *rp
!= quote_char
)
1216 error_at_line (0, 0, fname
, line
, gettext ("unterminated message"));
1218 /* Terminate string. */
1225 read_old (struct catalog
*catalog
, const char *file_name
)
1227 struct catalog_info old_cat_obj
;
1228 struct set_list
*set
= NULL
;
1232 /* Try to open catalog, but don't look through the NLSPATH. */
1233 if (__open_catalog (file_name
, NULL
, NULL
, &old_cat_obj
) != 0)
1235 if (errno
== ENOENT
)
1236 /* No problem, the catalog simply does not exist. */
1239 error (EXIT_FAILURE
, errno
,
1240 gettext ("while opening old catalog file"));
1243 /* OK, we have the catalog loaded. Now read all messages and merge
1244 them. When set and message number clash for any message the new
1245 one is used. If the new one is empty it indicates that the
1246 message should be deleted. */
1247 for (cnt
= 0; cnt
< old_cat_obj
.plane_size
* old_cat_obj
.plane_depth
; ++cnt
)
1249 struct message_list
*message
, *last
;
1251 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] == 0)
1252 /* No message in this slot. */
1255 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1 != (uint32_t) last_set
)
1257 last_set
= old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1;
1258 set
= find_set (catalog
, old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1);
1262 message
= set
->messages
;
1263 while (message
!= NULL
)
1265 if ((uint32_t) message
->number
>= old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1268 message
= message
->next
;
1272 || (uint32_t) message
->number
> old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1274 /* We have found a message which is not yet in the catalog.
1275 Insert it at the right position. */
1276 struct message_list
*newp
;
1278 newp
= (struct message_list
*) xmalloc (sizeof(*newp
));
1279 newp
->number
= old_cat_obj
.name_ptr
[cnt
* 3 + 1];
1281 &old_cat_obj
.strings
[old_cat_obj
.name_ptr
[cnt
* 3 + 2]];
1284 newp
->symbol
= NULL
;
1285 newp
->next
= message
;
1288 set
->messages
= newp
;
1292 ++catalog
->total_messages
;
1294 else if (*message
->message
== '\0')
1296 /* The new empty message has overridden the old one thus
1297 "deleting" it as required. Now remove the empty remains. */
1299 set
->messages
= message
->next
;
1301 last
->next
= message
->next
;
1308 open_conversion (const char *codeset
, iconv_t
*cd_towcp
, iconv_t
*cd_tombp
,
1309 wchar_t *escape_charp
)
1318 /* If the input file does not specify the codeset use the locale's. */
1319 if (codeset
== NULL
)
1321 setlocale (LC_ALL
, "");
1322 codeset
= nl_langinfo (CODESET
);
1323 setlocale (LC_ALL
, "C");
1326 /* Get the conversion modules. */
1327 *cd_towcp
= iconv_open ("WCHAR_T", codeset
);
1328 *cd_tombp
= iconv_open (codeset
, "WCHAR_T");
1329 if (*cd_towcp
== (iconv_t
) -1 || *cd_tombp
== (iconv_t
) -1)
1331 error (0, 0, gettext ("conversion modules not available"));
1332 if (*cd_towcp
!= (iconv_t
) -1)
1333 iconv_close (*cd_towcp
);
1338 /* One special case for historical reasons is the backslash
1339 character. In some codesets the byte value 0x5c is not mapped to
1340 U005c in Unicode. These charsets then don't have a backslash
1341 character at all. Therefore we have to live with whatever the
1342 codeset provides and recognize, instead of the U005c, the character
1343 the byte value 0x5c is mapped to. */
1349 wbufptr
= (char *) wbuf
;
1350 wbufsize
= sizeof (wbuf
);
1352 iconv (*cd_towcp
, &bufptr
, &bufsize
, &wbufptr
, &wbufsize
);
1353 if (bufsize
!= 0 || wbufsize
!= 0)
1355 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1356 on with using U005c. */
1357 error (0, 0, gettext ("cannot determine escape character"));
1358 *escape_charp
= L
'\\';
1361 *escape_charp
= wbuf
[0];