summaryrefslogtreecommitdiff
path: root/gettext-tools/src/write-tcl.c
blob: 1439431f71b198e033926519be4c6beee030d430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Writing tcl/msgcat .msg files.
   Copyright (C) 2002-2003, 2005, 2007-2009, 2015 Free Software
   Foundation, Inc.
   Written by Bruno Haible <bruno@clisp.org>, 2002.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <alloca.h>

/* Specification.  */
#include "write-tcl.h"

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "error.h"
#include "xerror.h"
#include "message.h"
#include "msgl-iconv.h"
#include "po-charset.h"
#include "xalloc.h"
#include "xmalloca.h"
#include "concat-filename.h"
#include "fwriteerror.h"
#include "unistr.h"
#include "gettext.h"

#define _(str) gettext (str)


/* Write a string in Tcl Unicode notation to the given stream.
   Tcl 8 uses Unicode for its internal string representation.
   In tcl-8.3.3, the .msg files are read in using the locale dependent
   encoding.  The only way to specify strings in an encoding independent
   form is the \unnnn notation.  Newer tcl versions have this fixed:
   they read the .msg files in UTF-8 encoding.  */
static void
write_tcl_string (FILE *stream, const char *str)
{
  static const char hexdigit[] = "0123456789abcdef";
  const char *str_limit = str + strlen (str);

  fprintf (stream, "\"");
  while (str < str_limit)
    {
      ucs4_t uc;
      unsigned int count;
      count = u8_mbtouc (&uc, (const unsigned char *) str, str_limit - str);
      if (uc < 0x10000)
        {
          /* Single UCS-2 'char'.  */
          if (uc == 0x000a)
            fprintf (stream, "\\n");
          else if (uc == 0x000d)
            fprintf (stream, "\\r");
          else if (uc == 0x0022)
            fprintf (stream, "\\\"");
          else if (uc == 0x0024)
            fprintf (stream, "\\$");
          else if (uc == 0x005b)
            fprintf (stream, "\\[");
          else if (uc == 0x005c)
            fprintf (stream, "\\\\");
          else if (uc == 0x005d)
            fprintf (stream, "\\]");
          /* No need to escape '{' and '}' because we don't have opening
             braces outside the strings.  */
#if 0
          else if (uc == 0x007b)
            fprintf (stream, "\\{");
          else if (uc == 0x007d)
            fprintf (stream, "\\}");
#endif
          else if (uc >= 0x0020 && uc < 0x007f)
            fprintf (stream, "%c", (int) uc);
          else
            fprintf (stream, "\\u%c%c%c%c",
                     hexdigit[(uc >> 12) & 0x0f], hexdigit[(uc >> 8) & 0x0f],
                     hexdigit[(uc >> 4) & 0x0f], hexdigit[uc & 0x0f]);
        }
      else
        /* The \unnnn notation doesn't support characters >= 0x10000.
           We output them as UTF-8 byte sequences and hope that either
           the Tcl version reading them will be new enough or that the
           user is using an UTF-8 locale.  */
        fwrite (str, 1, count, stream);
      str += count;
    }
  fprintf (stream, "\"");
}


static void
write_msg (FILE *output_file, message_list_ty *mlp, const char *locale_name)
{
  size_t j;

  /* We don't care about esthetic formattic of the output (like respecting
     a maximum line width, or including the translator comments) because
     the \unnnn notation is unesthetic anyway.  Translators shall edit
     the PO file.  */
  for (j = 0; j < mlp->nitems; j++)
    {
      message_ty *mp = mlp->item[j];

      if (is_header (mp))
        /* Tcl's msgcat unit ignores this, but msgunfmt needs it.  */
        fprintf (output_file, "set ::msgcat::header ");
      else
        {
          fprintf (output_file, "::msgcat::mcset %s ", locale_name);
          write_tcl_string (output_file, mp->msgid);
          fprintf (output_file, " ");
        }
      write_tcl_string (output_file, mp->msgstr);
      fprintf (output_file, "\n");
    }
}

int
msgdomain_write_tcl (message_list_ty *mlp, const char *canon_encoding,
                     const char *locale_name,
                     const char *directory)
{
  /* If no entry for this domain don't even create the file.  */
  if (mlp->nitems == 0)
    return 0;

  /* Determine whether mlp has entries with context.  */
  {
    bool has_context;
    size_t j;

    has_context = false;
    for (j = 0; j < mlp->nitems; j++)
      if (mlp->item[j]->msgctxt != NULL)
        has_context = true;
    if (has_context)
      {
        multiline_error (xstrdup (""),
                         xstrdup (_("\
message catalog has context dependent translations\n\
but the Tcl message catalog format doesn't support contexts\n")));
        return 1;
      }
  }

  /* Determine whether mlp has plural entries.  */
  {
    bool has_plural;
    size_t j;

    has_plural = false;
    for (j = 0; j < mlp->nitems; j++)
      if (mlp->item[j]->msgid_plural != NULL)
        has_plural = true;
    if (has_plural)
      {
        multiline_error (xstrdup (""),
                         xstrdup (_("\
message catalog has plural form translations\n\
but the Tcl message catalog format doesn't support plural handling\n")));
        return 1;
      }
  }

  /* Convert the messages to Unicode.  */
  iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);

  /* Now create the file.  */
  {
    size_t len;
    char *frobbed_locale_name;
    char *p;
    char *file_name;
    FILE *output_file;

    /* Convert the locale name to lowercase and remove any encoding.  */
    len = strlen (locale_name);
    frobbed_locale_name = (char *) xmalloca (len + 1);
    memcpy (frobbed_locale_name, locale_name, len + 1);
    for (p = frobbed_locale_name; *p != '\0'; p++)
      if (*p >= 'A' && *p <= 'Z')
        *p = *p - 'A' + 'a';
      else if (*p == '.')
        {
          *p = '\0';
          break;
        }

    file_name = xconcatenated_filename (directory, frobbed_locale_name, ".msg");

    output_file = fopen (file_name, "w");
    if (output_file == NULL)
      {
        error (0, errno, _("error while opening \"%s\" for writing"),
               file_name);
        freea (frobbed_locale_name);
        return 1;
      }

    write_msg (output_file, mlp, frobbed_locale_name);

    /* Make sure nothing went wrong.  */
    if (fwriteerror (output_file))
      error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
             file_name);

    freea (frobbed_locale_name);
  }

  return 0;
}