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
23 #include "linebreak.h"
25 #include <libpspp/msg-locator.h>
26 #include <libpspp/getl.h>
27 #include <data/settings.h>
28 #include <libpspp/message.h>
29 #include <libpspp/str.h>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
38 /* Number of errors, warnings reported. */
39 static int error_count;
40 static int warning_count;
41 static const char *error_file;
43 static void handle_msg (const struct msg *);
45 static FILE *msg_file ;
48 msg_ui_set_error_file (const char *filename)
50 error_file = filename;
54 msg_ui_init (struct source_stream *ss)
60 msg_file = fopen (error_file, "a");
61 if ( NULL == msg_file )
64 printf ( _("Cannot open %s (%s). "
65 "Writing errors to stdout instead.\n"),
66 error_file, strerror(err) );
70 msg_init (ss, handle_msg);
79 if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
83 /* Checks whether we've had so many errors that it's time to quit
84 processing this syntax file. */
86 check_msg_count (struct source_stream *ss)
88 if (!getl_is_interactive (ss))
90 if (get_errorbreak () && error_count)
91 msg (MN, _("Terminating execution of syntax file due to error."));
92 else if (error_count > get_mxerrs() )
93 msg (MN, _("Errors (%d) exceeds limit (%d)."),
94 error_count, get_mxerrs());
95 else if (error_count + warning_count > get_mxwarns() )
96 msg (MN, _("Warnings (%d) exceed limit (%d)."),
97 error_count + warning_count, get_mxwarns() );
101 getl_abort_noninteractive (ss);
106 reset_msg_count (void)
108 error_count = warning_count = 0;
114 return error_count > 0;
117 static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
118 static void dump_line (int line_indent, const char *line, size_t length,
122 handle_msg (const struct msg *m)
126 bool show_command_name; /* Show command name with error? */
127 bool show_file_location; /* Show syntax file location? */
130 static const struct category categories[] =
132 {false, false}, /* MSG_GENERAL. */
133 {true, true}, /* MSG_SYNTAX. */
134 {false, true}, /* MSG_DATA. */
139 const char *name; /* How to identify this severity. */
140 int *count; /* Number of msgs with this severity so far. */
143 static struct severity severities[] =
145 {N_("error"), &error_count}, /* MSG_ERROR. */
146 {N_("warning"), &warning_count}, /* MSG_WARNING. */
147 {NULL, NULL}, /* MSG_NOTE. */
150 const struct category *category = &categories[m->category];
151 const struct severity *severity = &severities[m->severity];
152 struct string string = DS_EMPTY_INITIALIZER;
154 if (category->show_file_location && m->where.file_name)
156 ds_put_format (&string, "%s:", m->where.file_name);
157 if (m->where.line_number != -1)
158 ds_put_format (&string, "%d:", m->where.line_number);
159 ds_put_char (&string, ' ');
162 if (severity->name != NULL)
163 ds_put_format (&string, "%s: ", gettext (severity->name));
165 if (severity->count != NULL)
168 if (category->show_command_name && msg_get_command_name () != NULL)
169 ds_put_format (&string, "%s: ", msg_get_command_name ());
171 ds_put_cstr (&string, m->text);
173 if (msg_file != stdout || get_error_routing_to_terminal ())
174 dump_message (ds_cstr (&string), get_viewwidth (), 8, msg_file);
176 ds_destroy (&string);
179 /* Divides MSG into lines of WIDTH width for the first line and
180 WIDTH - INDENT width for each succeeding line, and writes the
183 dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
185 size_t length = strlen (msg);
186 char *string, *breaks;
188 size_t line_start, i;
190 /* Allocate temporary buffers.
191 If we can't get memory for them, then just dump the whole
193 string = strdup (msg);
194 breaks = malloc (length);
195 if (string == NULL || breaks == NULL)
204 /* Break into lines. */
205 if (indent > width / 3)
207 mbs_width_linebreaks (string, length,
208 width - indent, -indent, 0,
209 NULL, locale_charset (), breaks);
211 /* Write out lines. */
214 for (i = 0; i < length; i++)
217 case UC_BREAK_POSSIBLE:
218 /* Break before this character,
219 and include this character in the next line. */
220 dump_line (line_indent, &string[line_start], i - line_start, stream);
222 line_indent = indent;
224 case UC_BREAK_MANDATORY:
225 /* Break before this character,
226 but don't include this character in the next line
227 (because it'string a new-line). */
228 dump_line (line_indent, &string[line_start], i - line_start, stream);
230 line_indent = indent;
235 if (line_start < length)
236 dump_line (line_indent, &string[line_start], length - line_start, stream);
242 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
243 a new-line to STREAM. */
245 dump_line (int line_indent, const char *line, size_t length, FILE *stream)
248 for (i = 0; i < line_indent; i++)
250 fwrite (line, 1, length, stream);