1 /* Copyright (C) 1996-2011, 2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 1996.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
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 __attribute_malloc__
__attribute_alloc_size (1);
142 extern void *xcalloc (size_t n
, size_t s
)
143 __attribute_malloc__
__attribute_alloc_size (1, 2);
144 extern void *xrealloc (void *o
, size_t n
)
145 __attribute_malloc__
__attribute_alloc_size (2);
146 extern char *xstrdup (const char *) __attribute_malloc__
;
148 /* Prototypes for local functions. */
149 static void error_print (void);
150 static struct catalog
*read_input_file (struct catalog
*current
,
152 static void write_out (struct catalog
*result
, const char *output_name
,
153 const char *header_name
);
154 static struct set_list
*find_set (struct catalog
*current
, int number
);
155 static void normalize_line (const char *fname
, size_t line
, iconv_t cd
,
156 wchar_t *string
, wchar_t quote_char
,
157 wchar_t escape_char
);
158 static void read_old (struct catalog
*catalog
, const char *file_name
);
159 static int open_conversion (const char *codesetp
, iconv_t
*cd_towcp
,
160 iconv_t
*cd_tombp
, wchar_t *escape_charp
);
164 main (int argc
, char *argv
[])
166 struct catalog
*result
;
169 /* Set program name for messages. */
170 error_print_progname
= error_print
;
172 /* Set locale via LC_ALL. */
173 setlocale (LC_ALL
, "");
175 /* Set the text message domain. */
176 textdomain (PACKAGE
);
178 /* Initialize local variables. */
181 /* Parse and process arguments. */
182 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
184 /* Determine output file. */
185 if (output_name
== NULL
)
186 output_name
= remaining
< argc
? argv
[remaining
++] : "-";
188 /* Process all input files. */
189 setlocale (LC_CTYPE
, "C");
190 if (remaining
< argc
)
192 result
= read_input_file (result
, argv
[remaining
]);
193 while (++remaining
< argc
);
195 result
= read_input_file (NULL
, "-");
197 /* Write out the result. */
199 write_out (result
, output_name
, header_name
);
201 return error_message_count
!= 0;
205 /* Handle program arguments. */
207 parse_opt (int key
, char *arg
, struct argp_state
*state
)
221 return ARGP_ERR_UNKNOWN
;
228 more_help (int key
, const char *text
, void *input
)
232 case ARGP_KEY_HELP_EXTRA
:
233 /* We print some extra information. */
234 return strdup (gettext ("\
235 For bug reporting instructions, please see:\n\
236 <http://www.gnu.org/software/libc/bugs.html>.\n"));
240 return (char *) text
;
243 /* Print the version information. */
245 print_version (FILE *stream
, struct argp_state
*state
)
247 fprintf (stream
, "gencat (GNU %s) %s\n", PACKAGE
, VERSION
);
248 fprintf (stream
, gettext ("\
249 Copyright (C) %s Free Software Foundation, Inc.\n\
250 This is free software; see the source for copying conditions. There is NO\n\
251 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
253 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
257 /* The address of this function will be assigned to the hook in the
262 /* We don't want the program name to be printed in messages. Emacs'
263 compile.el does not like this. */
267 static struct catalog
*
268 read_input_file (struct catalog
*current
, const char *fname
)
276 iconv_t cd_towc
= (iconv_t
) -1;
277 iconv_t cd_tomb
= (iconv_t
) -1;
278 wchar_t escape_char
= L
'\\';
279 char *codeset
= NULL
;
281 if (strcmp (fname
, "-") == 0 || strcmp (fname
, "/dev/stdin") == 0)
284 fname
= gettext ("*standard input*");
287 fp
= fopen (fname
, "r");
290 error (0, errno
, gettext ("cannot open input file `%s'"), fname
);
294 /* If we haven't seen anything yet, allocate result structure. */
297 current
= (struct catalog
*) xcalloc (1, sizeof (*current
));
299 #define obstack_chunk_alloc malloc
300 #define obstack_chunk_free free
301 obstack_init (¤t
->mem_pool
);
303 current
->current_set
= find_set (current
, NL_SETD
);
311 wbuf
= (wchar_t *) xmalloc (wbufsize
);
317 size_t start_line
= line_number
+ 1;
324 act_len
= getline (&buf
, &len
, fp
);
329 /* It the line continued? */
331 if (buf
[act_len
- 1] == '\n')
335 /* There might be more than one backslash at the end of
336 the line. Only if there is an odd number of them is
337 the line continued. */
338 if (act_len
> 0 && buf
[act_len
- 1] == '\\')
340 int temp_act_len
= act_len
;
345 continued
= !continued
;
347 while (temp_act_len
> 0 && buf
[temp_act_len
- 1] == '\\');
354 /* Append to currently selected line. */
355 obstack_grow (¤t
->mem_pool
, buf
, act_len
);
359 obstack_1grow (¤t
->mem_pool
, '\0');
360 this_line
= (char *) obstack_finish (¤t
->mem_pool
);
363 if (this_line
[0] == '$')
365 if (isblank (this_line
[1]))
368 while (isblank (this_line
[cnt
]))
370 if (strncmp (&this_line
[cnt
], "codeset=", 8) != 0)
371 /* This is a comment line. Do nothing. */;
372 else if (codeset
!= NULL
)
373 /* Ignore multiple codeset. */;
378 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
382 int len
= cnt
- start
;
383 codeset
= xmalloc (len
+ 1);
384 *((char *) mempcpy (codeset
, &this_line
[start
], len
))
389 else if (strncmp (&this_line
[1], "set", 3) == 0)
391 int cnt
= sizeof ("set");
393 const char *symbol
= NULL
;
394 while (isspace (this_line
[cnt
]))
397 if (isdigit (this_line
[cnt
]))
399 set_number
= atol (&this_line
[cnt
]);
401 /* If the given number for the character set is
402 higher than any we used for symbolic set names
403 avoid clashing by using only higher numbers for
404 the following symbolic definitions. */
405 if (set_number
> current
->last_set
)
406 current
->last_set
= set_number
;
410 /* See whether it is a reasonable identifier. */
412 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
417 /* No correct character found. */
418 error_at_line (0, 0, fname
, start_line
,
419 gettext ("illegal set number"));
424 /* We have found seomthing that looks like a
425 correct identifier. */
426 struct set_list
*runp
;
428 this_line
[cnt
] = '\0';
430 symbol
= &this_line
[start
];
432 /* Test whether the identifier was already used. */
433 runp
= current
->all_sets
;
435 if (runp
->symbol
!= NULL
436 && strcmp (runp
->symbol
, symbol
) == 0)
443 /* We cannot allow duplicate identifiers for
445 error_at_line (0, 0, fname
, start_line
,
446 gettext ("duplicate set definition"));
447 error_at_line (0, 0, runp
->fname
, runp
->line
,
449 this is the first definition"));
453 /* Allocate next free message set for identifier. */
454 set_number
= ++current
->last_set
;
460 /* We found a legal set number. */
461 current
->current_set
= find_set (current
, set_number
);
464 current
->current_set
->symbol
= symbol
;
465 current
->current_set
->fname
= fname
;
466 current
->current_set
->line
= start_line
;
469 else if (strncmp (&this_line
[1], "delset", 6) == 0)
471 int cnt
= sizeof ("delset");
472 while (isspace (this_line
[cnt
]))
475 if (isdigit (this_line
[cnt
]))
477 size_t set_number
= atol (&this_line
[cnt
]);
478 struct set_list
*set
;
480 /* Mark the message set with the given number as
482 set
= find_set (current
, set_number
);
487 /* See whether it is a reasonable identifier. */
489 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
493 error_at_line (0, 0, fname
, start_line
,
494 gettext ("illegal set number"));
498 struct set_list
*runp
;
500 this_line
[cnt
] = '\0';
502 symbol
= &this_line
[start
];
504 /* We have a symbolic set name. This name must
505 appear somewhere else in the catalogs read so
507 for (runp
= current
->all_sets
; runp
!= NULL
;
510 if (strcmp (runp
->symbol
, symbol
) == 0)
517 /* Name does not exist before. */
518 error_at_line (0, 0, fname
, start_line
,
519 gettext ("unknown set `%s'"), symbol
);
523 else if (strncmp (&this_line
[1], "quote", 5) == 0)
532 cnt
= sizeof ("quote");
533 while (isspace (this_line
[cnt
]))
536 /* We need the conversion. */
537 if (cd_towc
== (iconv_t
) -1
538 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
540 /* Something is wrong. */
543 /* Yes, the quote char can be '\0'; this means no quote
544 char. The function using the information works on
545 wide characters so we have to convert it here. */
546 buf
[0] = this_line
[cnt
];
551 wbufptr
= (char *) wbuf
;
554 /* Flush the state. */
555 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
557 iconv (cd_towc
, &bufptr
, &buflen
, &wbufptr
, &wbuflen
);
558 if (buflen
!= 0 || (wchar_t *) wbufptr
!= &wbuf
[2])
559 error_at_line (0, 0, fname
, start_line
,
560 gettext ("invalid quote character"));
562 /* Use the converted wide character. */
563 current
->quote_char
= wbuf
[0];
569 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
571 this_line
[cnt
] = '\0';
572 error_at_line (0, 0, fname
, start_line
,
573 gettext ("unknown directive `%s': line ignored"),
577 else if (isalnum (this_line
[0]) || this_line
[0] == '_')
579 const char *ident
= this_line
;
580 char *line
= this_line
;
585 while (line
[0] != '\0' && !isspace (line
[0]));
587 *line
++ = '\0'; /* Terminate the identifier. */
589 /* Now we found the beginning of the message itself. */
591 if (isdigit (ident
[0]))
593 struct message_list
*runp
;
594 struct message_list
*lastp
;
596 message_number
= atoi (ident
);
598 /* Find location to insert the new message. */
599 runp
= current
->current_set
->messages
;
602 if (runp
->number
== message_number
)
611 /* Oh, oh. There is already a message with this
612 number in the message set. */
613 if (runp
->symbol
== NULL
)
615 /* The existing message had its number specified
616 by the user. Fatal collision type uh, oh. */
617 error_at_line (0, 0, fname
, start_line
,
618 gettext ("duplicated message number"));
619 error_at_line (0, 0, runp
->fname
, runp
->line
,
620 gettext ("this is the first definition"));
625 /* Collision was with number auto-assigned to a
626 symbolic. Change existing symbolic number
627 and move to end the list (if not already there). */
628 runp
->number
= ++current
->current_set
->last_message
;
630 if (runp
->next
!= NULL
)
632 struct message_list
*endp
;
635 current
->current_set
->messages
=runp
->next
;
637 lastp
->next
=runp
->next
;
640 while (endp
->next
!= NULL
)
648 ident
= NULL
; /* We don't have a symbol. */
650 if (message_number
!= 0
651 && message_number
> current
->current_set
->last_message
)
652 current
->current_set
->last_message
= message_number
;
654 else if (ident
[0] != '\0')
656 struct message_list
*runp
;
658 /* Test whether the symbolic name was not used for
659 another message in this message set. */
660 runp
= current
->current_set
->messages
;
662 if (runp
->symbol
!= NULL
&& strcmp (ident
, runp
->symbol
) == 0)
668 /* The name is already used. */
669 error_at_line (0, 0, fname
, start_line
, gettext ("\
670 duplicated message identifier"));
671 error_at_line (0, 0, runp
->fname
, runp
->line
,
672 gettext ("this is the first definition"));
676 /* Give the message the next unused number. */
677 message_number
= ++current
->current_set
->last_message
;
682 if (message_number
!= 0)
688 struct message_list
*newp
;
689 size_t line_len
= strlen (line
) + 1;
690 size_t ident_len
= 0;
692 /* We need the conversion. */
693 if (cd_towc
== (iconv_t
) -1
694 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
696 /* Something is wrong. */
699 /* Convert to a wide character string. We have to
700 interpret escape sequences which will be impossible
701 without doing the conversion if the codeset of the
702 message is stateful. */
707 outbuf
= (char *) wbuf
;
710 /* Flush the state. */
711 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
713 iconv (cd_towc
, &inbuf
, &inlen
, &outbuf
, &outlen
);
716 /* The string is converted. */
717 assert (outlen
< wbufsize
);
718 assert (wbuf
[(wbufsize
- outlen
) / sizeof (wchar_t) - 1]
725 /* Something is wrong with this string, we ignore it. */
726 error_at_line (0, 0, fname
, start_line
, gettext ("\
727 invalid character: message ignored"));
731 /* The output buffer is too small. */
733 wbuf
= (wchar_t *) xrealloc (wbuf
, wbufsize
);
736 /* Strip quote characters, change escape sequences into
737 correct characters etc. */
738 normalize_line (fname
, start_line
, cd_towc
, wbuf
,
739 current
->quote_char
, escape_char
);
742 ident_len
= line
- this_line
;
744 /* Now the string is free of escape sequences. Convert it
745 back into a multibyte character string. First free the
746 memory allocated for the original string. */
747 obstack_free (¤t
->mem_pool
, this_line
);
749 used
= 1; /* Yes, we use the line. */
751 /* Now fill in the new string. It should never happen that
752 the replaced string is longer than the original. */
753 inbuf
= (char *) wbuf
;
754 inlen
= (wcslen (wbuf
) + 1) * sizeof (wchar_t);
756 outlen
= obstack_room (¤t
->mem_pool
);
757 obstack_blank (¤t
->mem_pool
, outlen
);
758 this_line
= (char *) obstack_base (¤t
->mem_pool
);
759 outbuf
= this_line
+ ident_len
;
762 /* Flush the state. */
763 iconv (cd_tomb
, NULL
, NULL
, NULL
, NULL
);
765 iconv (cd_tomb
, &inbuf
, &inlen
, &outbuf
, &outlen
);
768 error_at_line (0, 0, fname
, start_line
,
769 gettext ("invalid line"));
772 assert (outbuf
[-1] == '\0');
774 /* Free the memory in the obstack we don't use. */
775 obstack_blank (¤t
->mem_pool
, -(int) outlen
);
776 line
= obstack_finish (¤t
->mem_pool
);
778 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
779 newp
->number
= message_number
;
780 newp
->message
= line
+ ident_len
;
781 /* Remember symbolic name; is NULL if no is given. */
782 newp
->symbol
= ident
? line
: NULL
;
783 /* Remember where we found the character. */
785 newp
->line
= start_line
;
787 /* Find place to insert to message. We keep them in a
788 sorted single linked list. */
789 if (current
->current_set
->messages
== NULL
790 || current
->current_set
->messages
->number
> message_number
)
792 newp
->next
= current
->current_set
->messages
;
793 current
->current_set
->messages
= newp
;
797 struct message_list
*runp
;
798 runp
= current
->current_set
->messages
;
799 while (runp
->next
!= NULL
)
800 if (runp
->next
->number
> message_number
)
804 newp
->next
= runp
->next
;
808 ++current
->total_messages
;
815 /* See whether we have any non-white space character in this
817 while (this_line
[cnt
] != '\0' && isspace (this_line
[cnt
]))
820 if (this_line
[cnt
] != '\0')
821 /* Yes, some unknown characters found. */
822 error_at_line (0, 0, fname
, start_line
,
823 gettext ("malformed line ignored"));
827 /* We can save the memory for the line if it was not used. */
829 obstack_free (¤t
->mem_pool
, this_line
);
832 /* Close the conversion modules. */
833 iconv_close (cd_towc
);
834 iconv_close (cd_tomb
);
847 write_out (struct catalog
*catalog
, const char *output_name
,
848 const char *header_name
)
850 /* Computing the "optimal" size. */
851 struct set_list
*set_run
;
852 size_t best_total
, best_size
, best_depth
;
853 size_t act_size
, act_depth
;
854 struct catalog_obj obj
;
855 struct obstack string_pool
;
858 uint32_t *array1
, *array2
;
862 /* If not otherwise told try to read file with existing
865 read_old (catalog
, output_name
);
867 /* Initialize best_size with a very high value. */
868 best_total
= best_size
= best_depth
= UINT_MAX
;
870 /* We need some start size for testing. Let's start with
871 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
873 act_size
= 1 + catalog
->total_messages
/ 5;
875 /* We determine the size of a hash table here. Because the message
876 numbers can be chosen arbitrary by the programmer we cannot use
877 the simple method of accessing the array using the message
878 number. The algorithm is based on the trivial hash function
879 NUMBER % TABLE_SIZE, where collisions are stored in a second
880 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
881 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
882 while (act_size
<= best_total
)
884 size_t deep
[act_size
];
887 memset (deep
, '\0', act_size
* sizeof (size_t));
888 set_run
= catalog
->all_sets
;
889 while (set_run
!= NULL
)
891 struct message_list
*message_run
;
893 message_run
= set_run
->messages
;
894 while (message_run
!= NULL
)
896 size_t idx
= (message_run
->number
* set_run
->number
) % act_size
;
899 if (deep
[idx
] > act_depth
)
901 act_depth
= deep
[idx
];
902 if (act_depth
* act_size
> best_total
)
905 message_run
= message_run
->next
;
907 set_run
= set_run
->next
;
910 if (act_depth
* act_size
<= best_total
)
912 /* We have found a better solution. */
913 best_total
= act_depth
* act_size
;
914 best_size
= act_size
;
915 best_depth
= act_depth
;
921 /* let's be prepared for an empty message file. */
922 if (best_size
== UINT_MAX
)
928 /* OK, now we have the size we will use. Fill in the header, build
929 the table and the second one with swapped byte order. */
930 obj
.magic
= CATGETS_MAGIC
;
931 obj
.plane_size
= best_size
;
932 obj
.plane_depth
= best_depth
;
934 /* Allocate room for all needed arrays. */
936 (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
937 memset (array1
, '\0', best_size
* best_depth
* sizeof (uint32_t) * 3);
939 = (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
940 obstack_init (&string_pool
);
942 set_run
= catalog
->all_sets
;
943 while (set_run
!= NULL
)
945 struct message_list
*message_run
;
947 message_run
= set_run
->messages
;
948 while (message_run
!= NULL
)
950 size_t idx
= (((message_run
->number
* set_run
->number
) % best_size
)
952 /* Determine collision depth. */
953 while (array1
[idx
] != 0)
954 idx
+= best_size
* 3;
956 /* Store set number, message number and pointer into string
957 space, relative to the first string. */
958 array1
[idx
+ 0] = set_run
->number
;
959 array1
[idx
+ 1] = message_run
->number
;
960 array1
[idx
+ 2] = obstack_object_size (&string_pool
);
962 /* Add current string to the continuous space containing all
964 obstack_grow0 (&string_pool
, message_run
->message
,
965 strlen (message_run
->message
));
967 message_run
= message_run
->next
;
970 set_run
= set_run
->next
;
972 strings_size
= obstack_object_size (&string_pool
);
973 strings
= obstack_finish (&string_pool
);
975 /* Compute ARRAY2 by changing the byte order. */
976 for (cnt
= 0; cnt
< best_size
* best_depth
* 3; ++cnt
)
977 array2
[cnt
] = SWAPU32 (array1
[cnt
]);
979 /* Now we can write out the whole data. */
980 if (strcmp (output_name
, "-") == 0
981 || strcmp (output_name
, "/dev/stdout") == 0)
985 fd
= creat (output_name
, 0666);
987 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
991 /* Write out header. */
992 write (fd
, &obj
, sizeof (obj
));
994 /* We always write out the little endian version of the index
996 #if __BYTE_ORDER == __LITTLE_ENDIAN
997 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
998 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
999 #elif __BYTE_ORDER == __BIG_ENDIAN
1000 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1001 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1003 # error Cannot handle __BYTE_ORDER byte order
1006 /* Finally write the strings. */
1007 write (fd
, strings
, strings_size
);
1009 if (fd
!= STDOUT_FILENO
)
1012 /* If requested now write out the header file. */
1013 if (header_name
!= NULL
)
1018 /* Open output file. "-" or "/dev/stdout" means write to
1020 if (strcmp (header_name
, "-") == 0
1021 || strcmp (header_name
, "/dev/stdout") == 0)
1025 fp
= fopen (header_name
, "w");
1027 error (EXIT_FAILURE
, errno
,
1028 gettext ("cannot open output file `%s'"), header_name
);
1031 /* Iterate over all sets and all messages. */
1032 set_run
= catalog
->all_sets
;
1033 while (set_run
!= NULL
)
1035 struct message_list
*message_run
;
1037 /* If the current message set has a symbolic name write this
1039 if (set_run
->symbol
!= NULL
)
1040 fprintf (fp
, "%s#define %sSet %#x\t/* %s:%Zu */\n",
1041 first
? "" : "\n", set_run
->symbol
, set_run
->number
- 1,
1042 set_run
->fname
, set_run
->line
);
1045 message_run
= set_run
->messages
;
1046 while (message_run
!= NULL
)
1048 /* If the current message has a symbolic name write
1049 #define out. But we have to take care for the set
1050 not having a symbolic name. */
1051 if (message_run
->symbol
!= NULL
)
1053 if (set_run
->symbol
== NULL
)
1054 fprintf (fp
, "#define AutomaticSet%d%s %#x\t/* %s:%Zu */\n",
1055 set_run
->number
, message_run
->symbol
,
1056 message_run
->number
, message_run
->fname
,
1059 fprintf (fp
, "#define %s%s %#x\t/* %s:%Zu */\n",
1060 set_run
->symbol
, message_run
->symbol
,
1061 message_run
->number
, message_run
->fname
,
1065 message_run
= message_run
->next
;
1068 set_run
= set_run
->next
;
1077 static struct set_list
*
1078 find_set (struct catalog
*current
, int number
)
1080 struct set_list
*result
= current
->all_sets
;
1082 /* We must avoid set number 0 because a set of this number signals
1083 in the tables that the entry is not occupied. */
1086 while (result
!= NULL
)
1087 if (result
->number
== number
)
1090 result
= result
->next
;
1092 /* Prepare new message set. */
1093 result
= (struct set_list
*) xcalloc (1, sizeof (*result
));
1094 result
->number
= number
;
1095 result
->next
= current
->all_sets
;
1096 current
->all_sets
= result
;
1102 /* Normalize given string *in*place* by processing escape sequences
1103 and quote characters. */
1105 normalize_line (const char *fname
, size_t line
, iconv_t cd
, wchar_t *string
,
1106 wchar_t quote_char
, wchar_t escape_char
)
1109 wchar_t *rp
= string
;
1110 wchar_t *wp
= string
;
1112 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1120 while (*rp
!= L
'\0')
1121 if (*rp
== quote_char
)
1122 /* We simply end the string when we find the first time an
1123 not-escaped quote character. */
1125 else if (*rp
== escape_char
)
1128 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1129 /* This is an extension to XPG. */
1132 /* Recognize escape sequences. */
1169 number
= *rp
++ - L
'0';
1170 while (number
<= (255 / 8) && *rp
>= L
'0' && *rp
<= L
'7')
1173 number
+= *rp
++ - L
'0';
1176 cbuf
[0] = (char) number
;
1181 wcbufptr
= (char *) wcbuf
;
1182 wcbufin
= sizeof (wcbuf
);
1184 /* Flush the state. */
1185 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
1187 iconv (cd
, &cbufptr
, &cbufin
, &wcbufptr
, &wcbufin
);
1188 if (cbufptr
!= &cbuf
[2] || (wchar_t *) wcbufptr
!= &wcbuf
[2])
1189 error_at_line (0, 0, fname
, line
,
1190 gettext ("invalid escape sequence"));
1196 if (*rp
== escape_char
)
1198 *wp
++ = escape_char
;
1202 /* Simply ignore the backslash character. */;
1209 /* If we saw a quote character at the beginning we expect another
1211 if (is_quoted
&& *rp
!= quote_char
)
1212 error_at_line (0, 0, fname
, line
, gettext ("unterminated message"));
1214 /* Terminate string. */
1221 read_old (struct catalog
*catalog
, const char *file_name
)
1223 struct catalog_info old_cat_obj
;
1224 struct set_list
*set
= NULL
;
1228 /* Try to open catalog, but don't look through the NLSPATH. */
1229 if (__open_catalog (file_name
, NULL
, NULL
, &old_cat_obj
) != 0)
1231 if (errno
== ENOENT
)
1232 /* No problem, the catalog simply does not exist. */
1235 error (EXIT_FAILURE
, errno
,
1236 gettext ("while opening old catalog file"));
1239 /* OK, we have the catalog loaded. Now read all messages and merge
1240 them. When set and message number clash for any message the new
1241 one is used. If the new one is empty it indicates that the
1242 message should be deleted. */
1243 for (cnt
= 0; cnt
< old_cat_obj
.plane_size
* old_cat_obj
.plane_depth
; ++cnt
)
1245 struct message_list
*message
, *last
;
1247 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] == 0)
1248 /* No message in this slot. */
1251 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1 != (uint32_t) last_set
)
1253 last_set
= old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1;
1254 set
= find_set (catalog
, old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1);
1258 message
= set
->messages
;
1259 while (message
!= NULL
)
1261 if ((uint32_t) message
->number
>= old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1264 message
= message
->next
;
1268 || (uint32_t) message
->number
> old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1270 /* We have found a message which is not yet in the catalog.
1271 Insert it at the right position. */
1272 struct message_list
*newp
;
1274 newp
= (struct message_list
*) xmalloc (sizeof(*newp
));
1275 newp
->number
= old_cat_obj
.name_ptr
[cnt
* 3 + 1];
1277 &old_cat_obj
.strings
[old_cat_obj
.name_ptr
[cnt
* 3 + 2]];
1280 newp
->symbol
= NULL
;
1281 newp
->next
= message
;
1284 set
->messages
= newp
;
1288 ++catalog
->total_messages
;
1290 else if (*message
->message
== '\0')
1292 /* The new empty message has overridden the old one thus
1293 "deleting" it as required. Now remove the empty remains. */
1295 set
->messages
= message
->next
;
1297 last
->next
= message
->next
;
1304 open_conversion (const char *codeset
, iconv_t
*cd_towcp
, iconv_t
*cd_tombp
,
1305 wchar_t *escape_charp
)
1314 /* If the input file does not specify the codeset use the locale's. */
1315 if (codeset
== NULL
)
1317 setlocale (LC_ALL
, "");
1318 codeset
= nl_langinfo (CODESET
);
1319 setlocale (LC_ALL
, "C");
1322 /* Get the conversion modules. */
1323 *cd_towcp
= iconv_open ("WCHAR_T", codeset
);
1324 *cd_tombp
= iconv_open (codeset
, "WCHAR_T");
1325 if (*cd_towcp
== (iconv_t
) -1 || *cd_tombp
== (iconv_t
) -1)
1327 error (0, 0, gettext ("conversion modules not available"));
1328 if (*cd_towcp
!= (iconv_t
) -1)
1329 iconv_close (*cd_towcp
);
1334 /* One special case for historical reasons is the backslash
1335 character. In some codesets the byte value 0x5c is not mapped to
1336 U005c in Unicode. These charsets then don't have a backslash
1337 character at all. Therefore we have to live with whatever the
1338 codeset provides and recognize, instead of the U005c, the character
1339 the byte value 0x5c is mapped to. */
1345 wbufptr
= (char *) wbuf
;
1346 wbufsize
= sizeof (wbuf
);
1348 iconv (*cd_towcp
, &bufptr
, &bufsize
, &wbufptr
, &wbufsize
);
1349 if (bufsize
!= 0 || wbufsize
!= 0)
1351 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1352 on with using U005c. */
1353 error (0, 0, gettext ("cannot determine escape character"));
1354 *escape_charp
= L
'\\';
1357 *escape_charp
= wbuf
[0];