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 */
57 /* Returns a pointer to a static buffer. */
78 t
= -n
; /*must use unsigned to protect from overflow*/
81 p
= buffer
+ sizeof (buffer
) - 1;
86 *p
-- = '0' + (t
% 10);
96 /* xtoa()-- Integer to hexadecimal conversion. Returns a pointer to a
112 p
= buffer
+ sizeof (buffer
) - 1;
119 digit
+= 'A' - '0' - 10;
129 /* st_printf()-- simple printf() function for streams that handles the
130 * formats %d, %s and %c. This function handles printing of error
131 * messages that originate within the library itself, not from a user
135 st_printf (const char *format
, ...)
143 s
= init_error_stream ();
144 va_start (arg
, format
);
150 while (format
[count
] != '%' && format
[count
] != '\0')
155 p
= salloc_w (s
, &count
);
156 memmove (p
, format
, count
);
162 if (*format
++ == '\0')
170 p
= salloc_w (s
, &count
);
171 *p
= (char) va_arg (arg
, int);
177 q
= itoa (va_arg (arg
, int));
180 p
= salloc_w (s
, &count
);
181 memmove (p
, q
, count
);
186 q
= xtoa (va_arg (arg
, unsigned));
189 p
= salloc_w (s
, &count
);
190 memmove (p
, q
, count
);
195 q
= va_arg (arg
, char *);
198 p
= salloc_w (s
, &count
);
199 memmove (p
, q
, count
);
208 p
= salloc_w (s
, &count
);
224 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
227 st_sprintf (char *buffer
, const char *format
, ...)
233 va_start (arg
, format
);
250 *buffer
++ = (char) va_arg (arg
, int);
254 p
= itoa (va_arg (arg
, int));
257 memcpy (buffer
, p
, count
);
262 p
= va_arg (arg
, char *);
265 memcpy (buffer
, p
, count
);
278 /* show_locus()-- Print a line number and filename describing where
279 * something went wrong */
284 if (!options
.locus
|| filename
== NULL
)
287 st_printf ("At line %d of file %s\n", line
, filename
);
291 /* recursion_check()-- It's possible for additional errors to occur
292 * during fatal error processing. We detect this condition here and
293 * exit with code 4 immediately. */
295 #define MAGIC 0x20DE8101
298 recursion_check (void)
300 static int magic
= 0;
302 /* Don't even try to print something at this point */
310 /* os_error()-- Operating system error. We get a message from the
311 * operating system, show it and leave. Some operating system errors
312 * are caught and processed by the library. If not, we come here. */
315 os_error (const char *message
)
319 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message
);
324 /* void runtime_error()-- These are errors associated with an
325 * invalid fortran program. */
328 runtime_error (const char *message
)
332 st_printf ("Fortran runtime error: %s\n", message
);
337 /* void internal_error()-- These are this-can't-happen errors
338 * that indicate something deeply wrong. */
341 internal_error (const char *message
)
345 st_printf ("Internal Error: %s\n", message
);
350 /* translate_error()-- Given an integer error code, return a string
351 * describing the error. */
354 translate_error (int code
)
369 p
= "Successful return";
373 p
= "Operating system error";
376 case ERROR_BAD_OPTION
:
377 p
= "Bad statement option";
380 case ERROR_MISSING_OPTION
:
381 p
= "Missing statement option";
384 case ERROR_OPTION_CONFLICT
:
385 p
= "Conflicting statement options";
388 case ERROR_ALREADY_OPEN
:
389 p
= "File already opened in another unit";
393 p
= "Unattached unit";
400 case ERROR_BAD_ACTION
:
401 p
= "Incorrect ACTION specified";
405 p
= "Read past ENDFILE record";
409 p
= "Corrupt unformatted sequential file";
412 case ERROR_READ_VALUE
:
413 p
= "Bad value during read";
416 case ERROR_READ_OVERFLOW
:
417 p
= "Numeric overflow on read";
421 p
= "Unknown error code";
429 /* generate_error()-- Come here when an error happens. This
430 * subroutine is called if it is possible to continue on after the
431 * error. If an IOSTAT variable exists, we set it. If the IOSTAT or
432 * ERR label is present, we return, otherwise we terminate the program
433 * after print a message. The error code is always required but the
434 * message parameter can be NULL, in which case a string describing
435 * the most recent operating system error is used. */
438 generate_error (int family
, const char *message
)
440 /* Set the error status. */
441 if (ioparm
.iostat
!= NULL
)
442 *ioparm
.iostat
= family
;
444 /* Report status back to the compiler. */
448 ioparm
.library_return
= LIBRARY_EOR
;
454 ioparm
.library_return
= LIBRARY_END
;
460 ioparm
.library_return
= LIBRARY_ERROR
;
466 /* Return if the user supplied an iostat variable. */
467 if (ioparm
.iostat
!= NULL
)
470 /* Terminate the program */
474 (family
== ERROR_OS
) ? get_oserror () : translate_error (family
);
476 runtime_error (message
);