xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/src/read-csharp.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Reading C# satellite assemblies.
2*946379e7Schristos    Copyright (C) 2003-2004, 2006 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 "read-csharp.h"
25*946379e7Schristos 
26*946379e7Schristos #include <stdbool.h>
27*946379e7Schristos #include <stdio.h>
28*946379e7Schristos #include <stdlib.h>
29*946379e7Schristos #include <errno.h>
30*946379e7Schristos 
31*946379e7Schristos #include "msgunfmt.h"
32*946379e7Schristos #include "relocatable.h"
33*946379e7Schristos #include "csharpexec.h"
34*946379e7Schristos #include "pipe.h"
35*946379e7Schristos #include "wait-process.h"
36*946379e7Schristos #include "read-catalog.h"
37*946379e7Schristos #include "read-po.h"
38*946379e7Schristos #include "xalloc.h"
39*946379e7Schristos #include "pathname.h"
40*946379e7Schristos #include "error.h"
41*946379e7Schristos #include "exit.h"
42*946379e7Schristos #include "gettext.h"
43*946379e7Schristos 
44*946379e7Schristos #define _(str) gettext (str)
45*946379e7Schristos 
46*946379e7Schristos 
47*946379e7Schristos /* A C# satellite assembly can only be manipulated by a C# execution engine.
48*946379e7Schristos    So we start a C# process to execute the DumpResource program, and read its
49*946379e7Schristos    output, which is .po format without comments.  */
50*946379e7Schristos 
51*946379e7Schristos struct locals
52*946379e7Schristos {
53*946379e7Schristos   /* OUT */
54*946379e7Schristos   msgdomain_list_ty *mdlp;
55*946379e7Schristos };
56*946379e7Schristos 
57*946379e7Schristos static bool
execute_and_read_po_output(const char * progname,const char * prog_path,char ** prog_argv,void * private_data)58*946379e7Schristos execute_and_read_po_output (const char *progname,
59*946379e7Schristos 			    const char *prog_path, char **prog_argv,
60*946379e7Schristos 			    void *private_data)
61*946379e7Schristos {
62*946379e7Schristos   struct locals *l = (struct locals *) private_data;
63*946379e7Schristos   pid_t child;
64*946379e7Schristos   int fd[1];
65*946379e7Schristos   FILE *fp;
66*946379e7Schristos   int exitstatus;
67*946379e7Schristos 
68*946379e7Schristos   /* Open a pipe to the C# execution engine.  */
69*946379e7Schristos   child = create_pipe_in (progname, prog_path, prog_argv, DEV_NULL, false,
70*946379e7Schristos 			  true, true, fd);
71*946379e7Schristos 
72*946379e7Schristos   fp = fdopen (fd[0], "r");
73*946379e7Schristos   if (fp == NULL)
74*946379e7Schristos     error (EXIT_FAILURE, errno, _("fdopen() failed"));
75*946379e7Schristos 
76*946379e7Schristos   /* Read the message list.  */
77*946379e7Schristos   l->mdlp = read_catalog_stream (fp, "(pipe)", "(pipe)", &input_format_po);
78*946379e7Schristos 
79*946379e7Schristos   fclose (fp);
80*946379e7Schristos 
81*946379e7Schristos   /* Remove zombie process from process list, and retrieve exit status.  */
82*946379e7Schristos   exitstatus = wait_subprocess (child, progname, false, false, true, true);
83*946379e7Schristos   if (exitstatus != 0)
84*946379e7Schristos     error (EXIT_FAILURE, 0, _("%s subprocess failed with exit code %d"),
85*946379e7Schristos 	   progname, exitstatus);
86*946379e7Schristos 
87*946379e7Schristos   return false;
88*946379e7Schristos }
89*946379e7Schristos 
90*946379e7Schristos 
91*946379e7Schristos msgdomain_list_ty *
msgdomain_read_csharp(const char * resource_name,const char * locale_name,const char * directory)92*946379e7Schristos msgdomain_read_csharp (const char *resource_name, const char *locale_name,
93*946379e7Schristos 		       const char *directory)
94*946379e7Schristos {
95*946379e7Schristos   char *culture_name;
96*946379e7Schristos   const char *args[4];
97*946379e7Schristos   const char *gettextexedir;
98*946379e7Schristos   const char *gettextlibdir;
99*946379e7Schristos   char *assembly_path;
100*946379e7Schristos   const char *libdirs[1];
101*946379e7Schristos   struct locals locals;
102*946379e7Schristos 
103*946379e7Schristos   /* Assign a default value to the resource name.  */
104*946379e7Schristos   if (resource_name == NULL)
105*946379e7Schristos     resource_name = "Messages";
106*946379e7Schristos 
107*946379e7Schristos   /* Convert the locale name to a .NET specific culture name.  */
108*946379e7Schristos   culture_name = xstrdup (locale_name);
109*946379e7Schristos   {
110*946379e7Schristos     char *p;
111*946379e7Schristos     for (p = culture_name; *p != '\0'; p++)
112*946379e7Schristos       if (*p == '_')
113*946379e7Schristos 	*p = '-';
114*946379e7Schristos     if (strncmp (culture_name, "sr-CS", 5) == 0)
115*946379e7Schristos       memcpy (culture_name, "sr-SP", 5);
116*946379e7Schristos     p = strchr (culture_name, '@');
117*946379e7Schristos     if (p != NULL)
118*946379e7Schristos       {
119*946379e7Schristos 	if (strcmp (p, "@latin") == 0)
120*946379e7Schristos 	  strcpy (p, "-Latn");
121*946379e7Schristos 	else if (strcmp (p, "@cyrillic") == 0)
122*946379e7Schristos 	  strcpy (p, "-Cyrl");
123*946379e7Schristos       }
124*946379e7Schristos     if (strcmp (culture_name, "sr-SP") == 0)
125*946379e7Schristos       {
126*946379e7Schristos 	free (culture_name);
127*946379e7Schristos 	culture_name = xstrdup ("sr-SP-Latn");
128*946379e7Schristos       }
129*946379e7Schristos     else if (strcmp (culture_name, "uz-UZ") == 0)
130*946379e7Schristos       {
131*946379e7Schristos 	free (culture_name);
132*946379e7Schristos 	culture_name = xstrdup ("uz-UZ-Latn");
133*946379e7Schristos       }
134*946379e7Schristos   }
135*946379e7Schristos 
136*946379e7Schristos   /* Prepare arguments.  */
137*946379e7Schristos   args[0] = directory;
138*946379e7Schristos   args[1] = resource_name;
139*946379e7Schristos   args[2] = culture_name;
140*946379e7Schristos   args[3] = NULL;
141*946379e7Schristos 
142*946379e7Schristos   /* Make it possible to override the .exe location.  This is
143*946379e7Schristos      necessary for running the testsuite before "make install".  */
144*946379e7Schristos   gettextexedir = getenv ("GETTEXTCSHARPEXEDIR");
145*946379e7Schristos   if (gettextexedir == NULL || gettextexedir[0] == '\0')
146*946379e7Schristos     gettextexedir = relocate (LIBDIR "/gettext");
147*946379e7Schristos 
148*946379e7Schristos   /* Make it possible to override the .dll location.  This is
149*946379e7Schristos      necessary for running the testsuite before "make install".  */
150*946379e7Schristos   gettextlibdir = getenv ("GETTEXTCSHARPLIBDIR");
151*946379e7Schristos   if (gettextlibdir == NULL || gettextlibdir[0] == '\0')
152*946379e7Schristos     gettextlibdir = relocate (LIBDIR);
153*946379e7Schristos 
154*946379e7Schristos   /* Dump the resource and retrieve the resulting output.  */
155*946379e7Schristos   assembly_path = concatenated_pathname (gettextexedir, "msgunfmt.net", ".exe");
156*946379e7Schristos   libdirs[0] = gettextlibdir;
157*946379e7Schristos   if (execute_csharp_program (assembly_path, libdirs, 1,
158*946379e7Schristos 			      args,
159*946379e7Schristos 			      verbose, false,
160*946379e7Schristos 			      execute_and_read_po_output, &locals))
161*946379e7Schristos     /* An error message should already have been provided.  */
162*946379e7Schristos     exit (EXIT_FAILURE);
163*946379e7Schristos 
164*946379e7Schristos   free (assembly_path);
165*946379e7Schristos   free (culture_name);
166*946379e7Schristos 
167*946379e7Schristos   return locals.mdlp;
168*946379e7Schristos }
169