1 /* Copyright (C) 2002-2003 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfor).
6 Libgfor is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Libgfor is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libgfor; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include "libgfortran.h"
31 /* Error conditions. The tricky part here is printing a message when
32 * it is the I/O subsystem that is severely wounded. Our goal is to
33 * try and print something making the fewest assumptions possible,
34 * then try to clean up before actually exiting.
36 * The following exit conditions are defined:
37 * 0 Normal program exit.
38 * 1 Terminated because of operating system error.
39 * 2 Error in the runtime library
40 * 3 Internal error in runtime library
41 * 4 Error during error processing (very bad)
43 * Other error returns are reserved for the STOP statement with a numeric code.
46 /* locus variables. These are optionally set by a caller before a
47 * library subroutine is called. They are always cleared on exit so
48 * that files that report loci and those that do not can be linked
49 * together without reporting an erroneous position. */
54 static char buffer
[32]; /* buffer for integer/ascii conversions */
56 /* rtoa()-- Real to ascii conversion for base 10 and below.
57 * Returns a pointer to a static buffer. */
60 rtoa (double f
, int length
, int oprec
)
86 for (k
= 2; k
< 28 ; k
++)
98 prec
= sizeof (buffer
) - 2 - prec
;
101 prec
= prec
> oprec
? oprec
: prec
;
104 sprintf (formats
, "-%%.%df", prec
);
106 sprintf (formats
, "%%.%df", prec
);
108 sprintf (buffer
, formats
, n
);
113 /* Returns a pointer to a static buffer. */
135 p
= buffer
+ sizeof (buffer
) - 1;
140 *p
-- = '0' + (n
% 10);
150 /* xtoa()-- Integer to hexadecimal conversion. Returns a pointer to a
166 p
= buffer
+ sizeof (buffer
) - 1;
173 digit
+= 'A' - '0' - 10;
183 /* st_printf()-- simple printf() function for streams that handles the
184 * formats %d, %s and %c. This function handles printing of error
185 * messages that originate within the library itself, not from a user
189 st_printf (const char *format
, ...)
197 s
= init_error_stream ();
198 va_start (arg
, format
);
204 while (format
[count
] != '%' && format
[count
] != '\0')
209 p
= salloc_w (s
, &count
);
210 memmove (p
, format
, count
);
216 if (*format
++ == '\0')
224 p
= salloc_w (s
, &count
);
225 *p
= (char) va_arg (arg
, int);
231 q
= itoa (va_arg (arg
, int));
234 p
= salloc_w (s
, &count
);
235 memmove (p
, q
, count
);
240 q
= xtoa (va_arg (arg
, unsigned));
243 p
= salloc_w (s
, &count
);
244 memmove (p
, q
, count
);
249 q
= va_arg (arg
, char *);
252 p
= salloc_w (s
, &count
);
253 memmove (p
, q
, count
);
262 p
= salloc_w (s
, &count
);
278 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
281 st_sprintf (char *buffer
, const char *format
, ...)
287 va_start (arg
, format
);
304 *buffer
++ = (char) va_arg (arg
, int);
308 p
= itoa (va_arg (arg
, int));
311 memcpy (buffer
, p
, count
);
316 p
= va_arg (arg
, char *);
319 memcpy (buffer
, p
, count
);
332 /* show_locus()-- Print a line number and filename describing where
333 * something went wrong */
339 if (!options
.locus
|| filename
== NULL
)
342 st_printf ("At line %d of file %s\n", line
, filename
);
346 /* recursion_check()-- It's possible for additional errors to occur
347 * during fatal error processing. We detect this condition here and
348 * exit with code 4 immediately. */
350 #define MAGIC 0x20DE8101
353 recursion_check (void)
355 static int magic
= 0;
358 sys_exit (4); /* Don't even try to print something at this point */
364 /* os_error()-- Operating system error. We get a message from the
365 * operating system, show it and leave. Some operating system errors
366 * are caught and processed by the library. If not, we come here. */
369 os_error (const char *message
)
375 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message
);
381 /* void runtime_error()-- These are errors associated with an
382 * invalid fortran program. */
385 runtime_error (const char *message
)
391 st_printf ("Fortran runtime error: %s\n", message
);
397 /* void internal_error()-- These are this-can't-happen errors
398 * that indicate something deeply wrong. */
401 internal_error (const char *message
)
407 st_printf ("Internal Error: %s\n", message
);
412 /* translate_error()-- Given an integer error code, return a string
413 * describing the error. */
416 translate_error (int code
)
431 p
= "Successful return";
435 p
= "Operating system error";
438 case ERROR_BAD_OPTION
:
439 p
= "Bad statement option";
442 case ERROR_MISSING_OPTION
:
443 p
= "Missing statement option";
446 case ERROR_OPTION_CONFLICT
:
447 p
= "Conflicting statement options";
450 case ERROR_ALREADY_OPEN
:
451 p
= "File already opened in another unit";
455 p
= "Unattached unit";
462 case ERROR_BAD_ACTION
:
463 p
= "Incorrect ACTION specified";
467 p
= "Read past ENDFILE record";
471 p
= "Corrupt unformatted sequential file";
474 case ERROR_READ_VALUE
:
475 p
= "Bad value during read";
478 case ERROR_READ_OVERFLOW
:
479 p
= "Numeric overflow on read";
483 p
= "Unknown error code";
491 /* generate_error()-- Come here when an error happens. This
492 * subroutine is called if it is possible to continue on after the
493 * error. If an IOSTAT variable exists, we set it. If the IOSTAT or
494 * ERR label is present, we return, otherwise we terminate the program
495 * after print a message. The error code is always required but the
496 * message parameter can be NULL, in which case a string describing
497 * the most recent operating system error is used. */
500 generate_error (int family
, const char *message
)
503 if (ioparm
.iostat
!= NULL
)
505 *ioparm
.iostat
= family
;
512 ioparm
.library_return
= LIBRARY_EOR
;
518 ioparm
.library_return
= LIBRARY_END
;
524 ioparm
.library_return
= LIBRARY_ERROR
;
531 /* Terminate the program */
535 (family
== ERROR_OS
) ? get_oserror () : translate_error (family
);
537 runtime_error (message
);