1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "linebreak.h"
27 #include <language/line-buffer.h>
28 #include <data/settings.h>
29 #include <libpspp/message.h>
32 #define _(msgid) gettext (msgid)
33 #define N_(msgid) msgid
35 /* Number of errors, warnings reported. */
36 static int error_count;
37 static int warning_count;
39 static void handle_msg (const struct msg *);
44 msg_init (handle_msg, get_msg_location);
54 /* Checks whether we've had so many errors that it's time to quit
55 processing this syntax file. */
57 check_msg_count (void)
59 if (!getl_is_interactive ())
61 if (get_errorbreak () && error_count)
62 msg (MN, _("Terminating execution of syntax file due to error."));
63 else if (error_count > get_mxerrs() )
64 msg (MN, _("Errors (%d) exceeds limit (%d)."),
65 error_count, get_mxerrs());
66 else if (error_count + warning_count > get_mxwarns() )
67 msg (MN, _("Warnings (%d) exceed limit (%d)."),
68 error_count + warning_count, get_mxwarns() );
72 getl_abort_noninteractive ();
77 reset_msg_count (void)
79 error_count = warning_count = 0;
85 return error_count > 0;
88 static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
89 static void dump_line (int line_indent, const char *line, size_t length,
93 handle_msg (const struct msg *m)
97 bool show_command_name; /* Show command name with error? */
98 bool show_file_location; /* Show syntax file location? */
101 static const struct category categories[] =
103 {false, false}, /* MSG_GENERAL. */
104 {true, true}, /* MSG_SYNTAX. */
105 {false, true}, /* MSG_DATA. */
110 const char *name; /* How to identify this severity. */
111 int *count; /* Number of msgs with this severity so far. */
114 static struct severity severities[] =
116 {N_("error"), &error_count}, /* MSG_ERROR. */
117 {N_("warning"), &warning_count}, /* MSG_WARNING. */
118 {NULL, NULL}, /* MSG_NOTE. */
121 const struct category *category = &categories[m->category];
122 const struct severity *severity = &severities[m->severity];
123 struct string string = DS_EMPTY_INITIALIZER;
125 if (category->show_file_location && m->where.file_name)
127 ds_put_format (&string, "%s:", m->where.file_name);
128 if (m->where.line_number != -1)
129 ds_put_format (&string, "%d:", m->where.line_number);
130 ds_put_char (&string, ' ');
133 if (severity->name != NULL)
134 ds_put_format (&string, "%s: ", gettext (severity->name));
136 if (severity->count != NULL)
139 if (category->show_command_name && msg_get_command_name () != NULL)
140 ds_put_format (&string, "%s: ", msg_get_command_name ());
142 ds_put_cstr (&string, m->text);
144 /* FIXME: Check set_messages and set_errors to determine where to
145 send errors and messages. */
146 dump_message (ds_cstr (&string), get_viewwidth (), 8, stdout);
148 ds_destroy (&string);
151 /* Divides MSG into lines of WIDTH width for the first line and
152 WIDTH - INDENT width for each succeeding line, and writes the
155 dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
157 size_t length = strlen (msg);
158 char *string, *breaks;
160 size_t line_start, i;
162 /* Allocate temporary buffers.
163 If we can't get memory for them, then just dump the whole
165 string = strdup (msg);
166 breaks = malloc (length);
167 if (string == NULL || breaks == NULL)
176 /* Break into lines. */
177 if (indent > width / 3)
179 mbs_width_linebreaks (string, length,
180 width - indent, -indent, 0,
181 NULL, locale_charset (), breaks);
183 /* Write out lines. */
186 for (i = 0; i < length; i++)
189 case UC_BREAK_POSSIBLE:
190 /* Break before this character,
191 and include this character in the next line. */
192 dump_line (line_indent, &string[line_start], i - line_start, stream);
194 line_indent = indent;
196 case UC_BREAK_MANDATORY:
197 /* Break before this character,
198 but don't include this character in the next line
199 (because it'string a new-line). */
200 dump_line (line_indent, &string[line_start], i - line_start, stream);
202 line_indent = indent;
207 if (line_start < length)
208 dump_line (line_indent, &string[line_start], length - line_start, stream);
214 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
215 a new-line to STREAM. */
217 dump_line (int line_indent, const char *line, size_t length, FILE *stream)
220 for (i = 0; i < line_indent; i++)
222 fwrite (line, 1, length, stream);