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/str.h"
31 #include "libpspp/version.h"
32 #include "data/settings.h"
34 #include "gl/minmax.h"
35 #include "gl/progname.h"
36 #include "gl/relocatable.h"
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
41 #define _(msgid) gettext (msgid)
43 /* Message handler as set by msg_set_handler(). */
44 static void (*msg_handler) (const struct msg *, void *aux);
47 /* Disables emitting messages if positive. */
48 static int messages_disabled;
50 /* Public functions. */
54 vmsg (enum msg_class class, const char *format, va_list args)
56 struct msg *m = xmalloc (sizeof *m);
58 .category = msg_class_to_category (class),
59 .severity = msg_class_to_severity (class),
60 .text = xvasprintf (format, args),
65 /* Writes error message in CLASS, with text FORMAT, formatted with
66 printf, to the standard places. */
68 msg (enum msg_class class, const char *format, ...)
71 va_start (args, format);
72 vmsg (class, format, args);
79 msg_error (int errnum, const char *format, ...)
82 va_start (args, format);
83 struct string s = DS_EMPTY_INITIALIZER;
84 ds_put_vformat (&s, format, args);
86 ds_put_format (&s, ": %s", strerror (errnum));
88 struct msg *m = xmalloc (sizeof *m);
90 .category = MSG_C_GENERAL,
91 .severity = MSG_S_ERROR,
92 .text = ds_steal_cstr (&s),
100 msg_set_handler (void (*handler) (const struct msg *, void *aux), void *aux)
102 msg_handler = handler;
109 msg_location_uninit (struct msg_location *loc)
111 free (loc->file_name);
115 msg_location_destroy (struct msg_location *loc)
119 msg_location_uninit (loc);
124 struct msg_location *
125 msg_location_dup (const struct msg_location *src)
130 struct msg_location *dst = xmalloc (sizeof *dst);
131 *dst = (struct msg_location) {
132 .file_name = xstrdup_if_nonnull (src->file_name),
133 .first_line = src->first_line,
134 .last_line = src->last_line,
135 .first_column = src->first_column,
136 .last_column = src->last_column,
142 msg_location_is_empty (const struct msg_location *loc)
144 return !loc || (!loc->file_name
145 && loc->first_line <= 0
146 && loc->first_column <= 0);
150 msg_location_format (const struct msg_location *loc, struct string *s)
156 ds_put_cstr (s, loc->file_name);
158 int l1 = loc->first_line;
159 int l2 = MAX (loc->first_line, loc->last_line - 1);
160 int c1 = loc->first_column;
161 int c2 = MAX (loc->first_column, loc->last_column - 1);
166 ds_put_byte (s, ':');
171 ds_put_format (s, "%d.%d-%d.%d", l1, c1, l2, c2);
173 ds_put_format (s, "%d-%d", l1, l2);
181 /* The GNU coding standards say to use
182 LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
183 Emacs interprets COLUMN-2 as LINENO-2 if I do that.
184 I've submitted an Emacs bug report:
185 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
187 For now, let's be compatible. */
188 ds_put_format (s, "%d.%d-%d.%d", l1, c1, l1, c2);
191 ds_put_format (s, "%d.%d", l1, c1);
194 ds_put_format (s, "%d", l1);
200 ds_put_format (s, ".%d-%d", c1, c2);
202 ds_put_format (s, ".%d", c1);
209 msg_stack_destroy (struct msg_stack *stack)
213 msg_location_destroy (stack->location);
214 free (stack->description);
220 msg_stack_dup (const struct msg_stack *src)
222 struct msg_stack *dst = xmalloc (sizeof *src);
223 *dst = (struct msg_stack) {
224 .location = msg_location_dup (src->location),
225 .description = xstrdup_if_nonnull (src->description),
230 /* Working with messages. */
233 msg_severity_to_string (enum msg_severity severity)
247 /* Duplicate a message */
249 msg_dup (const struct msg *src)
251 struct msg_stack **ms = xmalloc (src->n_stack * sizeof *ms);
252 for (size_t i = 0; i < src->n_stack; i++)
253 ms[i] = msg_stack_dup (src->stack[i]);
255 struct msg *dst = xmalloc (sizeof *dst);
256 *dst = (struct msg) {
257 .category = src->category,
258 .severity = src->severity,
260 .n_stack = src->n_stack,
261 .location = msg_location_dup (src->location),
262 .command_name = xstrdup_if_nonnull (src->command_name),
263 .text = xstrdup (src->text),
268 /* Frees a message created by msg_dup().
270 (Messages not created by msg_dup(), as well as their file_name
271 members, are typically not dynamically allocated, so this function should
272 not be used to destroy them.) */
274 msg_destroy (struct msg *m)
278 for (size_t i = 0; i < m->n_stack; i++)
279 msg_stack_destroy (m->stack[i]);
281 msg_location_destroy (m->location);
283 free (m->command_name);
289 msg_to_string (const struct msg *m)
295 for (size_t i = 0; i < m->n_stack; i++)
297 const struct msg_stack *ms = m->stack[i];
298 if (!msg_location_is_empty (ms->location))
300 msg_location_format (ms->location, &s);
301 ds_put_cstr (&s, ": ");
303 ds_put_format (&s, "%s\n", ms->description);
305 if (m->category != MSG_C_GENERAL && !msg_location_is_empty (m->location))
307 msg_location_format (m->location, &s);
308 ds_put_cstr (&s, ": ");
311 ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
313 if (m->category == MSG_C_SYNTAX && m->command_name != NULL)
314 ds_put_format (&s, "%s: ", m->command_name);
316 ds_put_cstr (&s, m->text);
322 /* Number of messages reported, by severity level. */
323 static int counts[MSG_N_SEVERITIES];
325 /* True after the maximum number of errors or warnings has been exceeded. */
326 static bool too_many_errors;
328 /* True after the maximum number of notes has been exceeded. */
329 static bool too_many_notes;
331 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
332 static bool warnings_off = false;
334 /* Checks whether we've had so many errors that it's time to quit
335 processing this syntax file. */
337 msg_ui_too_many_errors (void)
339 return too_many_errors;
343 msg_ui_disable_warnings (bool x)
350 msg_ui_reset_counts (void)
354 for (i = 0; i < MSG_N_SEVERITIES; i++)
356 too_many_errors = false;
357 too_many_notes = false;
361 msg_ui_any_errors (void)
363 return counts[MSG_S_ERROR] > 0;
368 ship_message (const struct msg *m)
370 enum { MAX_STACK = 4 };
371 static const struct msg *stack[MAX_STACK];
374 /* If we're recursing on a given message, or recursing deeply, drop it. */
377 for (size_t i = 0; i < n; i++)
382 if (msg_handler && n <= 1)
383 msg_handler (m, msg_aux);
385 fprintf (stderr, "%s\n", m->text);
390 submit_note (char *s)
393 .category = MSG_C_GENERAL,
394 .severity = MSG_S_NOTE,
403 process_msg (struct msg *m)
405 int n_msgs, max_msgs;
408 || (too_many_notes && m->severity == MSG_S_NOTE)
409 || (warnings_off && m->severity == MSG_S_WARNING))
414 counts[m->severity]++;
415 max_msgs = settings_get_max_messages (m->severity);
416 n_msgs = counts[m->severity];
417 if (m->severity == MSG_S_WARNING)
418 n_msgs += counts[MSG_S_ERROR];
419 if (n_msgs > max_msgs)
421 if (m->severity == MSG_S_NOTE)
423 too_many_notes = true;
424 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
425 "Suppressing further notes."),
430 too_many_errors = true;
431 if (m->severity == MSG_S_WARNING)
432 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d). Syntax processing will be halted."),
435 submit_note (xasprintf (_("Errors (%d) exceed limit (%d). Syntax processing will be halted."),
442 /* Emits M as an error message. Takes ownership of M. */
444 msg_emit (struct msg *m)
446 if (!messages_disabled)
451 /* Disables message output until the next call to msg_enable. If
452 this function is called multiple times, msg_enable must be
453 called an equal number of times before messages are actually
461 /* Enables message output that was disabled by msg_disable. */
465 assert (messages_disabled > 0);
469 /* Private functions. */
471 static char fatal_error_message[1024];
472 static int fatal_error_message_bytes = 0;
474 static char diagnostic_information[1024];
475 static int diagnostic_information_bytes = 0;
478 append_message (char *msg, int bytes_used, const char *fmt, ...)
482 int ret = vsnprintf (msg + bytes_used, 1024 - bytes_used, fmt, va);
490 /* Generate a row of asterisks held in statically allocated memory */
491 static struct substring
492 generate_banner (void)
494 static struct substring banner;
496 banner = ss_cstr ("******************************************************\n");
501 prepare_fatal_error_message (void)
503 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, generate_banner ().string);
505 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "You have discovered a bug in PSPP. Please report this\n");
506 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
507 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "message, *plus* several lines of output just above it.\n");
508 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "For the best chance at having the bug fixed, also\n");
509 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "include the syntax file that triggered it and a sample\n");
510 fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "of any data file used for input.\n");
511 return fatal_error_message;
515 prepare_diagnostic_information (void)
517 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "version: %s\n", version);
518 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "host_system: %s\n", host_system);
519 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "build_system: %s\n", build_system);
520 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "locale_dir: %s\n", relocate (locale_dir));
521 diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "compiler version: %s\n",
529 return diagnostic_information;
533 request_bug_report (const char *msg)
535 write (STDERR_FILENO, fatal_error_message, fatal_error_message_bytes);
536 write (STDERR_FILENO, "proximate cause: ", 21);
537 write (STDERR_FILENO, msg, strlen (msg));
538 write (STDERR_FILENO, "\n", 1);
539 write (STDERR_FILENO, diagnostic_information, diagnostic_information_bytes);
540 const struct substring banner = generate_banner ();
541 write (STDERR_FILENO, banner.string, banner.length);