message: Make msg_dup() copy and msg_destroy() free the file name.
[pspp-builds.git] / src / libpspp / message.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009 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 "message.h"
20 #include "msg-locator.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/version.h>
30
31 #include "progname.h"
32 #include "xalloc.h"
33 #include "xvasprintf.h"
34
35 /* Current command name as set by msg_set_command_name(). */
36 static char *command_name;
37
38 /* Message handler as set by msg_init(). */
39 static void (*msg_handler)  (const struct msg *);
40
41 /* Disables emitting messages if positive. */
42 static int messages_disabled;
43
44 /* Public functions. */
45
46 /* Writes error message in CLASS, with text FORMAT, formatted with
47    printf, to the standard places. */
48 void
49 msg (enum msg_class class, const char *format, ...)
50 {
51   struct msg m;
52   va_list args;
53
54   m.category = msg_class_to_category (class);
55   m.severity = msg_class_to_severity (class);
56   va_start (args, format);
57   m.text = xvasprintf (format, args);
58   va_end (args);
59
60   msg_emit (&m);
61 }
62
63 static struct source_stream *s_stream;
64
65 void
66 msg_init (struct source_stream *ss,  void (*handler) (const struct msg *) )
67 {
68   s_stream = ss;
69   msg_handler = handler;
70 }
71
72 void
73 msg_done (void)
74 {
75 }
76
77
78 /* Duplicate a message */
79 struct msg *
80 msg_dup(const struct msg *m)
81 {
82   struct msg *new_msg;
83
84   new_msg = xmemdup (m, sizeof *m);
85   if (m->where.file_name != NULL)
86     new_msg->where.file_name = xstrdup (m->where.file_name);
87   new_msg->text = xstrdup (m->text);
88
89   return new_msg;
90 }
91
92 /* Frees a message created by msg_dup().
93
94    (Messages not created by msg_dup(), as well as their where.file_name
95    members, are typically not dynamically allocated, so this function should
96    not be used to destroy them.) */
97 void
98 msg_destroy (struct msg *m)
99 {
100   free (m->where.file_name);
101   free (m->text);
102   free (m);
103 }
104
105 /* Emits M as an error message.
106    Frees allocated data in M. */
107 void
108 msg_emit (struct msg *m)
109 {
110   if ( s_stream )
111     get_msg_location (s_stream, &m->where);
112
113   if (!messages_disabled)
114      msg_handler (m);
115   free (m->text);
116 }
117
118 /* Disables message output until the next call to msg_enable.  If
119    this function is called multiple times, msg_enable must be
120    called an equal number of times before messages are actually
121    re-enabled. */
122 void
123 msg_disable (void)
124 {
125   messages_disabled++;
126 }
127
128 /* Enables message output that was disabled by msg_disable. */
129 void
130 msg_enable (void)
131 {
132   assert (messages_disabled > 0);
133   messages_disabled--;
134 }
135 \f
136 /* Private functions. */
137
138 /* Sets COMMAND_NAME as the command name included in some kinds
139    of error messages. */
140 void
141 msg_set_command_name (const char *command_name_)
142 {
143   free (command_name);
144   command_name = command_name_ ? xstrdup (command_name_) : NULL;
145 }
146
147 /* Returns the current command name, or NULL if none. */
148 const char *
149 msg_get_command_name (void)
150 {
151   return command_name;
152 }
153
154 void
155 request_bug_report_and_abort (const char *msg)
156 {
157   fprintf (stderr, "******************************************************\n");
158   fprintf (stderr, "You have discovered a bug in PSPP.  Please report this\n");
159   fprintf (stderr, "to " PACKAGE_BUGREPORT ".  Please include this entire\n");
160   fprintf (stderr, "message, *plus* several lines of output just above it.\n");
161   fprintf (stderr, "For the best chance at having the bug fixed, also\n");
162   fprintf (stderr, "include the syntax file that triggered it and a sample\n");
163   fprintf (stderr, "of any data file used for input.\n");
164   fprintf (stderr, "proximate cause:     %s\n", msg);
165   fprintf (stderr, "version:             %s\n", stat_version);
166   fprintf (stderr, "host_system:         %s\n", host_system);
167   fprintf (stderr, "build_system:        %s\n", build_system);
168   fprintf (stderr, "default_config_path: %s\n", default_config_path);
169   fprintf (stderr, "include_path:        %s\n", include_path);
170   fprintf (stderr, "locale_dir:          %s\n", locale_dir);
171   fprintf (stderr, "compiler version:    %s\n",
172 #ifdef __VERSION__
173            __VERSION__
174 #else
175            "Unknown"
176 #endif
177            );
178   fprintf (stderr, "******************************************************\n");
179
180   _exit (EXIT_FAILURE);
181 }