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