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
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
44 int err_warning_count;
46 int err_already_flagged;
51 /* Fairly common public functions. */
53 /* Writes error message in CLASS, with title TITLE and text FORMAT,
54 formatted with printf, to the standard places. */
56 tmsg (int class, const char *title, const char *format, ...)
62 err_location (&e.where);
65 va_start (args, format);
66 err_vmsg (&e, format, args);
70 /* Writes error message in CLASS, with text FORMAT, formatted with
71 printf, to the standard places. */
73 msg (int class, const char *format, ...)
79 err_location (&e.where);
82 va_start (args, format);
83 err_vmsg (&e, format, args);
87 /* Terminate due to fatal error in input. */
94 fprintf (stderr, "%s: %s\n", program_name,
95 _("Terminating NOW due to a fatal error!"));
100 /* Terminate unless we're interactive or will go interactive when the
101 file is over with. */
105 if (getl_reading_script ())
107 if (getl_interactive)
114 /* Obscure public functions. */
116 /* Writes a blank line to the error device(s).
117 FIXME: currently a no-op. */
123 /* Checks whether we've had so many errors that it's time to quit
124 processing this syntax file. If so, then take appropriate
127 err_check_count (void)
129 int error_class = getl_interactive ? MM : FE;
131 if (get_errorbreak() && err_error_count)
132 msg (error_class, _("Terminating execution of syntax file due to error."));
133 else if (err_error_count > get_mxerrs() )
134 msg (error_class, _("Errors (%d) exceeds limit (%d)."),
135 err_error_count, get_mxerrs());
136 else if (err_error_count + err_warning_count > get_mxwarns() )
137 msg (error_class, _("Warnings (%d) exceed limit (%d)."),
138 err_error_count + err_warning_count, get_mxwarns() );
145 /* Some machines are broken. Compensate. */
147 #define EXIT_SUCCESS 0
151 #define EXIT_FAILURE 1
154 static void puts_stdout (const char *s);
155 static void dump_message (char *errbuf, unsigned indent,
156 void (*func) (const char *), unsigned width);
162 getl_uninitialize ();
163 readln_uninitialize();
167 err_vmsg (const struct error *e, const char *format, va_list args)
172 ERR_IN_PROCEDURE = 01, /* 1=Display name of current procedure. */
173 ERR_WITH_FILE = 02, /* 1=Display filename and line number. */
176 /* Describes one class of error. */
179 int flags; /* Zero or more of ERR_*. */
180 int *count; /* Counting category. */
181 const char *banner; /* Banner. */
184 static const struct error_class error_classes[ERR_CLASS_COUNT] =
186 {0, NULL, N_("fatal")}, /* FE */
188 {3, &err_error_count, N_("error")}, /* SE */
189 {3, &err_warning_count, N_("warning")}, /* SW */
190 {3, NULL, N_("note")}, /* SM */
192 {0, NULL, N_("installation error")}, /* IE */
193 {2, NULL, N_("installation error")}, /* IS */
195 {2, &err_error_count, N_("error")}, /* DE */
196 {2, &err_warning_count, N_("warning")}, /* DW */
198 {0, &err_error_count, N_("error")}, /* ME */
199 {0, &err_warning_count, N_("warning")}, /* MW */
200 {0, NULL, N_("note")}, /* MM */
206 /* Check verbosity level. */
208 if (((class >> ERR_VERBOSITY_SHIFT) & ERR_VERBOSITY_MASK) > err_verbosity)
210 class &= ERR_CLASS_MASK;
212 assert (class >= 0 && class < ERR_CLASS_COUNT);
213 assert (format != NULL);
216 if (e->where.filename && (error_classes[class].flags & ERR_WITH_FILE))
218 ds_printf (&msg, "%s:", e->where.filename);
219 if (e->where.line_number != -1)
220 ds_printf (&msg, "%d:", e->where.line_number);
224 ds_printf (&msg, "%s: ", gettext (error_classes[class].banner));
227 int *count = error_classes[class].count;
232 if (cur_proc && (error_classes[class].flags & ERR_IN_PROCEDURE))
233 ds_printf (&msg, "%s: ", cur_proc);
236 ds_puts (&msg, e->title);
238 ds_vprintf (&msg, format, args);
240 /* FIXME: Check set_messages and set_errors to determine where to
241 send errors and messages.
243 Please note that this is not trivial. We have to avoid an
244 infinite loop in reporting errors that originate in the output
246 dump_message (ds_c_str (&msg), 8, puts_stdout, get_viewwidth());
254 /* Private functions. */
257 /* Write S followed by a newline to stderr. */
259 puts_stderr (const char *s)
262 fputc ('\n', stderr);
266 /* Write S followed by a newline to stdout. */
268 puts_stdout (const char *s)
273 /* Returns 1 if the line must be broken here */
275 compulsory_break(int c)
277 return ( c == '\n' );
280 /* Returns 1 if C is a `break character', that is, if it is a good
281 place to break a message into lines. */
283 char_is_break (int quote, int c)
285 return ((quote && c == DIR_SEPARATOR)
286 || (!quote && (isspace (c) || c == '-' || c == '/')));
289 /* Returns 1 if C is a break character where the break should be made
290 BEFORE the character. */
292 break_before (int quote, int c)
294 return !quote && isspace (c);
297 /* If C is a break character, returns 1 if the break should be made
298 AFTER the character. Does not return a meaningful result if C is
299 not a break character. */
301 break_after (int quote, int c)
303 return !break_before (quote, c);
306 /* If you want very long words that occur at a bad break point to be
307 broken into two lines even if they're shorter than a whole line by
308 themselves, define as 2/3, or 4/5, or whatever fraction of a whole
309 line you think is necessary in order to consider a word long enough
310 to break into pieces. Otherwise, define as 0. See code to grok
311 the details. Do NOT parenthesize the expression! */
312 #define BREAK_LONG_WORD 0
313 /* #define BREAK_LONG_WORD 2/3 */
314 /* #define BREAK_LONG_WORD 4/5 */
316 /* Divides MSG into lines of WIDTH width for the first line and WIDTH
317 - INDENT width for each succeeding line. Each line is dumped
318 through FUNC, which may do with the string what it will. */
320 dump_message (char *msg, unsigned indent, void (*func) (const char *),
325 /* 1 when at a position inside double quotes ("). */
328 /* Buffer for a single line. */
331 /* If the message is short, just print the full thing. */
332 if (strlen (msg) < width)
338 /* Make sure the indent isn't too big relative to the page width. */
339 if (indent > width / 3)
342 buf = local_alloc (width + 2);
344 /* Advance WIDTH characters into MSG.
345 If that's a valid breakpoint, keep it; otherwise, back up.
347 for (cp = msg; (unsigned) (cp - msg) < width - 1 &&
348 ! compulsory_break(*cp); cp++)
352 if (break_after (quote, (unsigned char) *cp))
354 for (cp--; !char_is_break (quote, (unsigned char) *cp) && cp > msg; cp--)
358 if (break_after (quote, (unsigned char) *cp))
362 if (cp <= msg + width * BREAK_LONG_WORD)
363 for (; cp < msg + width - 1; cp++)
375 /* Repeat above procedure for remaining lines. */
378 static int hard_break=0;
383 /* Advance past whitespace. */
385 while ( isspace ((unsigned char) *cp) )
394 /* Advance WIDTH - INDENT characters. */
395 for (cp2 = cp; (unsigned) (cp2 - cp) < width - indent &&
396 *cp2 && !compulsory_break(*cp2); cp2++)
400 if ( compulsory_break(*cp2) )
406 /* Back up if this isn't a breakpoint. */
408 unsigned w = cp2 - cp;
409 if (*cp2 && ! compulsory_break(*cp2) )
410 for (cp2--; !char_is_break (quote, (unsigned char) *cp2) &&
419 if (w == width - indent
420 && (unsigned) (cp2 - cp) <= (width - indent) * BREAK_LONG_WORD)
421 for (; (unsigned) (cp2 - cp) < width - indent && *cp2 ; cp2++)
427 /* Write out the line. */
429 memset (buf, ' ', indent);
430 memcpy (&buf[indent], cp, cp2 - cp);
432 buf[indent + idx + cp2 - cp] = '\0';
442 request_bug_report_and_abort(const char *msg )
445 "******************************************************************\n"
446 "You have discovered a bug in PSPP.\n\n"
447 " Please report this, by sending "
448 "an email to " PACKAGE_BUGREPORT ",\n"
449 "explaining what you were doing when this happened, and including\n"
450 "a sample of your input file which caused it.\n");
453 "Also, please copy the following lines into your bug report:\n\n"
459 "default_config_path: %s\n"
461 "groff_font_path: %s\n"
463 "compiler version: %s\n"
483 fprintf(stderr,"Diagnosis: %s\n",msg);
486 "******************************************************************\n");
492 err_assert_fail(const char *expr, const char *file, int line)
495 snprintf(msg,256,"Assertion failed: %s:%d; (%s)",file,line,expr);
496 request_bug_report_and_abort( msg );