1 /* Reading tcl/msgcat .msg files.
2 Copyright (C) 2002-2003 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2002.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 #include "relocatable.h"
36 #include "wait-process.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
= concatenated_pathname (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 *) xallocsa (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
= concatenated_pathname (directory
, frobbed_locale_name
, ".msg");
90 freesa (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_po (fp
, "(pipe)", "(pipe)");
118 /* Remove zombie process from process list, and retrieve exit status. */
119 exitstatus
= wait_subprocess (child
, "tclsh", false, false, true, true);
123 /* Special exitcode provided by msgunfmt.tcl. */
124 error (EXIT_FAILURE
, ENOENT
,
125 _("error while opening \"%s\" for reading"), file_name
);
127 error (EXIT_FAILURE
, 0, _("%s subprocess failed with exit code %d"),
128 "tclsh", exitstatus
);
133 /* Move the header entry to the beginning. */
134 for (k
= 0; k
< mdlp
->nitems
; k
++)
136 message_list_ty
*mlp
= mdlp
->item
[k
]->messages
;
139 for (j
= 0; j
< mlp
->nitems
; j
++)
140 if (mlp
->item
[j
]->msgid
[0] == '\0')
142 /* Found the header entry. */
145 message_ty
*header
= mlp
->item
[j
];
148 for (i
= j
; i
> 0; i
--)
149 mlp
->item
[i
] = mlp
->item
[i
- 1];
150 mlp
->item
[0] = header
;