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/>. */
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>
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
41 /* Number of errors, warnings reported. */
42 static int error_count;
43 static int warning_count;
44 static const char *error_file;
46 static void handle_msg (const struct msg *);
48 static FILE *msg_file ;
51 msg_ui_set_error_file (const char *filename)
53 error_file = filename;
57 msg_ui_init (struct source_stream *ss)
63 msg_file = fopen (error_file, "a");
64 if ( NULL == msg_file )
67 printf ( _("Cannot open %s (%s). "
68 "Writing errors to stdout instead.\n"),
69 error_file, strerror(err) );
73 msg_init (ss, handle_msg);
82 if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
86 /* Checks whether we've had so many errors that it's time to quit
87 processing this syntax file. */
89 check_msg_count (struct source_stream *ss)
91 if (!getl_is_interactive (ss))
93 if (settings_get_errorbreak () && error_count)
94 msg (MN, _("Terminating execution of syntax file due to error."));
95 else if (error_count > settings_get_mxerrs () )
96 msg (MN, _("Errors (%d) exceeds limit (%d)."),
97 error_count, settings_get_mxerrs ());
98 else if (error_count + warning_count > settings_get_mxwarns () )
99 msg (MN, _("Warnings (%d) exceed limit (%d)."),
100 error_count + warning_count, settings_get_mxwarns () );
104 getl_abort_noninteractive (ss);
109 reset_msg_count (void)
111 error_count = warning_count = 0;
117 return error_count > 0;
120 typedef void write_line_func (int indent, struct substring line, void *aux);
121 static void dump_message (char *msg, unsigned width, unsigned indent,
122 write_line_func *, void *aux);
123 static write_line_func write_stream;
124 static write_line_func write_journal;
127 handle_msg (const struct msg *m)
131 bool show_command_name; /* Show command name with error? */
132 bool show_file_location; /* Show syntax file location? */
135 static const struct category categories[] =
137 {false, false}, /* MSG_GENERAL. */
138 {true, true}, /* MSG_SYNTAX. */
139 {false, true}, /* MSG_DATA. */
144 const char *name; /* How to identify this severity. */
145 int *count; /* Number of msgs with this severity so far. */
148 static struct severity severities[] =
150 {N_("error"), &error_count}, /* MSG_ERROR. */
151 {N_("warning"), &warning_count}, /* MSG_WARNING. */
152 {NULL, NULL}, /* MSG_NOTE. */
155 const struct category *category = &categories[m->category];
156 const struct severity *severity = &severities[m->severity];
157 struct string string = DS_EMPTY_INITIALIZER;
159 if (category->show_file_location && m->where.file_name)
161 ds_put_format (&string, "%s:", m->where.file_name);
162 if (m->where.line_number != -1)
163 ds_put_format (&string, "%d:", m->where.line_number);
164 ds_put_char (&string, ' ');
167 if (severity->name != NULL)
168 ds_put_format (&string, "%s: ", gettext (severity->name));
170 if (severity->count != NULL)
173 if (category->show_command_name && msg_get_command_name () != NULL)
174 ds_put_format (&string, "%s: ", msg_get_command_name ());
176 ds_put_cstr (&string, m->text);
178 if (msg_file != stdout || settings_get_error_routing_to_terminal ())
179 dump_message (ds_cstr (&string),
180 isatty (fileno (msg_file)) ? settings_get_viewwidth () : INT_MAX, 8,
181 write_stream, msg_file);
183 dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
185 if (settings_get_error_routing_to_listing ())
187 /* Disable screen output devices, because the error should
188 already have been reported to the screen with the
189 dump_message call above. */
190 outp_enable_device (false, OUTP_DEV_SCREEN);
191 tab_output_text (TAB_LEFT, ds_cstr (&string));
192 outp_enable_device (true, OUTP_DEV_SCREEN);
195 ds_destroy (&string);
198 /* Divides MSG into lines of WIDTH width for the first line and
199 WIDTH - INDENT width for each succeeding line, and writes the
200 lines by calling DUMP_LINE for each line, passing AUX as
203 dump_message (char *msg, unsigned width, unsigned indent,
204 write_line_func *dump_line, void *aux)
206 size_t length = strlen (msg);
207 char *string, *breaks;
209 size_t line_start, i;
211 /* Allocate temporary buffers.
212 If we can't get memory for them, then just dump the whole
214 string = strdup (msg);
215 breaks = malloc (length);
216 if (string == NULL || breaks == NULL)
220 dump_line (0, ss_cstr (msg), aux);
224 /* Break into lines. */
225 if (indent > width / 3)
227 ulc_width_linebreaks (string, length,
228 width - indent, -indent, 0,
229 NULL, locale_charset (), breaks);
231 /* Write out lines. */
234 for (i = 0; i < length; i++)
235 if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
237 dump_line (line_indent,
238 ss_buffer (string + line_start, i - line_start), aux);
239 line_indent = indent;
241 /* UC_BREAK_POSSIBLE means that a line break can be
242 inserted, and that the character should be included
244 UC_BREAK_MANDATORY means that this character is a line
245 break, so it should not be included in the next line. */
246 line_start = i + (breaks[i] == UC_BREAK_MANDATORY);
248 if (line_start < length)
249 dump_line (line_indent,
250 ss_buffer (string + line_start, length - line_start), aux);
256 /* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
258 write_stream (int line_indent, struct substring line, void *stream_)
260 FILE *stream = stream_;
262 for (i = 0; i < line_indent; i++)
264 fwrite (ss_data (line), 1, ss_length (line), stream);
268 /* Writes LINE to the journal. */
270 write_journal (int line_indent UNUSED, struct substring line, void *unused UNUSED)
272 char *s = xstrndup (ss_data (line), ss_length (line));
273 journal_write (true, s);