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 */
285 if (!options.locus || filename == NULL)
288 st_printf ("At line %d of file %s\n", line, filename);
292 /* recursion_check()-- It's possible for additional errors to occur
293 * during fatal error processing. We detect this condition here and
294 * exit with code 4 immediately. */
296 #define MAGIC 0x20DE8101
299 recursion_check (void)
301 static int magic = 0;
304 sys_exit (4); /* 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)
321 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message);
327 /* void runtime_error()-- These are errors associated with an
328 * invalid fortran program. */
331 runtime_error (const char *message)
337 st_printf ("Fortran runtime error: %s\n", message);
343 /* void internal_error()-- These are this-can't-happen errors
344 * that indicate something deeply wrong. */
347 internal_error (const char *message)
353 st_printf ("Internal Error: %s\n", message);
358 /* translate_error()-- Given an integer error code, return a string
359 * describing the error. */
362 translate_error (int code)
377 p = "Successful return";
381 p = "Operating system error";
384 case ERROR_BAD_OPTION:
385 p = "Bad statement option";
388 case ERROR_MISSING_OPTION:
389 p = "Missing statement option";
392 case ERROR_OPTION_CONFLICT:
393 p = "Conflicting statement options";
396 case ERROR_ALREADY_OPEN:
397 p = "File already opened in another unit";
401 p = "Unattached unit";
408 case ERROR_BAD_ACTION:
409 p = "Incorrect ACTION specified";
413 p = "Read past ENDFILE record";
417 p = "Corrupt unformatted sequential file";
420 case ERROR_READ_VALUE:
421 p = "Bad value during read";
424 case ERROR_READ_OVERFLOW:
425 p = "Numeric overflow on read";
429 p = "Unknown error code";
437 /* generate_error()-- Come here when an error happens. This
438 * subroutine is called if it is possible to continue on after the
439 * error. If an IOSTAT variable exists, we set it. If the IOSTAT or
440 * ERR label is present, we return, otherwise we terminate the program
441 * after print a message. The error code is always required but the
442 * message parameter can be NULL, in which case a string describing
443 * the most recent operating system error is used. */
446 generate_error (int family, const char *message)
448 /* Set the error status. */
449 if (ioparm.iostat != NULL)
450 *ioparm.iostat = family;
452 /* Report status back to the compiler. */
456 ioparm.library_return = LIBRARY_EOR;
462 ioparm.library_return = LIBRARY_END;
468 ioparm.library_return = LIBRARY_ERROR;
474 /* Return if the user supplied an iostat variable. */
475 if (ioparm.iostat != NULL)
478 /* Terminate the program */
482 (family == ERROR_OS) ? get_oserror () : translate_error (family);
484 runtime_error (message);