* Mainline merge as of 2006-02-16 (@111136).
[official-gcc.git] / libgfortran / runtime / error.c
blobb25cd0c8c160934e02308bbff6de54aa48dd0ee2
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)
9 any later version.
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
18 executable.)
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. */
31 #include "config.h"
32 #include <assert.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <float.h>
38 #include "libgfortran.h"
39 #include "../io/io.h"
40 #include "../io/unix.h"
42 /* Error conditions. The tricky part here is printing a message when
43 * it is the I/O subsystem that is severely wounded. Our goal is to
44 * try and print something making the fewest assumptions possible,
45 * then try to clean up before actually exiting.
47 * The following exit conditions are defined:
48 * 0 Normal program exit.
49 * 1 Terminated because of operating system error.
50 * 2 Error in the runtime library
51 * 3 Internal error in runtime library
52 * 4 Error during error processing (very bad)
54 * Other error returns are reserved for the STOP statement with a numeric code.
57 /* gfc_itoa()-- Integer to decimal conversion. */
59 const char *
60 gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len)
62 int negative;
63 char *p;
64 GFC_UINTEGER_LARGEST t;
66 assert (len >= GFC_ITOA_BUF_SIZE);
68 if (n == 0)
69 return "0";
71 negative = 0;
72 t = n;
73 if (n < 0)
75 negative = 1;
76 t = -n; /*must use unsigned to protect from overflow*/
79 p = buffer + GFC_ITOA_BUF_SIZE - 1;
80 *p = '\0';
82 while (t != 0)
84 *--p = '0' + (t % 10);
85 t /= 10;
88 if (negative)
89 *--p = '-';
90 return p;
94 /* xtoa()-- Integer to hexadecimal conversion. */
96 const char *
97 xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
99 int digit;
100 char *p;
102 assert (len >= GFC_XTOA_BUF_SIZE);
104 if (n == 0)
105 return "0";
107 p = buffer + GFC_XTOA_BUF_SIZE - 1;
108 *p = '\0';
110 while (n != 0)
112 digit = n & 0xF;
113 if (digit > 9)
114 digit += 'A' - '0' - 10;
116 *--p = '0' + digit;
117 n >>= 4;
120 return p;
124 /* st_printf()-- simple printf() function for streams that handles the
125 * formats %d, %s and %c. This function handles printing of error
126 * messages that originate within the library itself, not from a user
127 * program. */
130 st_printf (const char *format, ...)
132 int count, total;
133 va_list arg;
134 char *p;
135 const char *q;
136 stream *s;
137 char itoa_buf[GFC_ITOA_BUF_SIZE];
138 unix_stream err_stream;
140 total = 0;
141 s = init_error_stream (&err_stream);
142 va_start (arg, format);
144 for (;;)
146 count = 0;
148 while (format[count] != '%' && format[count] != '\0')
149 count++;
151 if (count != 0)
153 p = salloc_w (s, &count);
154 memmove (p, format, count);
155 sfree (s);
158 total += count;
159 format += count;
160 if (*format++ == '\0')
161 break;
163 switch (*format)
165 case 'c':
166 count = 1;
168 p = salloc_w (s, &count);
169 *p = (char) va_arg (arg, int);
171 sfree (s);
172 break;
174 case 'd':
175 q = gfc_itoa (va_arg (arg, int), itoa_buf, sizeof (itoa_buf));
176 count = strlen (q);
178 p = salloc_w (s, &count);
179 memmove (p, q, count);
180 sfree (s);
181 break;
183 case 'x':
184 q = xtoa (va_arg (arg, unsigned), itoa_buf, sizeof (itoa_buf));
185 count = strlen (q);
187 p = salloc_w (s, &count);
188 memmove (p, q, count);
189 sfree (s);
190 break;
192 case 's':
193 q = va_arg (arg, char *);
194 count = strlen (q);
196 p = salloc_w (s, &count);
197 memmove (p, q, count);
198 sfree (s);
199 break;
201 case '\0':
202 return total;
204 default:
205 count = 2;
206 p = salloc_w (s, &count);
207 p[0] = format[-1];
208 p[1] = format[0];
209 sfree (s);
210 break;
213 total += count;
214 format++;
217 va_end (arg);
218 return total;
222 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
224 void
225 st_sprintf (char *buffer, const char *format, ...)
227 va_list arg;
228 char c;
229 const char *p;
230 int count;
231 char itoa_buf[GFC_ITOA_BUF_SIZE];
233 va_start (arg, format);
235 for (;;)
237 c = *format++;
238 if (c != '%')
240 *buffer++ = c;
241 if (c == '\0')
242 break;
243 continue;
246 c = *format++;
247 switch (c)
249 case 'c':
250 *buffer++ = (char) va_arg (arg, int);
251 break;
253 case 'd':
254 p = gfc_itoa (va_arg (arg, int), itoa_buf, sizeof (itoa_buf));
255 count = strlen (p);
257 memcpy (buffer, p, count);
258 buffer += count;
259 break;
261 case 's':
262 p = va_arg (arg, char *);
263 count = strlen (p);
265 memcpy (buffer, p, count);
266 buffer += count;
267 break;
269 default:
270 *buffer++ = c;
274 va_end (arg);
278 /* show_locus()-- Print a line number and filename describing where
279 * something went wrong */
281 void
282 show_locus (st_parameter_common *cmp)
284 if (!options.locus || cmp == NULL || cmp->filename == NULL)
285 return;
287 st_printf ("At line %d of file %s\n", cmp->line, cmp->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
297 static void
298 recursion_check (void)
300 static int magic = 0;
302 /* Don't even try to print something at this point */
303 if (magic == MAGIC)
304 sys_exit (4);
306 magic = MAGIC;
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. */
314 void
315 os_error (const char *message)
317 recursion_check ();
318 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message);
319 sys_exit (1);
323 /* void runtime_error()-- These are errors associated with an
324 * invalid fortran program. */
326 void
327 runtime_error (const char *message)
329 recursion_check ();
330 st_printf ("Fortran runtime error: %s\n", message);
331 sys_exit (2);
333 iexport(runtime_error);
336 /* void internal_error()-- These are this-can't-happen errors
337 * that indicate something deeply wrong. */
339 void
340 internal_error (st_parameter_common *cmp, const char *message)
342 recursion_check ();
343 show_locus (cmp);
344 st_printf ("Internal Error: %s\n", message);
346 /* This function call is here to get the main.o object file included
347 when linking statically. This works because error.o is supposed to
348 be always linked in (and the function call is in internal_error
349 because hopefully it doesn't happen too often). */
350 stupid_function_name_for_static_linking();
352 sys_exit (3);
356 /* translate_error()-- Given an integer error code, return a string
357 * describing the error. */
359 const char *
360 translate_error (int code)
362 const char *p;
364 switch (code)
366 case ERROR_EOR:
367 p = "End of record";
368 break;
370 case ERROR_END:
371 p = "End of file";
372 break;
374 case ERROR_OK:
375 p = "Successful return";
376 break;
378 case ERROR_OS:
379 p = "Operating system error";
380 break;
382 case ERROR_BAD_OPTION:
383 p = "Bad statement option";
384 break;
386 case ERROR_MISSING_OPTION:
387 p = "Missing statement option";
388 break;
390 case ERROR_OPTION_CONFLICT:
391 p = "Conflicting statement options";
392 break;
394 case ERROR_ALREADY_OPEN:
395 p = "File already opened in another unit";
396 break;
398 case ERROR_BAD_UNIT:
399 p = "Unattached unit";
400 break;
402 case ERROR_FORMAT:
403 p = "FORMAT error";
404 break;
406 case ERROR_BAD_ACTION:
407 p = "Incorrect ACTION specified";
408 break;
410 case ERROR_ENDFILE:
411 p = "Read past ENDFILE record";
412 break;
414 case ERROR_BAD_US:
415 p = "Corrupt unformatted sequential file";
416 break;
418 case ERROR_READ_VALUE:
419 p = "Bad value during read";
420 break;
422 case ERROR_READ_OVERFLOW:
423 p = "Numeric overflow on read";
424 break;
426 case ERROR_INTERNAL:
427 p = "Internal error in run-time library";
428 break;
430 case ERROR_INTERNAL_UNIT:
431 p = "Internal unit I/O error";
432 break;
434 default:
435 p = "Unknown error code";
436 break;
439 return p;
443 /* generate_error()-- Come here when an error happens. This
444 * subroutine is called if it is possible to continue on after the error.
445 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
446 * ERR labels are present, we return, otherwise we terminate the program
447 * after printing a message. The error code is always required but the
448 * message parameter can be NULL, in which case a string describing
449 * the most recent operating system error is used. */
451 void
452 generate_error (st_parameter_common *cmp, int family, const char *message)
454 /* Set the error status. */
455 if ((cmp->flags & IOPARM_HAS_IOSTAT))
456 *cmp->iostat = family;
458 if (message == NULL)
459 message =
460 (family == ERROR_OS) ? get_oserror () : translate_error (family);
462 if (cmp->flags & IOPARM_HAS_IOMSG)
463 cf_strcpy (cmp->iomsg, cmp->iomsg_len, message);
465 /* Report status back to the compiler. */
466 cmp->flags &= ~IOPARM_LIBRETURN_MASK;
467 switch (family)
469 case ERROR_EOR:
470 cmp->flags |= IOPARM_LIBRETURN_EOR;
471 if ((cmp->flags & IOPARM_EOR))
472 return;
473 break;
475 case ERROR_END:
476 cmp->flags |= IOPARM_LIBRETURN_END;
477 if ((cmp->flags & IOPARM_END))
478 return;
479 break;
481 default:
482 cmp->flags |= IOPARM_LIBRETURN_ERROR;
483 if ((cmp->flags & IOPARM_ERR))
484 return;
485 break;
488 /* Return if the user supplied an iostat variable. */
489 if ((cmp->flags & IOPARM_HAS_IOSTAT))
490 return;
492 /* Terminate the program */
494 recursion_check ();
495 show_locus (cmp);
496 st_printf ("Fortran runtime error: %s\n", message);
497 sys_exit (2);
502 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
503 feature. An error/warning will be issued if the currently selected
504 standard does not contain the requested bits. */
507 notify_std (int std, const char * message)
509 int warning;
511 if (!compile_options.pedantic)
512 return SUCCESS;
514 warning = compile_options.warn_std & std;
515 if ((compile_options.allow_std & std) != 0 && !warning)
516 return SUCCESS;
518 if (!warning)
520 st_printf ("Fortran runtime error: %s\n", message);
521 sys_exit (2);
523 else
524 st_printf ("Fortran runtime warning: %s\n", message);
525 return FAILURE;