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);
79 /* Checks whether we've had so many errors that it's time to quit
80 processing this syntax file. */
82 check_msg_count (void)
84 if (!getl_is_interactive ())
86 if (get_errorbreak () && error_count)
87 msg (MN, _("Terminating execution of syntax file due to error."));
88 else if (error_count > get_mxerrs() )
89 msg (MN, _("Errors (%d) exceeds limit (%d)."),
90 error_count, get_mxerrs());
91 else if (error_count + warning_count > get_mxwarns() )
92 msg (MN, _("Warnings (%d) exceed limit (%d)."),
93 error_count + warning_count, get_mxwarns() );
97 getl_abort_noninteractive ();
102 reset_msg_count (void)
104 error_count = warning_count = 0;
110 return error_count > 0;
113 static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
114 static void dump_line (int line_indent, const char *line, size_t length,
118 handle_msg (const struct msg *m)
122 bool show_command_name; /* Show command name with error? */
123 bool show_file_location; /* Show syntax file location? */
126 static const struct category categories[] =
128 {false, false}, /* MSG_GENERAL. */
129 {true, true}, /* MSG_SYNTAX. */
130 {false, true}, /* MSG_DATA. */
135 const char *name; /* How to identify this severity. */
136 int *count; /* Number of msgs with this severity so far. */
139 static struct severity severities[] =
141 {N_("error"), &error_count}, /* MSG_ERROR. */
142 {N_("warning"), &warning_count}, /* MSG_WARNING. */
143 {NULL, NULL}, /* MSG_NOTE. */
146 const struct category *category = &categories[m->category];
147 const struct severity *severity = &severities[m->severity];
148 struct string string = DS_EMPTY_INITIALIZER;
150 if (category->show_file_location && m->where.file_name)
152 ds_put_format (&string, "%s:", m->where.file_name);
153 if (m->where.line_number != -1)
154 ds_put_format (&string, "%d:", m->where.line_number);
155 ds_put_char (&string, ' ');
158 if (severity->name != NULL)
159 ds_put_format (&string, "%s: ", gettext (severity->name));
161 if (severity->count != NULL)
164 if (category->show_command_name && msg_get_command_name () != NULL)
165 ds_put_format (&string, "%s: ", msg_get_command_name ());
167 ds_put_cstr (&string, m->text);
169 if (msg_file != stdout || get_error_routing_to_terminal ())
170 dump_message (ds_cstr (&string), get_viewwidth (), 8, msg_file);
172 ds_destroy (&string);
175 /* Divides MSG into lines of WIDTH width for the first line and
176 WIDTH - INDENT width for each succeeding line, and writes the
179 dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
181 size_t length = strlen (msg);
182 char *string, *breaks;
184 size_t line_start, i;
186 /* Allocate temporary buffers.
187 If we can't get memory for them, then just dump the whole
189 string = strdup (msg);
190 breaks = malloc (length);
191 if (string == NULL || breaks == NULL)
200 /* Break into lines. */
201 if (indent > width / 3)
203 mbs_width_linebreaks (string, length,
204 width - indent, -indent, 0,
205 NULL, locale_charset (), breaks);
207 /* Write out lines. */
210 for (i = 0; i < length; i++)
213 case UC_BREAK_POSSIBLE:
214 /* Break before this character,
215 and include this character in the next line. */
216 dump_line (line_indent, &string[line_start], i - line_start, stream);
218 line_indent = indent;
220 case UC_BREAK_MANDATORY:
221 /* Break before this character,
222 but don't include this character in the next line
223 (because it'string a new-line). */
224 dump_line (line_indent, &string[line_start], i - line_start, stream);
226 line_indent = indent;
231 if (line_start < length)
232 dump_line (line_indent, &string[line_start], length - line_start, stream);
238 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
239 a new-line to STREAM. */
241 dump_line (int line_indent, const char *line, size_t length, FILE *stream)
244 for (i = 0; i < line_indent; i++)
246 fwrite (line, 1, length, stream);