xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/src/read-java.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Reading Java ResourceBundles.
2*946379e7Schristos    Copyright (C) 2001-2003, 2006 Free Software Foundation, Inc.
3*946379e7Schristos    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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-java.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 "javaexec.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 "error.h"
39*946379e7Schristos #include "exit.h"
40*946379e7Schristos #include "gettext.h"
41*946379e7Schristos 
42*946379e7Schristos #define _(str) gettext (str)
43*946379e7Schristos 
44*946379e7Schristos 
45*946379e7Schristos /* A Java resource name can only be manipulated by a Java virtual machine.
46*946379e7Schristos    So we start a JVM to execute the DumpResource program, and read its
47*946379e7Schristos    output, which is .po format without comments.  */
48*946379e7Schristos 
49*946379e7Schristos struct locals
50*946379e7Schristos {
51*946379e7Schristos   /* OUT */
52*946379e7Schristos   msgdomain_list_ty *mdlp;
53*946379e7Schristos };
54*946379e7Schristos 
55*946379e7Schristos static bool
execute_and_read_po_output(const char * progname,const char * prog_path,char ** prog_argv,void * private_data)56*946379e7Schristos execute_and_read_po_output (const char *progname,
57*946379e7Schristos 			    const char *prog_path, char **prog_argv,
58*946379e7Schristos 			    void *private_data)
59*946379e7Schristos {
60*946379e7Schristos   struct locals *l = (struct locals *) private_data;
61*946379e7Schristos   pid_t child;
62*946379e7Schristos   int fd[1];
63*946379e7Schristos   FILE *fp;
64*946379e7Schristos   int exitstatus;
65*946379e7Schristos 
66*946379e7Schristos   /* Open a pipe to the JVM.  */
67*946379e7Schristos   child = create_pipe_in (progname, prog_path, prog_argv, DEV_NULL, false,
68*946379e7Schristos 			  true, true, fd);
69*946379e7Schristos 
70*946379e7Schristos   fp = fdopen (fd[0], "r");
71*946379e7Schristos   if (fp == NULL)
72*946379e7Schristos     error (EXIT_FAILURE, errno, _("fdopen() failed"));
73*946379e7Schristos 
74*946379e7Schristos   /* Read the message list.  */
75*946379e7Schristos   l->mdlp = read_catalog_stream (fp, "(pipe)", "(pipe)", &input_format_po);
76*946379e7Schristos 
77*946379e7Schristos   fclose (fp);
78*946379e7Schristos 
79*946379e7Schristos   /* Remove zombie process from process list, and retrieve exit status.  */
80*946379e7Schristos   exitstatus = wait_subprocess (child, progname, false, false, true, true);
81*946379e7Schristos   if (exitstatus != 0)
82*946379e7Schristos     error (EXIT_FAILURE, 0, _("%s subprocess failed with exit code %d"),
83*946379e7Schristos 	   progname, exitstatus);
84*946379e7Schristos 
85*946379e7Schristos   return false;
86*946379e7Schristos }
87*946379e7Schristos 
88*946379e7Schristos 
89*946379e7Schristos msgdomain_list_ty *
msgdomain_read_java(const char * resource_name,const char * locale_name)90*946379e7Schristos msgdomain_read_java (const char *resource_name, const char *locale_name)
91*946379e7Schristos {
92*946379e7Schristos   const char *class_name = "gnu.gettext.DumpResource";
93*946379e7Schristos   const char *gettextjexedir;
94*946379e7Schristos   const char *gettextjar;
95*946379e7Schristos   const char *args[3];
96*946379e7Schristos   struct locals locals;
97*946379e7Schristos 
98*946379e7Schristos #if USEJEXE
99*946379e7Schristos   /* Make it possible to override the executable's location.  This is
100*946379e7Schristos      necessary for running the testsuite before "make install".  */
101*946379e7Schristos   gettextjexedir = getenv ("GETTEXTJEXEDIR");
102*946379e7Schristos   if (gettextjexedir == NULL || gettextjexedir[0] == '\0')
103*946379e7Schristos     gettextjexedir = relocate (GETTEXTJEXEDIR);
104*946379e7Schristos #else
105*946379e7Schristos   gettextjexedir = NULL;
106*946379e7Schristos #endif
107*946379e7Schristos 
108*946379e7Schristos   /* Make it possible to override the gettext.jar location.  This is
109*946379e7Schristos      necessary for running the testsuite before "make install".  */
110*946379e7Schristos   gettextjar = getenv ("GETTEXTJAR");
111*946379e7Schristos   if (gettextjar == NULL || gettextjar[0] == '\0')
112*946379e7Schristos     gettextjar = relocate (GETTEXTJAR);
113*946379e7Schristos 
114*946379e7Schristos   /* Assign a default value to the resource name.  */
115*946379e7Schristos   if (resource_name == NULL)
116*946379e7Schristos     resource_name = "Messages";
117*946379e7Schristos 
118*946379e7Schristos   /* Prepare arguments.  */
119*946379e7Schristos   args[0] = resource_name;
120*946379e7Schristos   if (locale_name != NULL)
121*946379e7Schristos     {
122*946379e7Schristos       args[1] = locale_name;
123*946379e7Schristos       args[2] = NULL;
124*946379e7Schristos     }
125*946379e7Schristos   else
126*946379e7Schristos     args[1] = NULL;
127*946379e7Schristos 
128*946379e7Schristos   /* Dump the resource and retrieve the resulting output.
129*946379e7Schristos      Here we use the user's CLASSPATH, not a minimal one, so that the
130*946379e7Schristos      resource can be found.  */
131*946379e7Schristos   if (execute_java_class (class_name, &gettextjar, 1, false, gettextjexedir,
132*946379e7Schristos 			  args,
133*946379e7Schristos 			  verbose, false,
134*946379e7Schristos 			  execute_and_read_po_output, &locals))
135*946379e7Schristos     /* An error message should already have been provided.  */
136*946379e7Schristos     exit (EXIT_FAILURE);
137*946379e7Schristos 
138*946379e7Schristos   return locals.mdlp;
139*946379e7Schristos }
140