msgen: Convert charset=CHARSET to charset=ASCII.

Reported at <https://savannah.gnu.org/bugs/index.php?20923>.

* gettext-tools/src/msgen.c: Include msgl-ascii.h, c-strstr.h, xalloc.h.
(fill_header): New function, based on fill_header in msginit.c.
(main): Invoke it.
* gettext-tools/tests/msgen-1: Provide as input a POT file with charset=CHARSET,
and verify that the output has charset=ASCII.
This commit is contained in:
Bruno Haible 2024-10-15 16:04:45 +02:00
parent 3769eb40d6
commit eeab24002b
2 changed files with 72 additions and 4 deletions

View File

@ -42,7 +42,10 @@
#include "read-properties.h"
#include "read-stringtable.h"
#include "msgl-english.h"
#include "msgl-ascii.h"
#include "msgl-header.h"
#include "c-strstr.h"
#include "xalloc.h"
#include "write-catalog.h"
#include "write-po.h"
#include "write-properties.h"
@ -88,6 +91,7 @@ static const struct option long_options[] =
/* Forward declaration of local functions. */
_GL_NORETURN_FUNC static void usage (int status);
static msgdomain_list_ty *fill_header (msgdomain_list_ty *mdlp);
int
@ -271,6 +275,10 @@ There is NO WARRANTY, to the extent permitted by law.\n\
/* Read input file. */
result = read_catalog_file (argv[optind], input_syntax);
if (!output_syntax->requires_utf8)
/* Fill the header entry. */
result = fill_header (result);
/* Add English translations. */
result = msgdomain_list_english (result);
@ -399,3 +407,63 @@ or by email to <%s>.\n"),
exit (status);
}
/* Fill the templates in the most essential fields of the header entry,
namely to force a charset name. */
static msgdomain_list_ty *
fill_header (msgdomain_list_ty *mdlp)
{
size_t k, j;
if (mdlp->encoding == NULL
&& is_ascii_msgdomain_list (mdlp))
mdlp->encoding = "ASCII";
if (mdlp->encoding != NULL)
for (k = 0; k < mdlp->nitems; k++)
{
message_list_ty *mlp = mdlp->item[k]->messages;
if (mlp->nitems > 0)
{
message_ty *header_mp = NULL;
/* Search the header entry. */
for (j = 0; j < mlp->nitems; j++)
if (is_header (mlp->item[j]) && !mlp->item[j]->obsolete)
{
header_mp = mlp->item[j];
break;
}
/* If it wasn't found, provide one. */
if (header_mp == NULL)
{
static lex_pos_ty pos = { __FILE__, __LINE__ };
const char *msgstr =
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n";
header_mp =
message_alloc (NULL, "", NULL, msgstr, strlen (msgstr), &pos);
message_list_prepend (mlp, header_mp);
}
/* Fill in the charset name. */
{
const char *header = header_mp->msgstr;
const char *charsetstr = c_strstr (header, "charset=");
if (charsetstr != NULL)
{
charsetstr += strlen ("charset=");
header_set_charset (header_mp, charsetstr, mdlp->encoding);
}
}
/* Finally remove the fuzzy attribute. */
header_mp->is_fuzzy = false;
}
}
return mdlp;
}

View File

@ -3,12 +3,12 @@
# Test general operation.
cat <<\EOF > men-test1.po
cat <<\EOF > men-test1.pot
# HEADER.
#
msgid ""
msgstr ""
"Content-Type: text/plain; charset=ASCII\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "height must be positive"
@ -27,7 +27,7 @@ msgstr[1] ""
EOF
: ${MSGEN=msgen}
${MSGEN} -o men-test1.tmp men-test1.po || Exit 1
${MSGEN} -o men-test1.tmp men-test1.pot || Exit 1
LC_ALL=C tr -d '\r' < men-test1.tmp > men-test1.out || Exit 1
cat <<\EOF > men-test1.ok
@ -59,7 +59,7 @@ result=$?
test $result = 0 || exit $result
# Test --lang option.
${MSGEN} --lang=fr -o men-test1.tmp men-test1.po || Exit 1
${MSGEN} --lang=fr -o men-test1.tmp men-test1.pot || Exit 1
grep "Language: fr" men-test1.tmp 2>&1 >/dev/null || Exit 1
grep -v "Language: fr" men-test1.tmp | LC_ALL=C tr -d '\r' > men-test1.out || Exit 1