1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
21 #include <libpspp/message.h>
26 #include <libpspp/alloc.h>
27 #include <data/file-name.h>
28 #include <language/line-buffer.h>
29 #include <language/lexer/lexer.h>
30 #include <data/settings.h>
31 #include <ui/terminal/read-line.h>
32 #include <libpspp/version.h>
34 #include "linebreak.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
42 int err_warning_count;
44 int err_already_flagged;
48 static char *command_name;
50 /* Public functions. */
52 /* Writes error message in CLASS, with text FORMAT, formatted with
53 printf, to the standard places. */
55 msg (enum msg_class class, const char *format, ...)
60 e.category = msg_class_to_category (class);
61 e.severity = msg_class_to_severity (class);
62 err_location (&e.where);
63 va_start (args, format);
64 e.text = xvasprintf (format, args);
70 /* Writes MESSAGE formatted with printf, to stderr, if the
71 verbosity level is at least LEVEL. */
73 verbose_msg (int level, const char *format, ...)
75 if (err_verbosity >= level)
79 va_start (args, format);
80 fprintf (stderr, "%s: ", program_name);
81 vfprintf (stderr, format, args);
87 /* Checks whether we've had so many errors that it's time to quit
88 processing this syntax file. */
90 err_check_count (void)
92 if (get_errorbreak() && err_error_count)
93 msg (MN, _("Terminating execution of syntax file due to error."));
94 else if (err_error_count > get_mxerrs() )
95 msg (MN, _("Errors (%d) exceeds limit (%d)."),
96 err_error_count, get_mxerrs());
97 else if (err_error_count + err_warning_count > get_mxwarns() )
98 msg (MN, _("Warnings (%d) exceed limit (%d)."),
99 err_error_count + err_warning_count, get_mxwarns() );
103 getl_abort_noninteractive ();
106 static void puts_stdout (int line_indent, const char *line, size_t length);
107 static void dump_message (char *msg,
108 void (*func) (int line_indent,
109 const char *line, size_t length),
110 unsigned width, unsigned indent);
116 getl_uninitialize ();
117 readln_uninitialize();
120 /* Emits E as an error message.
121 Frees `text' member in E. */
123 err_msg (const struct error *e)
127 bool show_command_name; /* Show command name with error? */
128 bool show_file_location; /* Show syntax file location? */
131 static const struct category categories[] =
133 {false, false}, /* MSG_GENERAL. */
134 {true, true}, /* MSG_SYNTAX. */
135 {false, true}, /* MSG_DATA. */
140 const char *name; /* How to identify this severity. */
141 int *count; /* Number of msgs with this severity so far. */
144 static struct severity severities[] =
146 {N_("error"), &err_error_count}, /* MSG_ERROR. */
147 {N_("warning"), &err_warning_count}, /* MSG_WARNING. */
148 {NULL, NULL}, /* MSG_NOTE. */
151 const struct category *category = &categories[e->category];
152 const struct severity *severity = &severities[e->severity];
153 struct string msg = DS_INITIALIZER;
155 if (category->show_file_location && e->where.file_name)
157 ds_printf (&msg, "%s:", e->where.file_name);
158 if (e->where.line_number != -1)
159 ds_printf (&msg, "%d:", e->where.line_number);
163 if (severity->name != NULL)
164 ds_printf (&msg, "%s: ", gettext (severity->name));
166 if (severity->count != NULL)
169 if (category->show_command_name && command_name != NULL)
170 ds_printf (&msg, "%s: ", command_name);
172 ds_puts (&msg, e->text);
174 /* FIXME: Check set_messages and set_errors to determine where to
175 send errors and messages. */
176 dump_message (ds_c_str (&msg), puts_stdout, get_viewwidth (), 8);
182 /* Private functions. */
184 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
185 a new-line to stdout. */
186 static void puts_stdout (int line_indent,
187 const char *line, size_t length)
190 for (i = 0; i < line_indent; i++)
192 fwrite (line, 1, length, stdout);
196 /* Divides MSG into lines of WIDTH width for the first line and
197 WIDTH - INDENT width for each succeeding line. Each line is
198 passed to FUNC as a null-terminated string (no new-line
199 character is included in the string). */
201 dump_message (char *msg,
202 void (*func) (int line_indent, const char *line, size_t length),
203 unsigned width, unsigned indent)
205 size_t length = strlen (msg);
206 char *string, *breaks;
208 size_t line_start, i;
210 /* Allocate temporary buffers.
211 If we can't get memory for them, then just dump the whole
213 string = strdup (msg);
214 breaks = malloc (length);
215 if (string == NULL || breaks == NULL)
219 func (0, msg, length);
223 /* Break into lines. */
224 if (indent > width / 3)
226 mbs_width_linebreaks (string, length,
227 width - indent, -indent, 0,
228 NULL, locale_charset (), breaks);
230 /* Pass lines to FUNC. */
233 for (i = 0; i < length; i++)
236 case UC_BREAK_POSSIBLE:
237 /* Break before this character,
238 and include this character in the next line. */
239 func (line_indent, &string[line_start], i - line_start);
241 line_indent = indent;
243 case UC_BREAK_MANDATORY:
244 /* Break before this character,
245 but don't include this character in the next line
246 (because it'string a new-line). */
247 func (line_indent, &string[line_start], i - line_start);
249 line_indent = indent;
254 if (line_start < length)
255 func (line_indent, &string[line_start], length - line_start);
261 /* Sets COMMAND_NAME as the command name included in some kinds
262 of error messages. */
264 err_set_command_name (const char *command_name_)
267 command_name = command_name_ ? xstrdup (command_name_) : NULL;
271 request_bug_report_and_abort(const char *msg )
274 "******************************************************************\n"
275 "You have discovered a bug in PSPP.\n\n"
276 " Please report this, by sending "
277 "an email to " PACKAGE_BUGREPORT ",\n"
278 "explaining what you were doing when this happened, and including\n"
279 "a sample of your input file which caused it.\n");
282 "Also, please copy the following lines into your bug report:\n\n"
288 "default_config_path: %s\n"
290 "groff_font_path: %s\n"
292 "compiler version: %s\n"
312 fprintf(stderr,"Diagnosis: %s\n",msg);
315 "******************************************************************\n");
321 err_assert_fail(const char *expr, const char *file, int line)
324 snprintf(msg,256,"Assertion failed: %s:%d; (%s)",file,line,expr);
325 request_bug_report_and_abort( msg );