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