31a78d25e09d358e6250cc8200e14eb91701829d
[pspp-builds.git] / src / libpspp / message.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    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, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "message.h"
23 #include "msg-locator.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <libpspp/alloc.h>
33 #include <libpspp/version.h>
34
35 #include "progname.h"
36 #include "xvasprintf.h"
37
38 /* Current command name as set by msg_set_command_name(). */
39 static char *command_name;
40
41 /* Message handler as set by msg_init(). */
42 static void (*msg_handler)  (const struct msg *);
43
44 /* Disables emitting messages if positive. */
45 static int messages_disabled;
46
47 /* Public functions. */
48
49 /* Writes error message in CLASS, with text FORMAT, formatted with
50    printf, to the standard places. */
51 void
52 msg (enum msg_class class, const char *format, ...)
53 {
54   struct msg m;
55   va_list args;
56
57   m.category = msg_class_to_category (class);
58   m.severity = msg_class_to_severity (class);
59   va_start (args, format);
60   m.text = xvasprintf (format, args);
61   va_end (args);
62
63   msg_emit (&m);
64 }
65
66 static struct source_stream *s_stream;
67
68 void
69 msg_init (struct source_stream *ss,  void (*handler) (const struct msg *) )
70 {
71   s_stream = ss;
72   msg_handler = handler;
73 }
74
75 void
76 msg_done (void) 
77 {
78 }
79
80
81 /* Duplicate a message */
82 struct msg * 
83 msg_dup(const struct msg *m)
84 {
85   struct msg *new_msg = xmalloc (sizeof *m);
86
87   *new_msg = *m;
88   new_msg->text = strdup(m->text);
89
90   return new_msg;
91 }
92
93 void
94 msg_destroy(struct msg *m)
95 {
96   free(m->text);
97   free(m);
98 }
99
100 /* Emits M as an error message.
101    Frees allocated data in M. */
102 void
103 msg_emit (struct msg *m) 
104 {
105   get_msg_location (s_stream, &m->where);
106   if (!messages_disabled)
107     msg_handler (m);
108   free (m->text);
109 }
110
111 /* Disables message output until the next call to msg_enable.  If
112    this function is called multiple times, msg_enable must be
113    called an equal number of times before messages are actually
114    re-enabled. */
115 void
116 msg_disable (void) 
117 {
118   messages_disabled++;
119 }
120
121 /* Enables message output that was disabled by msg_disable. */
122 void
123 msg_enable (void) 
124 {
125   assert (messages_disabled > 0);
126   messages_disabled--;
127 }
128 \f
129 /* Private functions. */
130
131 /* Sets COMMAND_NAME as the command name included in some kinds
132    of error messages. */
133 void
134 msg_set_command_name (const char *command_name_) 
135 {
136   free (command_name);
137   command_name = command_name_ ? xstrdup (command_name_) : NULL;
138 }
139
140 /* Returns the current command name, or NULL if none. */
141 const char *
142 msg_get_command_name (void) 
143 {
144   return command_name;
145 }
146
147 void 
148 request_bug_report_and_abort (const char *msg)
149 {
150   fprintf (stderr, "******************************************************\n");
151   fprintf (stderr, "You have discovered a bug in PSPP.  Please report this\n");
152   fprintf (stderr, "to " PACKAGE_BUGREPORT ".  Please include this entire\n");
153   fprintf (stderr, "message, *plus* several lines of output just above it.\n");
154   fprintf (stderr, "For the best chance at having the bug fixed, also\n");
155   fprintf (stderr, "include the syntax file that triggered it and a sample\n");
156   fprintf (stderr, "of any data file used for input.\n");
157   fprintf (stderr, "proximate cause:     %s\n", msg);
158   fprintf (stderr, "version:             %s\n", stat_version);
159   fprintf (stderr, "host_system:         %s\n", host_system);
160   fprintf (stderr, "build_system:        %s\n", build_system);
161   fprintf (stderr, "default_config_path: %s\n", default_config_path);
162   fprintf (stderr, "include_path:        %s\n", include_path);
163   fprintf (stderr, "locale_dir:          %s\n", locale_dir);
164   fprintf (stderr, "compiler version:    %s\n",
165 #ifdef __VERSION__
166            __VERSION__
167 #else
168            "Unknown"
169 #endif
170            );     
171   fprintf (stderr, "******************************************************\n");
172
173   _exit (EXIT_FAILURE);
174 }