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/>. */
35 #include <scratch_buffer.h>
41 #include <unistd_ext.h>
46 #include "catgetsinfo.h"
50 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
61 struct message_list
*next
;
69 struct message_list
*messages
;
76 struct set_list
*next
;
82 struct set_list
*all_sets
;
83 struct set_list
*current_set
;
84 size_t total_messages
;
88 struct obstack mem_pool
;
92 /* If non-zero force creation of new file, not using existing one. */
95 /* Name of output file. */
96 static const char *output_name
;
98 /* Name of generated C header file. */
99 static const char *header_name
;
101 /* Name and version of program. */
102 static void print_version (FILE *stream
, struct argp_state
*state
);
103 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
107 /* Definitions of arguments for argp functions. */
108 static const struct argp_option options
[] =
110 { "header", 'H', N_("NAME"), 0,
111 N_("Create C header file NAME containing symbol definitions") },
112 { "new", OPT_NEW
, NULL
, 0,
113 N_("Do not use existing catalog, force new output file") },
114 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
115 { NULL
, 0, NULL
, 0, NULL
}
118 /* Short description of program. */
119 static const char doc
[] = N_("Generate message catalog.\
120 \vIf INPUT-FILE is -, input is read from standard input. If OUTPUT-FILE\n\
121 is -, output is written to standard output.\n");
123 /* Strings for arguments in help texts. */
124 static const char args_doc
[] = N_("\
125 -o OUTPUT-FILE [INPUT-FILE]...\n[OUTPUT-FILE [INPUT-FILE]...]");
127 /* Prototype for option handler. */
128 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
130 /* Function to print some extra text in the help message. */
131 static char *more_help (int key
, const char *text
, void *input
);
133 /* Data structure to communicate with argp functions. */
134 static struct argp argp
=
136 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
140 /* Wrapper functions with error checking for standard functions. */
141 #include <programs/xmalloc.h>
143 /* Prototypes for local functions. */
144 static void error_print (void);
145 static struct catalog
*read_input_file (struct catalog
*current
,
147 static void write_out (struct catalog
*result
, const char *output_name
,
148 const char *header_name
);
149 static struct set_list
*find_set (struct catalog
*current
, int number
);
150 static void normalize_line (const char *fname
, size_t line
, iconv_t cd
,
151 wchar_t *string
, wchar_t quote_char
,
152 wchar_t escape_char
);
153 static void read_old (struct catalog
*catalog
, const char *file_name
);
154 static int open_conversion (const char *codesetp
, iconv_t
*cd_towcp
,
155 iconv_t
*cd_tombp
, wchar_t *escape_charp
);
159 main (int argc
, char *argv
[])
161 struct catalog
*result
;
164 /* Set program name for messages. */
165 error_print_progname
= error_print
;
167 /* Set locale via LC_ALL. */
168 setlocale (LC_ALL
, "");
170 /* Set the text message domain. */
171 textdomain (PACKAGE
);
173 /* Initialize local variables. */
176 /* Parse and process arguments. */
177 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
179 /* Determine output file. */
180 if (output_name
== NULL
)
181 output_name
= remaining
< argc
? argv
[remaining
++] : "-";
183 /* Process all input files. */
184 setlocale (LC_CTYPE
, "C");
185 if (remaining
< argc
)
187 result
= read_input_file (result
, argv
[remaining
]);
188 while (++remaining
< argc
);
190 result
= read_input_file (NULL
, "-");
192 /* Write out the result. */
194 write_out (result
, output_name
, header_name
);
196 return error_message_count
!= 0;
200 /* Handle program arguments. */
202 parse_opt (int key
, char *arg
, struct argp_state
*state
)
216 return ARGP_ERR_UNKNOWN
;
223 more_help (int key
, const char *text
, void *input
)
228 case ARGP_KEY_HELP_EXTRA
:
229 /* We print some extra information. */
230 if (asprintf (&tp
, gettext ("\
231 For bug reporting instructions, please see:\n\
232 %s.\n"), REPORT_BUGS_TO
) < 0)
238 return (char *) text
;
241 /* Print the version information. */
243 print_version (FILE *stream
, struct argp_state
*state
)
245 fprintf (stream
, "gencat %s%s\n", PKGVERSION
, VERSION
);
246 fprintf (stream
, gettext ("\
247 Copyright (C) %s Free Software Foundation, Inc.\n\
248 This is free software; see the source for copying conditions. There is NO\n\
249 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
251 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
255 /* The address of this function will be assigned to the hook in the
260 /* We don't want the program name to be printed in messages. Emacs'
261 compile.el does not like this. */
265 static struct catalog
*
266 read_input_file (struct catalog
*current
, const char *fname
)
274 iconv_t cd_towc
= (iconv_t
) -1;
275 iconv_t cd_tomb
= (iconv_t
) -1;
276 wchar_t escape_char
= L
'\\';
277 char *codeset
= NULL
;
279 if (strcmp (fname
, "-") == 0 || strcmp (fname
, "/dev/stdin") == 0)
282 fname
= gettext ("*standard input*");
285 fp
= fopen (fname
, "r");
288 error (0, errno
, gettext ("cannot open input file `%s'"), fname
);
292 /* If we haven't seen anything yet, allocate result structure. */
295 current
= (struct catalog
*) xcalloc (1, sizeof (*current
));
297 #define obstack_chunk_alloc malloc
298 #define obstack_chunk_free free
299 obstack_init (¤t
->mem_pool
);
301 current
->current_set
= find_set (current
, NL_SETD
);
309 wbuf
= (wchar_t *) xmalloc (wbufsize
);
315 size_t start_line
= line_number
+ 1;
322 act_len
= getline (&buf
, &len
, fp
);
327 /* It the line continued? */
329 if (buf
[act_len
- 1] == '\n')
333 /* There might be more than one backslash at the end of
334 the line. Only if there is an odd number of them is
335 the line continued. */
336 if (act_len
> 0 && buf
[act_len
- 1] == '\\')
338 int temp_act_len
= act_len
;
343 continued
= !continued
;
345 while (temp_act_len
> 0 && buf
[temp_act_len
- 1] == '\\');
352 /* Append to currently selected line. */
353 obstack_grow (¤t
->mem_pool
, buf
, act_len
);
357 obstack_1grow (¤t
->mem_pool
, '\0');
358 this_line
= (char *) obstack_finish (¤t
->mem_pool
);
361 if (this_line
[0] == '$')
363 if (isblank (this_line
[1]))
366 while (isblank (this_line
[cnt
]))
368 if (strncmp (&this_line
[cnt
], "codeset=", 8) != 0)
369 /* This is a comment line. Do nothing. */;
370 else if (codeset
!= NULL
)
371 /* Ignore multiple codeset. */;
376 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
380 int len
= cnt
- start
;
381 codeset
= xmalloc (len
+ 1);
382 *((char *) mempcpy (codeset
, &this_line
[start
], len
))
387 else if (strncmp (&this_line
[1], "set", 3) == 0)
389 int cnt
= sizeof ("set");
391 const char *symbol
= NULL
;
392 while (isspace (this_line
[cnt
]))
395 if (isdigit (this_line
[cnt
]))
397 set_number
= atol (&this_line
[cnt
]);
399 /* If the given number for the character set is
400 higher than any we used for symbolic set names
401 avoid clashing by using only higher numbers for
402 the following symbolic definitions. */
403 if (set_number
> current
->last_set
)
404 current
->last_set
= set_number
;
408 /* See whether it is a reasonable identifier. */
410 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
415 /* No correct character found. */
416 error_at_line (0, 0, fname
, start_line
,
417 gettext ("illegal set number"));
422 /* We have found seomthing that looks like a
423 correct identifier. */
424 struct set_list
*runp
;
426 this_line
[cnt
] = '\0';
428 symbol
= &this_line
[start
];
430 /* Test whether the identifier was already used. */
431 runp
= current
->all_sets
;
433 if (runp
->symbol
!= NULL
434 && strcmp (runp
->symbol
, symbol
) == 0)
441 /* We cannot allow duplicate identifiers for
443 error_at_line (0, 0, fname
, start_line
,
444 gettext ("duplicate set definition"));
445 error_at_line (0, 0, runp
->fname
, runp
->line
,
447 this is the first definition"));
451 /* Allocate next free message set for identifier. */
452 set_number
= ++current
->last_set
;
458 /* We found a legal set number. */
459 current
->current_set
= find_set (current
, set_number
);
462 current
->current_set
->symbol
= symbol
;
463 current
->current_set
->fname
= fname
;
464 current
->current_set
->line
= start_line
;
467 else if (strncmp (&this_line
[1], "delset", 6) == 0)
469 int cnt
= sizeof ("delset");
470 while (isspace (this_line
[cnt
]))
473 if (isdigit (this_line
[cnt
]))
475 size_t set_number
= atol (&this_line
[cnt
]);
476 struct set_list
*set
;
478 /* Mark the message set with the given number as
480 set
= find_set (current
, set_number
);
485 /* See whether it is a reasonable identifier. */
487 while (isalnum (this_line
[cnt
]) || this_line
[cnt
] == '_')
491 error_at_line (0, 0, fname
, start_line
,
492 gettext ("illegal set number"));
496 struct set_list
*runp
;
498 this_line
[cnt
] = '\0';
500 symbol
= &this_line
[start
];
502 /* We have a symbolic set name. This name must
503 appear somewhere else in the catalogs read so
505 for (runp
= current
->all_sets
; runp
!= NULL
;
508 if (strcmp (runp
->symbol
, symbol
) == 0)
515 /* Name does not exist before. */
516 error_at_line (0, 0, fname
, start_line
,
517 gettext ("unknown set `%s'"), symbol
);
521 else if (strncmp (&this_line
[1], "quote", 5) == 0)
530 cnt
= sizeof ("quote");
531 while (isspace (this_line
[cnt
]))
534 /* We need the conversion. */
535 if (cd_towc
== (iconv_t
) -1
536 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
538 /* Something is wrong. */
541 /* Yes, the quote char can be '\0'; this means no quote
542 char. The function using the information works on
543 wide characters so we have to convert it here. */
544 buf
[0] = this_line
[cnt
];
549 wbufptr
= (char *) wbuf
;
552 /* Flush the state. */
553 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
555 iconv (cd_towc
, &bufptr
, &buflen
, &wbufptr
, &wbuflen
);
556 if (buflen
!= 0 || (wchar_t *) wbufptr
!= &wbuf
[2])
557 error_at_line (0, 0, fname
, start_line
,
558 gettext ("invalid quote character"));
560 /* Use the converted wide character. */
561 current
->quote_char
= wbuf
[0];
567 while (this_line
[cnt
] != '\0' && !isspace (this_line
[cnt
]))
569 this_line
[cnt
] = '\0';
570 error_at_line (0, 0, fname
, start_line
,
571 gettext ("unknown directive `%s': line ignored"),
575 else if (isalnum (this_line
[0]) || this_line
[0] == '_')
577 const char *ident
= this_line
;
578 char *line
= this_line
;
583 while (line
[0] != '\0' && !isspace (line
[0]));
585 *line
++ = '\0'; /* Terminate the identifier. */
587 /* Now we found the beginning of the message itself. */
589 if (isdigit (ident
[0]))
591 struct message_list
*runp
;
592 struct message_list
*lastp
;
594 message_number
= atoi (ident
);
596 /* Find location to insert the new message. */
597 runp
= current
->current_set
->messages
;
600 if (runp
->number
== message_number
)
609 /* Oh, oh. There is already a message with this
610 number in the message set. */
611 if (runp
->symbol
== NULL
)
613 /* The existing message had its number specified
614 by the user. Fatal collision type uh, oh. */
615 error_at_line (0, 0, fname
, start_line
,
616 gettext ("duplicated message number"));
617 error_at_line (0, 0, runp
->fname
, runp
->line
,
618 gettext ("this is the first definition"));
623 /* Collision was with number auto-assigned to a
624 symbolic. Change existing symbolic number
625 and move to end the list (if not already there). */
626 runp
->number
= ++current
->current_set
->last_message
;
628 if (runp
->next
!= NULL
)
630 struct message_list
*endp
;
633 current
->current_set
->messages
=runp
->next
;
635 lastp
->next
=runp
->next
;
638 while (endp
->next
!= NULL
)
646 ident
= NULL
; /* We don't have a symbol. */
648 if (message_number
!= 0
649 && message_number
> current
->current_set
->last_message
)
650 current
->current_set
->last_message
= message_number
;
652 else if (ident
[0] != '\0')
654 struct message_list
*runp
;
656 /* Test whether the symbolic name was not used for
657 another message in this message set. */
658 runp
= current
->current_set
->messages
;
660 if (runp
->symbol
!= NULL
&& strcmp (ident
, runp
->symbol
) == 0)
666 /* The name is already used. */
667 error_at_line (0, 0, fname
, start_line
, gettext ("\
668 duplicated message identifier"));
669 error_at_line (0, 0, runp
->fname
, runp
->line
,
670 gettext ("this is the first definition"));
674 /* Give the message the next unused number. */
675 message_number
= ++current
->current_set
->last_message
;
680 if (message_number
!= 0)
686 struct message_list
*newp
;
687 size_t line_len
= strlen (line
) + 1;
688 size_t ident_len
= 0;
690 /* We need the conversion. */
691 if (cd_towc
== (iconv_t
) -1
692 && open_conversion (codeset
, &cd_towc
, &cd_tomb
,
694 /* Something is wrong. */
697 /* Convert to a wide character string. We have to
698 interpret escape sequences which will be impossible
699 without doing the conversion if the codeset of the
700 message is stateful. */
705 outbuf
= (char *) wbuf
;
708 /* Flush the state. */
709 iconv (cd_towc
, NULL
, NULL
, NULL
, NULL
);
711 iconv (cd_towc
, &inbuf
, &inlen
, &outbuf
, &outlen
);
714 /* The string is converted. */
715 assert (outlen
< wbufsize
);
716 assert (wbuf
[(wbufsize
- outlen
) / sizeof (wchar_t) - 1]
723 /* Something is wrong with this string, we ignore it. */
724 error_at_line (0, 0, fname
, start_line
, gettext ("\
725 invalid character: message ignored"));
729 /* The output buffer is too small. */
731 wbuf
= (wchar_t *) xrealloc (wbuf
, wbufsize
);
734 /* Strip quote characters, change escape sequences into
735 correct characters etc. */
736 normalize_line (fname
, start_line
, cd_towc
, wbuf
,
737 current
->quote_char
, escape_char
);
740 ident_len
= line
- this_line
;
742 /* Now the string is free of escape sequences. Convert it
743 back into a multibyte character string. First free the
744 memory allocated for the original string. */
745 obstack_free (¤t
->mem_pool
, this_line
);
747 used
= 1; /* Yes, we use the line. */
749 /* Now fill in the new string. It should never happen that
750 the replaced string is longer than the original. */
751 inbuf
= (char *) wbuf
;
752 inlen
= (wcslen (wbuf
) + 1) * sizeof (wchar_t);
754 outlen
= obstack_room (¤t
->mem_pool
);
755 obstack_blank (¤t
->mem_pool
, outlen
);
756 this_line
= (char *) obstack_base (¤t
->mem_pool
);
757 outbuf
= this_line
+ ident_len
;
760 /* Flush the state. */
761 iconv (cd_tomb
, NULL
, NULL
, NULL
, NULL
);
763 iconv (cd_tomb
, &inbuf
, &inlen
, &outbuf
, &outlen
);
766 error_at_line (0, 0, fname
, start_line
,
767 gettext ("invalid line"));
770 assert (outbuf
[-1] == '\0');
772 /* Free the memory in the obstack we don't use. */
773 obstack_blank (¤t
->mem_pool
, -(int) outlen
);
774 line
= obstack_finish (¤t
->mem_pool
);
776 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
777 newp
->number
= message_number
;
778 newp
->message
= line
+ ident_len
;
779 /* Remember symbolic name; is NULL if no is given. */
780 newp
->symbol
= ident
? line
: NULL
;
781 /* Remember where we found the character. */
783 newp
->line
= start_line
;
785 /* Find place to insert to message. We keep them in a
786 sorted single linked list. */
787 if (current
->current_set
->messages
== NULL
788 || current
->current_set
->messages
->number
> message_number
)
790 newp
->next
= current
->current_set
->messages
;
791 current
->current_set
->messages
= newp
;
795 struct message_list
*runp
;
796 runp
= current
->current_set
->messages
;
797 while (runp
->next
!= NULL
)
798 if (runp
->next
->number
> message_number
)
802 newp
->next
= runp
->next
;
806 ++current
->total_messages
;
813 /* See whether we have any non-white space character in this
815 while (this_line
[cnt
] != '\0' && isspace (this_line
[cnt
]))
818 if (this_line
[cnt
] != '\0')
819 /* Yes, some unknown characters found. */
820 error_at_line (0, 0, fname
, start_line
,
821 gettext ("malformed line ignored"));
825 /* We can save the memory for the line if it was not used. */
827 obstack_free (¤t
->mem_pool
, this_line
);
830 /* Close the conversion modules. */
831 iconv_close (cd_towc
);
832 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
;
858 struct scratch_buffer buf1
;
859 scratch_buffer_init (&buf1
);
860 struct scratch_buffer buf2
;
861 scratch_buffer_init (&buf2
);
863 /* If not otherwise told try to read file with existing
866 read_old (catalog
, output_name
);
868 /* Initialize best_size with a very high value. */
869 best_total
= best_size
= best_depth
= UINT_MAX
;
871 /* We need some start size for testing. Let's start with
872 TOTAL_MESSAGES / 5, which theoretically provides a mean depth of
874 act_size
= 1 + catalog
->total_messages
/ 5;
876 /* We determine the size of a hash table here. Because the message
877 numbers can be chosen arbitrary by the programmer we cannot use
878 the simple method of accessing the array using the message
879 number. The algorithm is based on the trivial hash function
880 NUMBER % TABLE_SIZE, where collisions are stored in a second
881 dimension up to TABLE_DEPTH. We here compute TABLE_SIZE so that
882 the needed space (= TABLE_SIZE * TABLE_DEPTH) is minimal. */
883 while (act_size
<= best_total
)
885 size_t deep
[act_size
];
888 memset (deep
, '\0', act_size
* sizeof (size_t));
889 set_run
= catalog
->all_sets
;
890 while (set_run
!= NULL
)
892 struct message_list
*message_run
;
894 message_run
= set_run
->messages
;
895 while (message_run
!= NULL
)
897 size_t idx
= (message_run
->number
* set_run
->number
) % act_size
;
900 if (deep
[idx
] > act_depth
)
902 act_depth
= deep
[idx
];
903 if (act_depth
* act_size
> best_total
)
906 message_run
= message_run
->next
;
908 set_run
= set_run
->next
;
911 if (act_depth
* act_size
<= best_total
)
913 /* We have found a better solution. */
914 best_total
= act_depth
* act_size
;
915 best_size
= act_size
;
916 best_depth
= act_depth
;
922 /* let's be prepared for an empty message file. */
923 if (best_size
== UINT_MAX
)
929 /* OK, now we have the size we will use. Fill in the header, build
930 the table and the second one with swapped byte order. */
931 obj
.magic
= CATGETS_MAGIC
;
932 obj
.plane_size
= best_size
;
933 obj
.plane_depth
= best_depth
;
935 uint32_t array_size
= best_size
* best_depth
* sizeof (uint32_t) * 3;
936 /* Allocate room for all needed arrays. */
937 if (!scratch_buffer_set_array_size (&buf1
, best_size
* best_depth
* 3,
939 error (EXIT_FAILURE
, ENOMEM
, gettext ("cannot allocate memory"));
941 memset (array1
, '\0', array_size
);
943 if (!scratch_buffer_set_array_size (&buf2
, best_size
* best_depth
* 3,
946 scratch_buffer_free (&buf1
);
947 error (EXIT_FAILURE
, ENOMEM
, gettext ("cannot allocate memory"));
950 obstack_init (&string_pool
);
952 set_run
= catalog
->all_sets
;
953 while (set_run
!= NULL
)
955 struct message_list
*message_run
;
957 message_run
= set_run
->messages
;
958 while (message_run
!= NULL
)
960 size_t idx
= (((message_run
->number
* set_run
->number
) % best_size
)
962 /* Determine collision depth. */
963 while (array1
[idx
] != 0)
964 idx
+= best_size
* 3;
966 /* Store set number, message number and pointer into string
967 space, relative to the first string. */
968 array1
[idx
+ 0] = set_run
->number
;
969 array1
[idx
+ 1] = message_run
->number
;
970 array1
[idx
+ 2] = obstack_object_size (&string_pool
);
972 /* Add current string to the continuous space containing all
974 obstack_grow0 (&string_pool
, message_run
->message
,
975 strlen (message_run
->message
));
977 message_run
= message_run
->next
;
980 set_run
= set_run
->next
;
982 strings_size
= obstack_object_size (&string_pool
);
983 strings
= obstack_finish (&string_pool
);
985 /* Compute ARRAY2 by changing the byte order. */
986 for (cnt
= 0; cnt
< best_size
* best_depth
* 3; ++cnt
)
987 array2
[cnt
] = SWAPU32 (array1
[cnt
]);
989 /* Now we can write out the whole data. */
990 if (strcmp (output_name
, "-") == 0
991 || strcmp (output_name
, "/dev/stdout") == 0)
995 fd
= creat (output_name
, 0666);
998 scratch_buffer_free (&buf1
);
999 scratch_buffer_free (&buf2
);
1000 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
1005 /* Write out header. */
1006 write_all(fd
, &obj
, sizeof (obj
));
1008 /* We always write out the little endian version of the index
1010 #if __BYTE_ORDER == __LITTLE_ENDIAN
1011 write_all(fd
, array1
, array_size
);
1012 write_all(fd
, array2
, array_size
);
1013 #elif __BYTE_ORDER == __BIG_ENDIAN
1014 write_all(fd
, array2
, array_size
);
1015 write_all(fd
, array1
, array_size
);
1017 # error Cannot handle __BYTE_ORDER byte order
1020 /* Finally write the strings. */
1021 write_all(fd
, strings
, strings_size
);
1023 if (fd
!= STDOUT_FILENO
)
1026 /* If requested now write out the header file. */
1027 if (header_name
!= NULL
)
1032 /* Open output file. "-" or "/dev/stdout" means write to
1034 if (strcmp (header_name
, "-") == 0
1035 || strcmp (header_name
, "/dev/stdout") == 0)
1039 fp
= fopen (header_name
, "w");
1042 scratch_buffer_free (&buf1
);
1043 scratch_buffer_free (&buf2
);
1044 error (EXIT_FAILURE
, errno
,
1045 gettext ("cannot open output file `%s'"), header_name
);
1049 /* Iterate over all sets and all messages. */
1050 set_run
= catalog
->all_sets
;
1051 while (set_run
!= NULL
)
1053 struct message_list
*message_run
;
1055 /* If the current message set has a symbolic name write this
1057 if (set_run
->symbol
!= NULL
)
1058 fprintf (fp
, "%s#define %sSet %#x\t/* %s:%zu */\n",
1059 first
? "" : "\n", set_run
->symbol
, set_run
->number
- 1,
1060 set_run
->fname
, set_run
->line
);
1063 message_run
= set_run
->messages
;
1064 while (message_run
!= NULL
)
1066 /* If the current message has a symbolic name write
1067 #define out. But we have to take care for the set
1068 not having a symbolic name. */
1069 if (message_run
->symbol
!= NULL
)
1071 if (set_run
->symbol
== NULL
)
1072 fprintf (fp
, "#define AutomaticSet%d%s %#x\t/* %s:%zu */\n",
1073 set_run
->number
, message_run
->symbol
,
1074 message_run
->number
, message_run
->fname
,
1077 fprintf (fp
, "#define %s%s %#x\t/* %s:%zu */\n",
1078 set_run
->symbol
, message_run
->symbol
,
1079 message_run
->number
, message_run
->fname
,
1083 message_run
= message_run
->next
;
1086 set_run
= set_run
->next
;
1092 scratch_buffer_free (&buf1
);
1093 scratch_buffer_free (&buf2
);
1097 static struct set_list
*
1098 find_set (struct catalog
*current
, int number
)
1100 struct set_list
*result
= current
->all_sets
;
1102 /* We must avoid set number 0 because a set of this number signals
1103 in the tables that the entry is not occupied. */
1106 while (result
!= NULL
)
1107 if (result
->number
== number
)
1110 result
= result
->next
;
1112 /* Prepare new message set. */
1113 result
= (struct set_list
*) xcalloc (1, sizeof (*result
));
1114 result
->number
= number
;
1115 result
->next
= current
->all_sets
;
1116 current
->all_sets
= result
;
1122 /* Normalize given string *in*place* by processing escape sequences
1123 and quote characters. */
1125 normalize_line (const char *fname
, size_t line
, iconv_t cd
, wchar_t *string
,
1126 wchar_t quote_char
, wchar_t escape_char
)
1129 wchar_t *rp
= string
;
1130 wchar_t *wp
= string
;
1132 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1140 while (*rp
!= L
'\0')
1141 if (*rp
== quote_char
)
1142 /* We simply end the string when we find the first time an
1143 not-escaped quote character. */
1145 else if (*rp
== escape_char
)
1148 if (quote_char
!= L
'\0' && *rp
== quote_char
)
1149 /* This is an extension to XPG. */
1152 /* Recognize escape sequences. */
1189 number
= *rp
++ - L
'0';
1190 while (number
<= (255 / 8) && *rp
>= L
'0' && *rp
<= L
'7')
1193 number
+= *rp
++ - L
'0';
1196 cbuf
[0] = (char) number
;
1201 wcbufptr
= (char *) wcbuf
;
1202 wcbufin
= sizeof (wcbuf
);
1204 /* Flush the state. */
1205 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
1207 iconv (cd
, &cbufptr
, &cbufin
, &wcbufptr
, &wcbufin
);
1208 if (cbufptr
!= &cbuf
[2] || (wchar_t *) wcbufptr
!= &wcbuf
[2])
1209 error_at_line (0, 0, fname
, line
,
1210 gettext ("invalid escape sequence"));
1216 if (*rp
== escape_char
)
1218 *wp
++ = escape_char
;
1223 /* Simply ignore the backslash character. */
1231 /* If we saw a quote character at the beginning we expect another
1233 if (is_quoted
&& *rp
!= quote_char
)
1234 error_at_line (0, 0, fname
, line
, gettext ("unterminated message"));
1236 /* Terminate string. */
1243 read_old (struct catalog
*catalog
, const char *file_name
)
1245 struct catalog_info old_cat_obj
;
1246 struct set_list
*set
= NULL
;
1250 /* Try to open catalog, but don't look through the NLSPATH. */
1251 if (__open_catalog (file_name
, NULL
, NULL
, &old_cat_obj
) != 0)
1253 if (errno
== ENOENT
)
1254 /* No problem, the catalog simply does not exist. */
1257 error (EXIT_FAILURE
, errno
,
1258 gettext ("while opening old catalog file"));
1261 /* OK, we have the catalog loaded. Now read all messages and merge
1262 them. When set and message number clash for any message the new
1263 one is used. If the new one is empty it indicates that the
1264 message should be deleted. */
1265 for (cnt
= 0; cnt
< old_cat_obj
.plane_size
* old_cat_obj
.plane_depth
; ++cnt
)
1267 struct message_list
*message
, *last
;
1269 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] == 0)
1270 /* No message in this slot. */
1273 if (old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1 != (uint32_t) last_set
)
1275 last_set
= old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1;
1276 set
= find_set (catalog
, old_cat_obj
.name_ptr
[cnt
* 3 + 0] - 1);
1280 message
= set
->messages
;
1281 while (message
!= NULL
)
1283 if ((uint32_t) message
->number
>= old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1286 message
= message
->next
;
1290 || (uint32_t) message
->number
> old_cat_obj
.name_ptr
[cnt
* 3 + 1])
1292 /* We have found a message which is not yet in the catalog.
1293 Insert it at the right position. */
1294 struct message_list
*newp
;
1296 newp
= (struct message_list
*) xmalloc (sizeof (*newp
));
1297 newp
->number
= old_cat_obj
.name_ptr
[cnt
* 3 + 1];
1299 &old_cat_obj
.strings
[old_cat_obj
.name_ptr
[cnt
* 3 + 2]];
1302 newp
->symbol
= NULL
;
1303 newp
->next
= message
;
1306 set
->messages
= newp
;
1310 ++catalog
->total_messages
;
1312 else if (*message
->message
== '\0')
1314 /* The new empty message has overridden the old one thus
1315 "deleting" it as required. Now remove the empty remains. */
1317 set
->messages
= message
->next
;
1319 last
->next
= message
->next
;
1326 open_conversion (const char *codeset
, iconv_t
*cd_towcp
, iconv_t
*cd_tombp
,
1327 wchar_t *escape_charp
)
1336 /* If the input file does not specify the codeset use the locale's. */
1337 if (codeset
== NULL
)
1339 setlocale (LC_ALL
, "");
1340 codeset
= nl_langinfo (CODESET
);
1341 setlocale (LC_ALL
, "C");
1344 /* Get the conversion modules. */
1345 *cd_towcp
= iconv_open ("WCHAR_T", codeset
);
1346 *cd_tombp
= iconv_open (codeset
, "WCHAR_T");
1347 if (*cd_towcp
== (iconv_t
) -1 || *cd_tombp
== (iconv_t
) -1)
1349 error (0, 0, gettext ("conversion modules not available"));
1350 if (*cd_towcp
!= (iconv_t
) -1)
1351 iconv_close (*cd_towcp
);
1356 /* One special case for historical reasons is the backslash
1357 character. In some codesets the byte value 0x5c is not mapped to
1358 U005c in Unicode. These charsets then don't have a backslash
1359 character at all. Therefore we have to live with whatever the
1360 codeset provides and recognize, instead of the U005c, the character
1361 the byte value 0x5c is mapped to. */
1367 wbufptr
= (char *) wbuf
;
1368 wbufsize
= sizeof (wbuf
);
1370 iconv (*cd_towcp
, &bufptr
, &bufsize
, &wbufptr
, &wbufsize
);
1371 if (bufsize
!= 0 || wbufsize
!= 0)
1373 /* Something went wrong, we couldn't convert the byte 0x5c. Go
1374 on with using U005c. */
1375 error (0, 0, gettext ("cannot determine escape character"));
1376 *escape_charp
= L
'\\';
1379 *escape_charp
= wbuf
[0];