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., 59 Temple Place - Suite 330, Boston, MA
37 int err_warning_count;
39 int err_already_flagged;
43 /* File locator stack. */
44 static const struct file_locator **file_loc;
45 static int nfile_loc, mfile_loc;
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, ...)
56 /* Format the message into BUF. */
60 va_start (args, format);
61 vsnprintf (buf, 1024, format, args);
65 /* Output the message. */
70 err_location (&e.where);
77 /* Writes error message in CLASS, with text FORMAT, formatted with
78 printf, to the standard places. */
80 msg (int class, const char *format, ...)
84 ds_init (NULL, &buf, 1024);
86 /* Format the message into BUF. */
90 va_start (args, format);
91 ds_vprintf (&buf, format, args);
95 /* Output the message. */
100 err_location (&e.where);
109 /* Terminate due to fatal error in input. */
116 fprintf (stderr, "%s: %s\n", pgmname,
117 _("Terminating NOW due to a fatal error!"));
122 /* Terminate unless we're interactive or will go interactive when the
123 file is over with. */
127 if (getl_reading_script)
129 if (getl_interactive)
136 /* File locator stack functions. */
138 /* Pushes F onto the stack of file locations. */
140 err_push_file_locator (const struct file_locator *f)
142 if (nfile_loc >= mfile_loc)
149 file_loc = xrealloc (file_loc, mfile_loc * sizeof *file_loc);
152 file_loc[nfile_loc++] = f;
155 /* Pops F off the stack of file locations.
156 Argument F is only used for verification that that is actually the
157 item on top of the stack. */
159 err_pop_file_locator (const struct file_locator *f)
161 assert (nfile_loc >= 0 && file_loc[nfile_loc - 1] == f);
165 /* Puts the current file and line number in F, or NULL and -1 if
168 err_location (struct file_locator *f)
171 *f = *file_loc[nfile_loc - 1];
173 getl_location (&f->filename, &f->line_number);
176 /* Obscure public functions. */
178 /* Writes a blank line to the error device(s).
179 FIXME: currently a no-op. */
185 /* Checks whether we've had so many errors that it's time to quit
186 processing this syntax file. If so, then take appropriate
189 err_check_count (void)
191 int error_class = getl_interactive ? MM : FE;
193 if (get_errorbreak() && err_error_count)
194 msg (error_class, _("Terminating execution of syntax file due to error."));
195 else if (err_error_count > get_mxerrs() )
196 msg (error_class, _("Errors (%d) exceeds limit (%d)."),
197 err_error_count, get_mxerrs());
198 else if (err_error_count + err_warning_count > get_mxwarns() )
199 msg (error_class, _("Warnings (%d) exceed limit (%d)."),
200 err_error_count + err_warning_count, get_mxwarns() );
207 /* Some machines are broken. Compensate. */
209 #define EXIT_SUCCESS 0
213 #define EXIT_FAILURE 1
216 static int terminating;
218 /* Halt-catch-fire. SUCCESS should be nonzero if exiting successfully
219 or zero if not. Despite the name, this is the usual way to finish,
220 successfully or not. */
222 err_hcf (int success)
226 getl_uninitialize ();
230 exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
233 static void puts_stdout (const char *s);
234 static void dump_message (char *errbuf, unsigned indent,
235 void (*func) (const char *), unsigned width);
238 err_vmsg (const struct error *e)
243 ERR_IN_PROCEDURE = 01, /* 1=Display name of current procedure. */
244 ERR_WITH_FILE = 02, /* 1=Display filename and line number. */
247 /* Describes one class of error. */
250 int flags; /* Zero or more of ERR_*. */
251 int *count; /* Counting category. */
252 const char *banner; /* Banner. */
255 static const struct error_class error_classes[ERR_CLASS_COUNT] =
257 {0, NULL, N_("fatal")}, /* FE */
259 {3, &err_error_count, N_("error")}, /* SE */
260 {3, &err_warning_count, N_("warning")}, /* SW */
261 {3, NULL, N_("note")}, /* SM */
263 {0, NULL, N_("installation error")}, /* IE */
264 {2, NULL, N_("installation error")}, /* IS */
266 {2, &err_error_count, N_("error")}, /* DE */
267 {2, &err_warning_count, N_("warning")}, /* DW */
269 {0, &err_error_count, N_("error")}, /* ME */
270 {0, &err_warning_count, N_("warning")}, /* MW */
271 {0, NULL, N_("note")}, /* MM */
277 /* Check verbosity level. */
279 if (((class >> ERR_VERBOSITY_SHIFT) & ERR_VERBOSITY_MASK) > err_verbosity)
281 class &= ERR_CLASS_MASK;
283 assert (class >= 0 && class < ERR_CLASS_COUNT);
284 assert (e->text != NULL);
286 ds_init (NULL, &msg, 64);
287 if (e->where.filename && (error_classes[class].flags & ERR_WITH_FILE))
289 ds_printf (&msg, "%s:", e->where.filename);
290 if (e->where.line_number != -1)
291 ds_printf (&msg, "%d:", e->where.line_number);
292 ds_putchar (&msg, ' ');
295 ds_printf (&msg, "%s: ", gettext (error_classes[class].banner));
298 int *count = error_classes[class].count;
303 if (cur_proc && (error_classes[class].flags & ERR_IN_PROCEDURE))
304 ds_printf (&msg, "%s: ", cur_proc);
307 ds_concat (&msg, e->title);
309 ds_concat (&msg, e->text);
311 /* FIXME: Check set_messages and set_errors to determine where to
312 send errors and messages.
314 Please note that this is not trivial. We have to avoid an
315 infinite loop in reporting errors that originate in the output
317 dump_message (ds_value (&msg), 8, puts_stdout, get_viewwidth());
321 if (e->class == FE && !terminating)
325 /* Private functions. */
328 /* Write S followed by a newline to stderr. */
330 puts_stderr (const char *s)
333 fputc ('\n', stderr);
337 /* Write S followed by a newline to stdout. */
339 puts_stdout (const char *s)
344 /* Returns 1 if the line must be broken here */
346 compulsory_break(int c)
348 return ( c == '\n' );
351 /* Returns 1 if C is a `break character', that is, if it is a good
352 place to break a message into lines. */
354 char_is_break (int quote, int c)
356 return ((quote && c == DIR_SEPARATOR)
357 || (!quote && (isspace (c) || c == '-' || c == '/')));
360 /* Returns 1 if C is a break character where the break should be made
361 BEFORE the character. */
363 break_before (int quote, int c)
365 return !quote && isspace (c);
368 /* If C is a break character, returns 1 if the break should be made
369 AFTER the character. Does not return a meaningful result if C is
370 not a break character. */
372 break_after (int quote, int c)
374 return !break_before (quote, c);
377 /* If you want very long words that occur at a bad break point to be
378 broken into two lines even if they're shorter than a whole line by
379 themselves, define as 2/3, or 4/5, or whatever fraction of a whole
380 line you think is necessary in order to consider a word long enough
381 to break into pieces. Otherwise, define as 0. See code to grok
382 the details. Do NOT parenthesize the expression! */
383 #define BREAK_LONG_WORD 0
384 /* #define BREAK_LONG_WORD 2/3 */
385 /* #define BREAK_LONG_WORD 4/5 */
387 /* Divides MSG into lines of WIDTH width for the first line and WIDTH
388 - INDENT width for each succeeding line. Each line is dumped
389 through FUNC, which may do with the string what it will. */
391 dump_message (char *msg, unsigned indent, void (*func) (const char *),
396 /* 1 when at a position inside double quotes ("). */
399 /* Buffer for a single line. */
402 /* If the message is short, just print the full thing. */
403 if (strlen (msg) < width)
409 /* Make sure the indent isn't too big relative to the page width. */
410 if (indent > width / 3)
413 buf = local_alloc (width + 2);
415 /* Advance WIDTH characters into MSG.
416 If that's a valid breakpoint, keep it; otherwise, back up.
418 for (cp = msg; (unsigned) (cp - msg) < width - 1 &&
419 ! compulsory_break(*cp); cp++)
423 if (break_after (quote, (unsigned char) *cp))
425 for (cp--; !char_is_break (quote, (unsigned char) *cp) && cp > msg; cp--)
429 if (break_after (quote, (unsigned char) *cp))
433 if (cp <= msg + width * BREAK_LONG_WORD)
434 for (; cp < msg + width - 1; cp++)
446 /* Repeat above procedure for remaining lines. */
449 static int hard_break=0;
454 /* Advance past whitespace. */
456 while ( isspace ((unsigned char) *cp) )
465 /* Advance WIDTH - INDENT characters. */
466 for (cp2 = cp; (unsigned) (cp2 - cp) < width - indent &&
467 *cp2 && !compulsory_break(*cp2); cp2++)
471 if ( compulsory_break(*cp2) )
477 /* Back up if this isn't a breakpoint. */
479 unsigned w = cp2 - cp;
480 if (*cp2 && ! compulsory_break(*cp2) )
481 for (cp2--; !char_is_break (quote, (unsigned char) *cp2) &&
490 if (w == width - indent
491 && (unsigned) (cp2 - cp) <= (width - indent) * BREAK_LONG_WORD)
492 for (; (unsigned) (cp2 - cp) < width - indent && *cp2 ; cp2++)
498 /* Write out the line. */
500 memset (buf, ' ', indent);
501 memcpy (&buf[indent], cp, cp2 - cp);
505 buf[indent + idx + cp2 - cp] = '\n';
508 buf[indent + idx + cp2 - cp] = '\0';