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"
23 #include <libpspp/msg-locator.h>
24 #include <libpspp/getl.h>
25 #include <data/settings.h>
26 #include <libpspp/message.h>
27 #include <libpspp/str.h>
28 #include <output/journal.h>
29 #include <output/output.h>
30 #include <output/table.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
39 /* Number of errors, warnings reported. */
40 static int error_count;
41 static int warning_count;
42 static const char *error_file;
44 static void handle_msg (const struct msg *);
46 static FILE *msg_file ;
49 msg_ui_set_error_file (const char *filename)
51 error_file = filename;
55 msg_ui_init (struct source_stream *ss)
61 msg_file = fopen (error_file, "a");
62 if ( NULL == msg_file )
65 printf ( _("Cannot open %s (%s). "
66 "Writing errors to stdout instead.\n"),
67 error_file, strerror(err) );
71 msg_init (ss, handle_msg);
80 if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
84 /* Checks whether we've had so many errors that it's time to quit
85 processing this syntax file. */
87 check_msg_count (struct source_stream *ss)
89 if (!getl_is_interactive (ss))
91 if (get_errorbreak () && error_count)
92 msg (MN, _("Terminating execution of syntax file due to error."));
93 else if (error_count > get_mxerrs() )
94 msg (MN, _("Errors (%d) exceeds limit (%d)."),
95 error_count, get_mxerrs());
96 else if (error_count + warning_count > get_mxwarns() )
97 msg (MN, _("Warnings (%d) exceed limit (%d)."),
98 error_count + warning_count, get_mxwarns() );
102 getl_abort_noninteractive (ss);
107 reset_msg_count (void)
109 error_count = warning_count = 0;
115 return error_count > 0;
118 typedef void write_line_func (int indent, struct substring line, void *aux);
119 static void dump_message (char *msg, unsigned width, unsigned indent,
120 write_line_func *, void *aux);
121 static write_line_func write_stream;
122 static write_line_func write_journal;
125 handle_msg (const struct msg *m)
129 bool show_command_name; /* Show command name with error? */
130 bool show_file_location; /* Show syntax file location? */
133 static const struct category categories[] =
135 {false, false}, /* MSG_GENERAL. */
136 {true, true}, /* MSG_SYNTAX. */
137 {false, true}, /* MSG_DATA. */
142 const char *name; /* How to identify this severity. */
143 int *count; /* Number of msgs with this severity so far. */
146 static struct severity severities[] =
148 {N_("error"), &error_count}, /* MSG_ERROR. */
149 {N_("warning"), &warning_count}, /* MSG_WARNING. */
150 {NULL, NULL}, /* MSG_NOTE. */
153 const struct category *category = &categories[m->category];
154 const struct severity *severity = &severities[m->severity];
155 struct string string = DS_EMPTY_INITIALIZER;
157 if (category->show_file_location && m->where.file_name)
159 ds_put_format (&string, "%s:", m->where.file_name);
160 if (m->where.line_number != -1)
161 ds_put_format (&string, "%d:", m->where.line_number);
162 ds_put_char (&string, ' ');
165 if (severity->name != NULL)
166 ds_put_format (&string, "%s: ", gettext (severity->name));
168 if (severity->count != NULL)
171 if (category->show_command_name && msg_get_command_name () != NULL)
172 ds_put_format (&string, "%s: ", msg_get_command_name ());
174 ds_put_cstr (&string, m->text);
176 if (msg_file != stdout || get_error_routing_to_terminal ())
177 dump_message (ds_cstr (&string), get_viewwidth (), 8,
178 write_stream, msg_file);
180 dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
182 if (get_error_routing_to_listing ())
184 /* Disable screen output devices, because the error should
185 already have been reported to the screen with the
186 dump_message call above. */
187 outp_enable_device (false, OUTP_DEV_SCREEN);
188 tab_output_text (TAB_LEFT, ds_cstr (&string));
189 outp_enable_device (true, OUTP_DEV_SCREEN);
192 ds_destroy (&string);
195 /* Divides MSG into lines of WIDTH width for the first line and
196 WIDTH - INDENT width for each succeeding line, and writes the
197 lines by calling DUMP_LINE for each line, passing AUX as
200 dump_message (char *msg, unsigned width, unsigned indent,
201 write_line_func *dump_line, void *aux)
203 size_t length = strlen (msg);
204 char *string, *breaks;
206 size_t line_start, i;
208 /* Allocate temporary buffers.
209 If we can't get memory for them, then just dump the whole
211 string = strdup (msg);
212 breaks = malloc (length);
213 if (string == NULL || breaks == NULL)
217 dump_line (0, ss_cstr (msg), aux);
221 /* Break into lines. */
222 if (indent > width / 3)
224 mbs_width_linebreaks (string, length,
225 width - indent, -indent, 0,
226 NULL, locale_charset (), breaks);
228 /* Write out lines. */
231 for (i = 0; i < length; i++)
232 if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
234 dump_line (line_indent,
235 ss_buffer (string + line_start, i - line_start), aux);
236 line_indent = indent;
238 /* UC_BREAK_POSSIBLE means that a line break can be
239 inserted, and that the character should be included
241 UC_BREAK_MANDATORY means that this character is a line
242 break, so it should not be included in the next line. */
243 line_start = i + (breaks[i] == UC_BREAK_MANDATORY);
245 if (line_start < length)
246 dump_line (line_indent,
247 ss_buffer (string + line_start, length - line_start), aux);
253 /* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
255 write_stream (int line_indent, struct substring line, void *stream_)
257 FILE *stream = stream_;
259 for (i = 0; i < line_indent; i++)
261 fwrite (ss_data (line), 1, ss_length (line), stream);
265 /* Writes LINE to the journal. */
267 write_journal (int line_indent, struct substring line, void *unused UNUSED)
269 char *s = xstrndup (ss_data (line), ss_length (line));
270 journal_write (true, s);