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