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