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