1 /* Copyright (C) 2002,2003,2005 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, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
35 #include "libgfortran.h"
39 /* Environment scanner. Examine the environment for controlling minor
40 * aspects of the program's execution. Our philosophy here that the
41 * environment should not prevent the program from running, so an
42 * environment variable with a messed-up value will be interpreted in
45 * Most of the environment is checked early in the startup sequence,
46 * but other variables are checked during execution of the user's
49 options_t options
= { };
52 typedef struct variable
56 void (*init
) (struct variable
*);
57 void (*show
) (struct variable
*);
64 /* print_spaces()-- Print a particular number of spaces */
75 for (i
= 0; i
< n
; i
++)
84 /* var_source()-- Return a string that describes where the value of a
85 * variable comes from */
88 var_source (variable
* v
)
90 if (getenv (v
->name
) == NULL
)
100 /* init_integer()-- Initialize an integer environment variable */
103 init_integer (variable
* v
)
107 p
= getenv (v
->name
);
127 /* show_integer()-- Show an integer environment variable */
130 show_integer (variable
* v
)
132 st_printf ("%s %d\n", var_source (v
), *v
->var
);
136 /* init_boolean()-- Initialize a boolean environment variable. We
137 * only look at the first letter of the variable. */
140 init_boolean (variable
* v
)
144 p
= getenv (v
->name
);
148 if (*p
== '1' || *p
== 'Y' || *p
== 'y')
154 if (*p
== '0' || *p
== 'N' || *p
== 'n')
168 /* show_boolean()-- Show a boolean environment variable */
171 show_boolean (variable
* v
)
173 st_printf ("%s %s\n", var_source (v
), *v
->var
? "Yes" : "No");
177 /* init_mem()-- Initialize environment variables that have to do with
178 * how memory from an ALLOCATE statement is filled. A single flag
179 * enables filling and a second variable gives the value that is used
180 * to initialize the memory. */
183 init_mem (variable
* v
)
188 p
= getenv (v
->name
);
190 options
.allocate_init_flag
= 0; /* The default */
195 if (strcasecmp (p
, "NONE") == 0)
198 /* IEEE-754 Quiet Not-a-Number that will work for single and double
199 * precision. Look for the 'f95' mantissa in debug dumps. */
201 if (strcasecmp (p
, "NaN") == 0)
203 options
.allocate_init_flag
= 1;
204 options
.allocate_init_value
= 0xfff80f95;
208 /* Interpret the string as a hexadecimal constant */
225 n
= (n
<< 4) | (*p
++ - offset
);
228 options
.allocate_init_flag
= 1;
229 options
.allocate_init_value
= n
;
234 show_mem (variable
* v
)
238 p
= getenv (v
->name
);
240 st_printf ("%s ", var_source (v
));
242 if (options
.allocate_init_flag
)
243 st_printf ("0x%x", options
.allocate_init_value
);
250 init_sep (variable
* v
)
255 p
= getenv (v
->name
);
260 options
.separator
= p
;
261 options
.separator_len
= strlen (p
);
263 /* Make sure the separator is valid */
265 if (options
.separator_len
== 0)
288 options
.separator
= " ";
289 options
.separator_len
= 1;
294 show_sep (variable
* v
)
296 st_printf ("%s \"%s\"\n", var_source (v
), options
.separator
);
301 init_string (variable
* v
)
306 show_string (variable
* v
)
310 p
= getenv (v
->name
);
314 st_printf ("%s \"%s\"\n", var_source (v
), p
);
318 /* Structure for associating names and values. */
329 { FP_ROUND_NEAREST
, FP_ROUND_UP
, FP_ROUND_DOWN
, FP_ROUND_ZERO
};
331 static choice rounding
[] = {
332 {"NEAREST", FP_ROUND_NEAREST
},
334 {"DOWN", FP_ROUND_DOWN
},
335 {"ZERO", FP_ROUND_ZERO
},
339 static choice precision
[] =
347 static choice signal_choices
[] =
356 init_choice (variable
* v
, choice
* c
)
360 p
= getenv (v
->name
);
365 if (strcasecmp (c
->name
, p
) == 0)
383 show_choice (variable
* v
, choice
* c
)
385 st_printf ("%s ", var_source (v
));
388 if (c
->value
== *v
->var
)
392 st_printf ("%s\n", c
->name
);
394 st_printf ("(Unknown)\n");
399 init_round (variable
* v
)
401 init_choice (v
, rounding
);
405 show_round (variable
* v
)
407 show_choice (v
, rounding
);
411 init_precision (variable
* v
)
413 init_choice (v
, precision
);
417 show_precision (variable
* v
)
419 show_choice (v
, precision
);
423 init_signal (variable
* v
)
425 init_choice (v
, signal_choices
);
429 show_signal (variable
* v
)
431 show_choice (v
, signal_choices
);
435 static variable variable_table
[] = {
436 {"GFORTRAN_STDIN_UNIT", 5, &options
.stdin_unit
, init_integer
, show_integer
,
437 "Unit number that will be preconnected to standard input\n"
438 "(No preconnection if negative)"},
440 {"GFORTRAN_STDOUT_UNIT", 6, &options
.stdout_unit
, init_integer
,
442 "Unit number that will be preconnected to standard output\n"
443 "(No preconnection if negative)"},
445 {"GFORTRAN_STDERR_UNIT", 0, &options
.stderr_unit
, init_integer
,
447 "Unit number that will be preconnected to standard error\n"
448 "(No preconnection if negative)"},
450 {"GFORTRAN_USE_STDERR", 1, &options
.use_stderr
, init_boolean
,
452 "Sends library output to standard error instead of standard output."},
454 {"GFORTRAN_TMPDIR", 0, NULL
, init_string
, show_string
,
455 "Directory for scratch files. Overrides the TMP environment variable\n"
456 "If TMP is not set " DEFAULT_TEMPDIR
" is used."},
458 {"GFORTRAN_UNBUFFERED_ALL", 0, &options
.all_unbuffered
, init_boolean
,
460 "If TRUE, all output is unbuffered. This will slow down large writes "
461 "but can be\nuseful for forcing data to be displayed immediately."},
463 {"GFORTRAN_SHOW_LOCUS", 1, &options
.locus
, init_boolean
, show_boolean
,
464 "If TRUE, print filename and line number where runtime errors happen."},
466 {"GFORTRAN_OPTIONAL_PLUS", 0, &options
.optional_plus
, init_boolean
, show_boolean
,
467 "Print optional plus signs in numbers where permitted. Default FALSE."},
469 {"GFORTRAN_DEFAULT_RECL", DEFAULT_RECL
, &options
.default_recl
,
470 init_integer
, show_integer
,
471 "Default maximum record length for sequential files. Most useful for\n"
472 "adjusting line length of preconnected units. Default "
473 stringize (DEFAULT_RECL
)},
475 {"GFORTRAN_LIST_SEPARATOR", 0, NULL
, init_sep
, show_sep
,
476 "Separatator to use when writing list output. May contain any number of "
477 "spaces\nand at most one comma. Default is a single space."},
479 /* Memory related controls */
481 {"GFORTRAN_MEM_INIT", 0, NULL
, init_mem
, show_mem
,
482 "How to initialize allocated memory. Default value is NONE for no "
483 "initialization\n(faster), NAN for a Not-a-Number with the mantissa "
484 "0x40f95 or a custom\nhexadecimal value"},
486 {"GFORTRAN_MEM_CHECK", 0, &options
.mem_check
, init_boolean
, show_boolean
,
487 "Whether memory still allocated will be reported when the program ends."},
489 /* Signal handling (Unix). */
491 {"GFORTRAN_SIGHUP", 0, &options
.sighup
, init_signal
, show_signal
,
492 "Whether the program will IGNORE or ABORT on SIGHUP."},
494 {"GFORTRAN_SIGINT", 0, &options
.sigint
, init_signal
, show_signal
,
495 "Whether the program will IGNORE or ABORT on SIGINT."},
497 /* Floating point control */
499 {"GFORTRAN_FPU_ROUND", 0, &options
.fpu_round
, init_round
, show_round
,
500 "Set floating point rounding. Values are NEAREST, UP, DOWN, ZERO."},
502 {"GFORTRAN_FPU_PRECISION", 0, &options
.fpu_precision
, init_precision
,
504 "Precision of intermediate results. Values are 24, 53 and 64."},
506 {"GFORTRAN_FPU_INVALID", 1, &options
.fpu_invalid
, init_boolean
,
508 "Raise a floating point exception on invalid FP operation."},
510 {"GFORTRAN_FPU_DENORMAL", 1, &options
.fpu_denormal
, init_boolean
,
512 "Raise a floating point exception when denormal numbers are encountered."},
514 {"GFORTRAN_FPU_ZERO", 0, &options
.fpu_zerodiv
, init_boolean
, show_boolean
,
515 "Raise a floating point exception when dividing by zero."},
517 {"GFORTRAN_FPU_OVERFLOW", 0, &options
.fpu_overflow
, init_boolean
,
519 "Raise a floating point exception on overflow."},
521 {"GFORTRAN_FPU_UNDERFLOW", 0, &options
.fpu_underflow
, init_boolean
,
523 "Raise a floating point exception on underflow."},
525 {"GFORTRAN_FPU_PRECISION", 0, &options
.fpu_precision_loss
, init_boolean
,
527 "Raise a floating point exception on precision loss."},
533 /* init_variables()-- Initialize most runtime variables from
534 * environment variables. */
537 init_variables (void)
541 for (v
= variable_table
; v
->name
; v
++)
546 /* check_buffered()-- Given an unit number n, determine if an override
547 * for the stream exists. Returns zero for unbuffered, one for
548 * buffered or two for not set. */
551 check_buffered (int n
)
557 if (options
.all_unbuffered
)
560 strcpy (name
, "GFORTRAN_UNBUFFERED_");
561 strcat (name
, gfc_itoa (n
));
574 show_variables (void)
579 /* TODO: print version number. */
580 st_printf ("GNU Fortran 95 runtime library version "
583 st_printf ("Environment variables:\n");
584 st_printf ("----------------------\n");
586 for (v
= variable_table
; v
->name
; v
++)
588 n
= st_printf ("%s", v
->name
);
589 print_spaces (25 - n
);
591 if (v
->show
== show_integer
)
592 st_printf ("Integer ");
593 else if (v
->show
== show_boolean
)
594 st_printf ("Boolean ");
596 st_printf ("String ");
599 st_printf ("%s\n\n", v
->desc
);
602 /* System error codes */
604 st_printf ("\nRuntime error codes:");
605 st_printf ("\n--------------------\n");
607 for (n
= ERROR_FIRST
+ 1; n
< ERROR_LAST
; n
++)
609 st_printf ("%d %s\n", n
, translate_error (n
));
611 st_printf (" %d %s\n", n
, translate_error (n
));
613 st_printf ("\nCommand line arguments:\n");
614 st_printf (" --help Print this list\n");
616 /* st_printf(" --resume <dropfile> Resume program execution from dropfile\n"); */