c99_functions.c, [...]: Whitespace fixes.
[gcc.git] / libgfortran / runtime / error.c
1 /* Copyright (C) 2002-2003 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3
4 This file is part of the GNU Fortran 95 runtime library (libgfor).
5
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)
9 any later version.
10
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.
15
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. */
20
21
22 #include "config.h"
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <float.h>
27
28 #include "libgfortran.h"
29 #include "../io/io.h"
30
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.
35 *
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)
42 *
43 * Other error returns are reserved for the STOP statement with a numeric code.
44 */
45
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. */
50
51 char *filename;
52 unsigned line;
53
54 static char buffer[32]; /* buffer for integer/ascii conversions */
55
56
57 /* Returns a pointer to a static buffer. */
58
59 char *
60 itoa (int64_t n)
61 {
62 int negative;
63 char *p;
64 uint64_t t;
65
66 if (n == 0)
67 {
68 buffer[0] = '0';
69 buffer[1] = '\0';
70 return buffer;
71 }
72
73 negative = 0;
74 t = n;
75 if (n < 0)
76 {
77 negative = 1;
78 t = -n; /*must use unsigned to protect from overflow*/
79 }
80
81 p = buffer + sizeof (buffer) - 1;
82 *p-- = '\0';
83
84 while (t != 0)
85 {
86 *p-- = '0' + (t % 10);
87 t /= 10;
88 }
89
90 if (negative)
91 *p-- = '-';
92 return ++p;
93 }
94
95
96 /* xtoa()-- Integer to hexadecimal conversion. Returns a pointer to a
97 * static buffer. */
98
99 char *
100 xtoa (uint64_t n)
101 {
102 int digit;
103 char *p;
104
105 if (n == 0)
106 {
107 buffer[0] = '0';
108 buffer[1] = '\0';
109 return buffer;
110 }
111
112 p = buffer + sizeof (buffer) - 1;
113 *p-- = '\0';
114
115 while (n != 0)
116 {
117 digit = n & 0xF;
118 if (digit > 9)
119 digit += 'A' - '0' - 10;
120
121 *p-- = '0' + digit;
122 n >>= 4;
123 }
124
125 return ++p;
126 }
127
128
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
132 * program. */
133
134 int
135 st_printf (const char *format, ...)
136 {
137 int count, total;
138 va_list arg;
139 char *p, *q;
140 stream *s;
141
142 total = 0;
143 s = init_error_stream ();
144 va_start (arg, format);
145
146 for (;;)
147 {
148 count = 0;
149
150 while (format[count] != '%' && format[count] != '\0')
151 count++;
152
153 if (count != 0)
154 {
155 p = salloc_w (s, &count);
156 memmove (p, format, count);
157 sfree (s);
158 }
159
160 total += count;
161 format += count;
162 if (*format++ == '\0')
163 break;
164
165 switch (*format)
166 {
167 case 'c':
168 count = 1;
169
170 p = salloc_w (s, &count);
171 *p = (char) va_arg (arg, int);
172
173 sfree (s);
174 break;
175
176 case 'd':
177 q = itoa (va_arg (arg, int));
178 count = strlen (q);
179
180 p = salloc_w (s, &count);
181 memmove (p, q, count);
182 sfree (s);
183 break;
184
185 case 'x':
186 q = xtoa (va_arg (arg, unsigned));
187 count = strlen (q);
188
189 p = salloc_w (s, &count);
190 memmove (p, q, count);
191 sfree (s);
192 break;
193
194 case 's':
195 q = va_arg (arg, char *);
196 count = strlen (q);
197
198 p = salloc_w (s, &count);
199 memmove (p, q, count);
200 sfree (s);
201 break;
202
203 case '\0':
204 return total;
205
206 default:
207 count = 2;
208 p = salloc_w (s, &count);
209 p[0] = format[-1];
210 p[1] = format[0];
211 sfree (s);
212 break;
213 }
214
215 total += count;
216 format++;
217 }
218
219 va_end (arg);
220 return total;
221 }
222
223
224 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
225
226 void
227 st_sprintf (char *buffer, const char *format, ...)
228 {
229 va_list arg;
230 char c, *p;
231 int count;
232
233 va_start (arg, format);
234
235 for (;;)
236 {
237 c = *format++;
238 if (c != '%')
239 {
240 *buffer++ = c;
241 if (c == '\0')
242 break;
243 continue;
244 }
245
246 c = *format++;
247 switch (c)
248 {
249 case 'c':
250 *buffer++ = (char) va_arg (arg, int);
251 break;
252
253 case 'd':
254 p = itoa (va_arg (arg, int));
255 count = strlen (p);
256
257 memcpy (buffer, p, count);
258 buffer += count;
259 break;
260
261 case 's':
262 p = va_arg (arg, char *);
263 count = strlen (p);
264
265 memcpy (buffer, p, count);
266 buffer += count;
267 break;
268
269 default:
270 *buffer++ = c;
271 }
272 }
273
274 va_end (arg);
275 }
276
277
278 /* show_locus()-- Print a line number and filename describing where
279 * something went wrong */
280
281 void
282 show_locus (void)
283 {
284 if (!options.locus || filename == NULL)
285 return;
286
287 st_printf ("At line %d of file %s\n", line, filename);
288 }
289
290
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. */
294
295 #define MAGIC 0x20DE8101
296
297 static void
298 recursion_check (void)
299 {
300 static int magic = 0;
301
302 /* Don't even try to print something at this point */
303 if (magic == MAGIC)
304 sys_exit (4);
305
306 magic = MAGIC;
307 }
308
309
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. */
313
314 void
315 os_error (const char *message)
316 {
317 recursion_check ();
318 show_locus ();
319 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message);
320 sys_exit (1);
321 }
322
323
324 /* void runtime_error()-- These are errors associated with an
325 * invalid fortran program. */
326
327 void
328 runtime_error (const char *message)
329 {
330 recursion_check ();
331 show_locus ();
332 st_printf ("Fortran runtime error: %s\n", message);
333 sys_exit (2);
334 }
335
336
337 /* void internal_error()-- These are this-can't-happen errors
338 * that indicate something deeply wrong. */
339
340 void
341 internal_error (const char *message)
342 {
343 recursion_check ();
344 show_locus ();
345 st_printf ("Internal Error: %s\n", message);
346 sys_exit (3);
347 }
348
349
350 /* translate_error()-- Given an integer error code, return a string
351 * describing the error. */
352
353 const char *
354 translate_error (int code)
355 {
356 const char *p;
357
358 switch (code)
359 {
360 case ERROR_EOR:
361 p = "End of record";
362 break;
363
364 case ERROR_END:
365 p = "End of file";
366 break;
367
368 case ERROR_OK:
369 p = "Successful return";
370 break;
371
372 case ERROR_OS:
373 p = "Operating system error";
374 break;
375
376 case ERROR_BAD_OPTION:
377 p = "Bad statement option";
378 break;
379
380 case ERROR_MISSING_OPTION:
381 p = "Missing statement option";
382 break;
383
384 case ERROR_OPTION_CONFLICT:
385 p = "Conflicting statement options";
386 break;
387
388 case ERROR_ALREADY_OPEN:
389 p = "File already opened in another unit";
390 break;
391
392 case ERROR_BAD_UNIT:
393 p = "Unattached unit";
394 break;
395
396 case ERROR_FORMAT:
397 p = "FORMAT error";
398 break;
399
400 case ERROR_BAD_ACTION:
401 p = "Incorrect ACTION specified";
402 break;
403
404 case ERROR_ENDFILE:
405 p = "Read past ENDFILE record";
406 break;
407
408 case ERROR_BAD_US:
409 p = "Corrupt unformatted sequential file";
410 break;
411
412 case ERROR_READ_VALUE:
413 p = "Bad value during read";
414 break;
415
416 case ERROR_READ_OVERFLOW:
417 p = "Numeric overflow on read";
418 break;
419
420 default:
421 p = "Unknown error code";
422 break;
423 }
424
425 return p;
426 }
427
428
429 /* generate_error()-- Come here when an error happens. This
430 * subroutine is called if it is possible to continue on after the
431 * error. If an IOSTAT variable exists, we set it. If the IOSTAT or
432 * ERR label is present, we return, otherwise we terminate the program
433 * after print a message. The error code is always required but the
434 * message parameter can be NULL, in which case a string describing
435 * the most recent operating system error is used. */
436
437 void
438 generate_error (int family, const char *message)
439 {
440 /* Set the error status. */
441 if (ioparm.iostat != NULL)
442 *ioparm.iostat = family;
443
444 /* Report status back to the compiler. */
445 switch (family)
446 {
447 case ERROR_EOR:
448 ioparm.library_return = LIBRARY_EOR;
449 if (ioparm.eor != 0)
450 return;
451 break;
452
453 case ERROR_END:
454 ioparm.library_return = LIBRARY_END;
455 if (ioparm.end != 0)
456 return;
457 break;
458
459 default:
460 ioparm.library_return = LIBRARY_ERROR;
461 if (ioparm.err != 0)
462 return;
463 break;
464 }
465
466 /* Return if the user supplied an iostat variable. */
467 if (ioparm.iostat != NULL)
468 return;
469
470 /* Terminate the program */
471
472 if (message == NULL)
473 message =
474 (family == ERROR_OS) ? get_oserror () : translate_error (family);
475
476 runtime_error (message);
477 }