1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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/>. */
20 #include "msg-locator.h"
29 #include <libpspp/str.h>
30 #include <libpspp/version.h>
32 #include "gl/progname.h"
33 #include "gl/xalloc.h"
34 #include "gl/xvasprintf.h"
37 #define _(msgid) gettext (msgid)
39 /* Message handler as set by msg_init(). */
40 static void (*msg_handler) (const struct msg *);
42 /* Disables emitting messages if positive. */
43 static int messages_disabled;
45 /* Public functions. */
47 /* Writes error message in CLASS, with text FORMAT, formatted with
48 printf, to the standard places. */
50 msg (enum msg_class class, const char *format, ...)
55 m.category = msg_class_to_category (class);
56 m.severity = msg_class_to_severity (class);
57 va_start (args, format);
58 m.text = xvasprintf (format, args);
64 static struct source_stream *s_stream;
67 msg_init (struct source_stream *ss, void (*handler) (const struct msg *) )
70 msg_handler = handler;
78 /* Working with messages. */
80 /* Duplicate a message */
82 msg_dup (const struct msg *m)
86 new_msg = xmemdup (m, sizeof *m);
87 if (m->where.file_name != NULL)
88 new_msg->where.file_name = xstrdup (m->where.file_name);
89 new_msg->text = xstrdup (m->text);
94 /* Frees a message created by msg_dup().
96 (Messages not created by msg_dup(), as well as their where.file_name
97 members, are typically not dynamically allocated, so this function should
98 not be used to destroy them.) */
100 msg_destroy (struct msg *m)
102 free (m->where.file_name);
108 msg_to_string (const struct msg *m, const char *command_name)
115 if (m->category != MSG_C_GENERAL
116 && (m->where.file_name || m->where.line_number != -1))
118 if (m->where.file_name)
119 ds_put_format (&s, "%s:", m->where.file_name);
120 if (m->where.line_number != -1)
121 ds_put_format (&s, "%d:", m->where.line_number);
122 ds_put_char (&s, ' ');
131 label = _("warning");
138 ds_put_format (&s, "%s: ", label);
140 if (m->category == MSG_C_SYNTAX && command_name != NULL)
141 ds_put_format (&s, "%s: ", command_name);
143 ds_put_cstr (&s, m->text);
148 /* Emits M as an error message.
149 Frees allocated data in M. */
151 msg_emit (struct msg *m)
154 get_msg_location (s_stream, &m->where);
157 m->where.file_name = NULL;
158 m->where.line_number = -1;
161 if (!messages_disabled)
166 /* Disables message output until the next call to msg_enable. If
167 this function is called multiple times, msg_enable must be
168 called an equal number of times before messages are actually
176 /* Enables message output that was disabled by msg_disable. */
180 assert (messages_disabled > 0);
184 /* Private functions. */
187 request_bug_report_and_abort (const char *msg)
189 fprintf (stderr, "******************************************************\n");
190 fprintf (stderr, "You have discovered a bug in PSPP. Please report this\n");
191 fprintf (stderr, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
192 fprintf (stderr, "message, *plus* several lines of output just above it.\n");
193 fprintf (stderr, "For the best chance at having the bug fixed, also\n");
194 fprintf (stderr, "include the syntax file that triggered it and a sample\n");
195 fprintf (stderr, "of any data file used for input.\n");
196 fprintf (stderr, "proximate cause: %s\n", msg);
197 fprintf (stderr, "version: %s\n", stat_version);
198 fprintf (stderr, "host_system: %s\n", host_system);
199 fprintf (stderr, "build_system: %s\n", build_system);
200 fprintf (stderr, "locale_dir: %s\n", locale_dir);
201 fprintf (stderr, "compiler version: %s\n",
208 fprintf (stderr, "******************************************************\n");
210 _exit (EXIT_FAILURE);