Added simple queueing to message dialogs.
[pspp-builds.git] / src / libpspp / 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 <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <libpspp/alloc.h>
30 #include <libpspp/version.h>
31
32 #include "progname.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 /* Public functions. */
42
43 /* Writes error message in CLASS, with text FORMAT, formatted with
44    printf, to the standard places. */
45 void
46 msg (enum msg_class class, const char *format, ...)
47 {
48   struct msg m;
49   va_list args;
50
51   m.category = msg_class_to_category (class);
52   m.severity = msg_class_to_severity (class);
53   msg_location (&m.where);
54   va_start (args, format);
55   m.text = xvasprintf (format, args);
56   va_end (args);
57
58   msg_emit (&m);
59 }
60
61 void
62 msg_init (void (*handler) (const struct msg *)) 
63 {
64   msg_handler = handler;
65 }
66
67 void
68 msg_done (void) 
69 {
70 }
71
72
73 /* Duplicate a message */
74 struct msg * 
75 msg_dup(const struct msg *m)
76 {
77   struct msg *new_msg = xmalloc (sizeof *m);
78
79   *new_msg = *m;
80   new_msg->text = strdup(m->text);
81
82   return new_msg;
83 }
84
85 void
86 msg_destroy(struct msg *m)
87 {
88   free(m->text);
89   free(m);
90 }
91
92
93 /* Emits M as an error message.
94    Frees allocated data in M. */
95 void
96 msg_emit (struct msg *m) 
97 {
98   msg_handler (m);
99   free (m->text);
100 }
101 \f
102 /* Private functions. */
103
104 /* Sets COMMAND_NAME as the command name included in some kinds
105    of error messages. */
106 void
107 msg_set_command_name (const char *command_name_) 
108 {
109   free (command_name);
110   command_name = command_name_ ? xstrdup (command_name_) : NULL;
111 }
112
113 /* Returns the current command name, or NULL if none. */
114 const char *
115 msg_get_command_name (void) 
116 {
117   return command_name;
118 }
119
120 void 
121 request_bug_report_and_abort(const char *msg )
122 {
123   fprintf(stderr,
124           "******************************************************************\n"
125           "You have discovered a bug in PSPP.\n\n"
126           "  Please report this, by sending "
127           "an email to " PACKAGE_BUGREPORT ",\n"
128           "explaining what you were doing when this happened, and including\n"
129           "a sample of your input file which caused it.\n");
130
131   fprintf(stderr,
132           "Also, please copy the following lines into your bug report:\n\n"
133           "bare_version:        %s\n" 
134           "version:             %s\n"
135           "stat_version:        %s\n"
136           "host_system:         %s\n"
137           "build_system:        %s\n"
138           "default_config_path: %s\n"
139           "include_path:        %s\n"
140           "groff_font_path:     %s\n"
141           "locale_dir:          %s\n"
142           "compiler version:    %s\n"
143           ,
144
145           bare_version,         
146           version,
147           stat_version,
148           host_system,        
149           build_system,
150           default_config_path,
151           include_path, 
152           groff_font_path,
153           locale_dir,
154 #ifdef __VERSION__
155           __VERSION__
156 #else
157           "Unknown"
158 #endif
159           );     
160
161   if ( msg )
162     fprintf(stderr,"Diagnosis: %s\n",msg);
163
164   fprintf(stderr,
165     "******************************************************************\n");
166
167   abort();
168 }
169
170 void 
171 msg_assert_fail(const char *expr, const char *file, int line)
172 {
173   char msg[256];
174   snprintf(msg,256,"Assertion failed: %s:%d; (%s)",file,line,expr);
175   request_bug_report_and_abort( msg );
176 }
177