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. */
51 #ifdef HAVE_SYS_RESOURCE_H
52 #include <sys/resource.h>
55 #ifdef HAVE_SYS_TIME_H
59 #include "libgfortran.h"
67 /* sys_exit()-- Terminate the program with an exit code. */
72 /* Dump core if requested. */
74 && (options
.dump_core
== 1
75 || (options
.dump_core
== -1 && compile_options
.dump_core
== 1)))
77 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
78 /* Warn if a core file cannot be produced because
79 of core size limit. */
81 struct rlimit core_limit
;
83 if (getrlimit (RLIMIT_CORE
, &core_limit
) == 0 && core_limit
.rlim_cur
== 0)
84 st_printf ("** Warning: a core dump was requested, but the core size"
85 "limit\n** is currently zero.\n\n");
89 #if defined(HAVE_KILL) && defined(HAVE_GETPID) && defined(SIGQUIT)
90 kill (getpid (), SIGQUIT
);
92 st_printf ("Core dump not possible, sorry.");
100 /* Error conditions. The tricky part here is printing a message when
101 * it is the I/O subsystem that is severely wounded. Our goal is to
102 * try and print something making the fewest assumptions possible,
103 * then try to clean up before actually exiting.
105 * The following exit conditions are defined:
106 * 0 Normal program exit.
107 * 1 Terminated because of operating system error.
108 * 2 Error in the runtime library
109 * 3 Internal error in runtime library
110 * 4 Error during error processing (very bad)
112 * Other error returns are reserved for the STOP statement with a numeric code.
115 /* gfc_itoa()-- Integer to decimal conversion. */
118 gfc_itoa (GFC_INTEGER_LARGEST n
, char *buffer
, size_t len
)
122 GFC_UINTEGER_LARGEST t
;
124 assert (len
>= GFC_ITOA_BUF_SIZE
);
134 t
= -n
; /*must use unsigned to protect from overflow*/
137 p
= buffer
+ GFC_ITOA_BUF_SIZE
- 1;
142 *--p
= '0' + (t
% 10);
152 /* xtoa()-- Integer to hexadecimal conversion. */
155 xtoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
160 assert (len
>= GFC_XTOA_BUF_SIZE
);
165 p
= buffer
+ GFC_XTOA_BUF_SIZE
- 1;
172 digit
+= 'A' - '0' - 10;
182 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
185 st_sprintf (char *buffer
, const char *format
, ...)
191 char itoa_buf
[GFC_ITOA_BUF_SIZE
];
193 va_start (arg
, format
);
210 *buffer
++ = (char) va_arg (arg
, int);
214 p
= gfc_itoa (va_arg (arg
, int), itoa_buf
, sizeof (itoa_buf
));
217 memcpy (buffer
, p
, count
);
222 p
= va_arg (arg
, char *);
225 memcpy (buffer
, p
, count
);
238 /* show_locus()-- Print a line number and filename describing where
239 * something went wrong */
242 show_locus (st_parameter_common
*cmp
)
244 if (!options
.locus
|| cmp
== NULL
|| cmp
->filename
== NULL
)
247 st_printf ("At line %d of file %s\n", (int) cmp
->line
, cmp
->filename
);
251 /* recursion_check()-- It's possible for additional errors to occur
252 * during fatal error processing. We detect this condition here and
253 * exit with code 4 immediately. */
255 #define MAGIC 0x20DE8101
258 recursion_check (void)
260 static int magic
= 0;
262 /* Don't even try to print something at this point */
270 /* os_error()-- Operating system error. We get a message from the
271 * operating system, show it and leave. Some operating system errors
272 * are caught and processed by the library. If not, we come here. */
275 os_error (const char *message
)
278 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message
);
283 /* void runtime_error()-- These are errors associated with an
284 * invalid fortran program. */
287 runtime_error (const char *message
)
290 st_printf ("Fortran runtime error: %s\n", message
);
293 iexport(runtime_error
);
296 /* void internal_error()-- These are this-can't-happen errors
297 * that indicate something deeply wrong. */
300 internal_error (st_parameter_common
*cmp
, const char *message
)
304 st_printf ("Internal Error: %s\n", message
);
306 /* This function call is here to get the main.o object file included
307 when linking statically. This works because error.o is supposed to
308 be always linked in (and the function call is in internal_error
309 because hopefully it doesn't happen too often). */
310 stupid_function_name_for_static_linking();
316 /* translate_error()-- Given an integer error code, return a string
317 * describing the error. */
320 translate_error (int code
)
335 p
= "Successful return";
339 p
= "Operating system error";
342 case ERROR_BAD_OPTION
:
343 p
= "Bad statement option";
346 case ERROR_MISSING_OPTION
:
347 p
= "Missing statement option";
350 case ERROR_OPTION_CONFLICT
:
351 p
= "Conflicting statement options";
354 case ERROR_ALREADY_OPEN
:
355 p
= "File already opened in another unit";
359 p
= "Unattached unit";
366 case ERROR_BAD_ACTION
:
367 p
= "Incorrect ACTION specified";
371 p
= "Read past ENDFILE record";
375 p
= "Corrupt unformatted sequential file";
378 case ERROR_READ_VALUE
:
379 p
= "Bad value during read";
382 case ERROR_READ_OVERFLOW
:
383 p
= "Numeric overflow on read";
387 p
= "Internal error in run-time library";
390 case ERROR_INTERNAL_UNIT
:
391 p
= "Internal unit I/O error";
394 case ERROR_DIRECT_EOR
:
395 p
= "Write exceeds length of DIRECT access record";
398 case ERROR_SHORT_RECORD
:
399 p
= "I/O past end of record on unformatted file";
402 case ERROR_CORRUPT_FILE
:
403 p
= "Unformatted file structure has been corrupted";
407 p
= "Unknown error code";
415 /* generate_error()-- Come here when an error happens. This
416 * subroutine is called if it is possible to continue on after the error.
417 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
418 * ERR labels are present, we return, otherwise we terminate the program
419 * after printing a message. The error code is always required but the
420 * message parameter can be NULL, in which case a string describing
421 * the most recent operating system error is used. */
424 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
426 /* Set the error status. */
427 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
428 *cmp
->iostat
= (family
== ERROR_OS
) ? errno
: family
;
432 (family
== ERROR_OS
) ? get_oserror () : translate_error (family
);
434 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
435 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
437 /* Report status back to the compiler. */
438 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
442 cmp
->flags
|= IOPARM_LIBRETURN_EOR
;
443 if ((cmp
->flags
& IOPARM_EOR
))
448 cmp
->flags
|= IOPARM_LIBRETURN_END
;
449 if ((cmp
->flags
& IOPARM_END
))
454 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
;
455 if ((cmp
->flags
& IOPARM_ERR
))
460 /* Return if the user supplied an iostat variable. */
461 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
464 /* Terminate the program */
468 st_printf ("Fortran runtime error: %s\n", message
);
473 /* Whether, for a feature included in a given standard set (GFC_STD_*),
474 we should issue an error or a warning, or be quiet. */
477 notification_std (int std
)
481 if (!compile_options
.pedantic
)
484 warning
= compile_options
.warn_std
& std
;
485 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
488 return warning
? WARNING
: ERROR
;
493 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
494 feature. An error/warning will be issued if the currently selected
495 standard does not contain the requested bits. */
498 notify_std (st_parameter_common
*cmp
, int std
, const char * message
)
502 if (!compile_options
.pedantic
)
505 warning
= compile_options
.warn_std
& std
;
506 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
513 st_printf ("Fortran runtime error: %s\n", message
);
519 st_printf ("Fortran runtime warning: %s\n", message
);