#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <alloca.h>
#include "read-tcl.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "msgunfmt.h"
#include "relocatable.h"
#include "pathname.h"
#include "sh-quote.h"
#include "pipe.h"
#include "wait-process.h"
#include "read-po.h"
#include "error.h"
#include "exit.h"
#include "gettext.h"
#define _(str) gettext (str)
msgdomain_list_ty *
msgdomain_read_tcl (const char *locale_name, const char *directory)
{
const char *gettextdatadir;
char *tclscript;
size_t len;
char *frobbed_locale_name;
char *p;
char *file_name;
char *argv[4];
pid_t child;
int fd[1];
FILE *fp;
msgdomain_list_ty *mdlp;
int exitstatus;
size_t k;
gettextdatadir = getenv ("GETTEXTDATADIR");
if (gettextdatadir == NULL || gettextdatadir[0] == '\0')
gettextdatadir = relocate (GETTEXTDATADIR);
tclscript = concatenated_pathname (gettextdatadir, "msgunfmt.tcl", NULL);
len = strlen (locale_name);
frobbed_locale_name = (char *) alloca (len + 1);
memcpy (frobbed_locale_name, locale_name, len + 1);
for (p = frobbed_locale_name; *p != '\0'; p++)
if (*p >= 'A' && *p <= 'Z')
*p = *p - 'A' + 'a';
else if (*p == '.')
{
*p = '\0';
break;
}
file_name = concatenated_pathname (directory, frobbed_locale_name, ".msg");
argv[0] = "tclsh";
argv[1] = tclscript;
argv[2] = file_name;
argv[3] = NULL;
if (verbose)
{
char *command = shell_quote_argv (argv);
printf ("%s\n", command);
free (command);
}
child = create_pipe_in ("tclsh", "tclsh", argv, DEV_NULL, false, true, true,
fd);
fp = fdopen (fd[0], "r");
if (fp == NULL)
error (EXIT_FAILURE, errno, _("fdopen() failed"));
mdlp = read_po (fp, "(pipe)", "(pipe)");
fclose (fp);
exitstatus = wait_subprocess (child, "tclsh", false, true, true);
if (exitstatus != 0)
{
if (exitstatus == 2)
error (EXIT_FAILURE, ENOENT,
_("error while opening \"%s\" for reading"), file_name);
else
error (EXIT_FAILURE, 0, _("%s subprocess failed with exit code %d"),
"tclsh", exitstatus);
}
free (tclscript);
for (k = 0; k < mdlp->nitems; k++)
{
message_list_ty *mlp = mdlp->item[k]->messages;
size_t j;
for (j = 0; j < mlp->nitems; j++)
if (mlp->item[j]->msgid[0] == '\0')
{
if (j > 0)
{
message_ty *header = mlp->item[j];
size_t i;
for (i = j; i > 0; i--)
mlp->item[i] = mlp->item[i - 1];
mlp->item[0] = header;
}
break;
}
}
return mdlp;
}