summaryrefslogtreecommitdiff
path: root/gettext-tools/src/write-catalog.c
blob: 1b6959d8fa68afbe4b429ddb18aac9bbf0f20ae8 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* GNU gettext - internationalization aids
   Copyright (C) 1995-1998, 2000-2008, 2012, 2015-2016 Free Software
   Foundation, Inc.

   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

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

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#ifndef STDOUT_FILENO
# define STDOUT_FILENO 1
#endif

#include "ostream.h"
#include "file-ostream.h"
#include "fwriteerror.h"
#include "error-progname.h"
#include "xvasprintf.h"
#include "po-xerror.h"
#include "gettext.h"

/* Our regular abbreviation.  */
#define _(str) gettext (str)

/* When compiled in src, enable color support.
   When compiled in libgettextpo, don't enable color support.  */
#ifdef GETTEXTDATADIR

# define ENABLE_COLOR 1

# include "styled-ostream.h"
# include "term-styled-ostream.h"
# include "html-styled-ostream.h"
# include "fd-ostream.h"

# include "color.h"
# include "po-charset.h"
# include "msgl-iconv.h"

#endif


/* =========== Some parameters for use by 'msgdomain_list_print'. ========== */


/* This variable controls the page width when printing messages.
   Defaults to PAGE_WIDTH if not set.  Zero (0) given to message_page_-
   width_set will result in no wrapping being performed.  */
static size_t page_width = PAGE_WIDTH;

void
message_page_width_set (size_t n)
{
  if (n == 0)
    {
      page_width = INT_MAX;
      return;
    }

  if (n < 20)
    n = 20;

  page_width = n;
}


/* ======================== msgdomain_list_print() ======================== */


