1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010,
3 2011, 2013 Free Software Foundation, Inc.
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 <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include "libpspp/message.h"
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "libpspp/cast.h"
30 #include "libpspp/str.h"
31 #include "libpspp/version.h"
32 #include "data/settings.h"
33
34 #include "gl/minmax.h"
35 #include "gl/progname.h"
36 #include "gl/xalloc.h"
37 #include "gl/xvasprintf.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 /* Message handler as set by msg_set_handler(). */
43 static void (*msg_handler) (const struct msg *, void *aux);
44 static void *msg_aux;
45
46 /* Disables emitting messages if positive. */
47 static int messages_disabled;
48
49 /* Public functions. */
50
51
52 void
vmsg(enum msg_class class,const char * format,va_list args)53 vmsg (enum msg_class class, const char *format, va_list args)
54 {
55 struct msg m = {
56 .category = msg_class_to_category (class),
57 .severity = msg_class_to_severity (class),
58 .text = xvasprintf (format, args),
59 };
60
61 msg_emit (&m);
62 }
63
64 /* Writes error message in CLASS, with text FORMAT, formatted with
65 printf, to the standard places. */
66 void
msg(enum msg_class class,const char * format,...)67 msg (enum msg_class class, const char *format, ...)
68 {
69 va_list args;
70 va_start (args, format);
71 vmsg (class, format, args);
72 va_end (args);
73 }
74
75
76
77 void
msg_error(int errnum,const char * format,...)78 msg_error (int errnum, const char *format, ...)
79 {
80 va_list args;
81 va_start (args, format);
82 char *e = xvasprintf (format, args);
83 va_end (args);
84
85 struct msg m = {
86 .category = MSG_C_GENERAL,
87 .severity = MSG_S_ERROR,
88 .file_name = NULL,
89 .text = xasprintf (_("%s: %s"), e, strerror (errnum)),
90 };
91 msg_emit (&m);
92
93 free (e);
94 }
95
96
97
98 void
msg_set_handler(void (* handler)(const struct msg *,void * aux),void * aux)99 msg_set_handler (void (*handler) (const struct msg *, void *aux), void *aux)
100 {
101 msg_handler = handler;
102 msg_aux = aux;
103 }
104
105 /* Working with messages. */
106
107 const char *
msg_severity_to_string(enum msg_severity severity)108 msg_severity_to_string (enum msg_severity severity)
109 {
110 switch (severity)
111 {
112 case MSG_S_ERROR:
113 return _("error");
114 case MSG_S_WARNING:
115 return _("warning");
116 case MSG_S_NOTE:
117 default:
118 return _("note");
119 }
120 }
121
122 /* Duplicate a message */
123 struct msg *
msg_dup(const struct msg * m)124 msg_dup (const struct msg *m)
125 {
126 struct msg *new_msg;
127
128 new_msg = xmemdup (m, sizeof *m);
129 if (m->file_name != NULL)
130 new_msg->file_name = xstrdup (m->file_name);
131 if (m->command_name != NULL)
132 new_msg->command_name = xstrdup (m->command_name);
133 new_msg->text = xstrdup (m->text);
134
135 return new_msg;
136 }
137
138 /* Frees a message created by msg_dup().
139
140 (Messages not created by msg_dup(), as well as their file_name
141 members, are typically not dynamically allocated, so this function should
142 not be used to destroy them.) */
143 void
msg_destroy(struct msg * m)144 msg_destroy (struct msg *m)
145 {
146 free (m->file_name);
147 free (m->text);
148 free (m->command_name);
149 free (m);
150 }
151
152 char *
msg_to_string(const struct msg * m)153 msg_to_string (const struct msg *m)
154 {
155 struct string s;
156
157 ds_init_empty (&s);
158
159 if (m->category != MSG_C_GENERAL
160 && (m->file_name || m->first_line > 0 || m->first_column > 0))
161 {
162 int l1 = m->first_line;
163 int l2 = MAX (m->first_line, m->last_line - 1);
164 int c1 = m->first_column;
165 int c2 = MAX (m->first_column, m->last_column - 1);
166
167 if (m->file_name)
168 ds_put_format (&s, "%s", m->file_name);
169
170 if (l1 > 0)
171 {
172 if (!ds_is_empty (&s))
173 ds_put_byte (&s, ':');
174
175 if (l2 > l1)
176 {
177 if (c1 > 0)
178 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l2, c2);
179 else
180 ds_put_format (&s, "%d-%d", l1, l2);
181 }
182 else
183 {
184 if (c1 > 0)
185 {
186 if (c2 > c1)
187 {
188 /* The GNU coding standards say to use
189 LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
190 Emacs interprets COLUMN-2 as LINENO-2 if I do that.
191 I've submitted an Emacs bug report:
192 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
193
194 For now, let's be compatible. */
195 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l1, c2);
196 }
197 else
198 ds_put_format (&s, "%d.%d", l1, c1);
199 }
200 else
201 ds_put_format (&s, "%d", l1);
202 }
203 }
204 else if (c1 > 0)
205 {
206 if (c2 > c1)
207 ds_put_format (&s, ".%d-%d", c1, c2);
208 else
209 ds_put_format (&s, ".%d", c1);
210 }
211 ds_put_cstr (&s, ": ");
212 }
213
214 ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
215
216 if (m->category == MSG_C_SYNTAX && m->command_name != NULL)
217 ds_put_format (&s, "%s: ", m->command_name);
218
219 ds_put_cstr (&s, m->text);
220
221 return ds_cstr (&s);
222 }
223
224
225 /* Number of messages reported, by severity level. */
226 static int counts[MSG_N_SEVERITIES];
227
228 /* True after the maximum number of errors or warnings has been exceeded. */
229 static bool too_many_errors;
230
231 /* True after the maximum number of notes has been exceeded. */
232 static bool too_many_notes;
233
234 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
235 static bool warnings_off = false;
236
237 /* Checks whether we've had so many errors that it's time to quit
238 processing this syntax file. */
239 bool
msg_ui_too_many_errors(void)240 msg_ui_too_many_errors (void)
241 {
242 return too_many_errors;
243 }
244
245 void
msg_ui_disable_warnings(bool x)246 msg_ui_disable_warnings (bool x)
247 {
248 warnings_off = x;
249 }
250
251
252 void
msg_ui_reset_counts(void)253 msg_ui_reset_counts (void)
254 {
255 int i;
256
257 for (i = 0; i < MSG_N_SEVERITIES; i++)
258 counts[i] = 0;
259 too_many_errors = false;
260 too_many_notes = false;
261 }
262
263 bool
msg_ui_any_errors(void)264 msg_ui_any_errors (void)
265 {
266 return counts[MSG_S_ERROR] > 0;
267 }
268
269
270 static int entrances = 0;
271
272 static void
ship_message(struct msg * m)273 ship_message (struct msg *m)
274 {
275 entrances++;
276 if (! m->shipped)
277 {
278 if (msg_handler && entrances <= 1)
279 msg_handler (m, msg_aux);
280 else
281 {
282 fwrite (m->text, 1, strlen (m->text), stderr);
283 fwrite ("\n", 1, 1, stderr);
284 }
285 }
286 m->shipped = true;
287 entrances--;
288 }
289
290 static void
submit_note(char * s)291 submit_note (char *s)
292 {
293 struct msg m = {
294 .category = MSG_C_GENERAL,
295 .severity = MSG_S_NOTE,
296 .text = s,
297 };
298 ship_message (&m);
299
300 free (s);
301 }
302
303
304
305 static void
process_msg(struct msg * m)306 process_msg (struct msg *m)
307 {
308 int n_msgs, max_msgs;
309
310 if (too_many_errors
311 || (too_many_notes && m->severity == MSG_S_NOTE)
312 || (warnings_off && m->severity == MSG_S_WARNING))
313 return;
314
315 ship_message (m);
316
317 counts[m->severity]++;
318 max_msgs = settings_get_max_messages (m->severity);
319 n_msgs = counts[m->severity];
320 if (m->severity == MSG_S_WARNING)
321 n_msgs += counts[MSG_S_ERROR];
322 if (n_msgs > max_msgs)
323 {
324 if (m->severity == MSG_S_NOTE)
325 {
326 too_many_notes = true;
327 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
328 "Suppressing further notes."),
329 n_msgs, max_msgs));
330 }
331 else
332 {
333 too_many_errors = true;
334 if (m->severity == MSG_S_WARNING)
335 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d). Syntax processing will be halted."),
336 n_msgs, max_msgs));
337 else
338 submit_note (xasprintf (_("Errors (%d) exceed limit (%d). Syntax processing will be halted."),
339 n_msgs, max_msgs));
340 }
341 }
342 }
343
344
345 /* Emits M as an error message.
346 Frees allocated data in M. */
347 void
msg_emit(struct msg * m)348 msg_emit (struct msg *m)
349 {
350 m->shipped = false;
351 if (!messages_disabled)
352 process_msg (m);
353
354 free (m->text);
355 free (m->command_name);
356 }
357
358 /* Disables message output until the next call to msg_enable. If
359 this function is called multiple times, msg_enable must be
360 called an equal number of times before messages are actually
361 re-enabled. */
362 void
msg_disable(void)363 msg_disable (void)
364 {
365 messages_disabled++;
366 }
367
368 /* Enables message output that was disabled by msg_disable. */
369 void
msg_enable(void)370 msg_enable (void)
371 {
372 assert (messages_disabled > 0);
373 messages_disabled--;
374 }
375
376 /* Private functions. */
377
378 void
request_bug_report(const char * msg)379 request_bug_report (const char *msg)
380 {
381 fprintf (stderr, "******************************************************\n");
382 fprintf (stderr, "You have discovered a bug in PSPP. Please report this\n");
383 fprintf (stderr, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
384 fprintf (stderr, "message, *plus* several lines of output just above it.\n");
385 fprintf (stderr, "For the best chance at having the bug fixed, also\n");
386 fprintf (stderr, "include the syntax file that triggered it and a sample\n");
387 fprintf (stderr, "of any data file used for input.\n");
388 fprintf (stderr, "proximate cause: %s\n", msg);
389 fprintf (stderr, "version: %s\n", version);
390 fprintf (stderr, "host_system: %s\n", host_system);
391 fprintf (stderr, "build_system: %s\n", build_system);
392 fprintf (stderr, "locale_dir: %s\n", locale_dir);
393 fprintf (stderr, "compiler version: %s\n",
394 #ifdef __VERSION__
395 __VERSION__
396 #else
397 "Unknown"
398 #endif
399 );
400 fprintf (stderr, "******************************************************\n");
401 }
402
403