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;
50 /* File locator stack. */
51 static const struct file_locator **file_loc;
52 static int nfile_loc, mfile_loc;
54 /* Fairly common public functions. */
56 /* Writes error message in CLASS, with title TITLE and text FORMAT,
57 formatted with printf, to the standard places. */
59 tmsg (int class, const char *title, const char *format, ...)
65 err_location (&e.where);
68 va_start (args, format);
69 err_vmsg (&e, format, args);
73 /* Writes error message in CLASS, with text FORMAT, formatted with
74 printf, to the standard places. */
76 msg (int class, const char *format, ...)
82 err_location (&e.where);
85 va_start (args, format);
86 err_vmsg (&e, format, args);
90 /* Terminate due to fatal error in input. */
97 fprintf (stderr, "%s: %s\n", program_name,
98 _("Terminating NOW due to a fatal error!"));
103 /* Terminate unless we're interactive or will go interactive when the
104 file is over with. */
108 if (getl_reading_script)
110 if (getl_interactive)
117 /* File locator stack functions. */
119 /* Pushes F onto the stack of file locations. */
121 err_push_file_locator (const struct file_locator *f)
123 if (nfile_loc >= mfile_loc)
130 file_loc = xnrealloc (file_loc, mfile_loc, sizeof *file_loc);
133 file_loc[nfile_loc++] = f;
136 /* Pops F off the stack of file locations.
137 Argument F is only used for verification that that is actually the
138 item on top of the stack. */
140 err_pop_file_locator (const struct file_locator *f)
142 assert (nfile_loc >= 0 && file_loc[nfile_loc - 1] == f);
146 /* Puts the current file and line number in F, or NULL and -1 if
149 err_location (struct file_locator *f)
152 *f = *file_loc[nfile_loc - 1];
154 getl_location (&f->filename, &f->line_number);
157 /* Obscure public functions. */
159 /* Writes a blank line to the error device(s).
160 FIXME: currently a no-op. */
166 /* Checks whether we've had so many errors that it's time to quit
167 processing this syntax file. If so, then take appropriate
170 err_check_count (void)
172 int error_class = getl_interactive ? MM : FE;
174 if (get_errorbreak() && err_error_count)
175 msg (error_class, _("Terminating execution of syntax file due to error."));
176 else if (err_error_count > get_mxerrs() )
177 msg (error_class, _("Errors (%d) exceeds limit (%d)."),
178 err_error_count, get_mxerrs());
179 else if (err_error_count + err_warning_count > get_mxwarns() )
180 msg (error_class, _("Warnings (%d) exceed limit (%d)."),
181 err_error_count + err_warning_count, get_mxwarns() );
188 /* Some machines are broken. Compensate. */
190 #define EXIT_SUCCESS 0
194 #define EXIT_FAILURE 1
197 static void puts_stdout (const char *s);
198 static void dump_message (char *errbuf, unsigned indent,
199 void (*func) (const char *), unsigned width);
206 nfile_loc = mfile_loc = 0;
210 err_vmsg (const struct error *e, const char *format, va_list args)
215 ERR_IN_PROCEDURE = 01, /* 1=Display name of current procedure. */
216 ERR_WITH_FILE = 02, /* 1=Display filename and line number. */
219 /* Describes one class of error. */
222 int flags; /* Zero or more of ERR_*. */
223 int *count; /* Counting category. */
224 const char *banner; /* Banner. */
227 static const struct error_class error_classes[ERR_CLASS_COUNT] =
229 {0, NULL, N_("fatal")}, /* FE */
231 {3, &err_error_count, N_("error")}, /* SE */
232 {3, &err_warning_count, N_("warning")}, /* SW */
233 {3, NULL, N_("note")}, /* SM */
235 {0, NULL, N_("installation error")}, /* IE */
236 {2, NULL, N_("installation error")}, /* IS */
238 {2, &err_error_count, N_("error")}, /* DE */
239 {2, &err_warning_count, N_("warning")}, /* DW */
241 {0, &err_error_count, N_("error")}, /* ME */
242 {0, &err_warning_count, N_("warning")}, /* MW */
243 {0, NULL, N_("note")}, /* MM */
249 /* Check verbosity level. */
251 if (((class >> ERR_VERBOSITY_SHIFT) & ERR_VERBOSITY_MASK) > err_verbosity)
253 class &= ERR_CLASS_MASK;
255 assert (class >= 0 && class < ERR_CLASS_COUNT);
256 assert (format != NULL);
259 if (e->where.filename && (error_classes[class].flags & ERR_WITH_FILE))
261 ds_printf (&msg, "%s:", e->where.filename);
262 if (e->where.line_number != -1)
263 ds_printf (&msg, "%d:", e->where.line_number);
267 ds_printf (&msg, "%s: ", gettext (error_classes[class].banner));
270 int *count = error_classes[class].count;
275 if (cur_proc && (error_classes[class].flags & ERR_IN_PROCEDURE))
276 ds_printf (&msg, "%s: ", cur_proc);
279 ds_puts (&msg, e->title);
281 ds_vprintf (&msg, format, args);
283 /* FIXME: Check set_messages and set_errors to determine where to
284 send errors and messages.
286 Please note that this is not trivial. We have to avoid an
287 infinite loop in reporting errors that originate in the output
289 dump_message (ds_c_str (&msg), 8, puts_stdout, get_viewwidth());
297 /* Private functions. */
300 /* Write S followed by a newline to stderr. */
302 puts_stderr (const char *s)
305 fputc ('\n', stderr);
309 /* Write S followed by a newline to stdout. */
311 puts_stdout (const char *s)
316 /* Returns 1 if the line must be broken here */
318 compulsory_break(int c)
320 return ( c == '\n' );
323 /* Returns 1 if C is a `break character', that is, if it is a good
324 place to break a message into lines. */
326 char_is_break (int quote, int c)
328 return ((quote && c == DIR_SEPARATOR)
329 || (!quote && (isspace (c) || c == '-' || c == '/')));
332 /* Returns 1 if C is a break character where the break should be made
333 BEFORE the character. */
335 break_before (int quote, int c)
337 return !quote && isspace (c);
340 /* If C is a break character, returns 1 if the break should be made
341 AFTER the character. Does not return a meaningful result if C is
342 not a break character. */
344 break_after (int quote, int c)
346 return !break_before (quote, c);
349 /* If you want very long words that occur at a bad break point to be
350 broken into two lines even if they're shorter than a whole line by
351 themselves, define as 2/3, or 4/5, or whatever fraction of a whole
352 line you think is necessary in order to consider a word long enough
353 to break into pieces. Otherwise, define as 0. See code to grok
354 the details. Do NOT parenthesize the expression! */
355 #define BREAK_LONG_WORD 0
356 /* #define BREAK_LONG_WORD 2/3 */
357 /* #define BREAK_LONG_WORD 4/5 */
359 /* Divides MSG into lines of WIDTH width for the first line and WIDTH
360 - INDENT width for each succeeding line. Each line is dumped
361 through FUNC, which may do with the string what it will. */
363 dump_message (char *msg, unsigned indent, void (*func) (const char *),
368 /* 1 when at a position inside double quotes ("). */
371 /* Buffer for a single line. */
374 /* If the message is short, just print the full thing. */
375 if (strlen (msg) < width)
381 /* Make sure the indent isn't too big relative to the page width. */
382 if (indent > width / 3)
385 buf = local_alloc (width + 2);
387 /* Advance WIDTH characters into MSG.
388 If that's a valid breakpoint, keep it; otherwise, back up.
390 for (cp = msg; (unsigned) (cp - msg) < width - 1 &&
391 ! compulsory_break(*cp); cp++)
395 if (break_after (quote, (unsigned char) *cp))
397 for (cp--; !char_is_break (quote, (unsigned char) *cp) && cp > msg; cp--)
401 if (break_after (quote, (unsigned char) *cp))
405 if (cp <= msg + width * BREAK_LONG_WORD)
406 for (; cp < msg + width - 1; cp++)
418 /* Repeat above procedure for remaining lines. */
421 static int hard_break=0;
426 /* Advance past whitespace. */
428 while ( isspace ((unsigned char) *cp) )
437 /* Advance WIDTH - INDENT characters. */
438 for (cp2 = cp; (unsigned) (cp2 - cp) < width - indent &&
439 *cp2 && !compulsory_break(*cp2); cp2++)
443 if ( compulsory_break(*cp2) )
449 /* Back up if this isn't a breakpoint. */
451 unsigned w = cp2 - cp;
452 if (*cp2 && ! compulsory_break(*cp2) )
453 for (cp2--; !char_is_break (quote, (unsigned char) *cp2) &&
462 if (w == width - indent
463 && (unsigned) (cp2 - cp) <= (width - indent) * BREAK_LONG_WORD)
464 for (; (unsigned) (cp2 - cp) < width - indent && *cp2 ; cp2++)
470 /* Write out the line. */
472 memset (buf, ' ', indent);
473 memcpy (&buf[indent], cp, cp2 - cp);
475 buf[indent + idx + cp2 - cp] = '\0';
485 request_bug_report_and_abort(const char *msg )
488 "******************************************************************\n"
489 "You have discovered a bug in PSPP.\n\n"
490 " Please report this, by sending "
491 "an email to " PACKAGE_BUGREPORT ",\n"
492 "explaining what you were doing when this happened, and including\n"
493 "a sample of your input file which caused it.\n");
496 "Also, please copy the following lines into your bug report:\n\n"
502 "default_config_path: %s\n"
504 "groff_font_path: %s\n"
506 "compiler version: %s\n"
526 fprintf(stderr,"Diagnosis: %s\n",msg);
529 "******************************************************************\n");
535 err_assert_fail(const char *expr, const char *file, int line)
538 snprintf(msg,256,"Assertion failed: %s:%d; (%s)",file,line,expr);
539 request_bug_report_and_abort( msg );