void
msgdomain_list_print (msgdomain_list_ty *mdlp, const char *filename,
                      catalog_output_format_ty output_syntax,
                      bool force, bool debug)
{
  bool to_stdout;

  /* We will not write anything if, for every domain, we have no message
     or only the header entry.  */
  if (!force)
    {
      bool found_nonempty = false;
      size_t k;

      for (k = 0; k < mdlp->nitems; k++)
        {
          message_list_ty *mlp = mdlp->item[k]->messages;

          if (!(mlp->nitems == 0
                || (mlp->nitems == 1 && is_header (mlp->item[0]))))
            {
              found_nonempty = true;
              break;
            }
        }

      if (!found_nonempty)
        return;
    }

  /* Check whether the output format can accommodate all messages.  */
  if (!output_syntax->supports_multiple_domains && mdlp->nitems > 1)
    {
      if (output_syntax->alternative_is_po)
        po_xerror (PO_SEVERITY_FATAL_ERROR, NULL, NULL, 0, 0, false, _("\
Cannot output multiple translation domains into a single file with the specified output format. Try using PO file syntax instead."));
      else
        po_xerror (PO_SEVERITY_FATAL_ERROR, NULL, NULL, 0, 0, false, _("\
Cannot output multiple translation domains into a single file with the specified output format."));
    }
  else
    {
      if (!output_syntax->supports_contexts)
        {
          const lex_pos_ty *has_context;
          size_t k;

          has_context = NULL;
          for (k = 0; k < mdlp->nitems; k++)
            {
              message_list_ty *mlp = mdlp->item[k]->messages;
              size_t j;

              for (j = 0; j < mlp->nitems; j++)
                {
                  message_ty *mp = mlp->item[j];

                  if (mp->msgctxt != NULL)
                    {
                      has_context = &mp->pos;
                      break;
                    }
                }
            }

          if (has_context != NULL)
            {
              error_with_progname = false;
              po_xerror (PO_SEVERITY_FATAL_ERROR, NULL,
                         has_context->file_name, has_context->line_number,
                         (size_t)(-1), false, _("\
message catalog has context dependent translations, but the output format does not support them."));
              error_with_progname = true;
            }
        }

      if (!output_syntax->supports_plurals)
        {
          const lex_pos_ty *has_plural;
          size_t k;

          has_plural = NULL;
          for (k = 0; k < mdlp->nitems; k++)
            {
              message_list_ty *mlp = mdlp->item[k]->messages;
              size_t j;

              for (j = 0; j < mlp->nitems; j++)
                {
                  message_ty *mp = mlp->item[j];

                  if (mp->msgid_plural != NULL)
                    {
                      has_plural = &mp->pos;
                      break;
                    }
                }
            }

          if (has_plural != NULL)
            {
              error_with_progname = false;
              if (output_syntax->alternative_is_java_class)
                po_xerror (PO_SEVERITY_FATAL_ERROR, NULL,
                           has_plural->file_name, has_plural->line_number,
                           (size_t)(-1), false, _("\
message catalog has plural form translations, but the output format does not support them. Try generating a Java class using \"msgfmt --java\", instead of a properties file."));
              else
                po_xerror (PO_SEVERITY_FATAL_ERROR, NULL,
                           has_plural->file_name, has_plural->line_number,
                           (size_t)(-1), false, _("\
message catalog has plural form translations, but the output format does not support them."));
              error_with_progname = true;
            }
        }
    }

  to_stdout = (filename == NULL || strcmp (filename, "-") == 0
               || strcmp (filename, "/dev/stdout") == 0);

#if ENABLE_COLOR
  if (output_syntax->supports_color
      && (color_mode == color_yes
          || (color_mode == color_tty && to_stdout && isatty (STDOUT_FILENO))))
    {
      int fd;
      ostream_t stream;

      /* Open the output file.  */
      if (!to_stdout)
        {
          fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC,
                     /* 0666 in portable POSIX notation: */
                     S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
          if (fd < 0)
            {
              const char *errno_description = strerror (errno);
              po_xerror (PO_SEVERITY_FATAL_ERROR, NULL, NULL, 0, 0, false,
                         xasprintf ("%s: %s",
                                    xasprintf (_("cannot create output file \"%s\""),
                                               filename),
                                    errno_description));
            }
        }
      else
        {
          fd = STDOUT_FILENO;
          filename = _("standard output");
        }

      style_file_prepare ();
      stream = term_styled_ostream_create (fd, filename, style_file_name);
      if (stream == NULL)
        stream = fd_ostream_create (fd, filename, true);
      output_syntax->print (mdlp, stream, page_width, debug);
      ostream_free (stream);

      /* Make sure nothing went wrong.  */
      if (close (fd) < 0)
        {
          const char *errno_description = strerror (errno);
          po_xerror (PO_SEVERITY_FATAL_ERROR, NULL, NULL, 0, 0, false,
                     xasprintf ("%s: %s",
                                xasprintf (_("error while writing \"%s\" file"),
                                           filename),
                                errno_description));
        }
    }
  else
#endif
    {
      FILE *fp;
      file_ostream_t stream;

      /* Open the output file.  */
      if (!to_stdout)
        {
          fp = fopen (filename, "wb");
          if (fp == NULL)
            {
              const char *errno_description = strerror (errno);
              po_xerror (PO_SEVERITY_FATAL_ERROR, NULL, NULL, 0, 0, false,
                         xasprintf ("%s: %s",
                                    xasprintf (_("cannot create output file \"%s\""),
                                               filename),
                                    errno_description));
            }
        }
      else
        {
          fp = stdout;
          filename = _("standard output");
        }

      stream = file_ostream_create (fp);

#if ENABLE_COLOR
      if (output_syntax->supports_color && color_mode == color_html)
        {
          html_styled_ostream_t html_stream;

          /* Convert mdlp to UTF-8 encoding.  */
          if (mdlp->encoding != po_charset_utf8)
            {
              mdlp = msgdomain_list_copy (mdlp, 0);
              mdlp = iconv_msgdomain_list (mdlp, po_charset_utf8, false, NULL);
            }

          style_file_prepare ();
          html_stream = html_styled_ostream_create (stream, style_file_name);
          output_syntax->print (mdlp, html_stream, page_width, debug);
          ostream_free (html_stream);
        }
      else
#endif
        {
          output_syntax->print (mdlp, stream, page_width, debug);
        }

      ostream_free (stream);

      /* Make sure nothing went wrong.  */
      if (fwriteerror (fp))
        {
          const char *errno_description = strerror (errno);
          po_xerror (PO_SEVERITY_FATAL_ERROR, NULL, NULL, 0, 0, false,
                     xasprintf ("%s: %s",
                                xasprintf (_("error while writing \"%s\" file"),
                                           filename),
                                errno_description));
        }
    }
}


