1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010,
3 2011, 2013 Free Software Foundation, Inc.
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.
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.
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/>. */
20 #include "libpspp/message.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/intern.h"
31 #include "language/lexer/lexer.h"
32 #include "libpspp/str.h"
33 #include "libpspp/version.h"
34 #include "data/settings.h"
36 #include "gl/minmax.h"
37 #include "gl/progname.h"
38 #include "gl/relocatable.h"
39 #include "gl/xalloc.h"
40 #include "gl/xvasprintf.h"
43 #define _(msgid) gettext (msgid)
45 /* Message handler as set by msg_set_handler(). */
46 static void (*msg_handler) (const struct msg *, void *aux);
49 /* Disables emitting messages if positive. */
50 static int messages_disabled;
52 /* Public functions. */
56 vmsg (enum msg_class class, const struct msg_location *location,
57 const char *format, va_list args)
59 struct msg *m = xmalloc (sizeof *m);
61 .category = msg_class_to_category (class),
62 .severity = msg_class_to_severity (class),
63 .location = msg_location_dup (location),
64 .text = xvasprintf (format, args),
69 /* Writes error message in CLASS, with text FORMAT, formatted with
70 printf, to the standard places. */
72 msg (enum msg_class class, const char *format, ...)
75 va_start (args, format);
76 vmsg (class, NULL, format, args);
80 /* Outputs error message in CLASS, with text FORMAT, formatted with printf.
81 LOCATION is the reported location for the message. */
83 msg_at (enum msg_class class, const struct msg_location *location,
84 const char *format, ...)
87 va_start (args, format);
88 vmsg (class, location, format, args);
93 msg_error (int errnum, const char *format, ...)
96 va_start (args, format);
97 struct string s = DS_EMPTY_INITIALIZER;
98 ds_put_vformat (&s, format, args);
100 ds_put_format (&s, ": %s", strerror (errnum));
102 struct msg *m = xmalloc (sizeof *m);
104 .category = MSG_C_GENERAL,
105 .severity = MSG_S_ERROR,
106 .text = ds_steal_cstr (&s),
114 msg_set_handler (void (*handler) (const struct msg *, void *aux), void *aux)
116 msg_handler = handler;
123 msg_location_uninit (struct msg_location *loc)
125 lex_source_unref (loc->src);
126 intern_unref (loc->file_name);
130 msg_location_destroy (struct msg_location *loc)
134 msg_location_uninit (loc);
140 msg_point_compare_3way (const struct msg_point *a, const struct msg_point *b)
144 : a->line > b->line ? 1
145 : a->line < b->line ? -1
148 : a->column > b->column ? 1
149 : a->column < b->column ? -1
154 msg_location_merge (struct msg_location **dstp, const struct msg_location *src)
156 struct msg_location *dst = *dstp;
159 *dstp = msg_location_dup (src);
163 if (dst->file_name != src->file_name)
168 if (msg_point_compare_3way (&dst->p[0], &src->p[0]) > 0)
169 dst->p[0] = src->p[0];
170 if (msg_point_compare_3way (&dst->p[1], &src->p[1]) < 0)
171 dst->p[1] = src->p[1];
174 struct msg_location *
175 msg_location_dup (const struct msg_location *src)
180 struct msg_location *dst = xmalloc (sizeof *dst);
183 dst->file_name = intern_ref (src->file_name);
185 lex_source_ref (dst->src);
190 msg_location_is_empty (const struct msg_location *loc)
192 return !loc || (!loc->file_name
193 && loc->p[0].line <= 0
194 && loc->p[0].column <= 0);
198 msg_location_format (const struct msg_location *loc, struct string *s)
204 ds_put_cstr (s, loc->file_name);
206 int l1 = loc->p[0].line;
207 int l2 = MAX (l1, loc->p[1].line);
208 int c1 = loc->p[0].column;
209 int c2 = MAX (c1, loc->p[1].column);
214 ds_put_byte (s, ':');
219 ds_put_format (s, "%d.%d-%d.%d", l1, c1, l2, c2);
221 ds_put_format (s, "%d-%d", l1, l2);
229 /* The GNU coding standards say to use
230 LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
231 Emacs interprets COLUMN-2 as LINENO-2 if I do that.
232 I've submitted an Emacs bug report:
233 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
235 For now, let's be compatible. */
236 ds_put_format (s, "%d.%d-%d.%d", l1, c1, l1, c2);
239 ds_put_format (s, "%d.%d", l1, c1);
242 ds_put_format (s, "%d", l1);
248 ds_put_format (s, ".%d-%d", c1, c2);
250 ds_put_format (s, ".%d", c1);
257 msg_stack_destroy (struct msg_stack *stack)
261 msg_location_destroy (stack->location);
262 free (stack->description);
268 msg_stack_dup (const struct msg_stack *src)
270 struct msg_stack *dst = xmalloc (sizeof *src);
271 *dst = (struct msg_stack) {
272 .location = msg_location_dup (src->location),
273 .description = xstrdup_if_nonnull (src->description),
278 /* Working with messages. */
281 msg_severity_to_string (enum msg_severity severity)
295 /* Duplicate a message */
297 msg_dup (const struct msg *src)
299 struct msg_stack **ms = xmalloc (src->n_stack * sizeof *ms);
300 for (size_t i = 0; i < src->n_stack; i++)
301 ms[i] = msg_stack_dup (src->stack[i]);
303 struct msg *dst = xmalloc (sizeof *dst);
304 *dst = (struct msg) {
305 .category = src->category,
306 .severity = src->severity,
308 .n_stack = src->n_stack,
309 .location = msg_location_dup (src->location),
310 .command_name = xstrdup_if_nonnull (src->command_name),
311 .text = xstrdup (src->text),
316 /* Frees a message created by msg_dup().
318 (Messages not created by msg_dup(), as well as their file_name
319 members, are typically not dynamically allocated, so this function should
320 not be used to destroy them.) */
322 msg_destroy (struct msg *m)
326 for (size_t i = 0; i < m->n_stack; i++)
327 msg_stack_destroy (m->stack[i]);
329 msg_location_destroy (m->location);
331 free (m->command_name);
337 msg_to_string (const struct msg *m)
343 for (size_t i = 0; i < m->n_stack; i++)
345 const struct msg_stack *ms = m->stack[i];
346 if (!msg_location_is_empty (ms->location))
348 msg_location_format (ms->location, &s);
349 ds_put_cstr (&s, ": ");
351 ds_put_format (&s, "%s\n", ms->description);
353 if (m->category != MSG_C_GENERAL && !msg_location_is_empty (m->location))
355 msg_location_format (m->location, &s);
356 ds_put_cstr (&s, ": ");
359 ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
361 if (m->category == MSG_C_SYNTAX && m->command_name != NULL)
362 ds_put_format (&s, "%s: ", m->command_name);
364 ds_put_cstr (&s, m->text);
366 const struct msg_location *loc = m->location;
367 if (loc->src && loc->p[0].line)
369 struct substring line = lex_source_get_line (loc->src, m->location->p[0].line);
370 ds_put_format (&s, "\n%5d | %.*s", loc->p[0].line, (int) line.length, line.string);
371 if (loc->p[0].column && loc->p[1].column >= loc->p[0].column)
373 ds_put_cstr (&s, " | ");
374 ds_put_byte_multiple (&s, ' ', loc->p[0].column - 1);
375 int n = loc->p[1].column - loc->p[0].column + 1;
376 ds_put_byte (&s, '^');
378 ds_put_byte_multiple (&s, '~', n - 1);
386 /* Number of messages reported, by severity level. */
387 static int counts[MSG_N_SEVERITIES];
389 /* True after the maximum number of errors or warnings has been exceeded. */
390 static bool too_many_errors;
392 /* True after the maximum number of notes has been exceeded. */
393 static bool too_many_notes;
395 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
396 static bool warnings_off = false;
398 /* Checks whether we've had so many errors that it's time to quit
399 processing this syntax file. */
401 msg_ui_too_many_errors (void)
403 return too_many_errors;
407 msg_ui_disable_warnings (bool x)
414 msg_ui_reset_counts (void)
418 for (i = 0; i < MSG_N_SEVERITIES; i++)
420 too_many_errors = false;
421 too_many_notes = false;
425 msg_ui_any_errors (void)
427 return counts[MSG_S_ERROR] > 0;
432 ship_message (const struct msg *m)
434 enum { MAX_STACK = 4 };
435 static const struct msg *stack[MAX_STACK];
438 /* If we're recursing on a given message, or recursing deeply, drop it. */
441 for (size_t i = 0; i < n; i++)
446 if (msg_handler && n <= 1)
447 msg_handler (m, msg_aux);
449 fprintf (stderr, "%s\n", m->text);
454 submit_note (char *s)
457 .category = MSG_C_GENERAL,
458 .severity = MSG_S_NOTE,
467 process_msg (struct msg *m)
469 int n_msgs, max_msgs;
472 || (too_many_notes && m->severity == MSG_S_NOTE)
473 || (warnings_off && m->severity == MSG_S_WARNING))
478 counts[m->severity]++;
479 max_msgs = settings_get_max_messages (m->severity);
480 n_msgs = counts[m->severity];
481 if (m->severity == MSG_S_WARNING)
482 n_msgs += counts[MSG_S_ERROR];
483 if (n_msgs > max_msgs)
485 if (m->severity == MSG_S_NOTE)
487 too_many_notes = true;
488 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
489 "Suppressing further notes."),
494 too_many_errors = true;
495 if (m->severity == MSG_S_WARNING)
496 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d). Syntax processing will be halted."),
499 submit_note (xasprintf (_("Errors (%d) exceed limit (%d). Syntax processing will be halted."),
506 /* Emits M as an error message. Takes ownership of M. */
508 msg_emit (struct msg *m)
510 if (!messages_disabled)
515 /* Disables message output until the next call to msg_enable. If
516 this function is called multiple times, msg_enable must be
517 called an equal number of times before messages are actually
525 /* Enables message output that was disabled by msg_disable. */
529 assert (messages_disabled > 0);
533 /* Private functions. */
535 static char fatal_error_message[1024];
536 static int fatal_error_message_bytes = 0;
538 static char diagnostic_information[1024];
539 static int diagnostic_information_bytes = 0;
542 append_message (char *msg, int bytes_used, const char *fmt, ...)
546 int ret = vsnprintf (msg + bytes_used, 1024 - bytes_used, fmt, va);
554 /* Generate a row of asterisks held in statically allocated memory */
555 static struct substring
556 generate_banner (void)
558 static struct substring banner;
560 banner = ss_cstr ("******************************************************\n");
565 prepare_fatal_error_message (void)
567 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, generate_banner ().string);
569 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "You have discovered a bug in PSPP. Please report this\n");
570 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
571 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "message, *plus* several lines of output just above it.\n");
572 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "For the best chance at having the bug fixed, also\n");
573 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "include the syntax file that triggered it and a sample\n");
574 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "of any data file used for input.\n");
575 return fatal_error_message;
579 prepare_diagnostic_information (void)
581 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "version: %s\n", version);
582 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "host_system: %s\n", host_system);
583 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "build_system: %s\n", build_system);
584 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "locale_dir: %s\n", relocate (locale_dir));
585 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "compiler version: %s\n",
593 return diagnostic_information;
597 request_bug_report (const char *msg)
599 write (STDERR_FILENO, fatal_error_message, fatal_error_message_bytes);
600 write (STDERR_FILENO, "proximate cause: ", 21);
601 write (STDERR_FILENO, msg, strlen (msg));
602 write (STDERR_FILENO, "\n", 1);
603 write (STDERR_FILENO, diagnostic_information, diagnostic_information_bytes);
604 const struct substring banner = generate_banner ();
605 write (STDERR_FILENO, banner.string, banner.length);