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