No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / src / write-tcl.c
blob9af659c14acb530e93687ac6952ca2978089b0c9
1 /* Writing tcl/msgcat .msg files.
2 Copyright (C) 2002-2003, 2005 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2002.
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 2, or (at your option)
8 any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <alloca.h>
24 /* Specification. */
25 #include "write-tcl.h"
27 #include <errno.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "error.h"
33 #include "xerror.h"
34 #include "message.h"
35 #include "msgl-iconv.h"
36 #include "po-charset.h"
37 #include "xalloc.h"
38 #include "xallocsa.h"
39 #include "pathname.h"
40 #include "fwriteerror.h"
41 #include "exit.h"
42 #include "utf8-ucs4.h"
43 #include "gettext.h"
45 #define _(str) gettext (str)
48 /* Write a string in Tcl Unicode notation to the given stream.
49 Tcl 8 uses Unicode for its internal string representation.
50 In tcl-8.3.3, the .msg files are read in using the locale dependent
51 encoding. The only way to specify strings in an encoding independent
52 form is the \unnnn notation. Newer tcl versions have this fixed:
53 they read the .msg files in UTF-8 encoding. */
54 static void
55 write_tcl_string (FILE *stream, const char *str)
57 static const char hexdigit[] = "0123456789abcdef";
58 const char *str_limit = str + strlen (str);
60 fprintf (stream, "\"");
61 while (str < str_limit)
63 unsigned int uc;
64 unsigned int count;
65 count = u8_mbtouc (&uc, (const unsigned char *) str, str_limit - str);
66 if (uc < 0x10000)
68 /* Single UCS-2 'char'. */
69 if (uc == 0x000a)
70 fprintf (stream, "\\n");
71 else if (uc == 0x000d)
72 fprintf (stream, "\\r");
73 else if (uc == 0x0022)
74 fprintf (stream, "\\\"");
75 else if (uc == 0x0024)
76 fprintf (stream, "\\$");
77 else if (uc == 0x005b)
78 fprintf (stream, "\\[");
79 else if (uc == 0x005c)
80 fprintf (stream, "\\\\");
81 else if (uc == 0x005d)
82 fprintf (stream, "\\]");
83 /* No need to escape '{' and '}' because we don't have opening
84 braces outside the strings. */
85 #if 0
86 else if (uc == 0x007b)
87 fprintf (stream, "\\{");
88 else if (uc == 0x007d)
89 fprintf (stream, "\\}");
90 #endif
91 else if (uc >= 0x0020 && uc < 0x007f)
92 fprintf (stream, "%c", uc);
93 else
94 fprintf (stream, "\\u%c%c%c%c",
95 hexdigit[(uc >> 12) & 0x0f], hexdigit[(uc >> 8) & 0x0f],
96 hexdigit[(uc >> 4) & 0x0f], hexdigit[uc & 0x0f]);
98 else
99 /* The \unnnn notation doesn't support characters >= 0x10000.
100 We output them as UTF-8 byte sequences and hope that either
101 the Tcl version reading them will be new enough or that the
102 user is using an UTF-8 locale. */
103 fwrite (str, 1, count, stream);
104 str += count;
106 fprintf (stream, "\"");
110 static void
111 write_msg (FILE *output_file, message_list_ty *mlp, const char *locale_name)
113 size_t j;
115 /* We don't care about esthetic formattic of the output (like respecting
116 a maximum line width, or including the translator comments) because
117 the \unnnn notation is unesthetic anyway. Translators shall edit
118 the PO file. */
119 for (j = 0; j < mlp->nitems; j++)
121 message_ty *mp = mlp->item[j];
123 if (mp->msgid[0] == '\0')
124 /* Tcl's msgcat unit ignores this, but msgunfmt needs it. */
125 fprintf (output_file, "set ::msgcat::header ");
126 else
128 fprintf (output_file, "::msgcat::mcset %s ", locale_name);
129 write_tcl_string (output_file, mp->msgid);
130 fprintf (output_file, " ");
132 write_tcl_string (output_file, mp->msgstr);
133 fprintf (output_file, "\n");
138 msgdomain_write_tcl (message_list_ty *mlp, const char *canon_encoding,
139 const char *locale_name,
140 const char *directory)
142 /* If no entry for this domain don't even create the file. */
143 if (mlp->nitems == 0)
144 return 0;
146 /* Determine whether mlp has plural entries. */
148 bool has_plural;
149 size_t j;
151 has_plural = false;
152 for (j = 0; j < mlp->nitems; j++)
153 if (mlp->item[j]->msgid_plural != NULL)
154 has_plural = true;
155 if (has_plural)
157 multiline_error (xstrdup (""),
158 xstrdup (_("\
159 message catalog has plural form translations\n\
160 but the Tcl message catalog format doesn't support plural handling\n")));
161 return 1;
165 /* Convert the messages to Unicode. */
166 iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
168 /* Now create the file. */
170 size_t len;
171 char *frobbed_locale_name;
172 char *p;
173 char *file_name;
174 FILE *output_file;
176 /* Convert the locale name to lowercase and remove any encoding. */
177 len = strlen (locale_name);
178 frobbed_locale_name = (char *) xallocsa (len + 1);
179 memcpy (frobbed_locale_name, locale_name, len + 1);
180 for (p = frobbed_locale_name; *p != '\0'; p++)
181 if (*p >= 'A' && *p <= 'Z')
182 *p = *p - 'A' + 'a';
183 else if (*p == '.')
185 *p = '\0';
186 break;
189 file_name = concatenated_pathname (directory, frobbed_locale_name, ".msg");
191 output_file = fopen (file_name, "w");
192 if (output_file == NULL)
194 error (0, errno, _("error while opening \"%s\" for writing"),
195 file_name);
196 freesa (frobbed_locale_name);
197 return 1;
200 write_msg (output_file, mlp, frobbed_locale_name);
202 /* Make sure nothing went wrong. */
203 if (fwriteerror (output_file))
204 error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
205 file_name);
207 freesa (frobbed_locale_name);
210 return 0;