1 /* Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran 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 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
39 #include "libgfortran.h"
41 /* Error conditions. The tricky part here is printing a message when
42 * it is the I/O subsystem that is severely wounded. Our goal is to
43 * try and print something making the fewest assumptions possible,
44 * then try to clean up before actually exiting.
46 * The following exit conditions are defined:
47 * 0 Normal program exit.
48 * 1 Terminated because of operating system error.
49 * 2 Error in the runtime library
50 * 3 Internal error in runtime library
51 * 4 Error during error processing (very bad)
53 * Other error returns are reserved for the STOP statement with a numeric code.
56 /* gfc_itoa()-- Integer to decimal conversion. */
59 gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len)
63 GFC_UINTEGER_LARGEST t;
65 assert (len >= GFC_ITOA_BUF_SIZE);
75 t = -n; /*must use unsigned to protect from overflow*/
78 p = buffer + GFC_ITOA_BUF_SIZE - 1;
83 *--p = '0' + (t % 10);
93 /* xtoa()-- Integer to hexadecimal conversion. */
96 xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
101 assert (len >= GFC_XTOA_BUF_SIZE);
106 p = buffer + GFC_XTOA_BUF_SIZE - 1;
113 digit += 'A' - '0' - 10;
123 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
126 st_sprintf (char *buffer, const char *format, ...)
132 char itoa_buf[GFC_ITOA_BUF_SIZE];
134 va_start (arg, format);
151 *buffer++ = (char) va_arg (arg, int);
155 p = gfc_itoa (va_arg (arg, int), itoa_buf, sizeof (itoa_buf));
158 memcpy (buffer, p, count);
163 p = va_arg (arg, char *);
166 memcpy (buffer, p, count);
179 /* show_locus()-- Print a line number and filename describing where
180 * something went wrong */
183 show_locus (st_parameter_common *cmp)
185 if (!options.locus || cmp == NULL || cmp->filename == NULL)
188 st_printf ("At line %d of file %s\n", (int) cmp->line, cmp->filename);
192 /* recursion_check()-- It's possible for additional errors to occur
193 * during fatal error processing. We detect this condition here and
194 * exit with code 4 immediately. */
196 #define MAGIC 0x20DE8101
199 recursion_check (void)
201 static int magic = 0;
203 /* Don't even try to print something at this point */
211 /* os_error()-- Operating system error. We get a message from the
212 * operating system, show it and leave. Some operating system errors
213 * are caught and processed by the library. If not, we come here. */
216 os_error (const char *message)
219 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message);
224 /* void runtime_error()-- These are errors associated with an
225 * invalid fortran program. */
228 runtime_error (const char *message)
231 st_printf ("Fortran runtime error: %s\n", message);
234 iexport(runtime_error);
237 /* void internal_error()-- These are this-can't-happen errors
238 * that indicate something deeply wrong. */
241 internal_error (st_parameter_common *cmp, const char *message)
245 st_printf ("Internal Error: %s\n", message);
247 /* This function call is here to get the main.o object file included
248 when linking statically. This works because error.o is supposed to
249 be always linked in (and the function call is in internal_error
250 because hopefully it doesn't happen too often). */
251 stupid_function_name_for_static_linking();
257 /* translate_error()-- Given an integer error code, return a string
258 * describing the error. */
261 translate_error (int code)
276 p = "Successful return";
280 p = "Operating system error";
283 case ERROR_BAD_OPTION:
284 p = "Bad statement option";
287 case ERROR_MISSING_OPTION:
288 p = "Missing statement option";
291 case ERROR_OPTION_CONFLICT:
292 p = "Conflicting statement options";
295 case ERROR_ALREADY_OPEN:
296 p = "File already opened in another unit";
300 p = "Unattached unit";
307 case ERROR_BAD_ACTION:
308 p = "Incorrect ACTION specified";
312 p = "Read past ENDFILE record";
316 p = "Corrupt unformatted sequential file";
319 case ERROR_READ_VALUE:
320 p = "Bad value during read";
323 case ERROR_READ_OVERFLOW:
324 p = "Numeric overflow on read";
328 p = "Internal error in run-time library";
331 case ERROR_INTERNAL_UNIT:
332 p = "Internal unit I/O error";
335 case ERROR_DIRECT_EOR:
336 p = "Write exceeds length of DIRECT access record";
339 case ERROR_SHORT_RECORD:
340 p = "I/O past end of record on unformatted file";
343 case ERROR_CORRUPT_FILE:
344 p = "Unformatted file structure has been corrupted";
348 p = "Unknown error code";
356 /* generate_error()-- Come here when an error happens. This
357 * subroutine is called if it is possible to continue on after the error.
358 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
359 * ERR labels are present, we return, otherwise we terminate the program
360 * after printing a message. The error code is always required but the
361 * message parameter can be NULL, in which case a string describing
362 * the most recent operating system error is used. */
365 generate_error (st_parameter_common *cmp, int family, const char *message)
367 /* Set the error status. */
368 if ((cmp->flags & IOPARM_HAS_IOSTAT))
369 *cmp->iostat = (family == ERROR_OS) ? errno : family;
373 (family == ERROR_OS) ? get_oserror () : translate_error (family);
375 if (cmp->flags & IOPARM_HAS_IOMSG)
376 cf_strcpy (cmp->iomsg, cmp->iomsg_len, message);
378 /* Report status back to the compiler. */
379 cmp->flags &= ~IOPARM_LIBRETURN_MASK;
383 cmp->flags |= IOPARM_LIBRETURN_EOR;
384 if ((cmp->flags & IOPARM_EOR))
389 cmp->flags |= IOPARM_LIBRETURN_END;
390 if ((cmp->flags & IOPARM_END))
395 cmp->flags |= IOPARM_LIBRETURN_ERROR;
396 if ((cmp->flags & IOPARM_ERR))
401 /* Return if the user supplied an iostat variable. */
402 if ((cmp->flags & IOPARM_HAS_IOSTAT))
405 /* Terminate the program */
409 st_printf ("Fortran runtime error: %s\n", message);
414 /* Whether, for a feature included in a given standard set (GFC_STD_*),
415 we should issue an error or a warning, or be quiet. */
418 notification_std (int std)
422 if (!compile_options.pedantic)
425 warning = compile_options.warn_std & std;
426 if ((compile_options.allow_std & std) != 0 && !warning)
429 return warning ? WARNING : ERROR;
434 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
435 feature. An error/warning will be issued if the currently selected
436 standard does not contain the requested bits. */
439 notify_std (st_parameter_common *cmp, int std, const char * message)
443 if (!compile_options.pedantic)
446 warning = compile_options.warn_std & std;
447 if ((compile_options.allow_std & std) != 0 && !warning)
454 st_printf ("Fortran runtime error: %s\n", message);
460 st_printf ("Fortran runtime warning: %s\n", message);