1 /* Reading tcl/msgcat .msg files.
2 Copyright (C) 2002-2003, 2005-2008, 2011, 2015 Free Software
4 Written by Bruno Haible <bruno@clisp.org>, 2002.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 #include "relocatable.h"
33 #include "concat-filename.h"
35 #include "spawn-pipe.h"
36 #include "wait-process.h"
37 #include "read-catalog.h"
43 #define _(str) gettext (str)
46 /* A Tcl .msg file contains Tcl commands. It is best interpreted by Tcl
47 itself. But we redirect the msgcat::mcset function so that it passes
48 the msgid/msgstr pair to us, instead of storing it in the hash table. */
51 msgdomain_read_tcl (const char *locale_name, const char *directory)
53 const char *gettextdatadir;
56 char *frobbed_locale_name;
63 msgdomain_list_ty *mdlp;
67 /* Make it possible to override the msgunfmt.tcl location. This is
68 necessary for running the testsuite before "make install". */
69 gettextdatadir = getenv ("GETTEXTDATADIR");
70 if (gettextdatadir == NULL || gettextdatadir[0] == '\0')
71 gettextdatadir = relocate (GETTEXTDATADIR);
73 tclscript = xconcatenated_filename (gettextdatadir, "msgunfmt.tcl", NULL);
75 /* Convert the locale name to lowercase and remove any encoding. */
76 len = strlen (locale_name);
77 frobbed_locale_name = (char *) xmalloca (len + 1);
78 memcpy (frobbed_locale_name, locale_name, len + 1);
79 for (p = frobbed_locale_name; *p != '\0'; p++)
80 if (*p >= 'A' && *p <= 'Z')
88 file_name = xconcatenated_filename (directory, frobbed_locale_name, ".msg");
90 freea (frobbed_locale_name);
92 /* Prepare arguments. */
100 char *command = shell_quote_argv (argv);
101 printf ("%s\n", command);
105 /* Open a pipe to the Tcl interpreter. */
106 child = create_pipe_in ("tclsh", "tclsh", argv, DEV_NULL, false, true, true,
109 fp = fdopen (fd[0], "r");
111 error (EXIT_FAILURE, errno, _("fdopen() failed"));
113 /* Read the message list. */
114 mdlp = read_catalog_stream (fp, "(pipe)", "(pipe)", &input_format_po);
118 /* Remove zombie process from process list, and retrieve exit status. */
120 wait_subprocess (child, "tclsh", false, false, true, true, NULL);
124 /* Special exitcode provided by msgunfmt.tcl. */
125 error (EXIT_FAILURE, ENOENT,
126 _("error while opening \"%s\" for reading"), file_name);
128 error (EXIT_FAILURE, 0, _("%s subprocess failed with exit code %d"),
129 "tclsh", exitstatus);
134 /* Move the header entry to the beginning. */
135 for (k = 0; k < mdlp->nitems; k++)
137 message_list_ty *mlp = mdlp->item[k]->messages;
140 for (j = 0; j < mlp->nitems; j++)
141 if (is_header (mlp->item[j]))
143 /* Found the header entry. */
146 message_ty *header = mlp->item[j];
149 for (i = j; i > 0; i--)
150 mlp->item[i] = mlp->item[i - 1];
151 mlp->item[0] = header;