summaryrefslogtreecommitdiffstats
path: root/gettext-tools/src/po-xerror.c
blob: 30e50fa39d460a7d01474a1d8710971742d5aa3d (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
/* Error handling during reading and writing of PO files.
   Copyright (C) 2005-2007, 2015-2016 Free Software Foundation, Inc.
   Written by Bruno Haible <bruno@clisp.org>, 2005.

   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 "po-xerror.h"

#include <error.h>
#include <stdio.h>
#include <stdlib.h>

#include "message.h"
#include "progname.h"
#include "error-progname.h"
#include "xalloc.h"
#include "xerror.h"
#include "error.h"
#include "xvasprintf.h"
#include "po-error.h"
#include "gettext.h"

#define _(str) gettext (str)


static void
xerror (int severity, const char *prefix_tail,
        const char *filename, size_t lineno, size_t column,
        int multiline_p, const char *message_text)
{
  if (multiline_p)
    {
      bool old_error_with_progname = error_with_progname;
      char *prefix;

      if (filename != NULL)
        {
          if (lineno != (size_t)(-1))
            {
              if (column != (size_t)(-1))
                prefix =
                  xasprintf ("%s:%ld:%ld: %s", filename,
                             (long) lineno, (long) column, prefix_tail);
              else
                prefix =
                  xasprintf ("%s:%ld: %s", filename,
                             (long) lineno, prefix_tail);
            }
          else
            prefix = xasprintf ("%s: %s", filename, prefix_tail);
          error_with_progname = false;
        }
      else
        prefix = xasprintf ("%s: %s", program_name, prefix_tail);

      if (severity >= PO_SEVERITY_ERROR)
        po_multiline_error (prefix, xstrdup (message_text));
      else
        po_multiline_warning (prefix, xstrdup (message_text));
      error_with_progname = old_error_with_progname;

      if (severity == PO_SEVERITY_FATAL_ERROR)
        exit (EXIT_FAILURE);
    }
  else
    {
      int exit_status =
        (severity == PO_SEVERITY_FATAL_ERROR ? EXIT_FAILURE : 0);

      if (filename != NULL)
        {
          error_with_progname = false;
          if (lineno != (size_t)(-1))
            {
              if (column != (size_t)(-1))
                po_error (exit_status, 0, "%s:%ld:%ld: %s%s",
                          filename, (long) lineno, (long) column,
                          prefix_tail, message_text);
              else
                po_error_at_line (exit_status, 0, filename, lineno, "%s%s",
                                  prefix_tail, message_text);
            }
          else
            po_error (exit_status, 0, "%s: %s%s",
                      filename, prefix_tail, message_text);
          error_with_progname = true;
        }
      else
        po_error (exit_status, 0, "%s%s", prefix_tail, message_text);
      if (severity < PO_SEVERITY_ERROR)
        --error_message_count;
    }
}

/* The default error handler is based on the lower-level error handler
   in po-error.h, so that gettext-po.h can offer to override one or the
   other.  */
void
textmode_xerror (int severity,
                 const struct message_ty *message,
                 const char *filename, size_t lineno, size_t column,
                 int multiline_p, const char *message_text)
{
  const char *prefix_tail =
    (severity == PO_SEVERITY_WARNING ? _("warning: ") : "");

  if (message != NULL && (filename == NULL || lineno == (size_t)(-1)))
    {
      filename = message->pos.file_name;
      lineno = message->pos.line_number;
      column = (size_t)(-1);
    }

  xerror (severity, prefix_tail, filename, lineno, column,
          multiline_p, message_text);
}

void
textmode_xerror2 (int severity,
                  const struct message_ty *message1,
                  const char *filename1, size_t lineno1, size_t column1,
                  int multiline_p1, const char *message_text1,
                  const struct message_ty *message2,
                  const char *filename2, size_t lineno2, size_t column2,
                  int multiline_p2, const char *message_text2)
{
  int severity1 = /* Don't exit before both texts have been output.  */
    (severity == PO_SEVERITY_FATAL_ERROR ? PO_SEVERITY_ERROR : severity);
  const char *prefix_tail =
    (severity == PO_SEVERITY_WARNING ? _("warning: ") : "");

  if (message1 != NULL && (filename1 == NULL || lineno1 == (size_t)(-1)))
    {
      filename1 = message1->pos.file_name;
      lineno1 = message1->pos.line_number;
      column1 = (size_t)(-1);
    }

  if (message2 != NULL && (filename2 == NULL || lineno2 == (size_t)(-1)))
    {
      filename2 = message2->pos.file_name;
      lineno2 = message2->pos.line_number;
      column2 = (size_t)(-1);
    }

  if (multiline_p1)
    xerror (severity1, prefix_tail, filename1, lineno1, column1, multiline_p1,
            message_text1);
  else
    {
      char *message_text1_extended = xasprintf ("%s...", message_text1);
      xerror (severity1, prefix_tail, filename1, lineno1, column1,
              multiline_p1, message_text1_extended);
      free (message_text1_extended);
    }

  {
    char *message_text2_extended = xasprintf ("...%s", message_text2);
    xerror (severity, prefix_tail, filename2, lineno2, column2,
            multiline_p2, message_text2_extended);
    free (message_text2_extended);
  }

  if (severity >= PO_SEVERITY_ERROR)
    /* error_message_count needs to be incremented only by 1, not by 2.  */
    --error_message_count;
}

void (*po_xerror) (int severity,
                   const struct message_ty *message,
                   const char *filename, size_t lineno, size_t column,
                   int multiline_p, const char *message_text)
  = textmode_xerror;

void (*po_xerror2) (int severity,
                    const struct message_ty *message1,
                    const char *filename1, size_t lineno1, size_t column1,
                    int multiline_p1, const char *message_text1,
                    const struct message_ty *message2,
                    const char *filename2, size_t lineno2, size_t column2,
                    int multiline_p2, const char *message_text2)
  = textmode_xerror2;