/* =============================== Sorting. ================================ */


static int
cmp_by_msgid (const void *va, const void *vb)
{
  const message_ty *a = *(const message_ty **) va;
  const message_ty *b = *(const message_ty **) vb;

  /* Because msgids normally contain only ASCII characters or are UTF-8
     encoded, it is OK to sort them as if we were in a C.UTF-8 locale. And
     strcoll() in a C.UTF-8 locale is the same as strcmp().  */
  int cmp = strcmp (a->msgid, b->msgid);
  if (cmp != 0)
    return cmp;

  /* If the msgids are equal, disambiguate by comparing the contexts.  */
  if (a->msgctxt == b->msgctxt)
    return 0;
  if (a->msgctxt == NULL)
    return -1;
  if (b->msgctxt == NULL)
    return 1;
  return strcmp (a->msgctxt, b->msgctxt);
}


void
msgdomain_list_sort_by_msgid (msgdomain_list_ty *mdlp)
{
  size_t k;

  for (k = 0; k < mdlp->nitems; k++)
    {
      message_list_ty *mlp = mdlp->item[k]->messages;

      if (mlp->nitems > 0)
        qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), cmp_by_msgid);
    }
}


/* Sort the file positions of every message.  */

static int
cmp_filepos (const void *va, const void *vb)
{
  const lex_pos_ty *a = (const lex_pos_ty *) va;
  const lex_pos_ty *b = (const lex_pos_ty *) vb;
  int cmp;

  cmp = strcmp (a->file_name, b->file_name);
  if (cmp == 0)
    cmp = (int) a->line_number - (int) b->line_number;

  return cmp;
}

static void
msgdomain_list_sort_filepos (msgdomain_list_ty *mdlp)
{
  size_t j, k;

  for (k = 0; k < mdlp->nitems; k++)
    {
      message_list_ty *mlp = mdlp->item[k]->messages;

      for (j = 0; j < mlp->nitems; j++)
        {
          message_ty *mp = mlp->item[j];

          if (mp->filepos_count > 0)
            qsort (mp->filepos, mp->filepos_count, sizeof (mp->filepos[0]),
                   cmp_filepos);
        }
    }
}


/* Sort the messages according to the file position.  */

static int
cmp_by_filepos (const void *va, const void *vb)
{
  const message_ty *a = *(const message_ty **) va;
  const message_ty *b = *(const message_ty **) vb;
  int cmp;

  /* No filepos is smaller than any other filepos.  */
  if (a->filepos_count == 0)
    {
      if (b->filepos_count != 0)
        return -1;
    }
  if (b->filepos_count == 0)
    return 1;

  /* Compare on the file names...  */
  cmp = strcmp (a->filepos[0].file_name, b->filepos[0].file_name);
  if (cmp != 0)
    return cmp;

  /* If they are equal, compare on the line numbers...  */
  cmp = a->filepos[0].line_number - b->filepos[0].line_number;
  if (cmp != 0)
    return cmp;

  /* If they are equal, compare on the msgid strings.  */
  /* Because msgids normally contain only ASCII characters or are UTF-8
     encoded, it is OK to sort them as if we were in a C.UTF-8 locale. And
     strcoll() in a C.UTF-8 locale is the same as strcmp().  */
  cmp = strcmp (a->msgid, b->msgid);
  if (cmp != 0)
    return cmp;

  /* If the msgids are equal, disambiguate by comparing the contexts.  */
  if (a->msgctxt == b->msgctxt)
    return 0;
  if (a->msgctxt == NULL)
    return -1;
  if (b->msgctxt == NULL)
    return 1;
  return strcmp (a->msgctxt, b->msgctxt);
}


void
msgdomain_list_sort_by_filepos (msgdomain_list_ty *mdlp)
{
  size_t k;

  /* It makes sense to compare filepos[0] of different messages only after
     the filepos[] array of each message has been sorted.  Sort it now.  */
  msgdomain_list_sort_filepos (mdlp);

  for (k = 0; k < mdlp->nitems; k++)
    {
      message_list_ty *mlp = mdlp->item[k]->messages;

      if (mlp->nitems > 0)
        qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), cmp_by_filepos);
    }
}