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. */
79 /* Duplicate a message */
81 msg_dup (const struct msg *m)
85 new_msg = xmemdup (m, sizeof *m);
86 if (m->file_name != NULL)
87 new_msg->file_name = xstrdup (m->file_name);
88 new_msg->text = xstrdup (m->text);
93 /* Frees a message created by msg_dup().
95 (Messages not created by msg_dup(), as well as their file_name
96 members, are typically not dynamically allocated, so this function should
97 not be used to destroy them.) */
99 msg_destroy (struct msg *m)
107 msg_to_string (const struct msg *m, const char *command_name)
114 if (m->category != MSG_C_GENERAL
115 && (m->file_name || m->first_line > 0 || m->first_column > 0))
117 int l1 = m->first_line;
118 int l2 = MAX (m->first_line, m->last_line - 1);
119 int c1 = m->first_column;
120 int c2 = MAX (m->first_column, m->last_column - 1);
123 ds_put_format (&s, "%s", m->file_name);
127 if (!ds_is_empty (&s))
128 ds_put_byte (&s, ':');
133 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l2, c2);
135 ds_put_format (&s, "%d-%d", l1, l2);
143 /* The GNU coding standards say to use
144 LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
145 Emacs interprets COLUMN-2 as LINENO-2 if I do that.
146 I've submitted an Emacs bug report:
147 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
149 For now, let's be compatible. */
150 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l1, c2);
153 ds_put_format (&s, "%d.%d", l1, c1);
156 ds_put_format (&s, "%d", l1);
162 ds_put_format (&s, ".%d-%d", c1, c2);
164 ds_put_format (&s, ".%d", c1);
166 ds_put_cstr (&s, ": ");
175 label = _("warning");
182 ds_put_format (&s, "%s: ", label);
184 if (m->category == MSG_C_SYNTAX && command_name != NULL)
185 ds_put_format (&s, "%s: ", command_name);
187 ds_put_cstr (&s, m->text);
193 /* Number of messages reported, by severity level. */
194 static int counts[MSG_N_SEVERITIES];
196 /* True after the maximum number of errors or warnings has been exceeded. */
197 static bool too_many_errors;
199 /* True after the maximum number of notes has been exceeded. */
200 static bool too_many_notes;
202 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
203 static bool warnings_off = false;
205 /* Checks whether we've had so many errors that it's time to quit
206 processing this syntax file. */
208 msg_ui_too_many_errors (void)
210 return too_many_errors;
214 msg_ui_disable_warnings (bool x)
221 msg_ui_reset_counts (void)
225 for (i = 0; i < MSG_N_SEVERITIES; i++)
227 too_many_errors = false;
228 too_many_notes = false;
232 msg_ui_any_errors (void)
234 return counts[MSG_S_ERROR] > 0;
238 submit_note (char *s)
242 m.category = MSG_C_GENERAL;
243 m.severity = MSG_S_NOTE;
250 msg_handler (&m, msg_aux);
257 process_msg (const struct msg *m)
259 int n_msgs, max_msgs;
263 || (too_many_notes && m->severity == MSG_S_NOTE)
264 || (warnings_off && m->severity == MSG_S_WARNING) )
267 msg_handler (m, msg_aux);
269 counts[m->severity]++;
270 max_msgs = settings_get_max_messages (m->severity);
271 n_msgs = counts[m->severity];
272 if (m->severity == MSG_S_WARNING)
273 n_msgs += counts[MSG_S_ERROR];
274 if (n_msgs > max_msgs)
276 if (m->severity == MSG_S_NOTE)
278 too_many_notes = true;
279 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
280 "Suppressing further notes."),
285 too_many_errors = true;
286 if (m->severity == MSG_S_WARNING)
287 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d). Syntax processing will be halted."),
290 submit_note (xasprintf (_("Errors (%d) exceed limit (%d). Syntax processing will be halted."),
297 /* Emits M as an error message.
298 Frees allocated data in M. */
300 msg_emit (struct msg *m)
302 if (!messages_disabled)
308 /* Disables message output until the next call to msg_enable. If
309 this function is called multiple times, msg_enable must be
310 called an equal number of times before messages are actually
318 /* Enables message output that was disabled by msg_disable. */
322 assert (messages_disabled > 0);
326 /* Private functions. */
329 request_bug_report (const char *msg)
331 fprintf (stderr, "******************************************************\n");
332 fprintf (stderr, "You have discovered a bug in PSPP. Please report this\n");
333 fprintf (stderr, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
334 fprintf (stderr, "message, *plus* several lines of output just above it.\n");
335 fprintf (stderr, "For the best chance at having the bug fixed, also\n");
336 fprintf (stderr, "include the syntax file that triggered it and a sample\n");
337 fprintf (stderr, "of any data file used for input.\n");
338 fprintf (stderr, "proximate cause: %s\n", msg);
339 fprintf (stderr, "version: %s\n", stat_version);
340 fprintf (stderr, "host_system: %s\n", host_system);
341 fprintf (stderr, "build_system: %s\n", build_system);
342 fprintf (stderr, "locale_dir: %s\n", locale_dir);
343 fprintf (stderr, "compiler version: %s\n",
350 fprintf (stderr, "******************************************************\n");