SET MXWARNS = 0 to be interpreted as unlimited.
[pspp] / src / libpspp / message.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "message.h"
20 #include "msg-locator.h"
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include <libpspp/str.h>
30 #include <libpspp/version.h>
31 #include <data/settings.h>
32
33 #include "gl/progname.h"
34 #include "gl/xalloc.h"
35 #include "gl/xvasprintf.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* Message handler as set by msg_init(). */
41 static void (*msg_handler)  (const struct msg *);
42
43 /* Disables emitting messages if positive. */
44 static int messages_disabled;
45
46 /* Public functions. */
47
48 /* Writes error message in CLASS, with text FORMAT, formatted with
49    printf, to the standard places. */
50 void
51 msg (enum msg_class class, const char *format, ...)
52 {
53   struct msg m;
54   va_list args;
55
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);
60   va_end (args);
61
62   msg_emit (&m);
63 }
64
65 static struct source_stream *s_stream;
66
67 void
68 msg_init (struct source_stream *ss,  void (*handler) (const struct msg *) )
69 {
70   s_stream = ss;
71   msg_handler = handler;
72 }
73
74 void
75 msg_done (void)
76 {
77 }
78 \f
79 /* Working with messages. */
80
81 /* Duplicate a message */
82 struct msg *
83 msg_dup (const struct msg *m)
84 {
85   struct msg *new_msg;
86
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);
91
92   return new_msg;
93 }
94
95 /* Frees a message created by msg_dup().
96
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.) */
100 void
101 msg_destroy (struct msg *m)
102 {
103   free (m->where.file_name);
104   free (m->text);
105   free (m);
106 }
107
108 char *
109 msg_to_string (const struct msg *m, const char *command_name)
110 {
111   const char *label;
112   struct string s;
113
114   ds_init_empty (&s);
115
116   if (m->category != MSG_C_GENERAL
117       && (m->where.file_name || m->where.line_number != -1))
118     {
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, ' ');
124     }
125
126   switch (m->severity)
127     {
128     case MSG_S_ERROR:
129       label = _("error");
130       break;
131     case MSG_S_WARNING:
132       label = _("warning");
133       break;
134     case MSG_S_NOTE:
135     default:
136       label = _("note");
137       break;
138     }
139   ds_put_format (&s, "%s: ", label);
140
141   if (m->category == MSG_C_SYNTAX && command_name != NULL)
142     ds_put_format (&s, "%s: ", command_name);
143
144   ds_put_cstr (&s, m->text);
145
146   return ds_cstr (&s);
147 }
148 \f
149
150 /* Number of messages reported, by severity level. */
151 static int counts[MSG_N_SEVERITIES];
152
153 /* True after the maximum number of errors or warnings has been exceeded. */
154 static bool too_many_errors;
155
156 /* True after the maximum number of notes has been exceeded. */
157 static bool too_many_notes;
158
159 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
160 static bool warnings_off = false;
161
162 /* Checks whether we've had so many errors that it's time to quit
163    processing this syntax file. */
164 bool
165 msg_ui_too_many_errors (void)
166 {
167   return too_many_errors;
168 }
169
170 void
171 msg_ui_disable_warnings (bool x)
172 {
173   warnings_off = x;
174 }
175
176
177 void
178 msg_ui_reset_counts (void)
179 {
180   int i;
181
182   for (i = 0; i < MSG_N_SEVERITIES; i++)
183     counts[i] = 0;
184   too_many_errors = false;
185   too_many_notes = false;
186 }
187
188 bool
189 msg_ui_any_errors (void)
190 {
191   return counts[MSG_S_ERROR] > 0;
192 }
193
194 static void
195 submit_note (char *s)
196 {
197   struct msg m;
198
199   m.category = MSG_C_GENERAL;
200   m.severity = MSG_S_NOTE;
201   m.where.file_name = NULL;
202   m.where.line_number = -1;
203   m.text = s;
204   msg_handler (&m);
205   free (s);
206 }
207
208
209
210 static void
211 process_msg (const struct msg *m)
212 {
213   int n_msgs, max_msgs;
214
215
216   if (too_many_errors
217       || (too_many_notes && m->severity == MSG_S_NOTE)
218       || (warnings_off && m->severity == MSG_S_WARNING) )
219     return;
220
221   msg_handler (m);
222
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)
229     {
230       if (m->severity == MSG_S_NOTE)
231         {
232           too_many_notes = true;
233           submit_note (xasprintf (_("Notes (%d) exceed limit (%d).  "
234                                     "Suppressing further notes."),
235                                   n_msgs, max_msgs));
236         }
237       else
238         {
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."),
242                                     n_msgs, max_msgs));
243           else
244             submit_note (xasprintf (_("Errors (%d) exceed limit (%d).  Syntax processing will be halted."),
245                                     n_msgs, max_msgs));
246         }
247     }
248 }
249
250
251 /* Emits M as an error message.
252    Frees allocated data in M. */
253 void
254 msg_emit (struct msg *m)
255 {
256   if ( s_stream )
257     get_msg_location (s_stream, &m->where);
258   else
259     {
260       m->where.file_name = NULL;
261       m->where.line_number = -1;
262     }
263
264   if (!messages_disabled)
265      process_msg (m);
266
267   free (m->text);
268 }
269
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
273    re-enabled. */
274 void
275 msg_disable (void)
276 {
277   messages_disabled++;
278 }
279
280 /* Enables message output that was disabled by msg_disable. */
281 void
282 msg_enable (void)
283 {
284   assert (messages_disabled > 0);
285   messages_disabled--;
286 }
287 \f
288 /* Private functions. */
289
290 void
291 request_bug_report_and_abort (const char *msg)
292 {
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",
306 #ifdef __VERSION__
307            __VERSION__
308 #else
309            "Unknown"
310 #endif
311            );
312   fprintf (stderr, "******************************************************\n");
313
314   _exit (EXIT_FAILURE);
315 }
316