7d4f9d8430a9ffdd8c008563586aaa6189fbdc50
[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     .text = xasprintf (_("%s: %s"), e, strerror (errnum)),
90   };
91   msg_emit (&m);
92
93   free (e);
94 }
95
96
97
98 void
99 msg_set_handler (void (*handler) (const struct msg *, void *aux), void *aux)
100 {
101   msg_handler = handler;
102   msg_aux = aux;
103 }
104 \f
105 /* msg_location. */
106
107 void
108 msg_location_destroy (struct msg_location *loc)
109 {
110   if (loc)
111     {
112       free (loc->file_name);
113       free (loc);
114     }
115 }
116
117 struct msg_location *
118 msg_location_dup (const struct msg_location *src)
119 {
120   if (!src)
121     return NULL;
122
123   struct msg_location *dst = xmalloc (sizeof *dst);
124   *dst = (struct msg_location) {
125     .file_name = xstrdup_if_nonnull (src->file_name),
126     .first_line = src->first_line,
127     .last_line = src->last_line,
128     .first_column = src->first_column,
129     .last_column = src->last_column,
130   };
131   return dst;
132 }
133
134 bool
135 msg_location_is_empty (const struct msg_location *loc)
136 {
137   return !loc || (!loc->file_name
138                   && loc->first_line <= 0
139                   && loc->first_column <= 0);
140 }
141
142 void
143 msg_location_format (const struct msg_location *loc, struct string *s)
144 {
145   if (!loc)
146     return;
147
148   if (loc->file_name)
149     ds_put_cstr (s, loc->file_name);
150
151   int l1 = loc->first_line;
152   int l2 = MAX (loc->first_line, loc->last_line - 1);
153   int c1 = loc->first_column;
154   int c2 = MAX (loc->first_column, loc->last_column - 1);
155
156   if (l1 > 0)
157     {
158       if (loc->file_name)
159         ds_put_byte (s, ':');
160
161       if (l2 > l1)
162         {
163           if (c1 > 0)
164             ds_put_format (s, "%d.%d-%d.%d", l1, c1, l2, c2);
165           else
166             ds_put_format (s, "%d-%d", l1, l2);
167         }
168       else
169         {
170           if (c1 > 0)
171             {
172               if (c2 > c1)
173                 {
174                   /* The GNU coding standards say to use
175                      LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
176                      Emacs interprets COLUMN-2 as LINENO-2 if I do that.
177                      I've submitted an Emacs bug report:
178                      http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
179
180                      For now, let's be compatible. */
181                   ds_put_format (s, "%d.%d-%d.%d", l1, c1, l1, c2);
182                 }
183               else
184                 ds_put_format (s, "%d.%d", l1, c1);
185             }
186           else
187             ds_put_format (s, "%d", l1);
188         }
189     }
190   else if (c1 > 0)
191     {
192       if (c2 > c1)
193         ds_put_format (s, ".%d-%d", c1, c2);
194       else
195         ds_put_format (s, ".%d", c1);
196     }
197 }
198 \f
199 /* Working with messages. */
200
201 const char *
202 msg_severity_to_string (enum msg_severity severity)
203 {
204   switch (severity)
205     {
206     case MSG_S_ERROR:
207       return _("error");
208     case MSG_S_WARNING:
209       return _("warning");
210     case MSG_S_NOTE:
211     default:
212       return _("note");
213     }
214 }
215
216 /* Duplicate a message */
217 struct msg *
218 msg_dup (const struct msg *src)
219 {
220   struct msg *dst = xmalloc (sizeof *dst);
221   *dst = (struct msg) {
222     .category = src->category,
223     .severity = src->severity,
224     .location = msg_location_dup (src->location),
225     .command_name = xstrdup_if_nonnull (src->command_name),
226     .text = xstrdup (src->text),
227   };
228   return dst;
229 }
230
231 /* Frees a message created by msg_dup().
232
233    (Messages not created by msg_dup(), as well as their file_name
234    members, are typically not dynamically allocated, so this function should
235    not be used to destroy them.) */
236 void
237 msg_destroy (struct msg *m)
238 {
239   if (m)
240     {
241       msg_location_destroy (m->location);
242       free (m->text);
243       free (m->command_name);
244       free (m);
245     }
246 }
247
248 char *
249 msg_to_string (const struct msg *m)
250 {
251   struct string s;
252
253   ds_init_empty (&s);
254
255   if (m->category != MSG_C_GENERAL && !msg_location_is_empty (m->location))
256     {
257       msg_location_format (m->location, &s);
258       ds_put_cstr (&s, ": ");
259     }
260
261   ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
262
263   if (m->category == MSG_C_SYNTAX && m->command_name != NULL)
264     ds_put_format (&s, "%s: ", m->command_name);
265
266   ds_put_cstr (&s, m->text);
267
268   return ds_cstr (&s);
269 }
270 \f
271
272 /* Number of messages reported, by severity level. */
273 static int counts[MSG_N_SEVERITIES];
274
275 /* True after the maximum number of errors or warnings has been exceeded. */
276 static bool too_many_errors;
277
278 /* True after the maximum number of notes has been exceeded. */
279 static bool too_many_notes;
280
281 /* True iff warnings have been explicitly disabled (MXWARNS = 0) */
282 static bool warnings_off = false;
283
284 /* Checks whether we've had so many errors that it's time to quit
285    processing this syntax file. */
286 bool
287 msg_ui_too_many_errors (void)
288 {
289   return too_many_errors;
290 }
291
292 void
293 msg_ui_disable_warnings (bool x)
294 {
295   warnings_off = x;
296 }
297
298
299 void
300 msg_ui_reset_counts (void)
301 {
302   int i;
303
304   for (i = 0; i < MSG_N_SEVERITIES; i++)
305     counts[i] = 0;
306   too_many_errors = false;
307   too_many_notes = false;
308 }
309
310 bool
311 msg_ui_any_errors (void)
312 {
313   return counts[MSG_S_ERROR] > 0;
314 }
315
316
317 static void
318 ship_message (struct msg *m)
319 {
320   enum { MAX_STACK = 4 };
321   static struct msg *stack[MAX_STACK];
322   static size_t n;
323
324   /* If we're recursing on a given message, or recursing deeply, drop it. */
325   if (n >= MAX_STACK)
326     return;
327   for (size_t i = 0; i < n; i++)
328     if (stack[i] == m)
329       return;
330
331   stack[n++] = m;
332   if (msg_handler && n <= 1)
333     msg_handler (m, msg_aux);
334   else
335     fprintf (stderr, "%s\n", m->text);
336   n--;
337 }
338
339 static void
340 submit_note (char *s)
341 {
342   struct msg m = {
343     .category = MSG_C_GENERAL,
344     .severity = MSG_S_NOTE,
345     .text = s,
346   };
347   ship_message (&m);
348
349   free (s);
350 }
351
352
353
354 static void
355 process_msg (struct msg *m)
356 {
357   int n_msgs, max_msgs;
358
359   if (too_many_errors
360       || (too_many_notes && m->severity == MSG_S_NOTE)
361       || (warnings_off && m->severity == MSG_S_WARNING))
362     return;
363
364   ship_message (m);
365
366   counts[m->severity]++;
367   max_msgs = settings_get_max_messages (m->severity);
368   n_msgs = counts[m->severity];
369   if (m->severity == MSG_S_WARNING)
370     n_msgs += counts[MSG_S_ERROR];
371   if (n_msgs > max_msgs)
372     {
373       if (m->severity == MSG_S_NOTE)
374         {
375           too_many_notes = true;
376           submit_note (xasprintf (_("Notes (%d) exceed limit (%d).  "
377                                     "Suppressing further notes."),
378                                   n_msgs, max_msgs));
379         }
380       else
381         {
382           too_many_errors = true;
383           if (m->severity == MSG_S_WARNING)
384             submit_note (xasprintf (_("Warnings (%d) exceed limit (%d).  Syntax processing will be halted."),
385                                     n_msgs, max_msgs));
386           else
387             submit_note (xasprintf (_("Errors (%d) exceed limit (%d).  Syntax processing will be halted."),
388                                     n_msgs, max_msgs));
389         }
390     }
391 }
392
393
394 /* Emits M as an error message.
395    Frees allocated data in M. */
396 void
397 msg_emit (struct msg *m)
398 {
399   if (!messages_disabled)
400      process_msg (m);
401
402   free (m->text);
403   free (m->command_name);
404 }
405
406 /* Disables message output until the next call to msg_enable.  If
407    this function is called multiple times, msg_enable must be
408    called an equal number of times before messages are actually
409    re-enabled. */
410 void
411 msg_disable (void)
412 {
413   messages_disabled++;
414 }
415
416 /* Enables message output that was disabled by msg_disable. */
417 void
418 msg_enable (void)
419 {
420   assert (messages_disabled > 0);
421   messages_disabled--;
422 }
423 \f
424 /* Private functions. */
425
426 static char fatal_error_message[1024];
427 static int fatal_error_message_bytes = 0;
428
429 static char diagnostic_information[1024];
430 static int diagnostic_information_bytes = 0;
431
432 static int
433 append_message (char *msg, int bytes_used, const char *fmt, ...)
434 {
435   va_list va;
436   va_start (va, fmt);
437   int ret = vsnprintf (msg + bytes_used, 1024 - bytes_used, fmt, va);
438   va_end (va);
439   assert (ret >= 0);
440
441   return ret;
442 }
443
444
445 /* Generate a row of asterisks held in statically allocated memory  */
446 static struct substring
447 generate_banner (void)
448 {
449   static struct substring banner;
450   if (!banner.string)
451     banner = ss_cstr ("******************************************************\n");
452   return banner;
453 }
454
455 const char *
456 prepare_fatal_error_message (void)
457 {
458   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, generate_banner ().string);
459
460   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "You have discovered a bug in PSPP.  Please report this\n");
461   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "to " PACKAGE_BUGREPORT ".  Please include this entire\n");
462   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "message, *plus* several lines of output just above it.\n");
463   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "For the best chance at having the bug fixed, also\n");
464   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "include the syntax file that triggered it and a sample\n");
465   fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "of any data file used for input.\n");
466   return fatal_error_message;
467 }
468
469 const char *
470 prepare_diagnostic_information (void)
471 {
472   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "version:             %s\n", version);
473   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "host_system:         %s\n", host_system);
474   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "build_system:        %s\n", build_system);
475   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "locale_dir:          %s\n", relocate (locale_dir));
476   diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "compiler version:    %s\n",
477 #ifdef __VERSION__
478            __VERSION__
479 #else
480            "Unknown"
481 #endif
482 );
483
484   return diagnostic_information;
485 }
486
487 void
488 request_bug_report (const char *msg)
489 {
490   write (STDERR_FILENO, fatal_error_message, fatal_error_message_bytes);
491   write (STDERR_FILENO, "proximate cause:     ", 21);
492   write (STDERR_FILENO, msg, strlen (msg));
493   write (STDERR_FILENO, "\n", 1);
494   write (STDERR_FILENO, diagnostic_information, diagnostic_information_bytes);
495   const struct substring banner = generate_banner ();
496   write (STDERR_FILENO, banner.string, banner.length);
497 }