1*946379e7Schristos /* Writing C# .resources files.
2*946379e7Schristos Copyright (C) 2003, 2005 Free Software Foundation, Inc.
3*946379e7Schristos Written by Bruno Haible <bruno@clisp.org>, 2003.
4*946379e7Schristos
5*946379e7Schristos This program is free software; you can redistribute it and/or modify
6*946379e7Schristos it under the terms of the GNU General Public License as published by
7*946379e7Schristos the Free Software Foundation; either version 2, or (at your option)
8*946379e7Schristos any later version.
9*946379e7Schristos
10*946379e7Schristos This program is distributed in the hope that it will be useful,
11*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*946379e7Schristos GNU General Public License for more details.
14*946379e7Schristos
15*946379e7Schristos You should have received a copy of the GNU General Public License
16*946379e7Schristos along with this program; if not, write to the Free Software Foundation,
17*946379e7Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18*946379e7Schristos
19*946379e7Schristos #ifdef HAVE_CONFIG_H
20*946379e7Schristos # include <config.h>
21*946379e7Schristos #endif
22*946379e7Schristos
23*946379e7Schristos /* Specification. */
24*946379e7Schristos #include "write-resources.h"
25*946379e7Schristos
26*946379e7Schristos #include <errno.h>
27*946379e7Schristos #include <stdbool.h>
28*946379e7Schristos #include <stdlib.h>
29*946379e7Schristos #include <stdio.h>
30*946379e7Schristos #include <string.h>
31*946379e7Schristos
32*946379e7Schristos #include "error.h"
33*946379e7Schristos #include "xerror.h"
34*946379e7Schristos #include "relocatable.h"
35*946379e7Schristos #include "csharpexec.h"
36*946379e7Schristos #include "pipe.h"
37*946379e7Schristos #include "wait-process.h"
38*946379e7Schristos #include "message.h"
39*946379e7Schristos #include "msgfmt.h"
40*946379e7Schristos #include "msgl-iconv.h"
41*946379e7Schristos #include "po-charset.h"
42*946379e7Schristos #include "xalloc.h"
43*946379e7Schristos #include "pathname.h"
44*946379e7Schristos #include "fwriteerror.h"
45*946379e7Schristos #include "exit.h"
46*946379e7Schristos #include "gettext.h"
47*946379e7Schristos
48*946379e7Schristos #define _(str) gettext (str)
49*946379e7Schristos
50*946379e7Schristos
51*946379e7Schristos /* A .resources file has such a complex format that it's most easily generated
52*946379e7Schristos through the C# class ResourceWriter. So we start a C# process to execute
53*946379e7Schristos the WriteResource program, sending it the msgid/msgstr pairs as
54*946379e7Schristos NUL-terminated UTF-8 encoded strings. */
55*946379e7Schristos
56*946379e7Schristos struct locals
57*946379e7Schristos {
58*946379e7Schristos /* IN */
59*946379e7Schristos message_list_ty *mlp;
60*946379e7Schristos };
61*946379e7Schristos
62*946379e7Schristos static bool
execute_writing_input(const char * progname,const char * prog_path,char ** prog_argv,void * private_data)63*946379e7Schristos execute_writing_input (const char *progname,
64*946379e7Schristos const char *prog_path, char **prog_argv,
65*946379e7Schristos void *private_data)
66*946379e7Schristos {
67*946379e7Schristos struct locals *l = (struct locals *) private_data;
68*946379e7Schristos pid_t child;
69*946379e7Schristos int fd[1];
70*946379e7Schristos FILE *fp;
71*946379e7Schristos int exitstatus;
72*946379e7Schristos
73*946379e7Schristos /* Open a pipe to the C# execution engine. */
74*946379e7Schristos child = create_pipe_out (progname, prog_path, prog_argv, NULL, false,
75*946379e7Schristos true, true, fd);
76*946379e7Schristos
77*946379e7Schristos fp = fdopen (fd[0], "wb");
78*946379e7Schristos if (fp == NULL)
79*946379e7Schristos error (EXIT_FAILURE, errno, _("fdopen() failed"));
80*946379e7Schristos
81*946379e7Schristos /* Write the message list. */
82*946379e7Schristos {
83*946379e7Schristos message_list_ty *mlp = l->mlp;
84*946379e7Schristos size_t j;
85*946379e7Schristos
86*946379e7Schristos for (j = 0; j < mlp->nitems; j++)
87*946379e7Schristos {
88*946379e7Schristos message_ty *mp = mlp->item[j];
89*946379e7Schristos
90*946379e7Schristos fwrite (mp->msgid, 1, strlen (mp->msgid) + 1, fp);
91*946379e7Schristos fwrite (mp->msgstr, 1, strlen (mp->msgstr) + 1, fp);
92*946379e7Schristos }
93*946379e7Schristos }
94*946379e7Schristos
95*946379e7Schristos if (fwriteerror (fp))
96*946379e7Schristos error (EXIT_FAILURE, 0, _("error while writing to %s subprocess"),
97*946379e7Schristos progname);
98*946379e7Schristos
99*946379e7Schristos /* Remove zombie process from process list, and retrieve exit status. */
100*946379e7Schristos /* He we can ignore SIGPIPE because WriteResource either writes to a file
101*946379e7Schristos - then it never gets SIGPIPE - or to standard output, and in the latter
102*946379e7Schristos case it has no side effects other than writing to standard output. */
103*946379e7Schristos exitstatus = wait_subprocess (child, progname, true, false, true, true);
104*946379e7Schristos if (exitstatus != 0)
105*946379e7Schristos error (EXIT_FAILURE, 0, _("%s subprocess failed with exit code %d"),
106*946379e7Schristos progname, exitstatus);
107*946379e7Schristos
108*946379e7Schristos return false;
109*946379e7Schristos }
110*946379e7Schristos
111*946379e7Schristos int
msgdomain_write_csharp_resources(message_list_ty * mlp,const char * canon_encoding,const char * domain_name,const char * file_name)112*946379e7Schristos msgdomain_write_csharp_resources (message_list_ty *mlp,
113*946379e7Schristos const char *canon_encoding,
114*946379e7Schristos const char *domain_name,
115*946379e7Schristos const char *file_name)
116*946379e7Schristos {
117*946379e7Schristos /* If no entry for this domain don't even create the file. */
118*946379e7Schristos if (mlp->nitems != 0)
119*946379e7Schristos {
120*946379e7Schristos /* Determine whether mlp has entries with context. */
121*946379e7Schristos {
122*946379e7Schristos bool has_context;
123*946379e7Schristos size_t j;
124*946379e7Schristos
125*946379e7Schristos has_context = false;
126*946379e7Schristos for (j = 0; j < mlp->nitems; j++)
127*946379e7Schristos if (mlp->item[j]->msgctxt != NULL)
128*946379e7Schristos has_context = true;
129*946379e7Schristos if (has_context)
130*946379e7Schristos {
131*946379e7Schristos multiline_error (xstrdup (""),
132*946379e7Schristos xstrdup (_("\
133*946379e7Schristos message catalog has context dependent translations\n\
134*946379e7Schristos but the C# .resources format doesn't support contexts\n")));
135*946379e7Schristos return 1;
136*946379e7Schristos }
137*946379e7Schristos }
138*946379e7Schristos
139*946379e7Schristos /* Determine whether mlp has plural entries. */
140*946379e7Schristos {
141*946379e7Schristos bool has_plural;
142*946379e7Schristos size_t j;
143*946379e7Schristos
144*946379e7Schristos has_plural = false;
145*946379e7Schristos for (j = 0; j < mlp->nitems; j++)
146*946379e7Schristos if (mlp->item[j]->msgid_plural != NULL)
147*946379e7Schristos has_plural = true;
148*946379e7Schristos if (has_plural)
149*946379e7Schristos {
150*946379e7Schristos multiline_error (xstrdup (""),
151*946379e7Schristos xstrdup (_("\
152*946379e7Schristos message catalog has plural form translations\n\
153*946379e7Schristos but the C# .resources format doesn't support plural handling\n")));
154*946379e7Schristos return 1;
155*946379e7Schristos }
156*946379e7Schristos }
157*946379e7Schristos
158*946379e7Schristos /* Convert the messages to Unicode. */
159*946379e7Schristos iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
160*946379e7Schristos
161*946379e7Schristos /* Execute the WriteResource program. */
162*946379e7Schristos {
163*946379e7Schristos const char *args[2];
164*946379e7Schristos const char *gettextexedir;
165*946379e7Schristos char *assembly_path;
166*946379e7Schristos struct locals locals;
167*946379e7Schristos
168*946379e7Schristos /* Prepare arguments. */
169*946379e7Schristos args[0] = file_name;
170*946379e7Schristos args[1] = NULL;
171*946379e7Schristos
172*946379e7Schristos /* Make it possible to override the .exe location. This is
173*946379e7Schristos necessary for running the testsuite before "make install". */
174*946379e7Schristos gettextexedir = getenv ("GETTEXTCSHARPEXEDIR");
175*946379e7Schristos if (gettextexedir == NULL || gettextexedir[0] == '\0')
176*946379e7Schristos gettextexedir = relocate (LIBDIR "/gettext");
177*946379e7Schristos
178*946379e7Schristos assembly_path =
179*946379e7Schristos concatenated_pathname (gettextexedir, "msgfmt.net", ".exe");
180*946379e7Schristos
181*946379e7Schristos locals.mlp = mlp;
182*946379e7Schristos
183*946379e7Schristos if (execute_csharp_program (assembly_path, NULL, 0,
184*946379e7Schristos args,
185*946379e7Schristos verbose, false,
186*946379e7Schristos execute_writing_input, &locals))
187*946379e7Schristos /* An error message should already have been provided. */
188*946379e7Schristos exit (EXIT_FAILURE);
189*946379e7Schristos
190*946379e7Schristos free (assembly_path);
191*946379e7Schristos }
192*946379e7Schristos }
193*946379e7Schristos
194*946379e7Schristos return 0;
195*946379e7Schristos }
196