1 /* Copyright (C) 1996-2016 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 #include <programs/xmalloc.h>
142 /* Prototypes for local functions. */
143 static void error_print (void);
144 static struct catalog
*read_input_file (struct catalog
*current
,
146 static void write_out (struct catalog
*result
, const char *output_name
,
147 const char *header_name
);
148 static struct set_list
*find_set (struct catalog
*current
, int number
);
149 static void normalize_line (const char *fname
, size_t line
, iconv_t cd
,
150 wchar_t *string
, wchar_t quote_char
,
151 wchar_t escape_char
);
152 static void read_old (struct catalog
*catalog
, const char *file_name
);
153 static int open_conversion (const char *codesetp
, iconv_t
*cd_towcp
,
154 iconv_t
*cd_tombp
, wchar_t *escape_charp
);
158 main (int argc
, char *argv
[])
160 struct catalog
*result
;
163 /* Set program name for messages. */
164 error_print_progname
= error_print
;
166 /* Set locale via LC_ALL. */
167 setlocale (LC_ALL
, "");
169 /* Set the text message domain. */
170 textdomain (PACKAGE
);
172 /* Initialize local variables. */
175 /* Parse and process arguments. */
176 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
178 /* Determine output file. */
179 if (output_name
== NULL
)
180 output_name
= remaining
< argc
? argv
[remaining
++] : "-";
182 /* Process all input files. */
183 setlocale (LC_CTYPE
, "C");
184 if (remaining
< argc
)
186 result
= read_input_file (result
, argv
[remaining
]);
187 while (++remaining
< argc
);
189 result
= read_input_file (NULL
, "-");
191 /* Write out the result. */
193 write_out (result
, output_name
, header_name
);
195 return error_message_count
!= 0;
199 /* Handle program arguments. */
201 parse_opt (int key
, char *arg
, struct argp_state
*state
)
215 return ARGP_ERR_UNKNOWN
;
222 more_help (int key
, const char *text
, void *input
)
227 case ARGP_KEY_HELP_EXTRA
:
228 /* We print some extra information. */
229 if (asprintf (&tp
, gettext ("\
230 For bug reporting instructions, please see:\n\
231 %s.\n"), REPORT_BUGS_TO
) < 0)
237 return (char *) text
;
240 /* Print the version information. */
242 print_version (FILE *stream
, struct argp_state
*state
)
244 fprintf (stream
, "gencat %s%s\n", PKGVERSION
, 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");
469 while (isspace (this_line
[cnt
]))
472 if (isdigit (this_line
[cnt
]))
474 size_t set_number
= atol (&this_line
[cnt
]);
475 struct set_list
*set
;
477 /* Mark the message set with the given number as
479 set
= find_set (current
, set_number
);
484 /* See whether it is a reasonable identifier. */
486 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
490 error_at_line (0, 0, fname
, start_line
,
491 gettext ("illegal set number"));
495 struct set_list
*runp
;
497 this_line
[cnt
] = '\0';
499 symbol
= &this_line
[start
];
501 /* We have a symbolic set name. This name must
502 appear somewhere else in the catalogs read so
504 for (runp
= current
->all_sets
; runp
!= NULL
;
507 if (strcmp (runp
->symbol
, symbol
) == 0)
514 /* Name does not exist before. */
515 error_at_line (0, 0, fname
, start_line
,
516 gettext ("unknown set `%s'"), symbol
);
520 else if (strncmp (&this_line
[1], "quote", 5) == 0)
529 cnt
= sizeof ("quote");
530 while (isspace (this_line
[cnt
]))
533 /* We need the conversion. */
534 if (cd_towc
== (iconv_t
) -1
535 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
537 /* Something is wrong. */
540 /* Yes, the quote char can be '\0'; this means no quote
541 char. The function using the information works on
542 wide characters so we have to convert it here. */
543 buf
[0] = this_line
[cnt
];
548 wbufptr
= (char *) wbuf
;
551 /* Flush the state. */
552 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
554 iconv (cd_towc
, &bufptr
, &buflen
, &wbufptr
, &wbuflen
);
555 if (buflen
!= 0 || (wchar_t *) wbufptr
!= &wbuf
[2])
556 error_at_line (0, 0, fname
, start_line
,
557 gettext ("invalid quote character"));
559 /* Use the converted wide character. */
560 current
->quote_char
= wbuf
[0];
566 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
568 this_line
[cnt
] = '\0';
569 error_at_line (0, 0, fname
, start_line
,
570 gettext ("unknown directive `%s': line ignored"),
574 else if (isalnum (this_line
[0]) || this_line
[0] == '_')
576 const char *ident
= this_line
;
577 char *line
= this_line
;
582 while (line
[0] != '\0' && !isspace (line
[0]));
584 *line
++ = '\0'; /* Terminate the identifier. */
586 /* Now we found the beginning of the message itself. */
588 if (isdigit (ident
[0]))
590 struct message_list
*runp
;
591 struct message_list
*lastp
;
593 message_number
= atoi (ident
);
595 /* Find location to insert the new message. */
596 runp
= current
->current_set
->messages
;
599 if (runp
->number
== message_number
)
608 /* Oh, oh. There is already a message with this
609 number in the message set. */
610 if (runp
->symbol
== NULL
)
612 /* The existing message had its number specified
613 by the user. Fatal collision type uh, oh. */
614 error_at_line (0, 0, fname
, start_line
,
615 gettext ("duplicated message number"));
616 error_at_line (0, 0, runp
->fname
, runp
->line
,
617 gettext ("this is the first definition"));
622 /* Collision was with number auto-assigned to a
623 symbolic. Change existing symbolic number
624 and move to end the list (if not already there). */
625 runp
->number
= ++current
->current_set
->last_message
;
627 if (runp
->next
!= NULL
)
629 struct message_list
*endp
;
632 current
->current_set
->messages
=runp
->next
;
634 lastp
->next
=runp
->next
;
637 while (endp
->next
!= NULL
)
645 ident
= NULL
; /* We don't have a symbol. */
647 if (message_number
!= 0
648 && message_number
> current
->current_set
->last_message
)
649 current
->current_set
->last_message
= message_number
;
651 else if (ident
[0] != '\0')
653 struct message_list
*runp
;
655 /* Test whether the symbolic name was not used for
656 another message in this message set. */
657 runp
= current
->current_set
->messages
;
659 if (runp
->symbol
!= NULL
&& strcmp (ident
, runp
->symbol
) == 0)
665 /* The name is already used. */
666 error_at_line (0, 0, fname
, start_line
, gettext ("\
667 duplicated message identifier"));
668 error_at_line (0, 0, runp
->fname
, runp
->line
,
669 gettext ("this is the first definition"));
673 /* Give the message the next unused number. */
674 message_number
= ++current
->current_set
->last_message
;
679 if (message_number
!= 0)
685 struct message_list
*newp
;
686 size_t line_len
= strlen (line
) + 1;
687 size_t ident_len
= 0;
689 /* We need the conversion. */
690 if (cd_towc
== (iconv_t
) -1
691 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
693 /* Something is wrong. */
696 /* Convert to a wide character string. We have to
697 interpret escape sequences which will be impossible
698 without doing the conversion if the codeset of the
699 message is stateful. */
704 outbuf
= (char *) wbuf
;
707 /* Flush the state. */
708 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
710 iconv (cd_towc
, &inbuf
, &inlen
, &outbuf
, &outlen
);
713 /* The string is converted. */
714 assert (outlen
< wbufsize
);
715 assert (wbuf
[(wbufsize
- outlen
) / sizeof (wchar_t) - 1]
722 /* Something is wrong with this string, we ignore it. */
723 error_at_line (0, 0, fname
, start_line
, gettext ("\
724 invalid character: message ignored"));
728 /* The output buffer is too small. */
730 wbuf
= (wchar_t *) xrealloc (wbuf
, wbufsize
);
733 /* Strip quote characters, change escape sequences into
734 correct characters etc. */
735 normalize_line (fname
, start_line
, cd_towc
, wbuf
,
736 current
->quote_char
, escape_char
);
739 ident_len
= line
- this_line
;
741 /* Now the string is free of escape sequences. Convert it
742 back into a multibyte character string. First free the
743 memory allocated for the original string. */
744 obstack_free (¤t
->mem_pool
, this_line
);
746 used
= 1; /* Yes, we use the line. */
748 /* Now fill in the new string. It should never happen that
749 the replaced string is longer than the original. */
750 inbuf
= (char *) wbuf
;
751 inlen
= (wcslen (wbuf
) + 1) * sizeof (wchar_t);
753 outlen
= obstack_room (¤t
->mem_pool
);
754 obstack_blank (¤t
->mem_pool
, outlen
);
755 this_line
= (char *) obstack_base (¤t
->mem_pool
);
756 outbuf
= this_line
+ ident_len
;
759 /* Flush the state. */
760 iconv (cd_tomb
, NULL
, NULL
, NULL
, NULL
);
762 iconv (cd_tomb
, &inbuf
, &inlen
, &outbuf
, &outlen
);
765 error_at_line (0, 0, fname
, start_line
,
766 gettext ("invalid line"));
769 assert (outbuf
[-1] == '\0');
771 /* Free the memory in the obstack we don't use. */
772 obstack_blank (¤t
->mem_pool
, -(int) outlen
);
773 line
= obstack_finish (¤t
->mem_pool
);
775 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
776 newp
->number
= message_number
;
777 newp
->message
= line
+ ident_len
;
778 /* Remember symbolic name; is NULL if no is given. */
779 newp
->symbol
= ident
? line
: NULL
;
780 /* Remember where we found the character. */
782 newp
->line
= start_line
;
784 /* Find place to insert to message. We keep them in a
785 sorted single linked list. */
786 if (current
->current_set
->messages
== NULL
787 || current
->current_set
->messages
->number
> message_number
)
789 newp
->next
= current
->current_set
->messages
;
790 current
->current_set
->messages
= newp
;
794 struct message_list
*runp
;
795 runp
= current
->current_set
->messages
;
796 while (runp
->next
!= NULL
)
797 if (runp
->next
->number
> message_number
)
801 newp
->next
= runp
->next
;
805 ++current
->total_messages
;
812 /* See whether we have any non-white space character in this
814 while (this_line
[cnt
] != '\0' && isspace (this_line
[cnt
]))
817 if (this_line
[cnt
] != '\0')
818 /* Yes, some unknown characters found. */
819 error_at_line (0, 0, fname
, start_line
,
820 gettext ("malformed line ignored"));
824 /* We can save the memory for the line if it was not used. */
826 obstack_free (¤t
->mem_pool
, this_line
);
829 /* Close the conversion modules. */
830 iconv_close (cd_towc
);
831 iconv_close (cd_tomb
);
844 write_out (struct catalog
*catalog
, const char *output_name
,
845 const char *header_name
)
847 /* Computing the "optimal" size. */
848 struct set_list
*set_run
;
849 size_t best_total
, best_size
, best_depth
;
850 size_t act_size
, act_depth
;
851 struct catalog_obj obj
;
852 struct obstack string_pool
;
855 uint32_t *array1
, *array2
;
859 /* If not otherwise told try to read file with existing
862 read_old (catalog
, output_name
);
864 /* Initialize best_size with a very high value. */
865 best_total
= best_size
= best_depth
= UINT_MAX
;
867 /* We need some start size for testing. Let's start with
868 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
870 act_size
= 1 + catalog
->total_messages
/ 5;
872 /* We determine the size of a hash table here. Because the message
873 numbers can be chosen arbitrary by the programmer we cannot use
874 the simple method of accessing the array using the message
875 number. The algorithm is based on the trivial hash function
876 NUMBER % TABLE_SIZE, where collisions are stored in a second
877 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
878 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
879 while (act_size
<= best_total
)
881 size_t deep
[act_size
];
884 memset (deep
, '\0', act_size
* sizeof (size_t));
885 set_run
= catalog
->all_sets
;
886 while (set_run
!= NULL
)
888 struct message_list
*message_run
;
890 message_run
= set_run
->messages
;
891 while (message_run
!= NULL
)
893 size_t idx
= (message_run
->number
* set_run
->number
) % act_size
;
896 if (deep
[idx
] > act_depth
)
898 act_depth
= deep
[idx
];
899 if (act_depth
* act_size
> best_total
)
902 message_run
= message_run
->next
;
904 set_run
= set_run
->next
;
907 if (act_depth
* act_size
<= best_total
)
909 /* We have found a better solution. */
910 best_total
= act_depth
* act_size
;
911 best_size
= act_size
;
912 best_depth
= act_depth
;
918 /* let's be prepared for an empty message file. */
919 if (best_size
== UINT_MAX
)
925 /* OK, now we have the size we will use. Fill in the header, build
926 the table and the second one with swapped byte order. */
927 obj
.magic
= CATGETS_MAGIC
;
928 obj
.plane_size
= best_size
;
929 obj
.plane_depth
= best_depth
;
931 /* Allocate room for all needed arrays. */
933 (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
934 memset (array1
, '\0', best_size
* best_depth
* sizeof (uint32_t) * 3);
936 = (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
937 obstack_init (&string_pool
);
939 set_run
= catalog
->all_sets
;
940 while (set_run
!= NULL
)
942 struct message_list
*message_run
;
944 message_run
= set_run
->messages
;
945 while (message_run
!= NULL
)
947 size_t idx
= (((message_run
->number
* set_run
->number
) % best_size
)
949 /* Determine collision depth. */
950 while (array1
[idx
] != 0)
951 idx
+= best_size
* 3;
953 /* Store set number, message number and pointer into string
954 space, relative to the first string. */
955 array1
[idx
+ 0] = set_run
->number
;
956 array1
[idx
+ 1] = message_run
->number
;
957 array1
[idx
+ 2] = obstack_object_size (&string_pool
);
959 /* Add current string to the continuous space containing all
961 obstack_grow0 (&string_pool
, message_run
->message
,
962 strlen (message_run
->message
));
964 message_run
= message_run
->next
;
967 set_run
= set_run
->next
;
969 strings_size
= obstack_object_size (&string_pool
);
970 strings
= obstack_finish (&string_pool
);
972 /* Compute ARRAY2 by changing the byte order. */
973 for (cnt
= 0; cnt
< best_size
* best_depth
* 3; ++cnt
)
974 array2
[cnt
] = SWAPU32 (array1
[cnt
]);
976 /* Now we can write out the whole data. */
977 if (strcmp (output_name
, "-") == 0
978 || strcmp (output_name
, "/dev/stdout") == 0)
982 fd
= creat (output_name
, 0666);
984 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
988 /* Write out header. */
989 write (fd
, &obj
, sizeof (obj
));
991 /* We always write out the little endian version of the index
993 #if __BYTE_ORDER == __LITTLE_ENDIAN
994 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
995 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
996 #elif __BYTE_ORDER == __BIG_ENDIAN
997 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
998 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
1000 # error Cannot handle __BYTE_ORDER byte order
1003 /* Finally write the strings. */
1004 write (fd
, strings
, strings_size
);
1006 if (fd
!= STDOUT_FILENO
)
1009 /* If requested now write out the header file. */
1010 if (header_name
!= NULL
)
1015 /* Open output file. "-" or "/dev/stdout" means write to
1017 if (strcmp (header_name
, "-") == 0
1018 || strcmp (header_name
, "/dev/stdout") == 0)
1022 fp
= fopen (header_name
, "w");
1024 error (EXIT_FAILURE
, errno
,
1025 gettext ("cannot open output file `%s'"), header_name
);
1028 /* Iterate over all sets and all messages. */
1029 set_run
= catalog
->all_sets
;
1030 while (set_run
!= NULL
)
1032 struct message_list
*message_run
;
1034 /* If the current message set has a symbolic name write this
1036 if (set_run
->symbol
!= NULL
)
1037 fprintf (fp
, "%s#define %sSet %#x\t/* %s:%Zu */\n",
1038 first
? "" : "\n", set_run
->symbol
, set_run
->number
- 1,
1039 set_run
->fname
, set_run
->line
);
1042 message_run
= set_run
->messages
;
1043 while (message_run
!= NULL
)
1045 /* If the current message has a symbolic name write
1046 #define out. But we have to take care for the set
1047 not having a symbolic name. */
1048 if (message_run
->symbol
!= NULL
)
1050 if (set_run
->symbol
== NULL
)
1051 fprintf (fp
, "#define AutomaticSet%d%s %#x\t/* %s:%Zu */\n",
1052 set_run
->number
, message_run
->symbol
,
1053 message_run
->number
, message_run
->fname
,
1056 fprintf (fp
, "#define %s%s %#x\t/* %s:%Zu */\n",
1057 set_run
->symbol
, message_run
->symbol
,
1058 message_run
->number
, message_run
->fname
,
1062 message_run
= message_run
->next
;
1065 set_run
= set_run
->next
;
1074 static struct set_list
*
1075 find_set (struct catalog
*current
, int number
)
1077 struct set_list
*result
= current
->all_sets
;
1079 /* We must avoid set number 0 because a set of this number signals
1080 in the tables that the entry is not occupied. */
1083 while (result
!= NULL
)
1084 if (result
->number
== number
)
1087 result
= result
->next
;
1089 /* Prepare new message set. */
1090 result
= (struct set_list
*) xcalloc (1, sizeof (*result
));
1091 result
->number
= number
;
1092 result
->next
= current
->all_sets
;
1093 current
->all_sets
= result
;
1099 /* Normalize given string *in*place* by processing escape sequences
1100 and quote characters. */
1102 normalize_line (const char *fname
, size_t line
, iconv_t cd
, wchar_t *string
,
1103 wchar_t quote_char
, wchar_t escape_char
)
1106 wchar_t *rp
= string
;
1107 wchar_t *wp
= string
;
1109 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1117 while (*rp
!= L
'\0')
1118 if (*rp
== quote_char
)
1119 /* We simply end the string when we find the first time an
1120 not-escaped quote character. */
1122 else if (*rp
== escape_char
)
1125 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1126 /* This is an extension to XPG. */
1129 /* Recognize escape sequences. */
1166 number
= *rp
++ - L
'0';
1167 while (number
<= (255 / 8) && *rp
>= L
'0' && *rp
<= L
'7')
1170 number
+= *rp
++ - L
'0';
1173 cbuf
[0] = (char) number
;
1178 wcbufptr
= (char *) wcbuf
;
1179 wcbufin
= sizeof (wcbuf
);
1181 /* Flush the state. */
1182 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
1184 iconv (cd
, &cbufptr
, &cbufin
, &wcbufptr
, &wcbufin
);
1185 if (cbufptr
!= &cbuf
[2] || (wchar_t *) wcbufptr
!= &wcbuf
[2])
1186 error_at_line (0, 0, fname
, line
,
1187 gettext ("invalid escape sequence"));
1193 if (*rp
== escape_char
)
1195 *wp
++ = escape_char
;
1199 /* Simply ignore the backslash character. */;
1206 /* If we saw a quote character at the beginning we expect another
1208 if (is_quoted
&& *rp
!= quote_char
)
1209 error_at_line (0, 0, fname
, line
, gettext ("unterminated message"));
1211 /* Terminate string. */
1218 read_old (struct catalog
*catalog
, const char *file_name
)
1220 struct catalog_info old_cat_obj
;
1221 struct set_list
*set
= NULL
;
1225 /* Try to open catalog, but don't look through the NLSPATH. */
1226 if (__open_catalog (file_name
, NULL
, NULL
, &old_cat_obj
) != 0)
1228 if (errno
== ENOENT
)
1229 /* No problem, the catalog simply does not exist. */
1232 error (EXIT_FAILURE
, errno
,
1233 gettext ("while opening old catalog file"));
1236 /* OK, we have the catalog loaded. Now read all messages and merge
1237 them. When set and message number clash for any message the new
1238 one is used. If the new one is empty it indicates that the
1239 message should be deleted. */
1240 for (cnt
= 0; cnt
< old_cat_obj
.plane_size
* old_cat_obj
.plane_depth
; ++cnt
)
1242 struct message_list
*message
, *last
;
1244 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] == 0)
1245 /* No message in this slot. */
1248 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1 != (uint32_t) last_set
)
1250 last_set
= old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1;
1251 set
= find_set (catalog
, old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1);
1255 message
= set
->messages
;
1256 while (message
!= NULL
)
1258 if ((uint32_t) message
->number
>= old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1261 message
= message
->next
;
1265 || (uint32_t) message
->number
> old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1267 /* We have found a message which is not yet in the catalog.
1268 Insert it at the right position. */
1269 struct message_list
*newp
;
1271 newp
= (struct message_list
*) xmalloc (sizeof(*newp
));
1272 newp
->number
= old_cat_obj
.name_ptr
[cnt
* 3 + 1];
1274 &old_cat_obj
.strings
[old_cat_obj
.name_ptr
[cnt
* 3 + 2]];
1277 newp
->symbol
= NULL
;
1278 newp
->next
= message
;
1281 set
->messages
= newp
;
1285 ++catalog
->total_messages
;
1287 else if (*message
->message
== '\0')
1289 /* The new empty message has overridden the old one thus
1290 "deleting" it as required. Now remove the empty remains. */
1292 set
->messages
= message
->next
;
1294 last
->next
= message
->next
;
1301 open_conversion (const char *codeset
, iconv_t
*cd_towcp
, iconv_t
*cd_tombp
,
1302 wchar_t *escape_charp
)
1311 /* If the input file does not specify the codeset use the locale's. */
1312 if (codeset
== NULL
)
1314 setlocale (LC_ALL
, "");
1315 codeset
= nl_langinfo (CODESET
);
1316 setlocale (LC_ALL
, "C");
1319 /* Get the conversion modules. */
1320 *cd_towcp
= iconv_open ("WCHAR_T", codeset
);
1321 *cd_tombp
= iconv_open (codeset
, "WCHAR_T");
1322 if (*cd_towcp
== (iconv_t
) -1 || *cd_tombp
== (iconv_t
) -1)
1324 error (0, 0, gettext ("conversion modules not available"));
1325 if (*cd_towcp
!= (iconv_t
) -1)
1326 iconv_close (*cd_towcp
);
1331 /* One special case for historical reasons is the backslash
1332 character. In some codesets the byte value 0x5c is not mapped to
1333 U005c in Unicode. These charsets then don't have a backslash
1334 character at all. Therefore we have to live with whatever the
1335 codeset provides and recognize, instead of the U005c, the character
1336 the byte value 0x5c is mapped to. */
1342 wbufptr
= (char *) wbuf
;
1343 wbufsize
= sizeof (wbuf
);
1345 iconv (*cd_towcp
, &bufptr
, &bufsize
, &wbufptr
, &wbufsize
);
1346 if (bufsize
!= 0 || wbufsize
!= 0)
1348 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1349 on with using U005c. */
1350 error (0, 0, gettext ("cannot determine escape character"));
1351 *escape_charp
= L
'\\';
1354 *escape_charp
= wbuf
[0];