1 /* Copyright (C) 1996-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
44 #include "catgetsinfo.h"
48 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
59 struct message_list
*next
;
67 struct message_list
*messages
;
74 struct set_list
*next
;
80 struct set_list
*all_sets
;
81 struct set_list
*current_set
;
82 size_t total_messages
;
86 struct obstack mem_pool
;
90 /* If non-zero force creation of new file, not using existing one. */
93 /* Name of output file. */
94 static const char *output_name
;
96 /* Name of generated C header file. */
97 static const char *header_name
;
99 /* Name and version of program. */
100 static void print_version (FILE *stream
, struct argp_state
*state
);
101 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
105 /* Definitions of arguments for argp functions. */
106 static const struct argp_option options
[] =
108 { "header", 'H', N_("NAME"), 0,
109 N_("Create C header file NAME containing symbol definitions") },
110 { "new", OPT_NEW
, NULL
, 0,
111 N_("Do not use existing catalog, force new output file") },
112 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
113 { NULL
, 0, NULL
, 0, NULL
}
116 /* Short description of program. */
117 static const char doc
[] = N_("Generate message catalog.\
118 \vIf INPUT-FILE is -, input is read from standard input. If OUTPUT-FILE\n\
119 is -, output is written to standard output.\n");
121 /* Strings for arguments in help texts. */
122 static const char args_doc
[] = N_("\
123 -o OUTPUT-FILE [INPUT-FILE]...\n[OUTPUT-FILE [INPUT-FILE]...]");
125 /* Prototype for option handler. */
126 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
128 /* Function to print some extra text in the help message. */
129 static char *more_help (int key
, const char *text
, void *input
);
131 /* Data structure to communicate with argp functions. */
132 static struct argp argp
=
134 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
138 /* Wrapper functions with error checking for standard functions. */
139 #include <programs/xmalloc.h>
141 /* Prototypes for local functions. */
142 static void error_print (void);
143 static struct catalog
*read_input_file (struct catalog
*current
,
145 static void write_out (struct catalog
*result
, const char *output_name
,
146 const char *header_name
);
147 static struct set_list
*find_set (struct catalog
*current
, int number
);
148 static void normalize_line (const char *fname
, size_t line
, iconv_t cd
,
149 wchar_t *string
, wchar_t quote_char
,
150 wchar_t escape_char
);
151 static void read_old (struct catalog
*catalog
, const char *file_name
);
152 static int open_conversion (const char *codesetp
, iconv_t
*cd_towcp
,
153 iconv_t
*cd_tombp
, wchar_t *escape_charp
);
157 main (int argc
, char *argv
[])
159 struct catalog
*result
;
162 /* Set program name for messages. */
163 error_print_progname
= error_print
;
165 /* Set locale via LC_ALL. */
166 setlocale (LC_ALL
, "");
168 /* Set the text message domain. */
169 textdomain (PACKAGE
);
171 /* Initialize local variables. */
174 /* Parse and process arguments. */
175 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
177 /* Determine output file. */
178 if (output_name
== NULL
)
179 output_name
= remaining
< argc
? argv
[remaining
++] : "-";
181 /* Process all input files. */
182 setlocale (LC_CTYPE
, "C");
183 if (remaining
< argc
)
185 result
= read_input_file (result
, argv
[remaining
]);
186 while (++remaining
< argc
);
188 result
= read_input_file (NULL
, "-");
190 /* Write out the result. */
192 write_out (result
, output_name
, header_name
);
194 return error_message_count
!= 0;
198 /* Handle program arguments. */
200 parse_opt (int key
, char *arg
, struct argp_state
*state
)
214 return ARGP_ERR_UNKNOWN
;
221 more_help (int key
, const char *text
, void *input
)
226 case ARGP_KEY_HELP_EXTRA
:
227 /* We print some extra information. */
228 if (asprintf (&tp
, gettext ("\
229 For bug reporting instructions, please see:\n\
230 %s.\n"), REPORT_BUGS_TO
) < 0)
236 return (char *) text
;
239 /* Print the version information. */
241 print_version (FILE *stream
, struct argp_state
*state
)
243 fprintf (stream
, "gencat %s%s\n", PKGVERSION
, VERSION
);
244 fprintf (stream
, gettext ("\
245 Copyright (C) %s Free Software Foundation, Inc.\n\
246 This is free software; see the source for copying conditions. There is NO\n\
247 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
249 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
253 /* The address of this function will be assigned to the hook in the
258 /* We don't want the program name to be printed in messages. Emacs'
259 compile.el does not like this. */
263 static struct catalog
*
264 read_input_file (struct catalog
*current
, const char *fname
)
272 iconv_t cd_towc
= (iconv_t
) -1;
273 iconv_t cd_tomb
= (iconv_t
) -1;
274 wchar_t escape_char
= L
'\\';
275 char *codeset
= NULL
;
277 if (strcmp (fname
, "-") == 0 || strcmp (fname
, "/dev/stdin") == 0)
280 fname
= gettext ("*standard input*");
283 fp
= fopen (fname
, "r");
286 error (0, errno
, gettext ("cannot open input file `%s'"), fname
);
290 /* If we haven't seen anything yet, allocate result structure. */
293 current
= (struct catalog
*) xcalloc (1, sizeof (*current
));
295 #define obstack_chunk_alloc malloc
296 #define obstack_chunk_free free
297 obstack_init (¤t
->mem_pool
);
299 current
->current_set
= find_set (current
, NL_SETD
);
307 wbuf
= (wchar_t *) xmalloc (wbufsize
);
313 size_t start_line
= line_number
+ 1;
320 act_len
= getline (&buf
, &len
, fp
);
325 /* It the line continued? */
327 if (buf
[act_len
- 1] == '\n')
331 /* There might be more than one backslash at the end of
332 the line. Only if there is an odd number of them is
333 the line continued. */
334 if (act_len
> 0 && buf
[act_len
- 1] == '\\')
336 int temp_act_len
= act_len
;
341 continued
= !continued
;
343 while (temp_act_len
> 0 && buf
[temp_act_len
- 1] == '\\');
350 /* Append to currently selected line. */
351 obstack_grow (¤t
->mem_pool
, buf
, act_len
);
355 obstack_1grow (¤t
->mem_pool
, '\0');
356 this_line
= (char *) obstack_finish (¤t
->mem_pool
);
359 if (this_line
[0] == '$')
361 if (isblank (this_line
[1]))
364 while (isblank (this_line
[cnt
]))
366 if (strncmp (&this_line
[cnt
], "codeset=", 8) != 0)
367 /* This is a comment line. Do nothing. */;
368 else if (codeset
!= NULL
)
369 /* Ignore multiple codeset. */;
374 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
378 int len
= cnt
- start
;
379 codeset
= xmalloc (len
+ 1);
380 *((char *) mempcpy (codeset
, &this_line
[start
], len
))
385 else if (strncmp (&this_line
[1], "set", 3) == 0)
387 int cnt
= sizeof ("set");
389 const char *symbol
= NULL
;
390 while (isspace (this_line
[cnt
]))
393 if (isdigit (this_line
[cnt
]))
395 set_number
= atol (&this_line
[cnt
]);
397 /* If the given number for the character set is
398 higher than any we used for symbolic set names
399 avoid clashing by using only higher numbers for
400 the following symbolic definitions. */
401 if (set_number
> current
->last_set
)
402 current
->last_set
= set_number
;
406 /* See whether it is a reasonable identifier. */
408 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
413 /* No correct character found. */
414 error_at_line (0, 0, fname
, start_line
,
415 gettext ("illegal set number"));
420 /* We have found seomthing that looks like a
421 correct identifier. */
422 struct set_list
*runp
;
424 this_line
[cnt
] = '\0';
426 symbol
= &this_line
[start
];
428 /* Test whether the identifier was already used. */
429 runp
= current
->all_sets
;
431 if (runp
->symbol
!= NULL
432 && strcmp (runp
->symbol
, symbol
) == 0)
439 /* We cannot allow duplicate identifiers for
441 error_at_line (0, 0, fname
, start_line
,
442 gettext ("duplicate set definition"));
443 error_at_line (0, 0, runp
->fname
, runp
->line
,
445 this is the first definition"));
449 /* Allocate next free message set for identifier. */
450 set_number
= ++current
->last_set
;
456 /* We found a legal set number. */
457 current
->current_set
= find_set (current
, set_number
);
460 current
->current_set
->symbol
= symbol
;
461 current
->current_set
->fname
= fname
;
462 current
->current_set
->line
= start_line
;
465 else if (strncmp (&this_line
[1], "delset", 6) == 0)
467 int cnt
= sizeof ("delset");
468 while (isspace (this_line
[cnt
]))
471 if (isdigit (this_line
[cnt
]))
473 size_t set_number
= atol (&this_line
[cnt
]);
474 struct set_list
*set
;
476 /* Mark the message set with the given number as
478 set
= find_set (current
, set_number
);
483 /* See whether it is a reasonable identifier. */
485 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
489 error_at_line (0, 0, fname
, start_line
,
490 gettext ("illegal set number"));
494 struct set_list
*runp
;
496 this_line
[cnt
] = '\0';
498 symbol
= &this_line
[start
];
500 /* We have a symbolic set name. This name must
501 appear somewhere else in the catalogs read so
503 for (runp
= current
->all_sets
; runp
!= NULL
;
506 if (strcmp (runp
->symbol
, symbol
) == 0)
513 /* Name does not exist before. */
514 error_at_line (0, 0, fname
, start_line
,
515 gettext ("unknown set `%s'"), symbol
);
519 else if (strncmp (&this_line
[1], "quote", 5) == 0)
528 cnt
= sizeof ("quote");
529 while (isspace (this_line
[cnt
]))
532 /* We need the conversion. */
533 if (cd_towc
== (iconv_t
) -1
534 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
536 /* Something is wrong. */
539 /* Yes, the quote char can be '\0'; this means no quote
540 char. The function using the information works on
541 wide characters so we have to convert it here. */
542 buf
[0] = this_line
[cnt
];
547 wbufptr
= (char *) wbuf
;
550 /* Flush the state. */
551 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
553 iconv (cd_towc
, &bufptr
, &buflen
, &wbufptr
, &wbuflen
);
554 if (buflen
!= 0 || (wchar_t *) wbufptr
!= &wbuf
[2])
555 error_at_line (0, 0, fname
, start_line
,
556 gettext ("invalid quote character"));
558 /* Use the converted wide character. */
559 current
->quote_char
= wbuf
[0];
565 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
567 this_line
[cnt
] = '\0';
568 error_at_line (0, 0, fname
, start_line
,
569 gettext ("unknown directive `%s': line ignored"),
573 else if (isalnum (this_line
[0]) || this_line
[0] == '_')
575 const char *ident
= this_line
;
576 char *line
= this_line
;
581 while (line
[0] != '\0' && !isspace (line
[0]));
583 *line
++ = '\0'; /* Terminate the identifier. */
585 /* Now we found the beginning of the message itself. */
587 if (isdigit (ident
[0]))
589 struct message_list
*runp
;
590 struct message_list
*lastp
;
592 message_number
= atoi (ident
);
594 /* Find location to insert the new message. */
595 runp
= current
->current_set
->messages
;
598 if (runp
->number
== message_number
)
607 /* Oh, oh. There is already a message with this
608 number in the message set. */
609 if (runp
->symbol
== NULL
)
611 /* The existing message had its number specified
612 by the user. Fatal collision type uh, oh. */
613 error_at_line (0, 0, fname
, start_line
,
614 gettext ("duplicated message number"));
615 error_at_line (0, 0, runp
->fname
, runp
->line
,
616 gettext ("this is the first definition"));
621 /* Collision was with number auto-assigned to a
622 symbolic. Change existing symbolic number
623 and move to end the list (if not already there). */
624 runp
->number
= ++current
->current_set
->last_message
;
626 if (runp
->next
!= NULL
)
628 struct message_list
*endp
;
631 current
->current_set
->messages
=runp
->next
;
633 lastp
->next
=runp
->next
;
636 while (endp
->next
!= NULL
)
644 ident
= NULL
; /* We don't have a symbol. */
646 if (message_number
!= 0
647 && message_number
> current
->current_set
->last_message
)
648 current
->current_set
->last_message
= message_number
;
650 else if (ident
[0] != '\0')
652 struct message_list
*runp
;
654 /* Test whether the symbolic name was not used for
655 another message in this message set. */
656 runp
= current
->current_set
->messages
;
658 if (runp
->symbol
!= NULL
&& strcmp (ident
, runp
->symbol
) == 0)
664 /* The name is already used. */
665 error_at_line (0, 0, fname
, start_line
, gettext ("\
666 duplicated message identifier"));
667 error_at_line (0, 0, runp
->fname
, runp
->line
,
668 gettext ("this is the first definition"));
672 /* Give the message the next unused number. */
673 message_number
= ++current
->current_set
->last_message
;
678 if (message_number
!= 0)
684 struct message_list
*newp
;
685 size_t line_len
= strlen (line
) + 1;
686 size_t ident_len
= 0;
688 /* We need the conversion. */
689 if (cd_towc
== (iconv_t
) -1
690 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
692 /* Something is wrong. */
695 /* Convert to a wide character string. We have to
696 interpret escape sequences which will be impossible
697 without doing the conversion if the codeset of the
698 message is stateful. */
703 outbuf
= (char *) wbuf
;
706 /* Flush the state. */
707 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
709 iconv (cd_towc
, &inbuf
, &inlen
, &outbuf
, &outlen
);
712 /* The string is converted. */
713 assert (outlen
< wbufsize
);
714 assert (wbuf
[(wbufsize
- outlen
) / sizeof (wchar_t) - 1]
721 /* Something is wrong with this string, we ignore it. */
722 error_at_line (0, 0, fname
, start_line
, gettext ("\
723 invalid character: message ignored"));
727 /* The output buffer is too small. */
729 wbuf
= (wchar_t *) xrealloc (wbuf
, wbufsize
);
732 /* Strip quote characters, change escape sequences into
733 correct characters etc. */
734 normalize_line (fname
, start_line
, cd_towc
, wbuf
,
735 current
->quote_char
, escape_char
);
738 ident_len
= line
- this_line
;
740 /* Now the string is free of escape sequences. Convert it
741 back into a multibyte character string. First free the
742 memory allocated for the original string. */
743 obstack_free (¤t
->mem_pool
, this_line
);
745 used
= 1; /* Yes, we use the line. */
747 /* Now fill in the new string. It should never happen that
748 the replaced string is longer than the original. */
749 inbuf
= (char *) wbuf
;
750 inlen
= (wcslen (wbuf
) + 1) * sizeof (wchar_t);
752 outlen
= obstack_room (¤t
->mem_pool
);
753 obstack_blank (¤t
->mem_pool
, outlen
);
754 this_line
= (char *) obstack_base (¤t
->mem_pool
);
755 outbuf
= this_line
+ ident_len
;
758 /* Flush the state. */
759 iconv (cd_tomb
, NULL
, NULL
, NULL
, NULL
);
761 iconv (cd_tomb
, &inbuf
, &inlen
, &outbuf
, &outlen
);
764 error_at_line (0, 0, fname
, start_line
,
765 gettext ("invalid line"));
768 assert (outbuf
[-1] == '\0');
770 /* Free the memory in the obstack we don't use. */
771 obstack_blank (¤t
->mem_pool
, -(int) outlen
);
772 line
= obstack_finish (¤t
->mem_pool
);
774 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
775 newp
->number
= message_number
;
776 newp
->message
= line
+ ident_len
;
777 /* Remember symbolic name; is NULL if no is given. */
778 newp
->symbol
= ident
? line
: NULL
;
779 /* Remember where we found the character. */
781 newp
->line
= start_line
;
783 /* Find place to insert to message. We keep them in a
784 sorted single linked list. */
785 if (current
->current_set
->messages
== NULL
786 || current
->current_set
->messages
->number
> message_number
)
788 newp
->next
= current
->current_set
->messages
;
789 current
->current_set
->messages
= newp
;
793 struct message_list
*runp
;
794 runp
= current
->current_set
->messages
;
795 while (runp
->next
!= NULL
)
796 if (runp
->next
->number
> message_number
)
800 newp
->next
= runp
->next
;
804 ++current
->total_messages
;
811 /* See whether we have any non-white space character in this
813 while (this_line
[cnt
] != '\0' && isspace (this_line
[cnt
]))
816 if (this_line
[cnt
] != '\0')
817 /* Yes, some unknown characters found. */
818 error_at_line (0, 0, fname
, start_line
,
819 gettext ("malformed line ignored"));
823 /* We can save the memory for the line if it was not used. */
825 obstack_free (¤t
->mem_pool
, this_line
);
828 /* Close the conversion modules. */
829 iconv_close (cd_towc
);
830 iconv_close (cd_tomb
);
843 write_out (struct catalog
*catalog
, const char *output_name
,
844 const char *header_name
)
846 /* Computing the "optimal" size. */
847 struct set_list
*set_run
;
848 size_t best_total
, best_size
, best_depth
;
849 size_t act_size
, act_depth
;
850 struct catalog_obj obj
;
851 struct obstack string_pool
;
854 uint32_t *array1
, *array2
;
858 /* If not otherwise told try to read file with existing
861 read_old (catalog
, output_name
);
863 /* Initialize best_size with a very high value. */
864 best_total
= best_size
= best_depth
= UINT_MAX
;
866 /* We need some start size for testing. Let's start with
867 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
869 act_size
= 1 + catalog
->total_messages
/ 5;
871 /* We determine the size of a hash table here. Because the message
872 numbers can be chosen arbitrary by the programmer we cannot use
873 the simple method of accessing the array using the message
874 number. The algorithm is based on the trivial hash function
875 NUMBER % TABLE_SIZE, where collisions are stored in a second
876 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
877 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
878 while (act_size
<= best_total
)
880 size_t deep
[act_size
];
883 memset (deep
, '\0', act_size
* sizeof (size_t));
884 set_run
= catalog
->all_sets
;
885 while (set_run
!= NULL
)
887 struct message_list
*message_run
;
889 message_run
= set_run
->messages
;
890 while (message_run
!= NULL
)
892 size_t idx
= (message_run
->number
* set_run
->number
) % act_size
;
895 if (deep
[idx
] > act_depth
)
897 act_depth
= deep
[idx
];
898 if (act_depth
* act_size
> best_total
)
901 message_run
= message_run
->next
;
903 set_run
= set_run
->next
;
906 if (act_depth
* act_size
<= best_total
)
908 /* We have found a better solution. */
909 best_total
= act_depth
* act_size
;
910 best_size
= act_size
;
911 best_depth
= act_depth
;
917 /* let's be prepared for an empty message file. */
918 if (best_size
== UINT_MAX
)
924 /* OK, now we have the size we will use. Fill in the header, build
925 the table and the second one with swapped byte order. */
926 obj
.magic
= CATGETS_MAGIC
;
927 obj
.plane_size
= best_size
;
928 obj
.plane_depth
= best_depth
;
930 /* Allocate room for all needed arrays. */
932 (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
933 memset (array1
, '\0', best_size
* best_depth
* sizeof (uint32_t) * 3);
935 = (uint32_t *) alloca (best_size
* best_depth
* sizeof (uint32_t) * 3);
936 obstack_init (&string_pool
);
938 set_run
= catalog
->all_sets
;
939 while (set_run
!= NULL
)
941 struct message_list
*message_run
;
943 message_run
= set_run
->messages
;
944 while (message_run
!= NULL
)
946 size_t idx
= (((message_run
->number
* set_run
->number
) % best_size
)
948 /* Determine collision depth. */
949 while (array1
[idx
] != 0)
950 idx
+= best_size
* 3;
952 /* Store set number, message number and pointer into string
953 space, relative to the first string. */
954 array1
[idx
+ 0] = set_run
->number
;
955 array1
[idx
+ 1] = message_run
->number
;
956 array1
[idx
+ 2] = obstack_object_size (&string_pool
);
958 /* Add current string to the continuous space containing all
960 obstack_grow0 (&string_pool
, message_run
->message
,
961 strlen (message_run
->message
));
963 message_run
= message_run
->next
;
966 set_run
= set_run
->next
;
968 strings_size
= obstack_object_size (&string_pool
);
969 strings
= obstack_finish (&string_pool
);
971 /* Compute ARRAY2 by changing the byte order. */
972 for (cnt
= 0; cnt
< best_size
* best_depth
* 3; ++cnt
)
973 array2
[cnt
] = SWAPU32 (array1
[cnt
]);
975 /* Now we can write out the whole data. */
976 if (strcmp (output_name
, "-") == 0
977 || strcmp (output_name
, "/dev/stdout") == 0)
981 fd
= creat (output_name
, 0666);
983 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
987 /* Write out header. */
988 write (fd
, &obj
, sizeof (obj
));
990 /* We always write out the little endian version of the index
992 #if __BYTE_ORDER == __LITTLE_ENDIAN
993 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
994 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
995 #elif __BYTE_ORDER == __BIG_ENDIAN
996 write (fd
, array2
, best_size
* best_depth
* sizeof (uint32_t) * 3);
997 write (fd
, array1
, best_size
* best_depth
* sizeof (uint32_t) * 3);
999 # error Cannot handle __BYTE_ORDER byte order
1002 /* Finally write the strings. */
1003 write (fd
, strings
, strings_size
);
1005 if (fd
!= STDOUT_FILENO
)
1008 /* If requested now write out the header file. */
1009 if (header_name
!= NULL
)
1014 /* Open output file. "-" or "/dev/stdout" means write to
1016 if (strcmp (header_name
, "-") == 0
1017 || strcmp (header_name
, "/dev/stdout") == 0)
1021 fp
= fopen (header_name
, "w");
1023 error (EXIT_FAILURE
, errno
,
1024 gettext ("cannot open output file `%s'"), header_name
);
1027 /* Iterate over all sets and all messages. */
1028 set_run
= catalog
->all_sets
;
1029 while (set_run
!= NULL
)
1031 struct message_list
*message_run
;
1033 /* If the current message set has a symbolic name write this
1035 if (set_run
->symbol
!= NULL
)
1036 fprintf (fp
, "%s#define %sSet %#x\t/* %s:%zu */\n",
1037 first
? "" : "\n", set_run
->symbol
, set_run
->number
- 1,
1038 set_run
->fname
, set_run
->line
);
1041 message_run
= set_run
->messages
;
1042 while (message_run
!= NULL
)
1044 /* If the current message has a symbolic name write
1045 #define out. But we have to take care for the set
1046 not having a symbolic name. */
1047 if (message_run
->symbol
!= NULL
)
1049 if (set_run
->symbol
== NULL
)
1050 fprintf (fp
, "#define AutomaticSet%d%s %#x\t/* %s:%zu */\n",
1051 set_run
->number
, message_run
->symbol
,
1052 message_run
->number
, message_run
->fname
,
1055 fprintf (fp
, "#define %s%s %#x\t/* %s:%zu */\n",
1056 set_run
->symbol
, message_run
->symbol
,
1057 message_run
->number
, message_run
->fname
,
1061 message_run
= message_run
->next
;
1064 set_run
= set_run
->next
;
1073 static struct set_list
*
1074 find_set (struct catalog
*current
, int number
)
1076 struct set_list
*result
= current
->all_sets
;
1078 /* We must avoid set number 0 because a set of this number signals
1079 in the tables that the entry is not occupied. */
1082 while (result
!= NULL
)
1083 if (result
->number
== number
)
1086 result
= result
->next
;
1088 /* Prepare new message set. */
1089 result
= (struct set_list
*) xcalloc (1, sizeof (*result
));
1090 result
->number
= number
;
1091 result
->next
= current
->all_sets
;
1092 current
->all_sets
= result
;
1098 /* Normalize given string *in*place* by processing escape sequences
1099 and quote characters. */
1101 normalize_line (const char *fname
, size_t line
, iconv_t cd
, wchar_t *string
,
1102 wchar_t quote_char
, wchar_t escape_char
)
1105 wchar_t *rp
= string
;
1106 wchar_t *wp
= string
;
1108 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1116 while (*rp
!= L
'\0')
1117 if (*rp
== quote_char
)
1118 /* We simply end the string when we find the first time an
1119 not-escaped quote character. */
1121 else if (*rp
== escape_char
)
1124 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1125 /* This is an extension to XPG. */
1128 /* Recognize escape sequences. */
1165 number
= *rp
++ - L
'0';
1166 while (number
<= (255 / 8) && *rp
>= L
'0' && *rp
<= L
'7')
1169 number
+= *rp
++ - L
'0';
1172 cbuf
[0] = (char) number
;
1177 wcbufptr
= (char *) wcbuf
;
1178 wcbufin
= sizeof (wcbuf
);
1180 /* Flush the state. */
1181 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
1183 iconv (cd
, &cbufptr
, &cbufin
, &wcbufptr
, &wcbufin
);
1184 if (cbufptr
!= &cbuf
[2] || (wchar_t *) wcbufptr
!= &wcbuf
[2])
1185 error_at_line (0, 0, fname
, line
,
1186 gettext ("invalid escape sequence"));
1192 if (*rp
== escape_char
)
1194 *wp
++ = escape_char
;
1199 /* Simply ignore the backslash character. */
1207 /* If we saw a quote character at the beginning we expect another
1209 if (is_quoted
&& *rp
!= quote_char
)
1210 error_at_line (0, 0, fname
, line
, gettext ("unterminated message"));
1212 /* Terminate string. */
1219 read_old (struct catalog
*catalog
, const char *file_name
)
1221 struct catalog_info old_cat_obj
;
1222 struct set_list
*set
= NULL
;
1226 /* Try to open catalog, but don't look through the NLSPATH. */
1227 if (__open_catalog (file_name
, NULL
, NULL
, &old_cat_obj
) != 0)
1229 if (errno
== ENOENT
)
1230 /* No problem, the catalog simply does not exist. */
1233 error (EXIT_FAILURE
, errno
,
1234 gettext ("while opening old catalog file"));
1237 /* OK, we have the catalog loaded. Now read all messages and merge
1238 them. When set and message number clash for any message the new
1239 one is used. If the new one is empty it indicates that the
1240 message should be deleted. */
1241 for (cnt
= 0; cnt
< old_cat_obj
.plane_size
* old_cat_obj
.plane_depth
; ++cnt
)
1243 struct message_list
*message
, *last
;
1245 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] == 0)
1246 /* No message in this slot. */
1249 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1 != (uint32_t) last_set
)
1251 last_set
= old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1;
1252 set
= find_set (catalog
, old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1);
1256 message
= set
->messages
;
1257 while (message
!= NULL
)
1259 if ((uint32_t) message
->number
>= old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1262 message
= message
->next
;
1266 || (uint32_t) message
->number
> old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1268 /* We have found a message which is not yet in the catalog.
1269 Insert it at the right position. */
1270 struct message_list
*newp
;
1272 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
1273 newp
->number
= old_cat_obj
.name_ptr
[cnt
* 3 + 1];
1275 &old_cat_obj
.strings
[old_cat_obj
.name_ptr
[cnt
* 3 + 2]];
1278 newp
->symbol
= NULL
;
1279 newp
->next
= message
;
1282 set
->messages
= newp
;
1286 ++catalog
->total_messages
;
1288 else if (*message
->message
== '\0')
1290 /* The new empty message has overridden the old one thus
1291 "deleting" it as required. Now remove the empty remains. */
1293 set
->messages
= message
->next
;
1295 last
->next
= message
->next
;
1302 open_conversion (const char *codeset
, iconv_t
*cd_towcp
, iconv_t
*cd_tombp
,
1303 wchar_t *escape_charp
)
1312 /* If the input file does not specify the codeset use the locale's. */
1313 if (codeset
== NULL
)
1315 setlocale (LC_ALL
, "");
1316 codeset
= nl_langinfo (CODESET
);
1317 setlocale (LC_ALL
, "C");
1320 /* Get the conversion modules. */
1321 *cd_towcp
= iconv_open ("WCHAR_T", codeset
);
1322 *cd_tombp
= iconv_open (codeset
, "WCHAR_T");
1323 if (*cd_towcp
== (iconv_t
) -1 || *cd_tombp
== (iconv_t
) -1)
1325 error (0, 0, gettext ("conversion modules not available"));
1326 if (*cd_towcp
!= (iconv_t
) -1)
1327 iconv_close (*cd_towcp
);
1332 /* One special case for historical reasons is the backslash
1333 character. In some codesets the byte value 0x5c is not mapped to
1334 U005c in Unicode. These charsets then don't have a backslash
1335 character at all. Therefore we have to live with whatever the
1336 codeset provides and recognize, instead of the U005c, the character
1337 the byte value 0x5c is mapped to. */
1343 wbufptr
= (char *) wbuf
;
1344 wbufsize
= sizeof (wbuf
);
1346 iconv (*cd_towcp
, &bufptr
, &bufsize
, &wbufptr
, &wbufsize
);
1347 if (bufsize
!= 0 || wbufsize
!= 0)
1349 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1350 on with using U005c. */
1351 error (0, 0, gettext ("cannot determine escape character"));
1352 *escape_charp
= L
'\\';
1355 *escape_charp
= wbuf
[0];