1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "linebreak.h"
26 #include <libpspp/msg-locator.h>
27 #include <libpspp/getl.h>
28 #include <data/settings.h>
29 #include <libpspp/message.h>
30 #include <libpspp/str.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
39 /* Number of errors, warnings reported. */
40 static int error_count;
41 static int warning_count;
42 static const char *error_file;
44 static void handle_msg (const struct msg *);
46 static FILE *msg_file ;
49 msg_ui_set_error_file (const char *filename)
51 error_file = filename;
55 msg_ui_init (struct source_stream *ss)
61 msg_file = fopen (error_file, "a");
62 if ( NULL == msg_file )
65 printf ( _("Cannot open %s (%s). "
66 "Writing errors to stdout instead.\n"),
67 error_file, strerror(err) );
71 msg_init (ss, handle_msg);
80 if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
84 /* Checks whether we've had so many errors that it's time to quit
85 processing this syntax file. */
87 check_msg_count (struct source_stream *ss)
89 if (!getl_is_interactive (ss))
91 if (get_errorbreak () && error_count)
92 msg (MN, _("Terminating execution of syntax file due to error."));
93 else if (error_count > get_mxerrs() )
94 msg (MN, _("Errors (%d) exceeds limit (%d)."),
95 error_count, get_mxerrs());
96 else if (error_count + warning_count > get_mxwarns() )
97 msg (MN, _("Warnings (%d) exceed limit (%d)."),
98 error_count + warning_count, get_mxwarns() );
102 getl_abort_noninteractive (ss);
107 reset_msg_count (void)
109 error_count = warning_count = 0;
115 return error_count > 0;
118 static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
119 static void dump_line (int line_indent, const char *line, size_t length,
123 handle_msg (const struct msg *m)
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"), &error_count}, /* MSG_ERROR. */
147 {N_("warning"), &warning_count}, /* MSG_WARNING. */
148 {NULL, NULL}, /* MSG_NOTE. */
151 const struct category *category = &categories[m->category];
152 const struct severity *severity = &severities[m->severity];
153 struct string string = DS_EMPTY_INITIALIZER;
155 if (category->show_file_location && m->where.file_name)
157 ds_put_format (&string, "%s:", m->where.file_name);
158 if (m->where.line_number != -1)
159 ds_put_format (&string, "%d:", m->where.line_number);
160 ds_put_char (&string, ' ');
163 if (severity->name != NULL)
164 ds_put_format (&string, "%s: ", gettext (severity->name));
166 if (severity->count != NULL)
169 if (category->show_command_name && msg_get_command_name () != NULL)
170 ds_put_format (&string, "%s: ", msg_get_command_name ());
172 ds_put_cstr (&string, m->text);
174 if (msg_file != stdout || get_error_routing_to_terminal ())
175 dump_message (ds_cstr (&string), get_viewwidth (), 8, msg_file);
177 ds_destroy (&string);
180 /* Divides MSG into lines of WIDTH width for the first line and
181 WIDTH - INDENT width for each succeeding line, and writes the
184 dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
186 size_t length = strlen (msg);
187 char *string, *breaks;
189 size_t line_start, i;
191 /* Allocate temporary buffers.
192 If we can't get memory for them, then just dump the whole
194 string = strdup (msg);
195 breaks = malloc (length);
196 if (string == NULL || breaks == NULL)
205 /* Break into lines. */
206 if (indent > width / 3)
208 mbs_width_linebreaks (string, length,
209 width - indent, -indent, 0,
210 NULL, locale_charset (), breaks);
212 /* Write out lines. */
215 for (i = 0; i < length; i++)
218 case UC_BREAK_POSSIBLE:
219 /* Break before this character,
220 and include this character in the next line. */
221 dump_line (line_indent, &string[line_start], i - line_start, stream);
223 line_indent = indent;
225 case UC_BREAK_MANDATORY:
226 /* Break before this character,
227 but don't include this character in the next line
228 (because it'string a new-line). */
229 dump_line (line_indent, &string[line_start], i - line_start, stream);
231 line_indent = indent;
236 if (line_start < length)
237 dump_line (line_indent, &string[line_start], length - line_start, stream);
243 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
244 a new-line to STREAM. */
246 dump_line (int line_indent, const char *line, size_t length, FILE *stream)
249 for (i = 0; i < line_indent; i++)
251 fwrite (line, 1, length, stream);