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