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 "libpspp/str.h"
32 #include "libpspp/version.h"
33 #include "data/settings.h"
35 #include "gl/minmax.h"
36 #include "gl/progname.h"
37 #include "gl/relocatable.h"
38 #include "gl/xalloc.h"
39 #include "gl/xvasprintf.h"
42 #define _(msgid) gettext (msgid)
44 /* Message handler as set by msg_set_handler(). */
45 static struct msg_handler msg_handler = { .output_msg = NULL };
47 /* Disables emitting messages if positive. */
48 static int messages_disabled;
50 /* Public functions. */
54 vmsg (enum msg_class class, const struct msg_location *location,
55 const char *format, va_list args)
57 struct msg *m = xmalloc (sizeof *m);
59 .category = msg_class_to_category (class),
60 .severity = msg_class_to_severity (class),
61 .location = msg_location_dup (location),
62 .text = xvasprintf (format, args),
67 /* Writes error message in CLASS, with text FORMAT, formatted with
68 printf, to the standard places. */
70 msg (enum msg_class class, const char *format, ...)
73 va_start (args, format);
74 vmsg (class, NULL, format, args);
78 /* Outputs error message in CLASS, with text FORMAT, formatted with printf.
79 LOCATION is the reported location for the message. */
81 msg_at (enum msg_class class, const struct msg_location *location,
82 const char *format, ...)
85 va_start (args, format);
86 vmsg (class, location, format, args);
91 msg_error (int errnum, const char *format, ...)
94 va_start (args, format);
95 struct string s = DS_EMPTY_INITIALIZER;
96 ds_put_vformat (&s, format, args);
98 ds_put_format (&s, ": %s", strerror (errnum));
100 struct msg *m = xmalloc (sizeof *m);
102 .category = MSG_C_GENERAL,
103 .severity = MSG_S_ERROR,
104 .text = ds_steal_cstr (&s),
110 msg_set_handler (const struct msg_handler *handler)
112 msg_handler = *handler;
117 /* Takes POINT, adds to it the syntax in SYNTAX, incrementing the line number
118 for each new-line in SYNTAX and the column number for each column, and
119 returns the result. */
121 msg_point_advance (struct msg_point point, struct substring syntax)
125 size_t newline = ss_find_byte (syntax, '\n');
126 if (newline == SIZE_MAX)
130 ss_advance (&syntax, newline + 1);
133 point.column += ss_utf8_count_columns (syntax);
140 msg_location_uninit (struct msg_location *loc)
142 if (msg_handler.lex_source_unref)
143 msg_handler.lex_source_unref (loc->src);
144 intern_unref (loc->file_name);
148 msg_location_destroy (struct msg_location *loc)
152 msg_location_uninit (loc);
158 msg_point_compare_3way (const struct msg_point *a, const struct msg_point *b)
162 : a->line > b->line ? 1
163 : a->line < b->line ? -1
166 : a->column > b->column ? 1
167 : a->column < b->column ? -1
172 msg_location_remove_columns (struct msg_location *location)
174 location->start.column = 0;
175 location->end.column = 0;
179 msg_location_merge (struct msg_location **dstp, const struct msg_location *src)
181 struct msg_location *dst = *dstp;
184 *dstp = msg_location_dup (src);
188 if (dst->file_name != src->file_name)
193 if (msg_point_compare_3way (&dst->start, &src->start) > 0)
194 dst->start = src->start;
195 if (msg_point_compare_3way (&dst->end, &src->end) < 0)
199 struct msg_location *
200 msg_location_merged (const struct msg_location *a,
201 const struct msg_location *b)
203 struct msg_location *new = msg_location_dup (a);
205 msg_location_merge (&new, b);
209 struct msg_location *
210 msg_location_dup (const struct msg_location *src)
215 struct msg_location *dst = xmalloc (sizeof *dst);
218 dst->file_name = intern_ref (src->file_name);
219 if (msg_handler.lex_source_ref && src->src)
220 msg_handler.lex_source_ref (dst->src);
225 msg_location_is_empty (const struct msg_location *loc)
227 return !loc || (!loc->file_name
228 && loc->start.line <= 0
229 && loc->start.column <= 0);
233 msg_location_format (const struct msg_location *loc, struct string *s)
239 ds_put_cstr (s, loc->file_name);
241 int l1 = loc->start.line;
242 int l2 = MAX (l1, loc->end.line);
243 int c1 = loc->start.column;
244 int c2 = MAX (c1, loc->end.column);
249 ds_put_byte (s, ':');
254 ds_put_format (s, "%d.%d-%d.%d", l1, c1, l2, c2);
256 ds_put_format (s, "%d-%d", l1, l2);
264 /* The GNU coding standards say to use
265 LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
266 Emacs interprets COLUMN-2 as LINENO-2 if I do that.
267 I've submitted an Emacs bug report:
268 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
270 For now, let's be compatible. */
271 ds_put_format (s, "%d.%d-%d.%d", l1, c1, l1, c2);
274 ds_put_format (s, "%d.%d", l1, c1);
277 ds_put_format (s, "%d", l1);
283 ds_put_format (s, ".%d-%d", c1, c2);
285 ds_put_format (s, ".%d", c1);
292 msg_stack_destroy (struct msg_stack *stack)
296 msg_location_destroy (stack->location);
297 free (stack->description);
303 msg_stack_dup (const struct msg_stack *src)
305 struct msg_stack *dst = xmalloc (sizeof *src);
306 *dst = (struct msg_stack) {
307 .location = msg_location_dup (src->location),
308 .description = xstrdup_if_nonnull (src->description),
313 /* Working with messages. */
316 msg_severity_to_string (enum msg_severity severity)
330 /* Duplicate a message */
332 msg_dup (const struct msg *src)
334 struct msg_stack **ms = xmalloc (src->n_stack * sizeof *ms);
335 for (size_t i = 0; i < src->n_stack; i++)
336 ms[i] = msg_stack_dup (src->stack[i]);
338 struct msg *dst = xmalloc (sizeof *dst);
339 *dst = (struct msg) {
340 .category = src->category,
341 .severity = src->severity,
343 .n_stack = src->n_stack,
344 .location = msg_location_dup (src->location),
345 .command_name = xstrdup_if_nonnull (src->command_name),
346 .text = xstrdup (src->text),
351 /* Frees a message created by msg_dup().
353 (Messages not created by msg_dup(), as well as their file_name
354 members, are typically not dynamically allocated, so this function should
355 not be used to destroy them.) */
357 msg_destroy (struct msg *m)
361 for (size_t i = 0; i < m->n_stack; i++)
362 msg_stack_destroy (m->stack[i]);
364 msg_location_destroy (m->location);
366 free (m->command_name);
372 msg_to_string (const struct msg *m)
378 for (size_t i = 0; i < m->n_stack; i++)
380 const struct msg_stack *ms = m->stack[i];
381 if (!msg_location_is_empty (ms->location))
383 msg_location_format (ms->location, &s);
384 ds_put_cstr (&s, ": ");
386 ds_put_format (&s, "%s\n", ms->description);
388 if (m->category != MSG_C_GENERAL && !msg_location_is_empty (m->location))
390 msg_location_format (m->location, &s);
391 ds_put_cstr (&s, ": ");
394 ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
396 if (m->category == MSG_C_SYNTAX && m->command_name != NULL)
397 ds_put_format (&s, "%s: ", m->command_name);
399 ds_put_cstr (&s, m->text);
401 const struct msg_location *loc = m->location;
402 if (m->category != MSG_C_GENERAL
403 && loc->src && loc->start.line && loc->start.column
404 && msg_handler.lex_source_get_line)
406 int l0 = loc->start.line;
407 int l1 = loc->end.line;
409 for (int ln = l0; ln <= l1; ln++)
411 if (nl > 3 && ln == l0 + 2)
413 ds_put_cstr (&s, "\n ... |");
417 struct substring line = msg_handler.lex_source_get_line (
419 ss_rtrim (&line, ss_cstr ("\n\r"));
421 ds_put_format (&s, "\n%5d | ", ln);
422 ds_put_substring (&s, line);
424 int c0 = ln == l0 ? loc->start.column : 1;
425 int c1 = ln == l1 ? loc->end.column : ss_utf8_count_columns (line);
426 if (c0 > 0 && c1 >= c0)
428 ds_put_cstr (&s, "\n |");
429 ds_put_byte_multiple (&s, ' ', c0);
432 ds_put_byte (&s, '^');
434 ds_put_byte_multiple (&s, '~', c1 - c0);
437 ds_put_byte_multiple (&s, '-', c1 - c0 + 1);
446 /* Number of messages reported, by severity level. */
447 static int counts[MSG_N_SEVERITIES];
449 /* True after the maximum number of errors or warnings has been exceeded. */
450 static bool too_many_errors;
452 /* True after the maximum number of notes has been exceeded. */
453 static bool too_many_notes;
455 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
456 static bool warnings_off = false;
458 /* Checks whether we've had so many errors that it's time to quit
459 processing this syntax file. */
461 msg_ui_too_many_errors (void)
463 return too_many_errors;
467 msg_ui_disable_warnings (bool x)
474 msg_ui_reset_counts (void)
478 for (i = 0; i < MSG_N_SEVERITIES; i++)
480 too_many_errors = false;
481 too_many_notes = false;
485 msg_ui_any_errors (void)
487 return counts[MSG_S_ERROR] > 0;
492 ship_message (const struct msg *m)
494 enum { MAX_STACK = 4 };
495 static const struct msg *stack[MAX_STACK];
498 /* If we're recursing on a given message, or recursing deeply, drop it. */
501 for (size_t i = 0; i < n; i++)
506 if (msg_handler.output_msg && n <= 1)
507 msg_handler.output_msg (m, msg_handler.aux);
509 fprintf (stderr, "%s\n", m->text);
514 submit_note (char *s)
517 .category = MSG_C_GENERAL,
518 .severity = MSG_S_NOTE,
527 process_msg (struct msg *m)
529 int n_msgs, max_msgs;
532 || (too_many_notes && m->severity == MSG_S_NOTE)
533 || (warnings_off && m->severity == MSG_S_WARNING))
538 counts[m->severity]++;
539 max_msgs = settings_get_max_messages (m->severity);
540 n_msgs = counts[m->severity];
541 if (m->severity == MSG_S_WARNING)
542 n_msgs += counts[MSG_S_ERROR];
543 if (n_msgs > max_msgs)
545 if (m->severity == MSG_S_NOTE)
547 too_many_notes = true;
548 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
549 "Suppressing further notes."),
554 too_many_errors = true;
555 if (m->severity == MSG_S_WARNING)
556 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d). Syntax processing will be halted."),
559 submit_note (xasprintf (_("Errors (%d) exceed limit (%d). Syntax processing will be halted."),
566 /* Emits M as an error message. Takes ownership of M. */
568 msg_emit (struct msg *m)
570 if (!messages_disabled)
575 /* Disables message output until the next call to msg_enable. If
576 this function is called multiple times, msg_enable must be
577 called an equal number of times before messages are actually
585 /* Enables message output that was disabled by msg_disable. */
589 assert (messages_disabled > 0);
593 /* Private functions. */
595 static char fatal_error_message[1024];
596 static int fatal_error_message_bytes = 0;
598 static char diagnostic_information[1024];
599 static int diagnostic_information_bytes = 0;
602 append_message (char *msg, int bytes_used, const char *fmt, ...)
606 int ret = vsnprintf (msg + bytes_used, 1024 - bytes_used, fmt, va);
614 /* Generate a row of asterisks held in statically allocated memory */
615 static struct substring
616 generate_banner (void)
618 static struct substring banner;
620 banner = ss_cstr ("******************************************************\n");
625 prepare_fatal_error_message (void)
627 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, generate_banner ().string);
629 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "You have discovered a bug in PSPP. Please report this\n");
630 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
631 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "message, *plus* several lines of output just above it.\n");
632 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "For the best chance at having the bug fixed, also\n");
633 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "include the syntax file that triggered it and a sample\n");
634 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "of any data file used for input.\n");
635 return fatal_error_message;
639 prepare_diagnostic_information (void)
641 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "version: %s\n", version);
642 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "host_system: %s\n", host_system);
643 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "build_system: %s\n", build_system);
644 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "locale_dir: %s\n", relocate (locale_dir));
645 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "compiler version: %s\n",
653 return diagnostic_information;
657 request_bug_report (const char *msg)
659 write (STDERR_FILENO, fatal_error_message, fatal_error_message_bytes);
660 write (STDERR_FILENO, "proximate cause: ", 21);
661 write (STDERR_FILENO, msg, strlen (msg));
662 write (STDERR_FILENO, "\n", 1);
663 write (STDERR_FILENO, diagnostic_information, diagnostic_information_bytes);
664 const struct substring banner = generate_banner ();
665 write (STDERR_FILENO, banner.string, banner.length);