9cbbf10f7998f0bbd541b3356e23bb136edfa072
[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    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "msg-ui.h"
23
24 #include "exit.h"
25 #include "linebreak.h"
26
27 #include <libpspp/msg-locator.h>
28 #include <libpspp/getl.h>
29 #include <data/settings.h>
30 #include <libpspp/message.h>
31 #include <libpspp/str.h>
32 #include <errno.h>
33 #include <stdio.h>
34
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 static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
120 static void dump_line (int line_indent, const char *line, size_t length,
121                        FILE *);
122
123 static void
124 handle_msg (const struct msg *m)
125 {
126   struct category 
127     {
128       bool show_command_name;   /* Show command name with error? */
129       bool show_file_location;  /* Show syntax file location? */
130     };
131
132   static const struct category categories[] = 
133     {
134       {false, false},           /* MSG_GENERAL. */
135       {true, true},             /* MSG_SYNTAX. */
136       {false, true},            /* MSG_DATA. */
137     };
138
139   struct severity 
140     {
141       const char *name;         /* How to identify this severity. */
142       int *count;               /* Number of msgs with this severity so far. */
143     };
144   
145   static struct severity severities[] = 
146     {
147       {N_("error"), &error_count},          /* MSG_ERROR. */
148       {N_("warning"), &warning_count},      /* MSG_WARNING. */
149       {NULL, NULL},                         /* MSG_NOTE. */
150     };
151
152   const struct category *category = &categories[m->category];
153   const struct severity *severity = &severities[m->severity];
154   struct string string = DS_EMPTY_INITIALIZER;
155
156   if (category->show_file_location && m->where.file_name)
157     {
158       ds_put_format (&string, "%s:", m->where.file_name);
159       if (m->where.line_number != -1)
160         ds_put_format (&string, "%d:", m->where.line_number);
161       ds_put_char (&string, ' ');
162     }
163
164   if (severity->name != NULL)
165     ds_put_format (&string, "%s: ", gettext (severity->name));
166   
167   if (severity->count != NULL)
168     ++*severity->count;
169   
170   if (category->show_command_name && msg_get_command_name () != NULL)
171     ds_put_format (&string, "%s: ", msg_get_command_name ());
172
173   ds_put_cstr (&string, m->text);
174
175   if (msg_file != stdout || get_error_routing_to_terminal ())
176     dump_message (ds_cstr (&string), get_viewwidth (), 8, msg_file);
177
178   ds_destroy (&string);
179 }
180
181 /* Divides MSG into lines of WIDTH width for the first line and
182    WIDTH - INDENT width for each succeeding line, and writes the
183    lines to STREAM. */
184 static void
185 dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
186 {
187   size_t length = strlen (msg);
188   char *string, *breaks;
189   int line_indent;
190   size_t line_start, i;
191
192   /* Allocate temporary buffers.
193      If we can't get memory for them, then just dump the whole
194      message. */
195   string = strdup (msg);
196   breaks = malloc (length);
197   if (string == NULL || breaks == NULL)
198     {
199       free (string);
200       free (breaks);
201       fputs (msg, stream);
202       putc ('\n', stream);
203       return;
204     }
205
206   /* Break into lines. */
207   if (indent > width / 3)
208     indent = width / 3;
209   mbs_width_linebreaks (string, length,
210                         width - indent, -indent, 0,
211                         NULL, locale_charset (), breaks);
212
213   /* Write out lines. */
214   line_start = 0;
215   line_indent = 0;
216   for (i = 0; i < length; i++)
217     switch (breaks[i]) 
218       {
219       case UC_BREAK_POSSIBLE:
220         /* Break before this character,
221            and include this character in the next line. */
222         dump_line (line_indent, &string[line_start], i - line_start, stream);
223         line_start = i;
224         line_indent = indent;
225         break;
226       case UC_BREAK_MANDATORY:
227         /* Break before this character,
228            but don't include this character in the next line
229            (because it'string a new-line). */
230         dump_line (line_indent, &string[line_start], i - line_start, stream);
231         line_start = i + 1;
232         line_indent = indent;
233         break;
234       default:
235         break;
236       }
237   if (line_start < length)
238     dump_line (line_indent, &string[line_start], length - line_start, stream);
239
240   free (string);
241   free (breaks);
242 }
243
244 /* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
245    a new-line to STREAM. */
246 static void
247 dump_line (int line_indent, const char *line, size_t length, FILE *stream)
248 {
249   int i;
250   for (i = 0; i < line_indent; i++)
251     putc (' ', stream);
252   fwrite (line, 1, length, stream);
253   putc ('\n', stream);
254 }
255