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