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