1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010 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/>. */
20 #include "msg-locator.h"
29 #include <libpspp/str.h>
30 #include <libpspp/version.h>
31 #include <data/settings.h>
33 #include "gl/progname.h"
34 #include "gl/xalloc.h"
35 #include "gl/xvasprintf.h"
38 #define _(msgid) gettext (msgid)
40 /* Message handler as set by msg_init(). */
41 static void (*msg_handler) (const struct msg *);
43 /* Disables emitting messages if positive. */
44 static int messages_disabled;
46 /* Public functions. */
48 /* Writes error message in CLASS, with text FORMAT, formatted with
49 printf, to the standard places. */
51 msg (enum msg_class class, const char *format, ...)
56 m.category = msg_class_to_category (class);
57 m.severity = msg_class_to_severity (class);
58 va_start (args, format);
59 m.text = xvasprintf (format, args);
65 static struct source_stream *s_stream;
68 msg_init (struct source_stream *ss, void (*handler) (const struct msg *) )
71 msg_handler = handler;
79 /* Working with messages. */
81 /* Duplicate a message */
83 msg_dup (const struct msg *m)
87 new_msg = xmemdup (m, sizeof *m);
88 if (m->where.file_name != NULL)
89 new_msg->where.file_name = xstrdup (m->where.file_name);
90 new_msg->text = xstrdup (m->text);
95 /* Frees a message created by msg_dup().
97 (Messages not created by msg_dup(), as well as their where.file_name
98 members, are typically not dynamically allocated, so this function should
99 not be used to destroy them.) */
101 msg_destroy (struct msg *m)
103 free (m->where.file_name);
109 msg_to_string (const struct msg *m, const char *command_name)
116 if (m->category != MSG_C_GENERAL
117 && (m->where.file_name || m->where.line_number != -1))
119 if (m->where.file_name)
120 ds_put_format (&s, "%s:", m->where.file_name);
121 if (m->where.line_number != -1)
122 ds_put_format (&s, "%d:", m->where.line_number);
123 ds_put_char (&s, ' ');
132 label = _("warning");
139 ds_put_format (&s, "%s: ", label);
141 if (m->category == MSG_C_SYNTAX && command_name != NULL)
142 ds_put_format (&s, "%s: ", command_name);
144 ds_put_cstr (&s, m->text);
150 /* Number of messages reported, by severity level. */
151 static int counts[MSG_N_SEVERITIES];
153 /* True after the maximum number of errors or warnings has been exceeded. */
154 static bool too_many_errors;
156 /* True after the maximum number of notes has been exceeded. */
157 static bool too_many_notes;
159 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
160 static bool warnings_off = false;
162 /* Checks whether we've had so many errors that it's time to quit
163 processing this syntax file. */
165 msg_ui_too_many_errors (void)
167 return too_many_errors;
171 msg_ui_disable_warnings (bool x)
178 msg_ui_reset_counts (void)
182 for (i = 0; i < MSG_N_SEVERITIES; i++)
184 too_many_errors = false;
185 too_many_notes = false;
189 msg_ui_any_errors (void)
191 return counts[MSG_S_ERROR] > 0;
195 submit_note (char *s)
199 m.category = MSG_C_GENERAL;
200 m.severity = MSG_S_NOTE;
201 m.where.file_name = NULL;
202 m.where.line_number = -1;
211 process_msg (const struct msg *m)
213 int n_msgs, max_msgs;
217 || (too_many_notes && m->severity == MSG_S_NOTE)
218 || (warnings_off && m->severity == MSG_S_WARNING) )
223 counts[m->severity]++;
224 max_msgs = settings_get_max_messages (m->severity);
225 n_msgs = counts[m->severity];
226 if (m->severity == MSG_S_WARNING)
227 n_msgs += counts[MSG_S_ERROR];
228 if (n_msgs > max_msgs)
230 if (m->severity == MSG_S_NOTE)
232 too_many_notes = true;
233 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
234 "Suppressing further notes."),
239 too_many_errors = true;
240 if (m->severity == MSG_S_WARNING)
241 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d). Syntax processing will be halted."),
244 submit_note (xasprintf (_("Errors (%d) exceed limit (%d). Syntax processing will be halted."),
251 /* Emits M as an error message.
252 Frees allocated data in M. */
254 msg_emit (struct msg *m)
257 get_msg_location (s_stream, &m->where);
260 m->where.file_name = NULL;
261 m->where.line_number = -1;
264 if (!messages_disabled)
270 /* Disables message output until the next call to msg_enable. If
271 this function is called multiple times, msg_enable must be
272 called an equal number of times before messages are actually
280 /* Enables message output that was disabled by msg_disable. */
284 assert (messages_disabled > 0);
288 /* Private functions. */
291 request_bug_report_and_abort (const char *msg)
293 fprintf (stderr, "******************************************************\n");
294 fprintf (stderr, "You have discovered a bug in PSPP. Please report this\n");
295 fprintf (stderr, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
296 fprintf (stderr, "message, *plus* several lines of output just above it.\n");
297 fprintf (stderr, "For the best chance at having the bug fixed, also\n");
298 fprintf (stderr, "include the syntax file that triggered it and a sample\n");
299 fprintf (stderr, "of any data file used for input.\n");
300 fprintf (stderr, "proximate cause: %s\n", msg);
301 fprintf (stderr, "version: %s\n", stat_version);
302 fprintf (stderr, "host_system: %s\n", host_system);
303 fprintf (stderr, "build_system: %s\n", build_system);
304 fprintf (stderr, "locale_dir: %s\n", locale_dir);
305 fprintf (stderr, "compiler version: %s\n",
312 fprintf (stderr, "******************************************************\n");
314 _exit (EXIT_FAILURE);