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