e40b34c93c9a7b959ca03e06793b3d763f8f20bd
[pspp] / src / libpspp / message.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010,
3    2011, 2013 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18 #include <config.h>
19
20 #include "libpspp/message.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/cast.h"
30 #include "libpspp/str.h"
31 #include "libpspp/version.h"
32 #include "data/settings.h"
33
34 #include "gl/minmax.h"
35 #include "gl/progname.h"
36 #include "gl/relocatable.h"
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 /* Message handler as set by msg_set_handler(). */
44 static void (*msg_handler)  (const struct msg *, void *aux);
45 static void *msg_aux;
46
47 /* Disables emitting messages if positive. */
48 static int messages_disabled;
49
50 /* Public functions. */
51
52
53 void
54 vmsg (enum msg_class class, const char *format, va_list args)
55 {
56   struct msg m = {
57     .category = msg_class_to_category (class),
58     .severity = msg_class_to_severity (class),
59     .text = xvasprintf (format, args),
60   };
61
62   msg_emit (&m);
63 }
64
65 /* Writes error message in CLASS, with text FORMAT, formatted with
66    printf, to the standard places. */
67 void
68 msg (enum msg_class class, const char *format, ...)
69 {
70   va_list args;
71   va_start (args, format);
72   vmsg (class, format, args);
73   va_end (args);
74 }
75
76
77
78 void
79 msg_error (int errnum, const char *format, ...)
80 {
81   va_list args;
82   va_start (args, format);
83   char *e = xvasprintf (format, args);
84   va_end (args);
85
86   struct msg m = {
87     .category = MSG_C_GENERAL,
88     .severity = MSG_S_ERROR,
89     .file_name = NULL,
90     .text = xasprintf (_("%s: %s"), e, strerror (errnum)),
91   };
92   msg_emit (&m);
93
94   free (e);
95 }
96
97
98
99 void
100 msg_set_handler (void (*handler) (const struct msg *, void *aux), void *aux)
101 {
102   msg_handler = handler;
103   msg_aux = aux;
104 }
105 \f
106 /* Working with messages. */
107
108 const char *
109 msg_severity_to_string (enum msg_severity severity)
110 {
111   switch (severity)
112     {
113     case MSG_S_ERROR:
114       return _("error");
115     case MSG_S_WARNING:
116       return _("warning");
117     case MSG_S_NOTE:
118     default:
119       return _("note");
120     }
121 }
122
123 /* Duplicate a message */
124 struct msg *
125 msg_dup (const struct msg *m)
126 {
127   struct msg *new_msg;
128
129   new_msg = xmemdup (m, sizeof *m);
130   if (m->file_name != NULL)
131     new_msg->file_name = xstrdup (m->file_name);
132   if (m->command_name != NULL)
133     new_msg->command_name = xstrdup (m->command_name);
134   new_msg->text = xstrdup (m->text);
135
136   return new_msg;
137 }
138
139 /* Frees a message created by msg_dup().
140
141    (Messages not created by msg_dup(), as well as their file_name
142    members, are typically not dynamically allocated, so this function should
143    not be used to destroy them.) */
144 void
145 msg_destroy (struct msg *m)
146 {
147   free (m->file_name);
148   free (m->text);
149   free (m->command_name);
150   free (m);
151 }
152
153 char *
154 msg_to_string (const struct msg *m)
155 {
156   struct string s;
157
158   ds_init_empty (&s);
159
160   if (m->category != MSG_C_GENERAL
161       && (m->file_name || m->first_line > 0 || m->first_column > 0))
162     {
163       int l1 = m->first_line;
164       int l2 = MAX (m->first_line, m->last_line - 1);
165       int c1 = m->first_column;
166       int c2 = MAX (m->first_column, m->last_column - 1);
167
168       if (m->file_name)
169         ds_put_format (&s, "%s", m->file_name);
170
171       if (l1 > 0)
172         {
173           if (!ds_is_empty (&s))
174             ds_put_byte (&s, ':');
175
176           if (l2 > l1)
177             {
178               if (c1 > 0)
179                 ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l2, c2);
180               else
181                 ds_put_format (&s, "%d-%d", l1, l2);
182             }
183           else
184             {
185               if (c1 > 0)
186                 {
187                   if (c2 > c1)
188                     {
189                       /* The GNU coding standards say to use
190                          LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
191                          Emacs interprets COLUMN-2 as LINENO-2 if I do that.
192                          I've submitted an Emacs bug report:
193                          http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
194
195                          For now, let's be compatible. */
196                       ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l1, c2);
197                     }
198                   else
199                     ds_put_format (&s, "%d.%d", l1, c1);
200                 }
201               else
202                 ds_put_format (&s, "%d", l1);
203             }
204         }
205       else if (c1 > 0)
206         {
207           if (c2 > c1)
208             ds_put_format (&s, ".%d-%d", c1, c2);
209           else
210             ds_put_format (&s, ".%d", c1);
211         }
212       ds_put_cstr (&s, ": ");
213     }
214
215   ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
216
217   if (m->category == MSG_C_SYNTAX && m->command_name != NULL)
218     ds_put_format (&s, "%s: ", m->command_name);
219
220   ds_put_cstr (&s, m->text);
221
222   return ds_cstr (&s);
223 }
224 \f
225
226 /* Number of messages reported, by severity level. */
227 static int counts[MSG_N_SEVERITIES];
228
229 /* True after the maximum number of errors or warnings has been exceeded. */
230 static bool too_many_errors;
231
232 /* True after the maximum number of notes has been exceeded. */
233 static bool too_many_notes;
234
235 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
236 static bool warnings_off = false;
237
238 /* Checks whether we've had so many errors that it's time to quit
239    processing this syntax file. */
240 bool
241 msg_ui_too_many_errors (void)
242 {
243   return too_many_errors;
244 }
245
246 void
247 msg_ui_disable_warnings (bool x)
248 {
249   warnings_off = x;
250 }
251
252
253 void
254 msg_ui_reset_counts (void)
255 {
256   int i;
257
258   for (i = 0; i < MSG_N_SEVERITIES; i++)
259     counts[i] = 0;
260   too_many_errors = false;
261   too_many_notes = false;
262 }
263
264 bool
265 msg_ui_any_errors (void)
266 {
267   return counts[MSG_S_ERROR] > 0;
268 }
269
270
271 static void
272 ship_message (struct msg *m)
273 {
274   enum { MAX_STACK = 4 };
275   static struct msg *stack[MAX_STACK];
276   static size_t n;
277
278   /* If we're recursing on a given message, or recursing deeply, drop it. */
279   if (n >= MAX_STACK)
280     return;
281   for (size_t i = 0; i < n; i++)
282     if (stack[i] == m)
283       return;
284
285   stack[n++] = m;
286   if (msg_handler && n <= 1)
287     msg_handler (m, msg_aux);
288   else
289     fprintf (stderr, "%s\n", m->text);
290   n--;
291 }
292
293 static void
294 submit_note (char *s)
295 {
296   struct msg m = {
297     .category = MSG_C_GENERAL,
298     .severity = MSG_S_NOTE,
299     .text = s,
300   };
301   ship_message (&m);
302
303   free (s);
304 }
305
306
307
308 static void
309 process_msg (struct msg *m)
310 {
311   int n_msgs, max_msgs;
312
313   if (too_many_errors
314       || (too_many_notes && m->severity == MSG_S_NOTE)
315       || (warnings_off && m->severity == MSG_S_WARNING))
316     return;
317
318   ship_message (m);
319
320   counts[m->severity]++;
321   max_msgs = settings_get_max_messages (m->severity);
322   n_msgs = counts[m->severity];
323   if (m->severity == MSG_S_WARNING)
324     n_msgs += counts[MSG_S_ERROR];
325   if (n_msgs > max_msgs)
326     {
327       if (m->severity == MSG_S_NOTE)
328         {
329           too_many_notes = true;
330           submit_note (xasprintf (_("Notes (%d) exceed limit (%d).  "
331                                     "Suppressing further notes."),
332                                   n_msgs, max_msgs));
333         }
334       else
335         {
336           too_many_errors = true;
337           if (m->severity == MSG_S_WARNING)
338             submit_note (xasprintf (_("Warnings (%d) exceed limit (%d).  Syntax processing will be halted."),
339                                     n_msgs, max_msgs));
340           else
341             submit_note (xasprintf (_("Errors (%d) exceed limit (%d).  Syntax processing will be halted."),
342                                     n_msgs, max_msgs));
343         }
344     }
345 }
346
347
348 /* Emits M as an error message.
349    Frees allocated data in M. */
350 void
351 msg_emit (struct msg *m)
352 {
353   if (!messages_disabled)
354      process_msg (m);
355
356   free (m->text);
357   free (m->command_name);
358 }
359
360 /* Disables message output until the next call to msg_enable.  If
361    this function is called multiple times, msg_enable must be
362    called an equal number of times before messages are actually
363    re-enabled. */
364 void
365 msg_disable (void)
366 {
367   messages_disabled++;
368 }
369
370 /* Enables message output that was disabled by msg_disable. */
371 void
372 msg_enable (void)
373 {
374   assert (messages_disabled > 0);
375   messages_disabled--;
376 }
377 \f
378 /* Private functions. */
379
380 static char fatal_error_message[1024];
381 static int fatal_error_message_bytes = 0;
382
383 static char diagnostic_information[1024];
384 static int diagnostic_information_bytes = 0;
385
386 static int
387 append_message (char *msg, int bytes_used, const char *fmt, ...)
388 {
389   va_list va;
390   va_start (va, fmt);
391   int ret = vsnprintf (msg + bytes_used, 1024 - bytes_used, fmt, va);
392   va_end (va);
393   assert (ret >= 0);
394
395   return ret;
396 }
397
398
399 /* Generate a row of asterisks held in statically allocated memory  */
400 static struct substring
401 generate_banner (void)
402 {
403   static struct substring banner;
404   if (!banner.string)
405     banner = ss_cstr ("******************************************************\n");
406   return banner;
407 }
408
409 const char *
410 prepare_fatal_error_message (void)
411 {
412   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, generate_banner ().string);
413
414   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "You have discovered a bug in PSPP.  Please report this\n");
415   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "to " PACKAGE_BUGREPORT ".  Please include this entire\n");
416   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "message, *plus* several lines of output just above it.\n");
417   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "For the best chance at having the bug fixed, also\n");
418   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "include the syntax file that triggered it and a sample\n");
419   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "of any data file used for input.\n");
420   return fatal_error_message;
421 }
422
423 const char *
424 prepare_diagnostic_information (void)
425 {
426   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "version:             %s\n", version);
427   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "host_system:         %s\n", host_system);
428   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "build_system:        %s\n", build_system);
429   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "locale_dir:          %s\n", relocate (locale_dir));
430   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "compiler version:    %s\n",
431 #ifdef __VERSION__
432            __VERSION__
433 #else
434            "Unknown"
435 #endif
436 );
437
438   return diagnostic_information;
439 }
440
441 void
442 request_bug_report (const char *msg)
443 {
444   write (STDERR_FILENO, fatal_error_message, fatal_error_message_bytes);
445   write (STDERR_FILENO, "proximate cause:     ", 21);
446   write (STDERR_FILENO, msg, strlen (msg));
447   write (STDERR_FILENO, "\n", 1);
448   write (STDERR_FILENO, diagnostic_information, diagnostic_information_bytes);
449   const struct substring banner = generate_banner ();
450   write (STDERR_FILENO, banner.string, banner.length);
451 }