eaad1a69f3e596d612b7e97eb0a9e547324ae8df
[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 <libpspp/message.h>
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include <libpspp/alloc.h>
31 #include <libpspp/version.h>
32
33 #include "progname.h"
34 #include "xvasprintf.h"
35
36 /* Current command name as set by msg_set_command_name(). */
37 static char *command_name;
38
39 /* Message handler as set by msg_init(). */
40 static void (*msg_handler)  (const struct msg *);
41 static void (*msg_location) (struct msg_locator *);
42
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 void
64 msg_init ( void (*handler) (const struct msg *), 
65            void (*location) (struct msg_locator *) ) 
66 {
67   msg_handler = handler;
68   msg_location = location;
69 }
70
71 void
72 msg_done (void) 
73 {
74 }
75
76
77 /* Duplicate a message */
78 struct msg * 
79 msg_dup(const struct msg *m)
80 {
81   struct msg *new_msg = xmalloc (sizeof *m);
82
83   *new_msg = *m;
84   new_msg->text = strdup(m->text);
85
86   return new_msg;
87 }
88
89 void
90 msg_destroy(struct msg *m)
91 {
92   free(m->text);
93   free(m);
94 }
95
96
97 /* Emits M as an error message.
98    Frees allocated data in M. */
99 void
100 msg_emit (struct msg *m) 
101 {
102   msg_location (&m->where);
103   msg_handler (m);
104   free (m->text);
105 }
106 \f
107 /* Private functions. */
108
109 /* Sets COMMAND_NAME as the command name included in some kinds
110    of error messages. */
111 void
112 msg_set_command_name (const char *command_name_) 
113 {
114   free (command_name);
115   command_name = command_name_ ? xstrdup (command_name_) : NULL;
116 }
117
118 /* Returns the current command name, or NULL if none. */
119 const char *
120 msg_get_command_name (void) 
121 {
122   return command_name;
123 }
124
125 void 
126 request_bug_report_and_abort (const char *msg)
127 {
128   fprintf (stderr, "******************************************************\n");
129   fprintf (stderr, "You have discovered a bug in PSPP.  Please report this\n");
130   fprintf (stderr, "to " PACKAGE_BUGREPORT ".  Please include this entire\n");
131   fprintf (stderr, "message, *plus* several lines of output just above it.\n");
132   fprintf (stderr, "For the best chance at having the bug fixed, also\n");
133   fprintf (stderr, "include the syntax file that triggered it and a sample\n");
134   fprintf (stderr, "of any data file used for input.\n");
135   fprintf (stderr, "proximate cause:     %s\n", msg);
136   fprintf (stderr, "version:             %s\n", stat_version);
137   fprintf (stderr, "host_system:         %s\n", host_system);
138   fprintf (stderr, "build_system:        %s\n", build_system);
139   fprintf (stderr, "default_config_path: %s\n", default_config_path);
140   fprintf (stderr, "include_path:        %s\n", include_path);
141   fprintf (stderr, "locale_dir:          %s\n", locale_dir);
142   fprintf (stderr, "compiler version:    %s\n",
143 #ifdef __VERSION__
144            __VERSION__
145 #else
146            "Unknown"
147 #endif
148            );     
149   fprintf (stderr, "******************************************************\n");
150
151   _exit (EXIT_FAILURE);
152 }