Greatly simplify PSPP configuration.
[pspp-builds.git] / src / ui / terminal / msg-ui.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010 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       int max_errors = settings_get_max_messages (MSG_S_ERROR);
96       int max_warnings = settings_get_max_messages (MSG_S_WARNING);
97
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);
104       else
105         return;
106
107       getl_abort_noninteractive (ss);
108     }
109 }
110
111 void
112 reset_msg_count (void)
113 {
114   error_count = warning_count = 0;
115 }
116
117 bool
118 any_errors (void)
119 {
120   return error_count > 0;
121 }
122 \f
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;
128
129 static void
130 handle_msg (const struct msg *m)
131 {
132   struct category
133     {
134       bool show_command_name;   /* Show command name with error? */
135       bool show_file_location;  /* Show syntax file location? */
136     };
137
138   static const struct category categories[] =
139     {
140       {false, false},           /* MSG_GENERAL. */
141       {true, true},             /* MSG_SYNTAX. */
142       {false, true},            /* MSG_DATA. */
143     };
144
145   struct severity
146     {
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. */
150     };
151
152   static struct severity severities[] =
153     {
154       { SETTINGS_OUTPUT_ERROR, N_("error"), &error_count },
155       { SETTINGS_OUTPUT_ERROR, N_("warning"), &warning_count },
156       { SETTINGS_OUTPUT_NOTE, NULL, NULL},
157     };
158
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;
163
164   if (category->show_file_location && m->where.file_name)
165     {
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, ' ');
170     }
171
172   if (severity->name != NULL)
173     ds_put_format (&string, "%s: ", gettext (severity->name));
174
175   if (severity->count != NULL)
176     ++*severity->count;
177
178   if (category->show_command_name && msg_get_command_name () != NULL)
179     ds_put_format (&string, "%s: ", msg_get_command_name ());
180
181   ds_put_cstr (&string, m->text);
182
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);
188
189   dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
190
191   if (routing & SETTINGS_DEVICE_LISTING)
192     {
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);
199     }
200
201   ds_destroy (&string);
202 }
203
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
207    auxiliary data. */
208 static void
209 dump_message (char *msg, unsigned width, unsigned indent,
210               write_line_func *dump_line, void *aux)
211 {
212   size_t length = strlen (msg);
213   char *string, *breaks;
214   int line_indent;
215   size_t line_start, i;
216
217   /* Allocate temporary buffers.
218      If we can't get memory for them, then just dump the whole
219      message. */
220   string = strdup (msg);
221   breaks = malloc (length);
222   if (string == NULL || breaks == NULL)
223     {
224       free (string);
225       free (breaks);
226       dump_line (0, ss_cstr (msg), aux);
227       return;
228     }
229
230   /* Break into lines. */
231   if (indent > width / 3)
232     indent = width / 3;
233   ulc_width_linebreaks (string, length,
234                         width - indent, -indent, 0,
235                         NULL, locale_charset (), breaks);
236
237   /* Write out lines. */
238   line_start = 0;
239   line_indent = 0;
240   for (i = 0; i < length; i++)
241     if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
242       {
243         dump_line (line_indent,
244                    ss_buffer (string + line_start, i - line_start), aux);
245         line_indent = indent;
246
247         /* UC_BREAK_POSSIBLE means that a line break can be
248            inserted, and that the character should be included
249            in the next line.
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);
253       }
254   if (line_start < length)
255     dump_line (line_indent,
256                ss_buffer (string + line_start, length - line_start), aux);
257
258   free (string);
259   free (breaks);
260 }
261
262 /* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
263 static void
264 write_stream (int line_indent, struct substring line, void *stream_)
265 {
266   FILE *stream = stream_;
267   int i;
268   for (i = 0; i < line_indent; i++)
269     putc (' ', stream);
270   fwrite (ss_data (line), 1, ss_length (line), stream);
271   putc ('\n', stream);
272 }
273
274 /* Writes LINE to the journal. */
275 static void
276 write_journal (int line_indent UNUSED, struct substring line, void *unused UNUSED)
277 {
278   char *s = xstrndup (ss_data (line), ss_length (line));
279   journal_write (true, s);
280   free (s);
281 }