1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "libpspp/message.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/str.h"
30 #include "libpspp/version.h"
31 #include "data/settings.h"
33 #include "gl/minmax.h"
34 #include "gl/progname.h"
35 #include "gl/xalloc.h"
36 #include "gl/xvasprintf.h"
39 #define _(msgid) gettext (msgid)
41 /* Message handler as set by msg_set_handler(). */
42 static void (*msg_handler) (const struct msg *, void *aux);
45 /* Disables emitting messages if positive. */
46 static int messages_disabled;
48 /* Public functions. */
50 /* Writes error message in CLASS, with text FORMAT, formatted with
51 printf, to the standard places. */
53 msg (enum msg_class class, const char *format, ...)
58 m.category = msg_class_to_category (class);
59 m.severity = msg_class_to_severity (class);
60 va_start (args, format);
61 m.text = xvasprintf (format, args);
63 m.first_line = m.last_line = 0;
64 m.first_column = m.last_column = 0;
71 msg_set_handler (void (*handler) (const struct msg *, void *aux), void *aux)
73 msg_handler = handler;
77 /* Working with messages. */
80 msg_severity_to_string (enum msg_severity severity)
94 /* Duplicate a message */
96 msg_dup (const struct msg *m)
100 new_msg = xmemdup (m, sizeof *m);
101 if (m->file_name != NULL)
102 new_msg->file_name = xstrdup (m->file_name);
103 new_msg->text = xstrdup (m->text);
108 /* Frees a message created by msg_dup().
110 (Messages not created by msg_dup(), as well as their file_name
111 members, are typically not dynamically allocated, so this function should
112 not be used to destroy them.) */
114 msg_destroy (struct msg *m)
122 msg_to_string (const struct msg *m, const char *command_name)
128 if (m->category != MSG_C_GENERAL
129 && (m->file_name || m->first_line > 0 || m->first_column > 0))
131 int l1 = m->first_line;
132 int l2 = MAX (m->first_line, m->last_line - 1);
133 int c1 = m->first_column;
134 int c2 = MAX (m->first_column, m->last_column - 1);
137 ds_put_format (&s, "%s", m->file_name);
141 if (!ds_is_empty (&s))
142 ds_put_byte (&s, ':');
147 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l2, c2);
149 ds_put_format (&s, "%d-%d", l1, l2);
157 /* The GNU coding standards say to use
158 LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
159 Emacs interprets COLUMN-2 as LINENO-2 if I do that.
160 I've submitted an Emacs bug report:
161 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
163 For now, let's be compatible. */
164 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l1, c2);
167 ds_put_format (&s, "%d.%d", l1, c1);
170 ds_put_format (&s, "%d", l1);
176 ds_put_format (&s, ".%d-%d", c1, c2);
178 ds_put_format (&s, ".%d", c1);
180 ds_put_cstr (&s, ": ");
183 ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
185 if (m->category == MSG_C_SYNTAX && command_name != NULL)
186 ds_put_format (&s, "%s: ", command_name);
188 ds_put_cstr (&s, m->text);
194 /* Number of messages reported, by severity level. */
195 static int counts[MSG_N_SEVERITIES];
197 /* True after the maximum number of errors or warnings has been exceeded. */
198 static bool too_many_errors;
200 /* True after the maximum number of notes has been exceeded. */
201 static bool too_many_notes;
203 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
204 static bool warnings_off = false;
206 /* Checks whether we've had so many errors that it's time to quit
207 processing this syntax file. */
209 msg_ui_too_many_errors (void)
211 return too_many_errors;
215 msg_ui_disable_warnings (bool x)
222 msg_ui_reset_counts (void)
226 for (i = 0; i < MSG_N_SEVERITIES; i++)
228 too_many_errors = false;
229 too_many_notes = false;
233 msg_ui_any_errors (void)
235 return counts[MSG_S_ERROR] > 0;
239 submit_note (char *s)
243 m.category = MSG_C_GENERAL;
244 m.severity = MSG_S_NOTE;
251 msg_handler (&m, msg_aux);
258 process_msg (const struct msg *m)
260 int n_msgs, max_msgs;
264 || (too_many_notes && m->severity == MSG_S_NOTE)
265 || (warnings_off && m->severity == MSG_S_WARNING) )
268 msg_handler (m, msg_aux);
270 counts[m->severity]++;
271 max_msgs = settings_get_max_messages (m->severity);
272 n_msgs = counts[m->severity];
273 if (m->severity == MSG_S_WARNING)
274 n_msgs += counts[MSG_S_ERROR];
275 if (n_msgs > max_msgs)
277 if (m->severity == MSG_S_NOTE)
279 too_many_notes = true;
280 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
281 "Suppressing further notes."),
286 too_many_errors = true;
287 if (m->severity == MSG_S_WARNING)
288 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d). Syntax processing will be halted."),
291 submit_note (xasprintf (_("Errors (%d) exceed limit (%d). Syntax processing will be halted."),
298 /* Emits M as an error message.
299 Frees allocated data in M. */
301 msg_emit (struct msg *m)
303 if (!messages_disabled)
309 /* Disables message output until the next call to msg_enable. If
310 this function is called multiple times, msg_enable must be
311 called an equal number of times before messages are actually
319 /* Enables message output that was disabled by msg_disable. */
323 assert (messages_disabled > 0);
327 /* Private functions. */
330 request_bug_report (const char *msg)
332 fprintf (stderr, "******************************************************\n");
333 fprintf (stderr, "You have discovered a bug in PSPP. Please report this\n");
334 fprintf (stderr, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
335 fprintf (stderr, "message, *plus* several lines of output just above it.\n");
336 fprintf (stderr, "For the best chance at having the bug fixed, also\n");
337 fprintf (stderr, "include the syntax file that triggered it and a sample\n");
338 fprintf (stderr, "of any data file used for input.\n");
339 fprintf (stderr, "proximate cause: %s\n", msg);
340 fprintf (stderr, "version: %s\n", stat_version);
341 fprintf (stderr, "host_system: %s\n", host_system);
342 fprintf (stderr, "build_system: %s\n", build_system);
343 fprintf (stderr, "locale_dir: %s\n", locale_dir);
344 fprintf (stderr, "compiler version: %s\n",
351 fprintf (stderr, "******************************************************\n");