From: Bruno Haible Date: Tue, 15 Oct 2024 14:04:45 +0000 (+0200) Subject: msgen: Convert charset=CHARSET to charset=ASCII. X-Git-Tag: v0.23~49 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=eeab24002b569910c5d244435bd774f3cdeaed0b;p=thirdparty%2Fgettext.git msgen: Convert charset=CHARSET to charset=ASCII. Reported at . * 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. --- diff --git a/gettext-tools/src/msgen.c b/gettext-tools/src/msgen.c index b8c2210b6..045b1fbd3 100644 --- a/gettext-tools/src/msgen.c +++ b/gettext-tools/src/msgen.c @@ -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; +} diff --git a/gettext-tools/tests/msgen-1 b/gettext-tools/tests/msgen-1 index e6e9b4202..faa189583 100755 --- a/gettext-tools/tests/msgen-1 +++ b/gettext-tools/tests/msgen-1 @@ -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