Send error message to non-screen output devices also, if routing to
[pspp-builds.git] / src / ui / terminal / msg-ui.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "msg-ui.h"
20
21 #include "linebreak.h"
22
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>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
38
39 /* Number of errors, warnings reported. */
40 static int error_count;
41 static int warning_count;
42 static const char *error_file;
43
44 static void handle_msg (const struct msg *);
45
46 static FILE *msg_file ;
47
48 void
49 msg_ui_set_error_file (const char *filename)
50 {
51   error_file = filename;
52 }
53
54 void
55 msg_ui_init (struct source_stream *ss)
56 {
57   msg_file = stdout;
58
59   if ( error_file )
60     {
61       msg_file = fopen (error_file, "a");
62       if ( NULL == msg_file )
63         {
64           int err = errno;
65           printf ( _("Cannot open %s (%s). "
66                      "Writing errors to stdout instead.\n"),
67                    error_file, strerror(err) );
68           msg_file = stdout;
69         }
70     }
71   msg_init (ss, handle_msg);
72 }
73
74 void
75 msg_ui_done (void)
76 {
77   msg_done ();
78   msg_locator_done ();
79
80   if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
81     fclose (msg_file);
82 }
83
84 /* Checks whether we've had so many errors that it's time to quit
85    processing this syntax file. */
86 void
87 check_msg_count (struct source_stream *ss)
88 {
89   if (!getl_is_interactive (ss))
90     {
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() );
99       else
100         return;
101
102       getl_abort_noninteractive (ss);
103     }
104 }
105
106 void
107 reset_msg_count (void)
108 {
109   error_count = warning_count = 0;
110 }
111
112 bool
113 any_errors (void)
114 {
115   return error_count > 0;
116 }
117 \f
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;
123
124 static void
125 handle_msg (const struct msg *m)
126 {
127   struct category
128     {
129       bool show_command_name;   /* Show command name with error? */
130       bool show_file_location;  /* Show syntax file location? */
131     };
132
133   static const struct category categories[] =
134     {
135       {false, false},           /* MSG_GENERAL. */
136       {true, true},             /* MSG_SYNTAX. */
137       {false, true},            /* MSG_DATA. */
138     };
139
140   struct severity
141     {
142       const char *name;         /* How to identify this severity. */
143       int *count;               /* Number of msgs with this severity so far. */
144     };
145
146   static struct severity severities[] =
147     {
148       {N_("error"), &error_count},          /* MSG_ERROR. */
149       {N_("warning"), &warning_count},      /* MSG_WARNING. */
150       {NULL, NULL},                         /* MSG_NOTE. */
151     };
152
153   const struct category *category = &categories[m->category];
154   const struct severity *severity = &severities[m->severity];
155   struct string string = DS_EMPTY_INITIALIZER;
156
157   if (category->show_file_location && m->where.file_name)
158     {
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, ' ');
163     }
164
165   if (severity->name != NULL)
166     ds_put_format (&string, "%s: ", gettext (severity->name));
167
168   if (severity->count != NULL)
169     ++*severity->count;
170
171   if (category->show_command_name && msg_get_command_name () != NULL)
172     ds_put_format (&string, "%s: ", msg_get_command_name ());
173
174   ds_put_cstr (&string, m->text);
175
176   if (msg_file != stdout || get_error_routing_to_terminal ())
177     dump_message (ds_cstr (&string), get_viewwidth (), 8,
178                   write_stream, msg_file);
179
180   dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
181
182   if (get_error_routing_to_listing ())
183     {
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);
190     }
191
192   ds_destroy (&string);
193 }
194
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
198    auxiliary data. */
199 static void
200 dump_message (char *msg, unsigned width, unsigned indent,
201               write_line_func *dump_line, void *aux)
202 {
203   size_t length = strlen (msg);
204   char *string, *breaks;
205   int line_indent;
206   size_t line_start, i;
207
208   /* Allocate temporary buffers.
209      If we can't get memory for them, then just dump the whole
210      message. */
211   string = strdup (msg);
212   breaks = malloc (length);
213   if (string == NULL || breaks == NULL)
214     {
215       free (string);
216       free (breaks);
217       dump_line (0, ss_cstr (msg), aux);
218       return;
219     }
220
221   /* Break into lines. */
222   if (indent > width / 3)
223     indent = width / 3;
224   mbs_width_linebreaks (string, length,
225                         width - indent, -indent, 0,
226                         NULL, locale_charset (), breaks);
227
228   /* Write out lines. */
229   line_start = 0;
230   line_indent = 0;
231   for (i = 0; i < length; i++)
232     if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
233       {
234         dump_line (line_indent,
235                    ss_buffer (string + line_start, i - line_start), aux);
236         line_indent = indent;
237
238         /* UC_BREAK_POSSIBLE means that a line break can be
239            inserted, and that the character should be included
240            in the next line.
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);
244       }
245   if (line_start < length)
246     dump_line (line_indent,
247                ss_buffer (string + line_start, length - line_start), aux);
248
249   free (string);
250   free (breaks);
251 }
252
253 /* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
254 static void
255 write_stream (int line_indent, struct substring line, void *stream_)
256 {
257   FILE *stream = stream_;
258   int i;
259   for (i = 0; i < line_indent; i++)
260     putc (' ', stream);
261   fwrite (ss_data (line), 1, ss_length (line), stream);
262   putc ('\n', stream);
263 }
264
265 /* Writes LINE to the journal. */
266 static void
267 write_journal (int line_indent, struct substring line, void *unused UNUSED)
268 {
269   char *s = xstrndup (ss_data (line), ss_length (line));
270   journal_write (true, s);
271   free (s);
272 }