Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / ui / terminal / msg-ui.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include "msg-ui.h"
22
23 #include "exit.h"
24 #include "linebreak.h"
25
26 #include <libpspp/msg-locator.h>
27 #include <libpspp/getl.h>
28 #include <data/settings.h>
29 #include <libpspp/message.h>
30 #include <libpspp/str.h>
31 #include <errno.h>
32 #include <stdio.h>
33
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 static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
119 static void dump_line (int line_indent, const char *line, size_t length,
120                        FILE *);
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, msg_file);
176
177   ds_destroy (&string);
178 }
179
180 /* Divides MSG into lines of WIDTH width for the first line and
181    WIDTH - INDENT width for each succeeding line, and writes the
182    lines to STREAM. */
183 static void
184 dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
185 {
186   size_t length = strlen (msg);
187   char *string, *breaks;
188   int line_indent;
189   size_t line_start, i;
190
191   /* Allocate temporary buffers.
192      If we can't get memory for them, then just dump the whole
193      message. */
194   string = strdup (msg);
195   breaks = malloc (length);
196   if (string == NULL || breaks == NULL)
197     {
198       free (string);
199       free (breaks);
200       fputs (msg, stream);
201       putc ('\n', stream);
202       return;
203     }
204
205   /* Break into lines. */
206   if (indent > width / 3)
207     indent = width / 3;
208   mbs_width_linebreaks (string, length,
209                         width - indent, -indent, 0,
210                         NULL, locale_charset (), breaks);
211
212   /* Write out lines. */
213   line_start = 0;
214   line_indent = 0;
215   for (i = 0; i < length; i++)
216     switch (breaks[i]) 
217       {
218       case UC_BREAK_POSSIBLE:
219         /* Break before this character,
220            and include this character in the next line. */
221         dump_line (line_indent, &string[line_start], i - line_start, stream);
222         line_start = i;
223         line_indent = indent;
224         break;
225       case UC_BREAK_MANDATORY:
226         /* Break before this character,
227            but don't include this character in the next line
228            (because it'string a new-line). */
229         dump_line (line_indent, &string[line_start], i - line_start, stream);
230         line_start = i + 1;
231         line_indent = indent;
232         break;
233       default:
234         break;
235       }
236   if (line_start < length)
237     dump_line (line_indent, &string[line_start], length - line_start, stream);
238
239   free (string);
240   free (breaks);
241 }
242
243 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
244    a new-line to STREAM. */
245 static void
246 dump_line (int line_indent, const char *line, size_t length, FILE *stream)
247 {
248   int i;
249   for (i = 0; i < line_indent; i++)
250     putc (' ', stream);
251   fwrite (line, 1, length, stream);
252   putc ('\n', stream);
253 }
254