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 /* Checks whether we've had so many errors that it's time to quit
160 processing this syntax file. */
162 msg_ui_too_many_errors (void)
164 return too_many_errors;
168 msg_ui_reset_counts (void)
172 for (i = 0; i < MSG_N_SEVERITIES; i++)
174 too_many_errors = false;
175 too_many_notes = false;
179 msg_ui_any_errors (void)
181 return counts[MSG_S_ERROR] > 0;
185 submit_note (char *s)
189 m.category = MSG_C_GENERAL;
190 m.severity = MSG_S_NOTE;
191 m.where.file_name = NULL;
192 m.where.line_number = -1;
199 process_msg (const struct msg *m)
201 int n_msgs, max_msgs;
203 if (too_many_errors || (too_many_notes && m->severity == MSG_S_NOTE))
208 counts[m->severity]++;
209 max_msgs = settings_get_max_messages (m->severity);
210 n_msgs = counts[m->severity];
211 if (m->severity == MSG_S_WARNING)
212 n_msgs += counts[MSG_S_ERROR];
213 if (n_msgs > max_msgs)
215 if (m->severity == MSG_S_NOTE)
217 too_many_notes = true;
218 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
219 "Suppressing further notes."),
224 too_many_errors = true;
225 if (m->severity == MSG_S_WARNING)
226 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d)."),
229 submit_note (xasprintf (_("Errors (%d) exceed limit (%d)."),
236 /* Emits M as an error message.
237 Frees allocated data in M. */
239 msg_emit (struct msg *m)
242 get_msg_location (s_stream, &m->where);
245 m->where.file_name = NULL;
246 m->where.line_number = -1;
249 if (!messages_disabled)
255 /* Disables message output until the next call to msg_enable. If
256 this function is called multiple times, msg_enable must be
257 called an equal number of times before messages are actually
265 /* Enables message output that was disabled by msg_disable. */
269 assert (messages_disabled > 0);
273 /* Private functions. */
276 request_bug_report_and_abort (const char *msg)
278 fprintf (stderr, "******************************************************\n");
279 fprintf (stderr, "You have discovered a bug in PSPP. Please report this\n");
280 fprintf (stderr, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
281 fprintf (stderr, "message, *plus* several lines of output just above it.\n");
282 fprintf (stderr, "For the best chance at having the bug fixed, also\n");
283 fprintf (stderr, "include the syntax file that triggered it and a sample\n");
284 fprintf (stderr, "of any data file used for input.\n");
285 fprintf (stderr, "proximate cause: %s\n", msg);
286 fprintf (stderr, "version: %s\n", stat_version);
287 fprintf (stderr, "host_system: %s\n", host_system);
288 fprintf (stderr, "build_system: %s\n", build_system);
289 fprintf (stderr, "locale_dir: %s\n", locale_dir);
290 fprintf (stderr, "compiler version: %s\n",
297 fprintf (stderr, "******************************************************\n");
299 _exit (EXIT_FAILURE);