1/* Reading C# .resources files. 2 Copyright (C) 2003, 2006-2007 Free Software Foundation, Inc. 3 Written by Bruno Haible <bruno@clisp.org>, 2003. 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-resources.h" 24 25#include <errno.h> 26#include <stdbool.h> 27#include <stdlib.h> 28#include <stdio.h> 29 30#include "msgunfmt.h" 31#include "relocatable.h" 32#include "csharpexec.h" 33#include "pipe.h" 34#include "wait-process.h" 35#include "read-catalog.h" 36#include "read-po.h" 37#include "message.h" 38#include "filename.h" 39#include "error.h" 40#include "gettext.h" 41 42#define _(str) gettext (str) 43 44 45/* A .resources file has such a complex format that it's most easily read 46 through the C# class ResourceReader. So we start a C# process to execute 47 the DumpResource program, and read its output, which is .po format without 48 comments. */ 49 50struct locals 51{ 52 /* OUT */ 53 msgdomain_list_ty *mdlp; 54}; 55 56static bool 57execute_and_read_po_output (const char *progname, 58 const char *prog_path, char **prog_argv, 59 void *private_data) 60{ 61 struct locals *l = (struct locals *) private_data; 62 pid_t child; 63 int fd[1]; 64 FILE *fp; 65 int exitstatus; 66 67 /* Open a pipe to the C# execution engine. */ 68 child = create_pipe_in (progname, prog_path, prog_argv, NULL, false, 69 true, true, fd); 70 71 fp = fdopen (fd[0], "r"); 72 if (fp == NULL) 73 error (EXIT_FAILURE, errno, _("fdopen() failed")); 74 75 /* Read the message list. */ 76 l->mdlp = read_catalog_stream (fp, "(pipe)", "(pipe)", &input_format_po); 77 78 fclose (fp); 79 80 /* Remove zombie process from process list, and retrieve exit status. */ 81 exitstatus = wait_subprocess (child, progname, false, false, true, true); 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 90void 91read_resources_file (message_list_ty *mlp, const char *filename) 92{ 93 const char *args[2]; 94 const char *gettextexedir; 95 const char *gettextlibdir; 96 char *assembly_path; 97 const char *libdirs[1]; 98 struct locals locals; 99 100 /* Prepare arguments. */ 101 args[0] = filename; 102 args[1] = NULL; 103 104 /* Make it possible to override the .exe location. This is 105 necessary for running the testsuite before "make install". */ 106 gettextexedir = getenv ("GETTEXTCSHARPEXEDIR"); 107 if (gettextexedir == NULL || gettextexedir[0] == '\0') 108 gettextexedir = relocate (LIBDIR "/gettext"); 109 110 /* Make it possible to override the .dll location. This is 111 necessary for running the testsuite before "make install". */ 112 gettextlibdir = getenv ("GETTEXTCSHARPLIBDIR"); 113 if (gettextlibdir == NULL || gettextlibdir[0] == '\0') 114 gettextlibdir = relocate (LIBDIR); 115 116 /* Dump the resource and retrieve the resulting output. */ 117 assembly_path = concatenated_filename (gettextexedir, "msgunfmt.net", ".exe"); 118 libdirs[0] = gettextlibdir; 119 if (execute_csharp_program (assembly_path, libdirs, 1, 120 args, 121 verbose, false, 122 execute_and_read_po_output, &locals)) 123 /* An error message should already have been provided. */ 124 exit (EXIT_FAILURE); 125 126 /* Add the output to mlp. */ 127 { 128 message_list_ty *read_mlp = locals.mdlp->item[0]->messages; 129 size_t j; 130 131 for (j = 0; j < read_mlp->nitems; j++) 132 message_list_append (mlp, read_mlp->item[j]); 133 } 134 135 free (assembly_path); 136} 137