1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 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
25 #include "linebreak.h"
27 #include <libpspp/msg-locator.h>
28 #include <libpspp/getl.h>
29 #include <data/settings.h>
30 #include <libpspp/message.h>
31 #include <libpspp/str.h>
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
40 /* Number of errors, warnings reported. */
41 static int error_count;
42 static int warning_count;
43 static const char *error_file;
45 static void handle_msg (const struct msg *);
47 static FILE *msg_file ;
50 msg_ui_set_error_file (const char *filename)
52 error_file = filename;
56 msg_ui_init (struct source_stream *ss)
62 msg_file = fopen (error_file, "a");
63 if ( NULL == msg_file )
66 printf ( _("Cannot open %s (%s). "
67 "Writing errors to stdout instead.\n"),
68 error_file, strerror(err) );
72 msg_init (ss, handle_msg);
81 if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
85 /* Checks whether we've had so many errors that it's time to quit
86 processing this syntax file. */
88 check_msg_count (struct source_stream *ss)
90 if (!getl_is_interactive (ss))
92 if (get_errorbreak () && error_count)
93 msg (MN, _("Terminating execution of syntax file due to error."));
94 else if (error_count > get_mxerrs() )
95 msg (MN, _("Errors (%d) exceeds limit (%d)."),
96 error_count, get_mxerrs());
97 else if (error_count + warning_count > get_mxwarns() )
98 msg (MN, _("Warnings (%d) exceed limit (%d)."),
99 error_count + warning_count, get_mxwarns() );
103 getl_abort_noninteractive (ss);
108 reset_msg_count (void)
110 error_count = warning_count = 0;
116 return error_count > 0;
119 static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
120 static void dump_line (int line_indent, const char *line, size_t length,
124 handle_msg (const struct msg *m)
128 bool show_command_name; /* Show command name with error? */
129 bool show_file_location; /* Show syntax file location? */
132 static const struct category categories[] =
134 {false, false}, /* MSG_GENERAL. */
135 {true, true}, /* MSG_SYNTAX. */
136 {false, true}, /* MSG_DATA. */
141 const char *name; /* How to identify this severity. */
142 int *count; /* Number of msgs with this severity so far. */
145 static struct severity severities[] =
147 {N_("error"), &error_count}, /* MSG_ERROR. */
148 {N_("warning"), &warning_count}, /* MSG_WARNING. */
149 {NULL, NULL}, /* MSG_NOTE. */
152 const struct category *category = &categories[m->category];
153 const struct severity *severity = &severities[m->severity];
154 struct string string = DS_EMPTY_INITIALIZER;
156 if (category->show_file_location && m->where.file_name)
158 ds_put_format (&string, "%s:", m->where.file_name);
159 if (m->where.line_number != -1)
160 ds_put_format (&string, "%d:", m->where.line_number);
161 ds_put_char (&string, ' ');
164 if (severity->name != NULL)
165 ds_put_format (&string, "%s: ", gettext (severity->name));
167 if (severity->count != NULL)
170 if (category->show_command_name && msg_get_command_name () != NULL)
171 ds_put_format (&string, "%s: ", msg_get_command_name ());
173 ds_put_cstr (&string, m->text);
175 if (msg_file != stdout || get_error_routing_to_terminal ())
176 dump_message (ds_cstr (&string), get_viewwidth (), 8, msg_file);
178 ds_destroy (&string);
181 /* Divides MSG into lines of WIDTH width for the first line and
182 WIDTH - INDENT width for each succeeding line, and writes the
185 dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
187 size_t length = strlen (msg);
188 char *string, *breaks;
190 size_t line_start, i;
192 /* Allocate temporary buffers.
193 If we can't get memory for them, then just dump the whole
195 string = strdup (msg);
196 breaks = malloc (length);
197 if (string == NULL || breaks == NULL)
206 /* Break into lines. */
207 if (indent > width / 3)
209 mbs_width_linebreaks (string, length,
210 width - indent, -indent, 0,
211 NULL, locale_charset (), breaks);
213 /* Write out lines. */
216 for (i = 0; i < length; i++)
219 case UC_BREAK_POSSIBLE:
220 /* Break before this character,
221 and include this character in the next line. */
222 dump_line (line_indent, &string[line_start], i - line_start, stream);
224 line_indent = indent;
226 case UC_BREAK_MANDATORY:
227 /* Break before this character,
228 but don't include this character in the next line
229 (because it'string a new-line). */
230 dump_line (line_indent, &string[line_start], i - line_start, stream);
232 line_indent = indent;
237 if (line_start < length)
238 dump_line (line_indent, &string[line_start], length - line_start, stream);
244 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
245 a new-line to STREAM. */
247 dump_line (int line_indent, const char *line, size_t length, FILE *stream)
250 for (i = 0; i < line_indent; i++)
252 fwrite (line, 1, length, stream);