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"
22 #include "localcharset.h"
24 #include <libpspp/msg-locator.h>
25 #include <libpspp/getl.h>
26 #include <data/settings.h>
27 #include <libpspp/message.h>
28 #include <libpspp/str.h>
29 #include <output/journal.h>
30 #include <output/output.h>
31 #include <output/table.h>
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
40 /* Number of errors, warnings reported. */
41 static int error_count;
42 static int warning_count;
43 static const char *error_file;
45 static void handle_msg (const struct msg *);
47 static FILE *msg_file ;
50 msg_ui_set_error_file (const char *filename)
52 error_file = filename;
56 msg_ui_init (struct source_stream *ss)
62 msg_file = fopen (error_file, "a");
63 if ( NULL == msg_file )
66 printf ( _("Cannot open %s (%s). "
67 "Writing errors to stdout instead.\n"),
68 error_file, strerror(err) );
72 msg_init (ss, handle_msg);
81 if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
85 /* Checks whether we've had so many errors that it's time to quit
86 processing this syntax file. */
88 check_msg_count (struct source_stream *ss)
90 if (!getl_is_interactive (ss))
92 if (get_errorbreak () && error_count)
93 msg (MN, _("Terminating execution of syntax file due to error."));
94 else if (error_count > get_mxerrs() )
95 msg (MN, _("Errors (%d) exceeds limit (%d)."),
96 error_count, get_mxerrs());
97 else if (error_count + warning_count > get_mxwarns() )
98 msg (MN, _("Warnings (%d) exceed limit (%d)."),
99 error_count + warning_count, get_mxwarns() );
103 getl_abort_noninteractive (ss);
108 reset_msg_count (void)
110 error_count = warning_count = 0;
116 return error_count > 0;
119 typedef void write_line_func (int indent, struct substring line, void *aux);
120 static void dump_message (char *msg, unsigned width, unsigned indent,
121 write_line_func *, void *aux);
122 static write_line_func write_stream;
123 static write_line_func write_journal;
126 handle_msg (const struct msg *m)
130 bool show_command_name; /* Show command name with error? */
131 bool show_file_location; /* Show syntax file location? */
134 static const struct category categories[] =
136 {false, false}, /* MSG_GENERAL. */
137 {true, true}, /* MSG_SYNTAX. */
138 {false, true}, /* MSG_DATA. */
143 const char *name; /* How to identify this severity. */
144 int *count; /* Number of msgs with this severity so far. */
147 static struct severity severities[] =
149 {N_("error"), &error_count}, /* MSG_ERROR. */
150 {N_("warning"), &warning_count}, /* MSG_WARNING. */
151 {NULL, NULL}, /* MSG_NOTE. */
154 const struct category *category = &categories[m->category];
155 const struct severity *severity = &severities[m->severity];
156 struct string string = DS_EMPTY_INITIALIZER;
158 if (category->show_file_location && m->where.file_name)
160 ds_put_format (&string, "%s:", m->where.file_name);
161 if (m->where.line_number != -1)
162 ds_put_format (&string, "%d:", m->where.line_number);
163 ds_put_char (&string, ' ');
166 if (severity->name != NULL)
167 ds_put_format (&string, "%s: ", gettext (severity->name));
169 if (severity->count != NULL)
172 if (category->show_command_name && msg_get_command_name () != NULL)
173 ds_put_format (&string, "%s: ", msg_get_command_name ());
175 ds_put_cstr (&string, m->text);
177 if (msg_file != stdout || get_error_routing_to_terminal ())
178 dump_message (ds_cstr (&string), get_viewwidth (), 8,
179 write_stream, msg_file);
181 dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
183 if (get_error_routing_to_listing ())
185 /* Disable screen output devices, because the error should
186 already have been reported to the screen with the
187 dump_message call above. */
188 outp_enable_device (false, OUTP_DEV_SCREEN);
189 tab_output_text (TAB_LEFT, ds_cstr (&string));
190 outp_enable_device (true, OUTP_DEV_SCREEN);
193 ds_destroy (&string);
196 /* Divides MSG into lines of WIDTH width for the first line and
197 WIDTH - INDENT width for each succeeding line, and writes the
198 lines by calling DUMP_LINE for each line, passing AUX as
201 dump_message (char *msg, unsigned width, unsigned indent,
202 write_line_func *dump_line, void *aux)
204 size_t length = strlen (msg);
205 char *string, *breaks;
207 size_t line_start, i;
209 /* Allocate temporary buffers.
210 If we can't get memory for them, then just dump the whole
212 string = strdup (msg);
213 breaks = malloc (length);
214 if (string == NULL || breaks == NULL)
218 dump_line (0, ss_cstr (msg), aux);
222 /* Break into lines. */
223 if (indent > width / 3)
225 mbs_width_linebreaks (string, length,
226 width - indent, -indent, 0,
227 NULL, locale_charset (), breaks);
229 /* Write out lines. */
232 for (i = 0; i < length; i++)
233 if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
235 dump_line (line_indent,
236 ss_buffer (string + line_start, i - line_start), aux);
237 line_indent = indent;
239 /* UC_BREAK_POSSIBLE means that a line break can be
240 inserted, and that the character should be included
242 UC_BREAK_MANDATORY means that this character is a line
243 break, so it should not be included in the next line. */
244 line_start = i + (breaks[i] == UC_BREAK_MANDATORY);
246 if (line_start < length)
247 dump_line (line_indent,
248 ss_buffer (string + line_start, length - line_start), aux);
254 /* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
256 write_stream (int line_indent, struct substring line, void *stream_)
258 FILE *stream = stream_;
260 for (i = 0; i < line_indent; i++)
262 fwrite (ss_data (line), 1, ss_length (line), stream);
266 /* Writes LINE to the journal. */
268 write_journal (int line_indent, struct substring line, void *unused UNUSED)
270 char *s = xstrndup (ss_data (line), ss_length (line));
271 journal_write (true, s);