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 int max_errors = settings_get_max_messages (MSG_S_ERROR);
96 int max_warnings = settings_get_max_messages (MSG_S_WARNING);
98 if (error_count > max_errors)
99 msg (MN, _("Errors (%d) exceed limit (%d)."),
100 error_count, max_errors);
101 else if (error_count + warning_count > max_warnings)
102 msg (MN, _("Warnings (%d) exceed limit (%d)."),
103 error_count + warning_count, max_warnings);
107 getl_abort_noninteractive (ss);
112 reset_msg_count (void)
114 error_count = warning_count = 0;
120 return error_count > 0;
123 typedef void write_line_func (int indent, struct substring line, void *aux);
124 static void dump_message (char *msg, unsigned width, unsigned indent,
125 write_line_func *, void *aux);
126 static write_line_func write_stream;
127 static write_line_func write_journal;
130 handle_msg (const struct msg *m)
134 bool show_command_name; /* Show command name with error? */
135 bool show_file_location; /* Show syntax file location? */
138 static const struct category categories[] =
140 {false, false}, /* MSG_GENERAL. */
141 {true, true}, /* MSG_SYNTAX. */
142 {false, true}, /* MSG_DATA. */
147 enum settings_output_type type;
148 const char *name; /* How to identify this severity. */
149 int *count; /* Number of msgs with this severity so far. */
152 static struct severity severities[] =
154 { SETTINGS_OUTPUT_ERROR, N_("error"), &error_count },
155 { SETTINGS_OUTPUT_ERROR, N_("warning"), &warning_count },
156 { SETTINGS_OUTPUT_NOTE, NULL, NULL},
159 const struct category *category = &categories[m->category];
160 const struct severity *severity = &severities[m->severity];
161 struct string string = DS_EMPTY_INITIALIZER;
162 enum settings_output_devices routing;
164 if (category->show_file_location && m->where.file_name)
166 ds_put_format (&string, "%s:", m->where.file_name);
167 if (m->where.line_number != -1)
168 ds_put_format (&string, "%d:", m->where.line_number);
169 ds_put_char (&string, ' ');
172 if (severity->name != NULL)
173 ds_put_format (&string, "%s: ", gettext (severity->name));
175 if (severity->count != NULL)
178 if (category->show_command_name && msg_get_command_name () != NULL)
179 ds_put_format (&string, "%s: ", msg_get_command_name ());
181 ds_put_cstr (&string, m->text);
183 routing = settings_get_output_routing (severity->type);
184 if (msg_file != stdout || routing & SETTINGS_DEVICE_TERMINAL)
185 dump_message (ds_cstr (&string),
186 isatty (fileno (msg_file)) ? settings_get_viewwidth () : INT_MAX, 8,
187 write_stream, msg_file);
189 dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
191 if (routing & SETTINGS_DEVICE_LISTING)
193 /* Disable terminal output devices, because the error should already have
194 been reported to the terminal with the dump_message call above. */
195 settings_set_output_routing (severity->type,
196 routing & ~SETTINGS_DEVICE_TERMINAL);
197 tab_output_text (TAB_LEFT, ds_cstr (&string));
198 settings_set_output_routing (severity->type, routing);
201 ds_destroy (&string);
204 /* Divides MSG into lines of WIDTH width for the first line and
205 WIDTH - INDENT width for each succeeding line, and writes the
206 lines by calling DUMP_LINE for each line, passing AUX as
209 dump_message (char *msg, unsigned width, unsigned indent,
210 write_line_func *dump_line, void *aux)
212 size_t length = strlen (msg);
213 char *string, *breaks;
215 size_t line_start, i;
217 /* Allocate temporary buffers.
218 If we can't get memory for them, then just dump the whole
220 string = strdup (msg);
221 breaks = malloc (length);
222 if (string == NULL || breaks == NULL)
226 dump_line (0, ss_cstr (msg), aux);
230 /* Break into lines. */
231 if (indent > width / 3)
233 ulc_width_linebreaks (string, length,
234 width - indent, -indent, 0,
235 NULL, locale_charset (), breaks);
237 /* Write out lines. */
240 for (i = 0; i < length; i++)
241 if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
243 dump_line (line_indent,
244 ss_buffer (string + line_start, i - line_start), aux);
245 line_indent = indent;
247 /* UC_BREAK_POSSIBLE means that a line break can be
248 inserted, and that the character should be included
250 UC_BREAK_MANDATORY means that this character is a line
251 break, so it should not be included in the next line. */
252 line_start = i + (breaks[i] == UC_BREAK_MANDATORY);
254 if (line_start < length)
255 dump_line (line_indent,
256 ss_buffer (string + line_start, length - line_start), aux);
262 /* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
264 write_stream (int line_indent, struct substring line, void *stream_)
266 FILE *stream = stream_;
268 for (i = 0; i < line_indent; i++)
270 fwrite (ss_data (line), 1, ss_length (line), stream);
274 /* Writes LINE to the journal. */
276 write_journal (int line_indent UNUSED, struct substring line, void *unused UNUSED)
278 char *s = xstrndup (ss_data (line), ss_length (line));
279 journal_write (true, s);