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