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