1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2010 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 <data/settings.h>
22 #include <libpspp/getl.h>
23 #include <libpspp/message.h>
24 #include <libpspp/msg-locator.h>
25 #include <libpspp/str.h>
26 #include <output/journal.h>
27 #include <output/driver.h>
28 #include <output/tab.h>
37 #include "localcharset.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
43 /* Number of errors, warnings reported. */
44 static int error_count;
45 static int warning_count;
46 static const char *error_file;
48 static void handle_msg (const struct msg *);
50 static FILE *msg_file ;
53 msg_ui_set_error_file (const char *filename)
55 error_file = filename;
59 msg_ui_init (struct source_stream *ss)
65 msg_file = fopen (error_file, "a");
66 if ( NULL == msg_file )
69 printf ( _("Cannot open %s (%s). "
70 "Writing errors to stdout instead.\n"),
71 error_file, strerror(err) );
75 msg_init (ss, handle_msg);
84 if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
88 /* Checks whether we've had so many errors that it's time to quit
89 processing this syntax file. */
91 check_msg_count (struct source_stream *ss)
93 if (!getl_is_interactive (ss))
95 if (settings_get_errorbreak () && error_count)
96 msg (MN, _("Terminating execution of syntax file due to error."));
97 else if (error_count > settings_get_mxerrs () )
98 msg (MN, _("Errors (%d) exceed limit (%d)."),
99 error_count, settings_get_mxerrs ());
100 else if (error_count + warning_count > settings_get_mxwarns () )
101 msg (MN, _("Warnings (%d) exceed limit (%d)."),
102 error_count + warning_count, settings_get_mxwarns () );
106 getl_abort_noninteractive (ss);
111 reset_msg_count (void)
113 error_count = warning_count = 0;
119 return error_count > 0;
122 typedef void write_line_func (int indent, struct substring line, void *aux);
123 static void dump_message (char *msg, unsigned width, unsigned indent,
124 write_line_func *, void *aux);
125 static write_line_func write_stream;
126 static write_line_func write_journal;
129 handle_msg (const struct msg *m)
133 bool show_command_name; /* Show command name with error? */
134 bool show_file_location; /* Show syntax file location? */
137 static const struct category categories[] =
139 {false, false}, /* MSG_GENERAL. */
140 {true, true}, /* MSG_SYNTAX. */
141 {false, true}, /* MSG_DATA. */
146 const char *name; /* How to identify this severity. */
147 int *count; /* Number of msgs with this severity so far. */
150 static struct severity severities[] =
152 {N_("error"), &error_count}, /* MSG_ERROR. */
153 {N_("warning"), &warning_count}, /* MSG_WARNING. */
154 {NULL, NULL}, /* MSG_NOTE. */
157 const struct category *category = &categories[m->category];
158 const struct severity *severity = &severities[m->severity];
159 struct string string = DS_EMPTY_INITIALIZER;
161 if (category->show_file_location && m->where.file_name)
163 ds_put_format (&string, "%s:", m->where.file_name);
164 if (m->where.line_number != -1)
165 ds_put_format (&string, "%d:", m->where.line_number);
166 ds_put_char (&string, ' ');
169 if (severity->name != NULL)
170 ds_put_format (&string, "%s: ", gettext (severity->name));
172 if (severity->count != NULL)
175 if (category->show_command_name && msg_get_command_name () != NULL)
176 ds_put_format (&string, "%s: ", msg_get_command_name ());
178 ds_put_cstr (&string, m->text);
180 if (msg_file != stdout || settings_get_error_routing_to_terminal ())
181 dump_message (ds_cstr (&string),
182 isatty (fileno (msg_file)) ? settings_get_viewwidth () : INT_MAX, 8,
183 write_stream, msg_file);
185 dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
187 if (settings_get_error_routing_to_listing ())
189 /* Disable screen output devices, because the error should
190 already have been reported to the screen with the
191 dump_message call above. */
192 output_set_type_enabled (false, OUTPUT_DEVICE_SCREEN);
193 tab_output_text (TAB_LEFT, ds_cstr (&string));
194 output_set_type_enabled (true, OUTPUT_DEVICE_SCREEN);
197 ds_destroy (&string);
200 /* Divides MSG into lines of WIDTH width for the first line and
201 WIDTH - INDENT width for each succeeding line, and writes the
202 lines by calling DUMP_LINE for each line, passing AUX as
205 dump_message (char *msg, unsigned width, unsigned indent,
206 write_line_func *dump_line, void *aux)
208 size_t length = strlen (msg);
209 char *string, *breaks;
211 size_t line_start, i;
213 /* Allocate temporary buffers.
214 If we can't get memory for them, then just dump the whole
216 string = strdup (msg);
217 breaks = malloc (length);
218 if (string == NULL || breaks == NULL)
222 dump_line (0, ss_cstr (msg), aux);
226 /* Break into lines. */
227 if (indent > width / 3)
229 ulc_width_linebreaks (string, length,
230 width - indent, -indent, 0,
231 NULL, locale_charset (), breaks);
233 /* Write out lines. */
236 for (i = 0; i < length; i++)
237 if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
239 dump_line (line_indent,
240 ss_buffer (string + line_start, i - line_start), aux);
241 line_indent = indent;
243 /* UC_BREAK_POSSIBLE means that a line break can be
244 inserted, and that the character should be included
246 UC_BREAK_MANDATORY means that this character is a line
247 break, so it should not be included in the next line. */
248 line_start = i + (breaks[i] == UC_BREAK_MANDATORY);
250 if (line_start < length)
251 dump_line (line_indent,
252 ss_buffer (string + line_start, length - line_start), aux);
258 /* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
260 write_stream (int line_indent, struct substring line, void *stream_)
262 FILE *stream = stream_;
264 for (i = 0; i < line_indent; i++)
266 fwrite (ss_data (line), 1, ss_length (line), stream);
270 /* Writes LINE to the journal. */
272 write_journal (int line_indent UNUSED, struct substring line, void *unused UNUSED)
274 char *s = xstrndup (ss_data (line), ss_length (line));
275 journal_write (true, s);