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 <language/line-buffer.h>
28 #include <data/settings.h>
29 #include <libpspp/message.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;
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 (handle_msg, get_msg_location);
76 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 (void)
86 if (!getl_is_interactive ())
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 ();
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);