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, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
36 #include "libgfortran.h"
38 /* Environment scanner. Examine the environment for controlling minor
39 * aspects of the program's execution. Our philosophy here that the
40 * environment should not prevent the program from running, so an
41 * environment variable with a messed-up value will be interpreted in
44 * Most of the environment is checked early in the startup sequence,
45 * but other variables are checked during execution of the user's
51 typedef struct variable
55 void (*init) (struct variable *);
56 void (*show) (struct variable *);
62 static void init_unformatted (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);
112 if (!isdigit (*q) && (p != q || *q != '-'))
127 /* init_unsigned_integer()-- Initialize an integer environment variable
128 which has to be positive. */
131 init_unsigned_integer (variable * v)
135 p = getenv (v->name);
155 /* show_integer()-- Show an integer environment variable */
158 show_integer (variable * v)
160 st_printf ("%s %d\n", var_source (v), *v->var);
164 /* init_boolean()-- Initialize a boolean environment variable. We
165 * only look at the first letter of the variable. */
168 init_boolean (variable * v)
172 p = getenv (v->name);
176 if (*p == '1' || *p == 'Y' || *p == 'y')
182 if (*p == '0' || *p == 'N' || *p == 'n')
196 /* show_boolean()-- Show a boolean environment variable */
199 show_boolean (variable * v)
201 st_printf ("%s %s\n", var_source (v), *v->var ? "Yes" : "No");
205 /* init_mem()-- Initialize environment variables that have to do with
206 * how memory from an ALLOCATE statement is filled. A single flag
207 * enables filling and a second variable gives the value that is used
208 * to initialize the memory. */
211 init_mem (variable * v)
216 p = getenv (v->name);
218 options.allocate_init_flag = 0; /* The default */
223 if (strcasecmp (p, "NONE") == 0)
226 /* IEEE-754 Quiet Not-a-Number that will work for single and double
227 * precision. Look for the 'f95' mantissa in debug dumps. */
229 if (strcasecmp (p, "NaN") == 0)
231 options.allocate_init_flag = 1;
232 options.allocate_init_value = 0xfff80f95;
236 /* Interpret the string as a hexadecimal constant */
253 n = (n << 4) | (*p++ - offset);
256 options.allocate_init_flag = 1;
257 options.allocate_init_value = n;
262 show_mem (variable * v)
266 p = getenv (v->name);
268 st_printf ("%s ", var_source (v));
270 if (options.allocate_init_flag)
271 st_printf ("0x%x", options.allocate_init_value);
278 init_sep (variable * v)
283 p = getenv (v->name);
288 options.separator = p;
289 options.separator_len = strlen (p);
291 /* Make sure the separator is valid */
293 if (options.separator_len == 0)
316 options.separator = " ";
317 options.separator_len = 1;
322 show_sep (variable * v)
324 st_printf ("%s \"%s\"\n", var_source (v), options.separator);
329 init_string (variable * v __attribute__ ((unused)))
334 show_string (variable * v)
338 p = getenv (v->name);
342 st_printf ("%s \"%s\"\n", var_source (v), p);
346 /* Structure for associating names and values. */
357 { FP_ROUND_NEAREST, FP_ROUND_UP, FP_ROUND_DOWN, FP_ROUND_ZERO };
359 static const choice rounding[] = {
360 {"NEAREST", FP_ROUND_NEAREST},
362 {"DOWN", FP_ROUND_DOWN},
363 {"ZERO", FP_ROUND_ZERO},
367 static const choice precision[] =
375 static const choice signal_choices[] =
384 init_choice (variable * v, const choice * c)
388 p = getenv (v->name);
393 if (strcasecmp (c->name, p) == 0)
411 show_choice (variable * v, const choice * c)
413 st_printf ("%s ", var_source (v));
416 if (c->value == *v->var)
420 st_printf ("%s\n", c->name);
422 st_printf ("(Unknown)\n");
427 init_round (variable * v)
429 init_choice (v, rounding);
433 show_round (variable * v)
435 show_choice (v, rounding);
439 init_precision (variable * v)
441 init_choice (v, precision);
445 show_precision (variable * v)
447 show_choice (v, precision);
451 init_signal (variable * v)
453 init_choice (v, signal_choices);
457 show_signal (variable * v)
459 show_choice (v, signal_choices);
463 static variable variable_table[] = {
464 {"GFORTRAN_STDIN_UNIT", 5, &options.stdin_unit, init_integer, show_integer,
465 "Unit number that will be preconnected to standard input\n"
466 "(No preconnection if negative)", 0},
468 {"GFORTRAN_STDOUT_UNIT", 6, &options.stdout_unit, init_integer,
470 "Unit number that will be preconnected to standard output\n"
471 "(No preconnection if negative)", 0},
473 {"GFORTRAN_STDERR_UNIT", 0, &options.stderr_unit, init_integer,
475 "Unit number that will be preconnected to standard error\n"
476 "(No preconnection if negative)", 0},
478 {"GFORTRAN_USE_STDERR", 1, &options.use_stderr, init_boolean,
480 "Sends library output to standard error instead of standard output.", 0},
482 {"GFORTRAN_TMPDIR", 0, NULL, init_string, show_string,
483 "Directory for scratch files. Overrides the TMP environment variable\n"
484 "If TMP is not set " DEFAULT_TEMPDIR " is used.", 0},
486 {"GFORTRAN_UNBUFFERED_ALL", 0, &options.all_unbuffered, init_boolean,
488 "If TRUE, all output is unbuffered. This will slow down large writes "
489 "but can be\nuseful for forcing data to be displayed immediately.", 0},
491 {"GFORTRAN_SHOW_LOCUS", 1, &options.locus, init_boolean, show_boolean,
492 "If TRUE, print filename and line number where runtime errors happen.", 0},
494 {"GFORTRAN_OPTIONAL_PLUS", 0, &options.optional_plus, init_boolean, show_boolean,
495 "Print optional plus signs in numbers where permitted. Default FALSE.", 0},
497 {"GFORTRAN_DEFAULT_RECL", DEFAULT_RECL, &options.default_recl,
498 init_unsigned_integer, show_integer,
499 "Default maximum record length for sequential files. Most useful for\n"
500 "adjusting line length of preconnected units. Default "
501 stringize (DEFAULT_RECL), 0},
503 {"GFORTRAN_LIST_SEPARATOR", 0, NULL, init_sep, show_sep,
504 "Separator to use when writing list output. May contain any number of "
505 "spaces\nand at most one comma. Default is a single space.", 0},
507 /* Memory related controls */
509 {"GFORTRAN_MEM_INIT", 0, NULL, init_mem, show_mem,
510 "How to initialize allocated memory. Default value is NONE for no "
511 "initialization\n(faster), NAN for a Not-a-Number with the mantissa "
512 "0x40f95 or a custom\nhexadecimal value", 0},
514 {"GFORTRAN_MEM_CHECK", 0, &options.mem_check, init_boolean, show_boolean,
515 "Whether memory still allocated will be reported when the program ends.",
518 /* Signal handling (Unix). */
520 {"GFORTRAN_SIGHUP", 0, &options.sighup, init_signal, show_signal,
521 "Whether the program will IGNORE or ABORT on SIGHUP.", 0},
523 {"GFORTRAN_SIGINT", 0, &options.sigint, init_signal, show_signal,
524 "Whether the program will IGNORE or ABORT on SIGINT.", 0},
526 /* Floating point control */
528 {"GFORTRAN_FPU_ROUND", 0, &options.fpu_round, init_round, show_round,
529 "Set floating point rounding. Values are NEAREST, UP, DOWN, ZERO.", 0},
531 {"GFORTRAN_FPU_PRECISION", 0, &options.fpu_precision, init_precision,
533 "Precision of intermediate results. Values are 24, 53 and 64.", 0},
535 /* GFORTRAN_CONVERT_UNIT - Set the default data conversion for
537 {"GFORTRAN_CONVERT_UNIT", 0, 0, init_unformatted, show_string,
538 "Set format for unformatted files", 0},
540 {NULL, 0, NULL, NULL, NULL, NULL, 0}
544 /* init_variables()-- Initialize most runtime variables from
545 * environment variables. */
548 init_variables (void)
552 for (v = variable_table; v->name; v++)
557 /* check_buffered()-- Given an unit number n, determine if an override
558 * for the stream exists. Returns zero for unbuffered, one for
559 * buffered or two for not set. */
562 check_buffered (int n)
564 char name[22 + sizeof (n) * 3];
568 if (options.all_unbuffered)
571 sprintf (name, "GFORTRAN_UNBUFFERED_%d", n);
584 show_variables (void)
589 /* TODO: print version number. */
590 st_printf ("GNU Fortran 95 runtime library version "
593 st_printf ("Environment variables:\n");
594 st_printf ("----------------------\n");
596 for (v = variable_table; v->name; v++)
598 n = st_printf ("%s", v->name);
599 print_spaces (25 - n);
601 if (v->show == show_integer)
602 st_printf ("Integer ");
603 else if (v->show == show_boolean)
604 st_printf ("Boolean ");
606 st_printf ("String ");
609 st_printf ("%s\n\n", v->desc);
612 /* System error codes */
614 st_printf ("\nRuntime error codes:");
615 st_printf ("\n--------------------\n");
617 for (n = ERROR_FIRST + 1; n < ERROR_LAST; n++)
619 st_printf ("%d %s\n", n, translate_error (n));
621 st_printf (" %d %s\n", n, translate_error (n));
623 st_printf ("\nCommand line arguments:\n");
624 st_printf (" --help Print this list\n");
626 /* st_printf(" --resume <dropfile> Resume program execution from dropfile\n"); */
631 /* This is the handling of the GFORTRAN_CONVERT_UNITS environment variable.
632 It is called from environ.c to parse this variable, and from
633 open.c to determine if the user specified a default for an
635 The syntax of the environment variable is, in bison grammar:
637 GFORTRAN_CONVERT_UNITS: mode | mode ';' exception ;
638 mode: 'native' | 'swap' | 'big_endian' | 'little_endian' ;
639 exception: mode ':' unit_list | unit_list ;
640 unit_list: unit_spec | unit_list unit_spec ;
641 unit_spec: INTEGER | INTEGER '-' INTEGER ;
644 /* Defines for the tokens. Other valid tokens are ',', ':', '-'. */
651 /* Some space for additional tokens later. */
663 static char *p; /* Main character pointer for parsing. */
664 static char *lastpos; /* Auxiliary pointer, for backing up. */
665 static int unit_num; /* The last unit number read. */
666 static int unit_count; /* The number of units found. */
667 static int do_count; /* Parsing is done twice - first to count the number
668 of units, then to fill in the table. This
669 variable controls what to do. */
670 static exception_t *elist; /* The list of exceptions to the default. This is
671 sorted according to unit number. */
672 static int n_elist; /* Number of exceptions to the default. */
674 static unit_convert endian; /* Current endianness. */
676 static unit_convert def; /* Default as specified (if any). */
678 /* Search for a unit number, using a binary search. The
679 first argument is the unit number to search for. The second argument
680 is a pointer to an index.
681 If the unit number is found, the function returns 1, and the index
682 is that of the element.
683 If the unit number is not found, the function returns 0, and the
684 index is the one where the element would be inserted. */
687 search_unit (int unit, int *ip)
693 while (high - low > 1)
695 mid = (low + high) / 2;
696 if (unit <= elist[mid].unit)
702 if (elist[high].unit == unit)
708 /* This matches a keyword. If it is found, return the token supplied,
709 otherwise return ILLEGAL. */
712 match_word (const char *word, int tok)
716 if (strncasecmp (p, word, strlen (word)) == 0)
727 /* Match an integer and store its value in unit_num. This only works
728 if p actually points to the start of an integer. The caller has
736 unit_num = unit_num * 10 + (*p++ - '0');
741 /* This reads the next token from the GFORTRAN_CONVERT_UNITS variable.
742 Returned values are the different tokens. */
766 result = match_word ("big_endian", BIG);
771 result = match_word ("little_endian", LITTLE);
776 result = match_word ("native", NATIVE);
781 result = match_word ("swap", SWAP);
784 case '1': case '2': case '3': case '4': case '5':
785 case '6': case '7': case '8': case '9':
786 result = match_integer ();
796 /* Back up the last token by setting back the character pointer. */
804 /* This is called when a unit is identified. If do_count is nonzero,
805 increment the number of units by one. If do_count is zero,
806 put the unit into the table. */
809 mark_single (int unit)
818 if (search_unit (unit, &i))
820 elist[unit].conv = endian;
824 for (j=n_elist; j>=i; j--)
825 elist[j+1] = elist[j];
828 elist[i].unit = unit;
829 elist[i].conv = endian;
833 /* This is called when a unit range is identified. If do_count is
834 nonzero, increase the number of units. If do_count is zero,
835 put the unit into the table. */
838 mark_range (int unit1, int unit2)
842 unit_count += abs (unit2 - unit1) + 1;
846 for (i=unit2; i<=unit1; i++)
849 for (i=unit1; i<=unit2; i++)
854 /* Parse the GFORTRAN_CONVERT_UNITS variable. This is called
855 twice, once to count the units and once to actually mark them in
856 the table. When counting, we don't check for double occurrences
872 /* Parse the string. First, let's look for a default. */
877 endian = CONVERT_NATIVE;
881 endian = CONVERT_SWAP;
885 endian = CONVERT_BIG;
889 endian = CONVERT_LITTLE;
893 /* A leading digit means that we are looking at an exception.
894 Reset the position to the beginning, and continue processing
895 at the exception list. */
917 /* This isn't a default after all. Reset the position to the
918 beginning, and continue processing at the exception list. */
934 /* Loop over all exceptions. */
941 if (next_token () != ':')
943 endian = CONVERT_LITTLE;
947 if (next_token () != ':')
949 endian = CONVERT_BIG;
964 /* We arrive here when we want to parse a list of
975 /* The number can be followed by a - and another number,
976 which means that this is a unit range, a comma
980 if (next_token () != INTEGER)
983 mark_range (unit1, unit_num);
1012 } while (continue_ulist);
1021 void init_unformatted (variable * v)
1024 val = getenv (v->name);
1040 elist = get_mem (unit_count * sizeof (exception_t));
1047 /* Get the default conversion for for an unformatted unit. */
1050 get_unformatted_convert (int unit)
1056 else if (search_unit (unit, &i))
1057 return elist[i].conv;