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
)
233 case ARGP_KEY_HELP_EXTRA
:
234 /* We print some extra information. */
235 if (asprintf (&tp
, gettext ("\
236 For bug reporting instructions, please see:\n\
237 %s.\n"), REPORT_BUGS_TO
) < 0)
243 return (char *) text
;
246 /* Print the version information. */
248 print_version (FILE *stream
, struct argp_state
*state
)
250 fprintf (stream
, "gencat %s%s\n", PKGVERSION
, VERSION
);
251 fprintf (stream
, gettext ("\
252 Copyright (C) %s Free Software Foundation, Inc.\n\
253 This is free software; see the source for copying conditions. There is NO\n\
254 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
256 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
260 /* The address of this function will be assigned to the hook in the
265 /* We don't want the program name to be printed in messages. Emacs'
266 compile.el does not like this. */
270 static struct catalog
*
271 read_input_file (struct catalog
*current
, const char *fname
)
279 iconv_t cd_towc
= (iconv_t
) -1;
280 iconv_t cd_tomb
= (iconv_t
) -1;
281 wchar_t escape_char
= L
'\\';
282 char *codeset
= NULL
;
284 if (strcmp (fname
, "-") == 0 || strcmp (fname
, "/dev/stdin") == 0)
287 fname
= gettext ("*standard input*");
290 fp
= fopen (fname
, "r");
293 error (0, errno
, gettext ("cannot open input file `%s'"), fname
);
297 /* If we haven't seen anything yet, allocate result structure. */
300 current
= (struct catalog
*) xcalloc (1, sizeof (*current
));
302 #define obstack_chunk_alloc malloc
303 #define obstack_chunk_free free
304 obstack_init (¤t
->mem_pool
);
306 current
->current_set
= find_set (current
, NL_SETD
);
314 wbuf
= (wchar_t *) xmalloc (wbufsize
);
320 size_t start_line
= line_number
+ 1;
327 act_len
= getline (&buf
, &len
, fp
);
332 /* It the line continued? */
334 if (buf
[act_len
- 1] == '\n')
338 /* There might be more than one backslash at the end of
339 the line. Only if there is an odd number of them is
340 the line continued. */
341 if (act_len
> 0 && buf
[act_len
- 1] == '\\')
343 int temp_act_len
= act_len
;
348 continued
= !continued
;
350 while (temp_act_len
> 0 && buf
[temp_act_len
- 1] == '\\');
357 /* Append to currently selected line. */
358 obstack_grow (¤t
->mem_pool
, buf
, act_len
);
362 obstack_1grow (¤t
->mem_pool
, '\0');
363 this_line
= (char *) obstack_finish (¤t
->mem_pool
);
366 if (this_line
[0] == '$')
368 if (isblank (this_line
[1]))
371 while (isblank (this_line
[cnt
]))
373 if (strncmp (&this_line
[cnt
], "codeset=", 8) != 0)
374 /* This is a comment line. Do nothing. */;
375 else if (codeset
!= NULL
)
376 /* Ignore multiple codeset. */;
381 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
385 int len
= cnt
- start
;
386 codeset
= xmalloc (len
+ 1);
387 *((char *) mempcpy (codeset
, &this_line
[start
], len
))
392 else if (strncmp (&this_line
[1], "set", 3) == 0)
394 int cnt
= sizeof ("set");
396 const char *symbol
= NULL
;
397 while (isspace (this_line
[cnt
]))
400 if (isdigit (this_line
[cnt
]))
402 set_number
= atol (&this_line
[cnt
]);
404 /* If the given number for the character set is
405 higher than any we used for symbolic set names
406 avoid clashing by using only higher numbers for
407 the following symbolic definitions. */
408 if (set_number
> current
->last_set
)
409 current
->last_set
= set_number
;
413 /* See whether it is a reasonable identifier. */
415 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
420 /* No correct character found. */
421 error_at_line (0, 0, fname
, start_line
,
422 gettext ("illegal set number"));
427 /* We have found seomthing that looks like a
428 correct identifier. */
429 struct set_list
*runp
;
431 this_line
[cnt
] = '\0';
433 symbol
= &this_line
[start
];
435 /* Test whether the identifier was already used. */
436 runp
= current
->all_sets
;
438 if (runp
->symbol
!= NULL
439 && strcmp (runp
->symbol
, symbol
) == 0)
446 /* We cannot allow duplicate identifiers for
448 error_at_line (0, 0, fname
, start_line
,
449 gettext ("duplicate set definition"));
450 error_at_line (0, 0, runp
->fname
, runp
->line
,
452 this is the first definition"));
456 /* Allocate next free message set for identifier. */
457 set_number
= ++current
->last_set
;
463 /* We found a legal set number. */
464 current
->current_set
= find_set (current
, set_number
);
467 current
->current_set
->symbol
= symbol
;
468 current
->current_set
->fname
= fname
;
469 current
->current_set
->line
= start_line
;
472 else if (strncmp (&this_line
[1], "delset", 6) == 0)
474 int cnt
= sizeof ("delset");
475 while (isspace (this_line
[cnt
]))
478 if (isdigit (this_line
[cnt
]))
480 size_t set_number
= atol (&this_line
[cnt
]);
481 struct set_list
*set
;
483 /* Mark the message set with the given number as
485 set
= find_set (current
, set_number
);
490 /* See whether it is a reasonable identifier. */
492 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
496 error_at_line (0, 0, fname
, start_line
,
497 gettext ("illegal set number"));
501 struct set_list
*runp
;
503 this_line
[cnt
] = '\0';
505 symbol
= &this_line
[start
];
507 /* We have a symbolic set name. This name must
508 appear somewhere else in the catalogs read so
510 for (runp
= current
->all_sets
; runp
!= NULL
;
513 if (strcmp (runp
->symbol
, symbol
) == 0)
520 /* Name does not exist before. */
521 error_at_line (0, 0, fname
, start_line
,
522 gettext ("unknown set `%s'"), symbol
);
526 else if (strncmp (&this_line
[1], "quote", 5) == 0)
535 cnt
= sizeof ("quote");
536 while (isspace (this_line
[cnt
]))
539 /* We need the conversion. */
540 if (cd_towc
== (iconv_t
) -1
541 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
543 /* Something is wrong. */
546 /* Yes, the quote char can be '\0'; this means no quote
547 char. The function using the information works on
548 wide characters so we have to convert it here. */
549 buf
[0] = this_line
[cnt
];
554 wbufptr
= (char *) wbuf
;
557 /* Flush the state. */
558 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
560 iconv (cd_towc
, &bufptr
, &buflen
, &wbufptr
, &wbuflen
);
561 if (buflen
!= 0 || (wchar_t *) wbufptr
!= &wbuf
[2])
562 error_at_line (0, 0, fname
, start_line
,
563 gettext ("invalid quote character"));
565 /* Use the converted wide character. */
566 current
->quote_char
= wbuf
[0];
572 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
574 this_line
[cnt
] = '\0';
575 error_at_line (0, 0, fname
, start_line
,
576 gettext ("unknown directive `%s': line ignored"),
580 else if (isalnum (this_line
[0]) || this_line
[0] == '_')
582 const char *ident
= this_line
;
583 char *line
= this_line
;
588 while (line
[0] != '\0' && !isspace (line
[0]));
590 *line
++ = '\0'; /* Terminate the identifier. */
592 /* Now we found the beginning of the message itself. */
594 if (isdigit (ident
[0]))
596 struct message_list
*runp
;
597 struct message_list
*lastp
;
599 message_number
= atoi (ident
);
601 /* Find location to insert the new message. */
602 runp
= current
->current_set
->messages
;
605 if (runp
->number
== message_number
)
614 /* Oh, oh. There is already a message with this
615 number in the message set. */
616 if (runp
->symbol
== NULL
)
618 /* The existing message had its number specified
619 by the user. Fatal collision type uh, oh. */
620 error_at_line (0, 0, fname
, start_line
,
621 gettext ("duplicated message number"));
622 error_at_line (0, 0, runp
->fname
, runp
->line
,
623 gettext ("this is the first definition"));
628 /* Collision was with number auto-assigned to a
629 symbolic. Change existing symbolic number
630 and move to end the list (if not already there). */
631 runp
->number
= ++current
->current_set
->last_message
;
633 if (runp
->next
!= NULL
)
635 struct message_list
*endp
;
638 current
->current_set
->messages
=runp
->next
;
640 lastp
->next
=runp
->next
;
643 while (endp
->next
!= NULL
)
651 ident
= NULL
; /* We don't have a symbol. */
653 if (message_number
!= 0
654 && message_number
> current
->current_set
->last_message
)
655 current
->current_set
->last_message
= message_number
;
657 else if (ident
[0] != '\0')
659 struct message_list
*runp
;
661 /* Test whether the symbolic name was not used for
662 another message in this message set. */
663 runp
= current
->current_set
->messages
;
665 if (runp
->symbol
!= NULL
&& strcmp (ident
, runp
->symbol
) == 0)
671 /* The name is already used. */
672 error_at_line (0, 0, fname
, start_line
, gettext ("\
673 duplicated message identifier"));
674 error_at_line (0, 0, runp
->fname
, runp
->line
,
675 gettext ("this is the first definition"));
679 /* Give the message the next unused number. */
680 message_number
= ++current
->current_set
->last_message
;
685 if (message_number
!= 0)
691 struct message_list
*newp
;
692 size_t line_len
= strlen (line
) + 1;
693 size_t ident_len
= 0;
695 /* We need the conversion. */
696 if (cd_towc
== (iconv_t
) -1
697 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
699 /* Something is wrong. */
702 /* Convert to a wide character string. We have to
703 interpret escape sequences which will be impossible
704 without doing the conversion if the codeset of the
705 message is stateful. */
710 outbuf
= (char *) wbuf
;
713 /* Flush the state. */
714 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
716 iconv (cd_towc
, &inbuf
, &inlen
, &outbuf
, &outlen
);
719 /* The string is converted. */
720 assert (outlen
< wbufsize
);
721 assert (wbuf
[(wbufsize
- outlen
) / sizeof (wchar_t) - 1]
728 /* Something is wrong with this string, we ignore it. */
729 error_at_line (0, 0, fname
, start_line
, gettext ("\
730 invalid character: message ignored"));
734 /* The output buffer is too small. */
736 wbuf
= (wchar_t *) xrealloc (wbuf
, wbufsize
);
739 /* Strip quote characters, change escape sequences into
740 correct characters etc. */
741 normalize_line (fname
, start_line
, cd_towc
, wbuf
,
742 current
->quote_char
, escape_char
);
745 ident_len
= line
- this_line
;
747 /* Now the string is free of escape sequences. Convert it
748 back into a multibyte character string. First free the
749 memory allocated for the original string. */
750 obstack_free (¤t
->mem_pool
, this_line
);
752 used
= 1; /* Yes, we use the line. */
754 /* Now fill in the new string. It should never happen that
755 the replaced string is longer than the original. */
756 inbuf
= (char *) wbuf
;
757 inlen
= (wcslen (wbuf
) + 1) * sizeof (wchar_t);
759 outlen
= obstack_room (¤t
->mem_pool
);
760 obstack_blank (¤t
->mem_pool
, outlen
);
761 this_line
= (char *) obstack_base (¤t
->mem_pool
);
762 outbuf
= this_line
+ ident_len
;
765 /* Flush the state. */
766 iconv (cd_tomb
, NULL
, NULL
, NULL
, NULL
);
768 iconv (cd_tomb
, &inbuf
, &inlen
, &outbuf
, &outlen
);
771 error_at_line (0, 0, fname
, start_line
,
772 gettext ("invalid line"));
775 assert (outbuf
[-1] == '\0');
777 /* Free the memory in the obstack we don't use. */
778 obstack_blank (¤t
->mem_pool
, -(int) outlen
);
779 line
= obstack_finish (¤t
->mem_pool
);
781 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
782 newp
->number
= message_number
;
783 newp
->message
= line
+ ident_len
;
784 /* Remember symbolic name; is NULL if no is given. */
785 newp
->symbol
= ident
? line
: NULL
;
786 /* Remember where we found the character. */
788 newp
->line
= start_line
;
790 /* Find place to insert to message. We keep them in a
791 sorted single linked list. */
792 if (current
->current_set
->messages
== NULL
793 || current
->current_set
->messages
->number
> message_number
)
795 newp
->next
= current
->current_set
->messages
;
796 current
->current_set
->messages
= newp
;
800 struct message_list
*runp
;
801 runp
= current
->current_set
->messages
;
802 while (runp
->next
!= NULL
)
803 if (runp
->next
->number
> message_number
)
807 newp
->next
= runp
->next
;
811 ++current
->total_messages
;
818 /* See whether we have any non-white space character in this
820 while (this_line
[cnt
] != '\0' && isspace (this_line
[cnt
]))
823 if (this_line
[cnt
] != '\0')
824 /* Yes, some unknown characters found. */
825 error_at_line (0, 0, fname
, start_line
,
826 gettext ("malformed line ignored"));
830 /* We can save the memory for the line if it was not used. */
832 obstack_free (¤t
->mem_pool
, this_line
);
835 /* Close the conversion modules. */
836 iconv_close (cd_towc
);
837 iconv_close (cd_tomb
);
850 write_out (struct catalog
*catalog
, const char *output_name
,
851 const char *header_name
)
853 /* Computing the "optimal" size. */
854 struct set_list
*set_run
;
855 size_t best_total
, best_size
, best_depth
;
856 size_t act_size
, act_depth
;
857 struct catalog_obj obj
;
858 struct obstack string_pool
;
861 uint32_t *array1
, *array2
;
865 /* If not otherwise told try to read file with existing
868 read_old (catalog
, output_name
);
870 /* Initialize best_size with a very high value. */
871 best_total
= best_size
= best_depth
= UINT_MAX
;
873 /* We need some start size for testing. Let's start with
874 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
876 act_size
= 1 + catalog
->total_messages
/ 5;
878 /* We determine the size of a hash table here. Because the message
879 numbers can be chosen arbitrary by the programmer we cannot use
880 the simple method of accessing the array using the message
881 number. The algorithm is based on the trivial hash function
882 NUMBER % TABLE_SIZE, where collisions are stored in a second
883 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
884 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
885 while (act_size
<= best_total
)
887 size_t deep
[act_size
];
890 memset (deep
, '\0', act_size
* sizeof (size_t));
891 set_run
= catalog
->all_sets
;
892 while (set_run
!= NULL
)
894 struct message_list
*message_run
;
896 message_run
= set_run
->messages
;
897 while (message_run
!= NULL
)
899 size_t idx
= (message_run
->number
* set_run
->number
) % act_size
;
902 if (deep
[idx
] > act_depth
)
904 act_depth
= deep
[idx
];
905 if (act_depth
* act_size
> best_total
)
908 message_run
= message_run
->next
;
910 set_run
= set_run
->next
;
913 if (act_depth
* act_size
<= best_total
)
915 /* We have found a better solution. */
916 best_total
= act_depth
* act_size
;
917 best_size
= act_size
;
918 best_depth
= act_depth
;
924 /* let's be prepared for an empty message file. */
925 if (best_size
== UINT_MAX
)
931 /* OK, now we have the size we will use. Fill in the header, build
932 the table and the second one with swapped byte order. */
933 obj
.magic
= CATGETS_MAGIC
;
934 obj
.plane_size
= best_size
;
935 obj
.plane_depth
= best_depth
;
937 /* Allocate room for all needed arrays. */
939 (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
940 memset (array1
, '\0', best_size
* best_depth
* sizeof (uint32_t) * 3);
942 = (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
943 obstack_init (&string_pool
);
945 set_run
= catalog
->all_sets
;
946 while (set_run
!= NULL
)
948 struct message_list
*message_run
;
950 message_run
= set_run
->messages
;
951 while (message_run
!= NULL
)
953 size_t idx
= (((message_run
->number
* set_run
->number
) % best_size
)
955 /* Determine collision depth. */
956 while (array1
[idx
] != 0)
957 idx
+= best_size
* 3;
959 /* Store set number, message number and pointer into string
960 space, relative to the first string. */
961 array1
[idx
+ 0] = set_run
->number
;
962 array1
[idx
+ 1] = message_run
->number
;
963 array1
[idx
+ 2] = obstack_object_size (&string_pool
);
965 /* Add current string to the continuous space containing all
967 obstack_grow0 (&string_pool
, message_run
->message
,
968 strlen (message_run
->message
));
970 message_run
= message_run
->next
;
973 set_run
= set_run
->next
;
975 strings_size
= obstack_object_size (&string_pool
);
976 strings
= obstack_finish (&string_pool
);
978 /* Compute ARRAY2 by changing the byte order. */
979 for (cnt
= 0; cnt
< best_size
* best_depth
* 3; ++cnt
)
980 array2
[cnt
] = SWAPU32 (array1
[cnt
]);
982 /* Now we can write out the whole data. */
983 if (strcmp (output_name
, "-") == 0
984 || strcmp (output_name
, "/dev/stdout") == 0)
988 fd
= creat (output_name
, 0666);
990 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
994 /* Write out header. */
995 write (fd
, &obj
, sizeof (obj
));
997 /* We always write out the little endian version of the index
999 #if __BYTE_ORDER == __LITTLE_ENDIAN
1000 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1001 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1002 #elif __BYTE_ORDER == __BIG_ENDIAN
1003 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1004 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1006 # error Cannot handle __BYTE_ORDER byte order
1009 /* Finally write the strings. */
1010 write (fd
, strings
, strings_size
);
1012 if (fd
!= STDOUT_FILENO
)
1015 /* If requested now write out the header file. */
1016 if (header_name
!= NULL
)
1021 /* Open output file. "-" or "/dev/stdout" means write to
1023 if (strcmp (header_name
, "-") == 0
1024 || strcmp (header_name
, "/dev/stdout") == 0)
1028 fp
= fopen (header_name
, "w");
1030 error (EXIT_FAILURE
, errno
,
1031 gettext ("cannot open output file `%s'"), header_name
);
1034 /* Iterate over all sets and all messages. */
1035 set_run
= catalog
->all_sets
;
1036 while (set_run
!= NULL
)
1038 struct message_list
*message_run
;
1040 /* If the current message set has a symbolic name write this
1042 if (set_run
->symbol
!= NULL
)
1043 fprintf (fp
, "%s#define %sSet %#x\t/* %s:%Zu */\n",
1044 first
? "" : "\n", set_run
->symbol
, set_run
->number
- 1,
1045 set_run
->fname
, set_run
->line
);
1048 message_run
= set_run
->messages
;
1049 while (message_run
!= NULL
)
1051 /* If the current message has a symbolic name write
1052 #define out. But we have to take care for the set
1053 not having a symbolic name. */
1054 if (message_run
->symbol
!= NULL
)
1056 if (set_run
->symbol
== NULL
)
1057 fprintf (fp
, "#define AutomaticSet%d%s %#x\t/* %s:%Zu */\n",
1058 set_run
->number
, message_run
->symbol
,
1059 message_run
->number
, message_run
->fname
,
1062 fprintf (fp
, "#define %s%s %#x\t/* %s:%Zu */\n",
1063 set_run
->symbol
, message_run
->symbol
,
1064 message_run
->number
, message_run
->fname
,
1068 message_run
= message_run
->next
;
1071 set_run
= set_run
->next
;
1080 static struct set_list
*
1081 find_set (struct catalog
*current
, int number
)
1083 struct set_list
*result
= current
->all_sets
;
1085 /* We must avoid set number 0 because a set of this number signals
1086 in the tables that the entry is not occupied. */
1089 while (result
!= NULL
)
1090 if (result
->number
== number
)
1093 result
= result
->next
;
1095 /* Prepare new message set. */
1096 result
= (struct set_list
*) xcalloc (1, sizeof (*result
));
1097 result
->number
= number
;
1098 result
->next
= current
->all_sets
;
1099 current
->all_sets
= result
;
1105 /* Normalize given string *in*place* by processing escape sequences
1106 and quote characters. */
1108 normalize_line (const char *fname
, size_t line
, iconv_t cd
, wchar_t *string
,
1109 wchar_t quote_char
, wchar_t escape_char
)
1112 wchar_t *rp
= string
;
1113 wchar_t *wp
= string
;
1115 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1123 while (*rp
!= L
'\0')
1124 if (*rp
== quote_char
)
1125 /* We simply end the string when we find the first time an
1126 not-escaped quote character. */
1128 else if (*rp
== escape_char
)
1131 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1132 /* This is an extension to XPG. */
1135 /* Recognize escape sequences. */
1172 number
= *rp
++ - L
'0';
1173 while (number
<= (255 / 8) && *rp
>= L
'0' && *rp
<= L
'7')
1176 number
+= *rp
++ - L
'0';
1179 cbuf
[0] = (char) number
;
1184 wcbufptr
= (char *) wcbuf
;
1185 wcbufin
= sizeof (wcbuf
);
1187 /* Flush the state. */
1188 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
1190 iconv (cd
, &cbufptr
, &cbufin
, &wcbufptr
, &wcbufin
);
1191 if (cbufptr
!= &cbuf
[2] || (wchar_t *) wcbufptr
!= &wcbuf
[2])
1192 error_at_line (0, 0, fname
, line
,
1193 gettext ("invalid escape sequence"));
1199 if (*rp
== escape_char
)
1201 *wp
++ = escape_char
;
1205 /* Simply ignore the backslash character. */;
1212 /* If we saw a quote character at the beginning we expect another
1214 if (is_quoted
&& *rp
!= quote_char
)
1215 error_at_line (0, 0, fname
, line
, gettext ("unterminated message"));
1217 /* Terminate string. */
1224 read_old (struct catalog
*catalog
, const char *file_name
)
1226 struct catalog_info old_cat_obj
;
1227 struct set_list
*set
= NULL
;
1231 /* Try to open catalog, but don't look through the NLSPATH. */
1232 if (__open_catalog (file_name
, NULL
, NULL
, &old_cat_obj
) != 0)
1234 if (errno
== ENOENT
)
1235 /* No problem, the catalog simply does not exist. */
1238 error (EXIT_FAILURE
, errno
,
1239 gettext ("while opening old catalog file"));
1242 /* OK, we have the catalog loaded. Now read all messages and merge
1243 them. When set and message number clash for any message the new
1244 one is used. If the new one is empty it indicates that the
1245 message should be deleted. */
1246 for (cnt
= 0; cnt
< old_cat_obj
.plane_size
* old_cat_obj
.plane_depth
; ++cnt
)
1248 struct message_list
*message
, *last
;
1250 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] == 0)
1251 /* No message in this slot. */
1254 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1 != (uint32_t) last_set
)
1256 last_set
= old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1;
1257 set
= find_set (catalog
, old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1);
1261 message
= set
->messages
;
1262 while (message
!= NULL
)
1264 if ((uint32_t) message
->number
>= old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1267 message
= message
->next
;
1271 || (uint32_t) message
->number
> old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1273 /* We have found a message which is not yet in the catalog.
1274 Insert it at the right position. */
1275 struct message_list
*newp
;
1277 newp
= (struct message_list
*) xmalloc (sizeof(*newp
));
1278 newp
->number
= old_cat_obj
.name_ptr
[cnt
* 3 + 1];
1280 &old_cat_obj
.strings
[old_cat_obj
.name_ptr
[cnt
* 3 + 2]];
1283 newp
->symbol
= NULL
;
1284 newp
->next
= message
;
1287 set
->messages
= newp
;
1291 ++catalog
->total_messages
;
1293 else if (*message
->message
== '\0')
1295 /* The new empty message has overridden the old one thus
1296 "deleting" it as required. Now remove the empty remains. */
1298 set
->messages
= message
->next
;
1300 last
->next
= message
->next
;
1307 open_conversion (const char *codeset
, iconv_t
*cd_towcp
, iconv_t
*cd_tombp
,
1308 wchar_t *escape_charp
)
1317 /* If the input file does not specify the codeset use the locale's. */
1318 if (codeset
== NULL
)
1320 setlocale (LC_ALL
, "");
1321 codeset
= nl_langinfo (CODESET
);
1322 setlocale (LC_ALL
, "C");
1325 /* Get the conversion modules. */
1326 *cd_towcp
= iconv_open ("WCHAR_T", codeset
);
1327 *cd_tombp
= iconv_open (codeset
, "WCHAR_T");
1328 if (*cd_towcp
== (iconv_t
) -1 || *cd_tombp
== (iconv_t
) -1)
1330 error (0, 0, gettext ("conversion modules not available"));
1331 if (*cd_towcp
!= (iconv_t
) -1)
1332 iconv_close (*cd_towcp
);
1337 /* One special case for historical reasons is the backslash
1338 character. In some codesets the byte value 0x5c is not mapped to
1339 U005c in Unicode. These charsets then don't have a backslash
1340 character at all. Therefore we have to live with whatever the
1341 codeset provides and recognize, instead of the U005c, the character
1342 the byte value 0x5c is mapped to. */
1348 wbufptr
= (char *) wbuf
;
1349 wbufsize
= sizeof (wbuf
);
1351 iconv (*cd_towcp
, &bufptr
, &bufsize
, &wbufptr
, &wbufsize
);
1352 if (bufsize
!= 0 || wbufsize
!= 0)
1354 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1355 on with using U005c. */
1356 error (0, 0, gettext ("cannot determine escape character"));
1357 *escape_charp
= L
'\\';
1360 *escape_charp
= wbuf
[0];