Rewrite PSPP output engine.
[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 <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>
29
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #include "unilbrk.h"
37 #include "localcharset.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
42
43 /* Number of errors, warnings reported. */
44 static int error_count;
45 static int warning_count;
46 static const char *error_file;
47
48 static void handle_msg (const struct msg *);
49
50 static FILE *msg_file ;
51
52 void
53 msg_ui_set_error_file (const char *filename)
54 {
55   error_file = filename;
56 }
57
58 void
59 msg_ui_init (struct source_stream *ss)
60 {
61   msg_file = stdout;
62
63   if ( error_file )
64     {
65       msg_file = fopen (error_file, "a");
66       if ( NULL == msg_file )
67         {
68           int err = errno;
69           printf ( _("Cannot open %s (%s). "
70                      "Writing errors to stdout instead.\n"),
71                    error_file, strerror(err) );
72           msg_file = stdout;
73         }
74     }
75   msg_init (ss, handle_msg);
76 }
77
78 void
79 msg_ui_done (void)
80 {
81   msg_done ();
82   msg_locator_done ();
83
84   if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
85     fclose (msg_file);
86 }
87
88 /* Checks whether we've had so many errors that it's time to quit
89    processing this syntax file. */
90 void
91 check_msg_count (struct source_stream *ss)
92 {
93   if (!getl_is_interactive (ss))
94     {
95       if (settings_get_errorbreak () && error_count)
96         msg (MN, _("Terminating execution of syntax file due to error."));
97       else if (error_count > settings_get_mxerrs () )
98         msg (MN, _("Errors (%d) exceeds limit (%d)."),
99              error_count, settings_get_mxerrs ());
100       else if (error_count + warning_count > settings_get_mxwarns () )
101         msg (MN, _("Warnings (%d) exceed limit (%d)."),
102              error_count + warning_count, settings_get_mxwarns () );
103       else
104         return;
105
106       getl_abort_noninteractive (ss);
107     }
108 }
109
110 void
111 reset_msg_count (void)
112 {
113   error_count = warning_count = 0;
114 }
115
116 bool
117 any_errors (void)
118 {
119   return error_count > 0;
120 }
121 \f
122 typedef void write_line_func (int indent, struct substring line, void *aux);
123 static void dump_message (char *msg, unsigned width, unsigned indent,
124                           write_line_func *, void *aux);
125 static write_line_func write_stream;
126 static write_line_func write_journal;
127
128 static void
129 handle_msg (const struct msg *m)
130 {
131   struct category
132     {
133       bool show_command_name;   /* Show command name with error? */
134       bool show_file_location;  /* Show syntax file location? */
135     };
136
137   static const struct category categories[] =
138     {
139       {false, false},           /* MSG_GENERAL. */
140       {true, true},             /* MSG_SYNTAX. */
141       {false, true},            /* MSG_DATA. */
142     };
143
144   struct severity
145     {
146       const char *name;         /* How to identify this severity. */
147       int *count;               /* Number of msgs with this severity so far. */
148     };
149
150   static struct severity severities[] =
151     {
152       {N_("error"), &error_count},          /* MSG_ERROR. */
153       {N_("warning"), &warning_count},      /* MSG_WARNING. */
154       {NULL, NULL},                         /* MSG_NOTE. */
155     };
156
157   const struct category *category = &categories[m->category];
158   const struct severity *severity = &severities[m->severity];
159   struct string string = DS_EMPTY_INITIALIZER;
160
161   if (category->show_file_location && m->where.file_name)
162     {
163       ds_put_format (&string, "%s:", m->where.file_name);
164       if (m->where.line_number != -1)
165         ds_put_format (&string, "%d:", m->where.line_number);
166       ds_put_char (&string, ' ');
167     }
168
169   if (severity->name != NULL)
170     ds_put_format (&string, "%s: ", gettext (severity->name));
171
172   if (severity->count != NULL)
173     ++*severity->count;
174
175   if (category->show_command_name && msg_get_command_name () != NULL)
176     ds_put_format (&string, "%s: ", msg_get_command_name ());
177
178   ds_put_cstr (&string, m->text);
179
180   if (msg_file != stdout || settings_get_error_routing_to_terminal ())
181     dump_message (ds_cstr (&string),
182                   isatty (fileno (msg_file)) ? settings_get_viewwidth () : INT_MAX, 8,
183                   write_stream, msg_file);
184
185   dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
186
187   if (settings_get_error_routing_to_listing ())
188     {
189       /* Disable screen output devices, because the error should
190          already have been reported to the screen with the
191          dump_message call above. */
192       output_set_type_enabled (false, OUTPUT_DEVICE_SCREEN);
193       tab_output_text (TAB_LEFT, ds_cstr (&string));
194       output_set_type_enabled (true, OUTPUT_DEVICE_SCREEN);
195     }
196
197   ds_destroy (&string);
198 }
199
200 /* Divides MSG into lines of WIDTH width for the first line and
201    WIDTH - INDENT width for each succeeding line, and writes the
202    lines by calling DUMP_LINE for each line, passing AUX as
203    auxiliary data. */
204 static void
205 dump_message (char *msg, unsigned width, unsigned indent,
206               write_line_func *dump_line, void *aux)
207 {
208   size_t length = strlen (msg);
209   char *string, *breaks;
210   int line_indent;
211   size_t line_start, i;
212
213   /* Allocate temporary buffers.
214      If we can't get memory for them, then just dump the whole
215      message. */
216   string = strdup (msg);
217   breaks = malloc (length);
218   if (string == NULL || breaks == NULL)
219     {
220       free (string);
221       free (breaks);
222       dump_line (0, ss_cstr (msg), aux);
223       return;
224     }
225
226   /* Break into lines. */
227   if (indent > width / 3)
228     indent = width / 3;
229   ulc_width_linebreaks (string, length,
230                         width - indent, -indent, 0,
231                         NULL, locale_charset (), breaks);
232
233   /* Write out lines. */
234   line_start = 0;
235   line_indent = 0;
236   for (i = 0; i < length; i++)
237     if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
238       {
239         dump_line (line_indent,
240                    ss_buffer (string + line_start, i - line_start), aux);
241         line_indent = indent;
242
243         /* UC_BREAK_POSSIBLE means that a line break can be
244            inserted, and that the character should be included
245            in the next line.
246            UC_BREAK_MANDATORY means that this character is a line
247            break, so it should not be included in the next line. */
248         line_start = i + (breaks[i] == UC_BREAK_MANDATORY);
249       }
250   if (line_start < length)
251     dump_line (line_indent,
252                ss_buffer (string + line_start, length - line_start), aux);
253
254   free (string);
255   free (breaks);
256 }
257
258 /* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
259 static void
260 write_stream (int line_indent, struct substring line, void *stream_)
261 {
262   FILE *stream = stream_;
263   int i;
264   for (i = 0; i < line_indent; i++)
265     putc (' ', stream);
266   fwrite (ss_data (line), 1, ss_length (line), stream);
267   putc ('\n', stream);
268 }
269
270 /* Writes LINE to the journal. */
271 static void
272 write_journal (int line_indent UNUSED, struct substring line, void *unused UNUSED)
273 {
274   char *s = xstrndup (ss_data (line), ss_length (line));
275   journal_write (true, s);
276   free (s);
277 }