1b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o/* Log file output.
2b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   Copyright (C) 2003 Free Software Foundation, Inc.
3b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
4b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   This program is free software; you can redistribute it and/or modify it
5b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   under the terms of the GNU Library General Public License as published
6b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   by the Free Software Foundation; either version 2, or (at your option)
7b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   any later version.
8b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
9b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   This program is distributed in the hope that it will be useful,
10b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   but WITHOUT ANY WARRANTY; without even the implied warranty of
11b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   Library General Public License for more details.
13b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
14b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   You should have received a copy of the GNU Library General Public
15b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   License along with this program; if not, write to the Free Software
16b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o   USA.  */
18b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
19b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o/* Written by Bruno Haible <bruno@clisp.org>.  */
20b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
21b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o#ifdef HAVE_CONFIG_H
22b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o# include <config.h>
23b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o#endif
24b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
25b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o#include <stdio.h>
26b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o#include <stdlib.h>
27b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o#include <string.h>
28b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
29b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o/* Print an ASCII string with quotes and escape sequences where needed.  */
30b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'ostatic void
31b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'oprint_escaped (FILE *stream, const char *str)
32b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o{
33b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  putc ('"', stream);
34b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  for (; *str != '\0'; str++)
35b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o    if (*str == '\n')
36b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      {
37b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	fputs ("\\n\"", stream);
38b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	if (str[1] == '\0')
39b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	  return;
40b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	fputs ("\n\"", stream);
41b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      }
42b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o    else
43b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      {
44b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	if (*str == '"' || *str == '\\')
45b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	  putc ('\\', stream);
46b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	putc (*str, stream);
47b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      }
48b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  putc ('"', stream);
49b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o}
50b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
51b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o/* Add to the log file an entry denoting a failed translation.  */
52b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'ovoid
53b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o_nl_log_untranslated (const char *logfilename, const char *domainname,
54b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o		      const char *msgid1, const char *msgid2, int plural)
55b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o{
56b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  static char *last_logfilename = NULL;
57b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  static FILE *last_logfile = NULL;
58b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  FILE *logfile;
59b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
60b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  /* Can we reuse the last opened logfile?  */
61b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  if (last_logfilename == NULL || strcmp (logfilename, last_logfilename) != 0)
62b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o    {
63b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      /* Close the last used logfile.  */
64b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      if (last_logfilename != NULL)
65b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	{
66b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	  if (last_logfile != NULL)
67b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	    {
68b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	      fclose (last_logfile);
69b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	      last_logfile = NULL;
70b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	    }
71b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	  free (last_logfilename);
72b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	  last_logfilename = NULL;
73b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	}
74b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      /* Open the logfile.  */
75b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      last_logfilename = (char *) malloc (strlen (logfilename) + 1);
76b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      if (last_logfilename == NULL)
77b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	return;
78b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      strcpy (last_logfilename, logfilename);
79b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      last_logfile = fopen (logfilename, "a");
80b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      if (last_logfile == NULL)
81b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o	return;
82b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o    }
83b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  logfile = last_logfile;
84b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o
85b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  fprintf (logfile, "domain ");
86b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  print_escaped (logfile, domainname);
87b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  fprintf (logfile, "\nmsgid ");
88b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  print_escaped (logfile, msgid1);
89b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  if (plural)
90b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o    {
91b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      fprintf (logfile, "\nmsgid_plural ");
92b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      print_escaped (logfile, msgid2);
93b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o      fprintf (logfile, "\nmsgstr[0] \"\"\n");
94b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o    }
95b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  else
96b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o    fprintf (logfile, "\nmsgstr \"\"\n");
97b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o  putc ('\n', logfile);
98b0cacab066000b940551d59aad3e4553d4bad268Theodore Ts'o}
99