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. */
52 iexport_data(filename
);
57 static char buffer
[32]; /* buffer for integer/ascii conversions */
60 /* Returns a pointer to a static buffer. */
81 t
= -n
; /*must use unsigned to protect from overflow*/
84 p
= buffer
+ sizeof (buffer
) - 1;
89 *p
-- = '0' + (t
% 10);
99 /* xtoa()-- Integer to hexadecimal conversion. Returns a pointer to a
115 p
= buffer
+ sizeof (buffer
) - 1;
122 digit
+= 'A' - '0' - 10;
132 /* st_printf()-- simple printf() function for streams that handles the
133 * formats %d, %s and %c. This function handles printing of error
134 * messages that originate within the library itself, not from a user
138 st_printf (const char *format
, ...)
146 s
= init_error_stream ();
147 va_start (arg
, format
);
153 while (format
[count
] != '%' && format
[count
] != '\0')
158 p
= salloc_w (s
, &count
);
159 memmove (p
, format
, count
);
165 if (*format
++ == '\0')
173 p
= salloc_w (s
, &count
);
174 *p
= (char) va_arg (arg
, int);
180 q
= gfc_itoa (va_arg (arg
, int));
183 p
= salloc_w (s
, &count
);
184 memmove (p
, q
, count
);
189 q
= xtoa (va_arg (arg
, unsigned));
192 p
= salloc_w (s
, &count
);
193 memmove (p
, q
, count
);
198 q
= va_arg (arg
, char *);
201 p
= salloc_w (s
, &count
);
202 memmove (p
, q
, count
);
211 p
= salloc_w (s
, &count
);
227 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
230 st_sprintf (char *buffer
, const char *format
, ...)
236 va_start (arg
, format
);
253 *buffer
++ = (char) va_arg (arg
, int);
257 p
= gfc_itoa (va_arg (arg
, int));
260 memcpy (buffer
, p
, count
);
265 p
= va_arg (arg
, char *);
268 memcpy (buffer
, p
, count
);
281 /* show_locus()-- Print a line number and filename describing where
282 * something went wrong */
287 if (!options
.locus
|| filename
== NULL
)
290 st_printf ("At line %d of file %s\n", line
, filename
);
294 /* recursion_check()-- It's possible for additional errors to occur
295 * during fatal error processing. We detect this condition here and
296 * exit with code 4 immediately. */
298 #define MAGIC 0x20DE8101
301 recursion_check (void)
303 static int magic
= 0;
305 /* Don't even try to print something at this point */
313 /* os_error()-- Operating system error. We get a message from the
314 * operating system, show it and leave. Some operating system errors
315 * are caught and processed by the library. If not, we come here. */
318 os_error (const char *message
)
322 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
)
335 st_printf ("Fortran runtime error: %s\n", message
);
338 iexport(runtime_error
);
341 /* void internal_error()-- These are this-can't-happen errors
342 * that indicate something deeply wrong. */
345 internal_error (const char *message
)
349 st_printf ("Internal Error: %s\n", message
);
354 /* translate_error()-- Given an integer error code, return a string
355 * describing the error. */
358 translate_error (int code
)
373 p
= "Successful return";
377 p
= "Operating system error";
380 case ERROR_BAD_OPTION
:
381 p
= "Bad statement option";
384 case ERROR_MISSING_OPTION
:
385 p
= "Missing statement option";
388 case ERROR_OPTION_CONFLICT
:
389 p
= "Conflicting statement options";
392 case ERROR_ALREADY_OPEN
:
393 p
= "File already opened in another unit";
397 p
= "Unattached unit";
404 case ERROR_BAD_ACTION
:
405 p
= "Incorrect ACTION specified";
409 p
= "Read past ENDFILE record";
413 p
= "Corrupt unformatted sequential file";
416 case ERROR_READ_VALUE
:
417 p
= "Bad value during read";
420 case ERROR_READ_OVERFLOW
:
421 p
= "Numeric overflow on read";
425 p
= "Unknown error code";
433 /* generate_error()-- Come here when an error happens. This
434 * subroutine is called if it is possible to continue on after the
435 * error. If an IOSTAT variable exists, we set it. If the IOSTAT or
436 * ERR label is present, we return, otherwise we terminate the program
437 * after print a message. The error code is always required but the
438 * message parameter can be NULL, in which case a string describing
439 * the most recent operating system error is used. */
442 generate_error (int family
, const char *message
)
444 /* Set the error status. */
445 if (ioparm
.iostat
!= NULL
)
446 *ioparm
.iostat
= family
;
448 /* Report status back to the compiler. */
452 ioparm
.library_return
= LIBRARY_EOR
;
458 ioparm
.library_return
= LIBRARY_END
;
464 ioparm
.library_return
= LIBRARY_ERROR
;
470 /* Return if the user supplied an iostat variable. */
471 if (ioparm
.iostat
!= NULL
)
474 /* Terminate the program */
478 (family
== ERROR_OS
) ? get_oserror () : translate_error (family
);
480 runtime_error (message
);