Continue reforming error message support. In this phase, drop actual
[pspp-builds.git] / src / message.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 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 <ctype.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <data/file-name.h>
30 #include <language/lexer/lexer.h>
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 /* Public functions. */
44
45 /* Writes error message in CLASS, with text FORMAT, formatted with
46    printf, to the standard places. */
47 void
48 msg (enum msg_class class, const char *format, ...)
49 {
50   struct msg m;
51   va_list args;
52
53   m.category = msg_class_to_category (class);
54   m.severity = msg_class_to_severity (class);
55   msg_location (&m.where);
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 {
66   msg_handler = handler;
67 }
68
69 void
70 msg_done (void) 
71 {
72 }
73
74 /* Emits M as an error message.
75    Frees allocated data in M. */
76 void
77 msg_emit (struct msg *m) 
78 {
79   msg_handler (m);
80   free (m->text);
81 }
82 \f
83 /* Private functions. */
84
85 /* Sets COMMAND_NAME as the command name included in some kinds
86    of error messages. */
87 void
88 msg_set_command_name (const char *command_name_) 
89 {
90   free (command_name);
91   command_name = command_name_ ? xstrdup (command_name_) : NULL;
92 }
93
94 /* Returns the current command name, or NULL if none. */
95 const char *
96 msg_get_command_name (void) 
97 {
98   return command_name;
99 }
100
101 void 
102 request_bug_report_and_abort(const char *msg )
103 {
104   fprintf(stderr,
105           "******************************************************************\n"
106           "You have discovered a bug in PSPP.\n\n"
107           "  Please report this, by sending "
108           "an email to " PACKAGE_BUGREPORT ",\n"
109           "explaining what you were doing when this happened, and including\n"
110           "a sample of your input file which caused it.\n");
111
112   fprintf(stderr,
113           "Also, please copy the following lines into your bug report:\n\n"
114           "bare_version:        %s\n" 
115           "version:             %s\n"
116           "stat_version:        %s\n"
117           "host_system:         %s\n"
118           "build_system:        %s\n"
119           "default_config_path: %s\n"
120           "include_path:        %s\n"
121           "groff_font_path:     %s\n"
122           "locale_dir:          %s\n"
123           "compiler version:    %s\n"
124           ,
125
126           bare_version,         
127           version,
128           stat_version,
129           host_system,        
130           build_system,
131           default_config_path,
132           include_path, 
133           groff_font_path,
134           locale_dir,
135 #ifdef __VERSION__
136           __VERSION__
137 #else
138           "Unknown"
139 #endif
140           );     
141
142   if ( msg )
143     fprintf(stderr,"Diagnosis: %s\n",msg);
144
145   fprintf(stderr,
146     "******************************************************************\n");
147
148   abort();
149 }
150
151 void 
152 msg_assert_fail(const char *expr, const char *file, int line)
153 {
154   char msg[256];
155   snprintf(msg,256,"Assertion failed: %s:%d; (%s)",file,line,expr);
156   request_bug_report_and_abort( msg );
157 }
158