1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
21 #include "linebreak.h"
23 #include <libpspp/msg-locator.h>
24 #include <libpspp/getl.h>
25 #include <data/settings.h>
26 #include <libpspp/message.h>
27 #include <libpspp/str.h>
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) msgid
36 /* Number of errors, warnings reported. */
37 static int error_count;
38 static int warning_count;
39 static const char *error_file;
41 static void handle_msg (const struct msg *);
43 static FILE *msg_file ;
46 msg_ui_set_error_file (const char *filename)
48 error_file = filename;
52 msg_ui_init (struct source_stream *ss)
58 msg_file = fopen (error_file, "a");
59 if ( NULL == msg_file )
62 printf ( _("Cannot open %s (%s). "
63 "Writing errors to stdout instead.\n"),
64 error_file, strerror(err) );
68 msg_init (ss, handle_msg);
77 if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
81 /* Checks whether we've had so many errors that it's time to quit
82 processing this syntax file. */
84 check_msg_count (struct source_stream *ss)
86 if (!getl_is_interactive (ss))
88 if (get_errorbreak () && error_count)
89 msg (MN, _("Terminating execution of syntax file due to error."));
90 else if (error_count > get_mxerrs() )
91 msg (MN, _("Errors (%d) exceeds limit (%d)."),
92 error_count, get_mxerrs());
93 else if (error_count + warning_count > get_mxwarns() )
94 msg (MN, _("Warnings (%d) exceed limit (%d)."),
95 error_count + warning_count, get_mxwarns() );
99 getl_abort_noninteractive (ss);
104 reset_msg_count (void)
106 error_count = warning_count = 0;
112 return error_count > 0;
115 static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
116 static void dump_line (int line_indent, const char *line, size_t length,
120 handle_msg (const struct msg *m)
124 bool show_command_name; /* Show command name with error? */
125 bool show_file_location; /* Show syntax file location? */
128 static const struct category categories[] =
130 {false, false}, /* MSG_GENERAL. */
131 {true, true}, /* MSG_SYNTAX. */
132 {false, true}, /* MSG_DATA. */
137 const char *name; /* How to identify this severity. */
138 int *count; /* Number of msgs with this severity so far. */
141 static struct severity severities[] =
143 {N_("error"), &error_count}, /* MSG_ERROR. */
144 {N_("warning"), &warning_count}, /* MSG_WARNING. */
145 {NULL, NULL}, /* MSG_NOTE. */
148 const struct category *category = &categories[m->category];
149 const struct severity *severity = &severities[m->severity];
150 struct string string = DS_EMPTY_INITIALIZER;
152 if (category->show_file_location && m->where.file_name)
154 ds_put_format (&string, "%s:", m->where.file_name);
155 if (m->where.line_number != -1)
156 ds_put_format (&string, "%d:", m->where.line_number);
157 ds_put_char (&string, ' ');
160 if (severity->name != NULL)
161 ds_put_format (&string, "%s: ", gettext (severity->name));
163 if (severity->count != NULL)
166 if (category->show_command_name && msg_get_command_name () != NULL)
167 ds_put_format (&string, "%s: ", msg_get_command_name ());
169 ds_put_cstr (&string, m->text);
171 if (msg_file != stdout || get_error_routing_to_terminal ())
172 dump_message (ds_cstr (&string), get_viewwidth (), 8, msg_file);
174 ds_destroy (&string);
177 /* Divides MSG into lines of WIDTH width for the first line and
178 WIDTH - INDENT width for each succeeding line, and writes the
181 dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
183 size_t length = strlen (msg);
184 char *string, *breaks;
186 size_t line_start, i;
188 /* Allocate temporary buffers.
189 If we can't get memory for them, then just dump the whole
191 string = strdup (msg);
192 breaks = malloc (length);
193 if (string == NULL || breaks == NULL)
202 /* Break into lines. */
203 if (indent > width / 3)
205 mbs_width_linebreaks (string, length,
206 width - indent, -indent, 0,
207 NULL, locale_charset (), breaks);
209 /* Write out lines. */
212 for (i = 0; i < length; i++)
215 case UC_BREAK_POSSIBLE:
216 /* Break before this character,
217 and include this character in the next line. */
218 dump_line (line_indent, &string[line_start], i - line_start, stream);
220 line_indent = indent;
222 case UC_BREAK_MANDATORY:
223 /* Break before this character,
224 but don't include this character in the next line
225 (because it'string a new-line). */
226 dump_line (line_indent, &string[line_start], i - line_start, stream);
228 line_indent = indent;
233 if (line_start < length)
234 dump_line (line_indent, &string[line_start], length - line_start, stream);
240 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
241 a new-line to STREAM. */
243 dump_line (int line_indent, const char *line, size_t length, FILE *stream)
246 for (i = 0; i < line_indent; i++)
248 fwrite (line, 1, length, stream);