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