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