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/xalloc.h"
37 #include "gl/xvasprintf.h"
40 #define _(msgid) gettext (msgid)
42 /* Message handler as set by msg_set_handler(). */
43 static void (*msg_handler) (const struct msg *, void *aux);
46 /* Disables emitting messages if positive. */
47 static int messages_disabled;
49 /* Public functions. */
53 vmsg (enum msg_class class, const char *format, va_list args)
56 .category = msg_class_to_category (class),
57 .severity = msg_class_to_severity (class),
58 .text = xvasprintf (format, args),
64 /* Writes error message in CLASS, with text FORMAT, formatted with
65 printf, to the standard places. */
67 msg (enum msg_class class, const char *format, ...)
70 va_start (args, format);
71 vmsg (class, format, args);
78 msg_error (int errnum, const char *format, ...)
81 va_start (args, format);
82 char *e = xvasprintf (format, args);
86 .category = MSG_C_GENERAL,
87 .severity = MSG_S_ERROR,
89 .text = xasprintf (_("%s: %s"), e, strerror (errnum)),
99 msg_set_handler (void (*handler) (const struct msg *, void *aux), void *aux)
101 msg_handler = handler;
105 /* Working with messages. */
108 msg_severity_to_string (enum msg_severity severity)
122 /* Duplicate a message */
124 msg_dup (const struct msg *m)
128 new_msg = xmemdup (m, sizeof *m);
129 if (m->file_name != NULL)
130 new_msg->file_name = xstrdup (m->file_name);
131 new_msg->text = xstrdup (m->text);
136 /* Frees a message created by msg_dup().
138 (Messages not created by msg_dup(), as well as their file_name
139 members, are typically not dynamically allocated, so this function should
140 not be used to destroy them.) */
142 msg_destroy (struct msg *m)
150 msg_to_string (const struct msg *m, const char *command_name)
156 if (m->category != MSG_C_GENERAL
157 && (m->file_name || m->first_line > 0 || m->first_column > 0))
159 int l1 = m->first_line;
160 int l2 = MAX (m->first_line, m->last_line - 1);
161 int c1 = m->first_column;
162 int c2 = MAX (m->first_column, m->last_column - 1);
165 ds_put_format (&s, "%s", m->file_name);
169 if (!ds_is_empty (&s))
170 ds_put_byte (&s, ':');
175 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l2, c2);
177 ds_put_format (&s, "%d-%d", l1, l2);
185 /* The GNU coding standards say to use
186 LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
187 Emacs interprets COLUMN-2 as LINENO-2 if I do that.
188 I've submitted an Emacs bug report:
189 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
191 For now, let's be compatible. */
192 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l1, c2);
195 ds_put_format (&s, "%d.%d", l1, c1);
198 ds_put_format (&s, "%d", l1);
204 ds_put_format (&s, ".%d-%d", c1, c2);
206 ds_put_format (&s, ".%d", c1);
208 ds_put_cstr (&s, ": ");
211 ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
213 if (m->category == MSG_C_SYNTAX && command_name != NULL)
214 ds_put_format (&s, "%s: ", command_name);
216 ds_put_cstr (&s, m->text);
222 /* Number of messages reported, by severity level. */
223 static int counts[MSG_N_SEVERITIES];
225 /* True after the maximum number of errors or warnings has been exceeded. */
226 static bool too_many_errors;
228 /* True after the maximum number of notes has been exceeded. */
229 static bool too_many_notes;
231 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
232 static bool warnings_off = false;
234 /* Checks whether we've had so many errors that it's time to quit
235 processing this syntax file. */
237 msg_ui_too_many_errors (void)
239 return too_many_errors;
243 msg_ui_disable_warnings (bool x)
250 msg_ui_reset_counts (void)
254 for (i = 0; i < MSG_N_SEVERITIES; i++)
256 too_many_errors = false;
257 too_many_notes = false;
261 msg_ui_any_errors (void)
263 return counts[MSG_S_ERROR] > 0;
267 static int entrances = 0;
270 ship_message (struct msg *m)
275 if (msg_handler && entrances <= 1)
276 msg_handler (m, msg_aux);
279 fwrite (m->text, 1, strlen (m->text), stderr);
280 fwrite ("\n", 1, 1, stderr);
288 submit_note (char *s)
291 .category = MSG_C_GENERAL,
292 .severity = MSG_S_NOTE,
303 process_msg (struct msg *m)
305 int n_msgs, max_msgs;
308 || (too_many_notes && m->severity == MSG_S_NOTE)
309 || (warnings_off && m->severity == MSG_S_WARNING) )
314 counts[m->severity]++;
315 max_msgs = settings_get_max_messages (m->severity);
316 n_msgs = counts[m->severity];
317 if (m->severity == MSG_S_WARNING)
318 n_msgs += counts[MSG_S_ERROR];
319 if (n_msgs > max_msgs)
321 if (m->severity == MSG_S_NOTE)
323 too_many_notes = true;
324 submit_note (xasprintf (_("Notes (%d) exceed limit (%d). "
325 "Suppressing further notes."),
330 too_many_errors = true;
331 if (m->severity == MSG_S_WARNING)
332 submit_note (xasprintf (_("Warnings (%d) exceed limit (%d). Syntax processing will be halted."),
335 submit_note (xasprintf (_("Errors (%d) exceed limit (%d). Syntax processing will be halted."),
342 /* Emits M as an error message.
343 Frees allocated data in M. */
345 msg_emit (struct msg *m)
348 if (!messages_disabled)
354 /* Disables message output until the next call to msg_enable. If
355 this function is called multiple times, msg_enable must be
356 called an equal number of times before messages are actually
364 /* Enables message output that was disabled by msg_disable. */
368 assert (messages_disabled > 0);
372 /* Private functions. */
375 request_bug_report (const char *msg)
377 fprintf (stderr, "******************************************************\n");
378 fprintf (stderr, "You have discovered a bug in PSPP. Please report this\n");
379 fprintf (stderr, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
380 fprintf (stderr, "message, *plus* several lines of output just above it.\n");
381 fprintf (stderr, "For the best chance at having the bug fixed, also\n");
382 fprintf (stderr, "include the syntax file that triggered it and a sample\n");
383 fprintf (stderr, "of any data file used for input.\n");
384 fprintf (stderr, "proximate cause: %s\n", msg);
385 fprintf (stderr, "version: %s\n", version);
386 fprintf (stderr, "host_system: %s\n", host_system);
387 fprintf (stderr, "build_system: %s\n", build_system);
388 fprintf (stderr, "locale_dir: %s\n", locale_dir);
389 fprintf (stderr, "compiler version: %s\n",
396 fprintf (stderr, "******************************************************\n");