1 /* Error handling during reading and writing of PO files.
2 Copyright (C) 2005-2007, 2013, 2019 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2005.
4
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 3 of the License, or
8 (at your option) any later version.
9
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.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 /* Specification. */
24 #include "po-xerror.h"
25
26 #include <error.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "message.h"
31 #include "error-progname.h"
32 #include "xalloc.h"
33 #include "xerror.h"
34 #include "error.h"
35 #include "xvasprintf.h"
36 #include "po-error.h"
37 #if IN_LIBGETTEXTPO
38 # include "getprogname.h"
39 # define program_name getprogname ()
40 #else
41 # include "progname.h"
42 #endif
43 #include "gettext.h"
44
45 #define _(str) gettext (str)
46
47
48 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)49 xerror (int severity, const char *prefix_tail,
50 const char *filename, size_t lineno, size_t column,
51 int multiline_p, const char *message_text)
52 {
53 if (multiline_p)
54 {
55 bool old_error_with_progname = error_with_progname;
56 char *prefix;
57
58 if (filename != NULL)
59 {
60 if (lineno != (size_t)(-1))
61 {
62 if (column != (size_t)(-1))
63 prefix =
64 xasprintf ("%s:%ld:%ld: %s", filename,
65 (long) lineno, (long) column, prefix_tail);
66 else
67 prefix =
68 xasprintf ("%s:%ld: %s", filename,
69 (long) lineno, prefix_tail);
70 }
71 else
72 prefix = xasprintf ("%s: %s", filename, prefix_tail);
73 error_with_progname = false;
74 }
75 else
76 prefix = xasprintf ("%s: %s", program_name, prefix_tail);
77
78 if (severity >= PO_SEVERITY_ERROR)
79 po_multiline_error (prefix, xstrdup (message_text));
80 else
81 po_multiline_warning (prefix, xstrdup (message_text));
82 error_with_progname = old_error_with_progname;
83
84 if (severity == PO_SEVERITY_FATAL_ERROR)
85 exit (EXIT_FAILURE);
86 }
87 else
88 {
89 int exit_status =
90 (severity == PO_SEVERITY_FATAL_ERROR ? EXIT_FAILURE : 0);
91
92 if (filename != NULL)
93 {
94 error_with_progname = false;
95 if (lineno != (size_t)(-1))
96 {
97 if (column != (size_t)(-1))
98 po_error (exit_status, 0, "%s:%ld:%ld: %s%s",
99 filename, (long) lineno, (long) column,
100 prefix_tail, message_text);
101 else
102 po_error_at_line (exit_status, 0, filename, lineno, "%s%s",
103 prefix_tail, message_text);
104 }
105 else
106 po_error (exit_status, 0, "%s: %s%s",
107 filename, prefix_tail, message_text);
108 error_with_progname = true;
109 }
110 else
111 po_error (exit_status, 0, "%s%s", prefix_tail, message_text);
112 if (severity < PO_SEVERITY_ERROR)
113 --error_message_count;
114 }
115 }
116
117 /* The default error handler is based on the lower-level error handler
118 in po-error.h, so that gettext-po.h can offer to override one or the
119 other. */
120 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)121 textmode_xerror (int severity,
122 const struct message_ty *message,
123 const char *filename, size_t lineno, size_t column,
124 int multiline_p, const char *message_text)
125 {
126 const char *prefix_tail =
127 (severity == PO_SEVERITY_WARNING ? _("warning: ") : "");
128
129 if (message != NULL && (filename == NULL || lineno == (size_t)(-1)))
130 {
131 filename = message->pos.file_name;
132 lineno = message->pos.line_number;
133 column = (size_t)(-1);
134 }
135
136 xerror (severity, prefix_tail, filename, lineno, column,
137 multiline_p, message_text);
138 }
139
140 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)141 textmode_xerror2 (int severity,
142 const struct message_ty *message1,
143 const char *filename1, size_t lineno1, size_t column1,
144 int multiline_p1, const char *message_text1,
145 const struct message_ty *message2,
146 const char *filename2, size_t lineno2, size_t column2,
147 int multiline_p2, const char *message_text2)
148 {
149 int severity1 = /* Don't exit before both texts have been output. */
150 (severity == PO_SEVERITY_FATAL_ERROR ? PO_SEVERITY_ERROR : severity);
151 const char *prefix_tail =
152 (severity == PO_SEVERITY_WARNING ? _("warning: ") : "");
153
154 if (message1 != NULL && (filename1 == NULL || lineno1 == (size_t)(-1)))
155 {
156 filename1 = message1->pos.file_name;
157 lineno1 = message1->pos.line_number;
158 column1 = (size_t)(-1);
159 }
160
161 if (message2 != NULL && (filename2 == NULL || lineno2 == (size_t)(-1)))
162 {
163 filename2 = message2->pos.file_name;
164 lineno2 = message2->pos.line_number;
165 column2 = (size_t)(-1);
166 }
167
168 if (multiline_p1)
169 xerror (severity1, prefix_tail, filename1, lineno1, column1, multiline_p1,
170 message_text1);
171 else
172 {
173 char *message_text1_extended = xasprintf ("%s...", message_text1);
174 xerror (severity1, prefix_tail, filename1, lineno1, column1,
175 multiline_p1, message_text1_extended);
176 free (message_text1_extended);
177 }
178
179 {
180 char *message_text2_extended = xasprintf ("...%s", message_text2);
181 xerror (severity, prefix_tail, filename2, lineno2, column2,
182 multiline_p2, message_text2_extended);
183 free (message_text2_extended);
184 }
185
186 if (severity >= PO_SEVERITY_ERROR)
187 /* error_message_count needs to be incremented only by 1, not by 2. */
188 --error_message_count;
189 }
190
191 void (*po_xerror) (int severity,
192 const struct message_ty *message,
193 const char *filename, size_t lineno, size_t column,
194 int multiline_p, const char *message_text)
195 = textmode_xerror;
196
197 void (*po_xerror2) (int severity,
198 const struct message_ty *message1,
199 const char *filename1, size_t lineno1, size_t column1,
200 int multiline_p1, const char *message_text1,
201 const struct message_ty *message2,
202 const char *filename2, size_t lineno2, size_t column2,
203 int multiline_p2, const char *message_text2)
204 = textmode_xerror2;
205