1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
21 #include <libpspp/message.h>
26 #include <libpspp/alloc.h>
27 #include <data/filename.h>
28 #include <language/line-buffer.h>
29 #include <language/lexer/lexer.h>
30 #include <data/settings.h>
31 #include <ui/terminal/read-line.h>
32 #include <libpspp/version.h>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
39 int err_warning_count;
41 int err_already_flagged;
45 static char *command_name;
47 /* Fairly common public functions. */
49 /* Writes error message in CLASS, with title TITLE and text FORMAT,
50 formatted with printf, to the standard places. */
52 tmsg (int class, const char *title, const char *format, ...)
58 err_location (&e.where);
61 va_start (args, format);
62 err_vmsg (&e, format, args);
66 /* Writes error message in CLASS, with text FORMAT, formatted with
67 printf, to the standard places. */
69 msg (int class, const char *format, ...)
75 err_location (&e.where);
78 va_start (args, format);
79 err_vmsg (&e, format, args);
83 /* Checks whether we've had so many errors that it's time to quit
84 processing this syntax file. */
86 err_check_count (void)
88 if (get_errorbreak() && err_error_count)
89 msg (MM, _("Terminating execution of syntax file due to error."));
90 else if (err_error_count > get_mxerrs() )
91 msg (MM, _("Errors (%d) exceeds limit (%d)."),
92 err_error_count, get_mxerrs());
93 else if (err_error_count + err_warning_count > get_mxwarns() )
94 msg (MM, _("Warnings (%d) exceed limit (%d)."),
95 err_error_count + err_warning_count, get_mxwarns() );
99 getl_abort_noninteractive ();
102 /* Some machines are broken. Compensate. */
104 #define EXIT_SUCCESS 0
108 #define EXIT_FAILURE 1
111 static void puts_stdout (const char *s);
112 static void dump_message (char *errbuf, unsigned indent,
113 void (*func) (const char *), unsigned width);
119 getl_uninitialize ();
120 readln_uninitialize();
124 err_vmsg (const struct error *e, const char *format, va_list args)
129 ERR_IN_PROCEDURE = 01, /* 1=Display name of current procedure. */
130 ERR_WITH_FILE = 02, /* 1=Display filename and line number. */
133 /* Describes one class of error. */
136 int flags; /* Zero or more of ERR_*. */
137 int *count; /* Counting category. */
138 const char *banner; /* Banner. */
141 static const struct error_class error_classes[ERR_CLASS_COUNT] =
143 {3, &err_error_count, N_("error")}, /* SE */
144 {3, &err_warning_count, N_("warning")}, /* SW */
145 {3, NULL, N_("note")}, /* SM */
147 {2, &err_error_count, N_("error")}, /* DE */
148 {2, &err_warning_count, N_("warning")}, /* DW */
150 {0, &err_error_count, N_("error")}, /* ME */
151 {0, &err_warning_count, N_("warning")}, /* MW */
152 {0, NULL, N_("note")}, /* MM */
158 /* Check verbosity level. */
160 if (((class >> ERR_VERBOSITY_SHIFT) & ERR_VERBOSITY_MASK) > err_verbosity)
162 class &= ERR_CLASS_MASK;
164 assert (class >= 0 && class < ERR_CLASS_COUNT);
165 assert (format != NULL);
168 if (e->where.filename && (error_classes[class].flags & ERR_WITH_FILE))
170 ds_printf (&msg, "%s:", e->where.filename);
171 if (e->where.line_number != -1)
172 ds_printf (&msg, "%d:", e->where.line_number);
176 ds_printf (&msg, "%s: ", gettext (error_classes[class].banner));
179 int *count = error_classes[class].count;
184 if (command_name != NULL && (error_classes[class].flags & ERR_IN_PROCEDURE))
185 ds_printf (&msg, "%s: ", command_name);
188 ds_puts (&msg, e->title);
190 ds_vprintf (&msg, format, args);
192 /* FIXME: Check set_messages and set_errors to determine where to
193 send errors and messages.
195 Please note that this is not trivial. We have to avoid an
196 infinite loop in reporting errors that originate in the output
198 dump_message (ds_c_str (&msg), 8, puts_stdout, get_viewwidth());
203 /* Private functions. */
206 /* Write S followed by a newline to stderr. */
208 puts_stderr (const char *s)
211 fputc ('\n', stderr);
215 /* Write S followed by a newline to stdout. */
217 puts_stdout (const char *s)
222 /* Returns 1 if the line must be broken here */
224 compulsory_break(int c)
226 return ( c == '\n' );
229 /* Returns 1 if C is a `break character', that is, if it is a good
230 place to break a message into lines. */
232 char_is_break (int quote, int c)
234 return ((quote && c == DIR_SEPARATOR)
235 || (!quote && (isspace (c) || c == '-' || c == '/')));
238 /* Returns 1 if C is a break character where the break should be made
239 BEFORE the character. */
241 break_before (int quote, int c)
243 return !quote && isspace (c);
246 /* If C is a break character, returns 1 if the break should be made
247 AFTER the character. Does not return a meaningful result if C is
248 not a break character. */
250 break_after (int quote, int c)
252 return !break_before (quote, c);
255 /* If you want very long words that occur at a bad break point to be
256 broken into two lines even if they're shorter than a whole line by
257 themselves, define as 2/3, or 4/5, or whatever fraction of a whole
258 line you think is necessary in order to consider a word long enough
259 to break into pieces. Otherwise, define as 0. See code to grok
260 the details. Do NOT parenthesize the expression! */
261 #define BREAK_LONG_WORD 0
262 /* #define BREAK_LONG_WORD 2/3 */
263 /* #define BREAK_LONG_WORD 4/5 */
265 /* Divides MSG into lines of WIDTH width for the first line and WIDTH
266 - INDENT width for each succeeding line. Each line is dumped
267 through FUNC, which may do with the string what it will. */
269 dump_message (char *msg, unsigned indent, void (*func) (const char *),
274 /* 1 when at a position inside double quotes ("). */
277 /* Buffer for a single line. */
280 /* If the message is short, just print the full thing. */
281 if (strlen (msg) < width)
287 /* Make sure the indent isn't too big relative to the page width. */
288 if (indent > width / 3)
291 buf = local_alloc (width + 2);
293 /* Advance WIDTH characters into MSG.
294 If that's a valid breakpoint, keep it; otherwise, back up.
296 for (cp = msg; (unsigned) (cp - msg) < width - 1 &&
297 ! compulsory_break(*cp); cp++)
301 if (break_after (quote, (unsigned char) *cp))
303 for (cp--; !char_is_break (quote, (unsigned char) *cp) && cp > msg; cp--)
307 if (break_after (quote, (unsigned char) *cp))
311 if (cp <= msg + width * BREAK_LONG_WORD)
312 for (; cp < msg + width - 1; cp++)
324 /* Repeat above procedure for remaining lines. */
327 static int hard_break=0;
332 /* Advance past whitespace. */
334 while ( isspace ((unsigned char) *cp) )
343 /* Advance WIDTH - INDENT characters. */
344 for (cp2 = cp; (unsigned) (cp2 - cp) < width - indent &&
345 *cp2 && !compulsory_break(*cp2); cp2++)
349 if ( compulsory_break(*cp2) )
355 /* Back up if this isn't a breakpoint. */
357 unsigned w = cp2 - cp;
358 if (*cp2 && ! compulsory_break(*cp2) )
359 for (cp2--; !char_is_break (quote, (unsigned char) *cp2) &&
368 if (w == width - indent
369 && (unsigned) (cp2 - cp) <= (width - indent) * BREAK_LONG_WORD)
370 for (; (unsigned) (cp2 - cp) < width - indent && *cp2 ; cp2++)
376 /* Write out the line. */
378 memset (buf, ' ', indent);
379 memcpy (&buf[indent], cp, cp2 - cp);
381 buf[indent + idx + cp2 - cp] = '\0';
389 /* Sets COMMAND_NAME as the command name included in some kinds
390 of error messages. */
392 err_set_command_name (const char *command_name_)
395 command_name = command_name_ ? xstrdup (command_name_) : NULL;
399 request_bug_report_and_abort(const char *msg )
402 "******************************************************************\n"
403 "You have discovered a bug in PSPP.\n\n"
404 " Please report this, by sending "
405 "an email to " PACKAGE_BUGREPORT ",\n"
406 "explaining what you were doing when this happened, and including\n"
407 "a sample of your input file which caused it.\n");
410 "Also, please copy the following lines into your bug report:\n\n"
416 "default_config_path: %s\n"
418 "groff_font_path: %s\n"
420 "compiler version: %s\n"
440 fprintf(stderr,"Diagnosis: %s\n",msg);
443 "******************************************************************\n");
449 err_assert_fail(const char *expr, const char *file, int line)
452 snprintf(msg,256,"Assertion failed: %s:%d; (%s)",file,line,expr);
453 request_bug_report_and_abort( msg );