1 /* Copyright (C) 2002-2013 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 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 3, or (at your option)
11 Libgfortran 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.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
32 /* Environment scanner. Examine the environment for controlling minor
33 * aspects of the program's execution. Our philosophy here that the
34 * environment should not prevent the program from running, so an
35 * environment variable with a messed-up value will be interpreted in
38 * Most of the environment is checked early in the startup sequence,
39 * but other variables are checked during execution of the user's
45 typedef struct variable
49 void (*init
) (struct variable
*);
50 void (*show
) (struct variable
*);
56 static void init_unformatted (variable
*);
59 #ifdef FALLBACK_SECURE_GETENV
61 secure_getenv (const char *name
)
63 if ((getuid () == geteuid ()) && (getgid () == getegid ()))
71 /* print_spaces()-- Print a particular number of spaces. */
82 for (i
= 0; i
< n
; i
++)
91 /* var_source()-- Return a string that describes where the value of a
92 * variable comes from */
95 var_source (variable
* v
)
97 if (getenv (v
->name
) == NULL
)
107 /* init_integer()-- Initialize an integer environment variable. */
110 init_integer (variable
* v
)
114 p
= getenv (v
->name
);
119 if (!isdigit (*q
) && (p
!= q
|| *q
!= '-'))
134 /* init_unsigned_integer()-- Initialize an integer environment variable
135 which has to be positive. */
138 init_unsigned_integer (variable
* v
)
142 p
= getenv (v
->name
);
162 /* show_integer()-- Show an integer environment variable */
165 show_integer (variable
* v
)
167 st_printf ("%s %d\n", var_source (v
), *v
->var
);
171 /* init_boolean()-- Initialize a boolean environment variable. We
172 * only look at the first letter of the variable. */
175 init_boolean (variable
* v
)
179 p
= getenv (v
->name
);
183 if (*p
== '1' || *p
== 'Y' || *p
== 'y')
189 if (*p
== '0' || *p
== 'N' || *p
== 'n')
203 /* show_boolean()-- Show a boolean environment variable */
206 show_boolean (variable
* v
)
208 st_printf ("%s %s\n", var_source (v
), *v
->var
? "Yes" : "No");
213 init_sep (variable
* v
)
218 p
= getenv (v
->name
);
223 options
.separator
= p
;
224 options
.separator_len
= strlen (p
);
226 /* Make sure the separator is valid */
228 if (options
.separator_len
== 0)
251 options
.separator
= " ";
252 options
.separator_len
= 1;
257 show_sep (variable
* v
)
259 st_printf ("%s \"%s\"\n", var_source (v
), options
.separator
);
264 init_string (variable
* v
__attribute__ ((unused
)))
269 show_string (variable
* v
)
273 p
= getenv (v
->name
);
277 estr_write (var_source (v
));
284 static variable variable_table
[] = {
285 {"GFORTRAN_STDIN_UNIT", GFC_STDIN_UNIT_NUMBER
, &options
.stdin_unit
,
286 init_integer
, show_integer
,
287 "Unit number that will be preconnected to standard input\n"
288 "(No preconnection if negative)", 0},
290 {"GFORTRAN_STDOUT_UNIT", GFC_STDOUT_UNIT_NUMBER
, &options
.stdout_unit
,
291 init_integer
, show_integer
,
292 "Unit number that will be preconnected to standard output\n"
293 "(No preconnection if negative)", 0},
295 {"GFORTRAN_STDERR_UNIT", GFC_STDERR_UNIT_NUMBER
, &options
.stderr_unit
,
296 init_integer
, show_integer
,
297 "Unit number that will be preconnected to standard error\n"
298 "(No preconnection if negative)", 0},
300 {"TMPDIR", 0, NULL
, init_string
, show_string
,
301 "Directory for scratch files.", 0},
303 {"GFORTRAN_UNBUFFERED_ALL", 0, &options
.all_unbuffered
, init_boolean
,
305 "If TRUE, all output is unbuffered. This will slow down large writes "
306 "but can be\nuseful for forcing data to be displayed immediately.", 0},
308 {"GFORTRAN_UNBUFFERED_PRECONNECTED", 0, &options
.unbuffered_preconnected
,
309 init_boolean
, show_boolean
,
310 "If TRUE, output to preconnected units is unbuffered.", 0},
312 {"GFORTRAN_SHOW_LOCUS", 1, &options
.locus
, init_boolean
, show_boolean
,
313 "If TRUE, print filename and line number where runtime errors happen.", 0},
315 {"GFORTRAN_OPTIONAL_PLUS", 0, &options
.optional_plus
, init_boolean
, show_boolean
,
316 "Print optional plus signs in numbers where permitted. Default FALSE.", 0},
318 {"GFORTRAN_DEFAULT_RECL", DEFAULT_RECL
, &options
.default_recl
,
319 init_unsigned_integer
, show_integer
,
320 "Default maximum record length for sequential files. Most useful for\n"
321 "adjusting line length of preconnected units. Default "
322 stringize (DEFAULT_RECL
), 0},
324 {"GFORTRAN_LIST_SEPARATOR", 0, NULL
, init_sep
, show_sep
,
325 "Separator to use when writing list output. May contain any number of "
326 "spaces\nand at most one comma. Default is a single space.", 0},
328 /* GFORTRAN_CONVERT_UNIT - Set the default data conversion for
330 {"GFORTRAN_CONVERT_UNIT", 0, 0, init_unformatted
, show_string
,
331 "Set format for unformatted files", 0},
333 {"GFORTRAN_ERROR_BACKTRACE", -1, &options
.backtrace
,
334 init_boolean
, show_boolean
,
335 "Print out a backtrace (if possible) on runtime error", -1},
337 {NULL
, 0, NULL
, NULL
, NULL
, NULL
, 0}
341 /* init_variables()-- Initialize most runtime variables from
342 * environment variables. */
345 init_variables (void)
349 for (v
= variable_table
; v
->name
; v
++)
355 show_variables (void)
360 /* TODO: print version number. */
361 estr_write ("GNU Fortran runtime library version "
364 estr_write ("Environment variables:\n");
365 estr_write ("----------------------\n");
367 for (v
= variable_table
; v
->name
; v
++)
369 n
= estr_write (v
->name
);
370 print_spaces (25 - n
);
372 if (v
->show
== show_integer
)
373 estr_write ("Integer ");
374 else if (v
->show
== show_boolean
)
375 estr_write ("Boolean ");
377 estr_write ("String ");
380 estr_write (v
->desc
);
384 /* System error codes */
386 estr_write ("\nRuntime error codes:");
387 estr_write ("\n--------------------\n");
389 for (n
= LIBERROR_FIRST
+ 1; n
< LIBERROR_LAST
; n
++)
391 st_printf ("%d %s\n", n
, translate_error (n
));
393 st_printf (" %d %s\n", n
, translate_error (n
));
395 estr_write ("\nCommand line arguments:\n");
396 estr_write (" --help Print this list\n");
401 /* This is the handling of the GFORTRAN_CONVERT_UNITS environment variable.
402 It is called from environ.c to parse this variable, and from
403 open.c to determine if the user specified a default for an
405 The syntax of the environment variable is, in bison grammar:
407 GFORTRAN_CONVERT_UNITS: mode | mode ';' exception ;
408 mode: 'native' | 'swap' | 'big_endian' | 'little_endian' ;
409 exception: mode ':' unit_list | unit_list ;
410 unit_list: unit_spec | unit_list unit_spec ;
411 unit_spec: INTEGER | INTEGER '-' INTEGER ;
414 /* Defines for the tokens. Other valid tokens are ',', ':', '-'. */
421 /* Some space for additional tokens later. */
433 static char *p
; /* Main character pointer for parsing. */
434 static char *lastpos
; /* Auxiliary pointer, for backing up. */
435 static int unit_num
; /* The last unit number read. */
436 static int unit_count
; /* The number of units found. */
437 static int do_count
; /* Parsing is done twice - first to count the number
438 of units, then to fill in the table. This
439 variable controls what to do. */
440 static exception_t
*elist
; /* The list of exceptions to the default. This is
441 sorted according to unit number. */
442 static int n_elist
; /* Number of exceptions to the default. */
444 static unit_convert endian
; /* Current endianness. */
446 static unit_convert def
; /* Default as specified (if any). */
448 /* Search for a unit number, using a binary search. The
449 first argument is the unit number to search for. The second argument
450 is a pointer to an index.
451 If the unit number is found, the function returns 1, and the index
452 is that of the element.
453 If the unit number is not found, the function returns 0, and the
454 index is the one where the element would be inserted. */
457 search_unit (int unit
, int *ip
)
472 mid
= (low
+ high
) / 2;
473 if (unit
== elist
[mid
].unit
)
478 else if (unit
> elist
[mid
].unit
)
482 } while (low
<= high
);
484 if (unit
> elist
[mid
].unit
)
492 /* This matches a keyword. If it is found, return the token supplied,
493 otherwise return ILLEGAL. */
496 match_word (const char *word
, int tok
)
500 if (strncasecmp (p
, word
, strlen (word
)) == 0)
511 /* Match an integer and store its value in unit_num. This only works
512 if p actually points to the start of an integer. The caller has
520 unit_num
= unit_num
* 10 + (*p
++ - '0');
525 /* This reads the next token from the GFORTRAN_CONVERT_UNITS variable.
526 Returned values are the different tokens. */
550 result
= match_word ("big_endian", BIG
);
555 result
= match_word ("little_endian", LITTLE
);
560 result
= match_word ("native", NATIVE
);
565 result
= match_word ("swap", SWAP
);
568 case '1': case '2': case '3': case '4': case '5':
569 case '6': case '7': case '8': case '9':
570 result
= match_integer ();
580 /* Back up the last token by setting back the character pointer. */
588 /* This is called when a unit is identified. If do_count is nonzero,
589 increment the number of units by one. If do_count is zero,
590 put the unit into the table. */
593 mark_single (int unit
)
602 if (search_unit (unit
, &i
))
604 elist
[i
].conv
= endian
;
608 for (j
=n_elist
-1; j
>=i
; j
--)
609 elist
[j
+1] = elist
[j
];
612 elist
[i
].unit
= unit
;
613 elist
[i
].conv
= endian
;
617 /* This is called when a unit range is identified. If do_count is
618 nonzero, increase the number of units. If do_count is zero,
619 put the unit into the table. */
622 mark_range (int unit1
, int unit2
)
626 unit_count
+= abs (unit2
- unit1
) + 1;
630 for (i
=unit2
; i
<=unit1
; i
++)
633 for (i
=unit1
; i
<=unit2
; i
++)
638 /* Parse the GFORTRAN_CONVERT_UNITS variable. This is called
639 twice, once to count the units and once to actually mark them in
640 the table. When counting, we don't check for double occurrences
655 /* Parse the string. First, let's look for a default. */
660 endian
= GFC_CONVERT_NATIVE
;
664 endian
= GFC_CONVERT_SWAP
;
668 endian
= GFC_CONVERT_BIG
;
672 endian
= GFC_CONVERT_LITTLE
;
676 /* A leading digit means that we are looking at an exception.
677 Reset the position to the beginning, and continue processing
678 at the exception list. */
700 /* This isn't a default after all. Reset the position to the
701 beginning, and continue processing at the exception list. */
718 /* Loop over all exceptions. */
725 if (next_token () != ':')
727 endian
= GFC_CONVERT_NATIVE
;
731 if (next_token () != ':')
733 endian
= GFC_CONVERT_SWAP
;
737 if (next_token () != ':')
739 endian
= GFC_CONVERT_LITTLE
;
743 if (next_token () != ':')
745 endian
= GFC_CONVERT_BIG
;
760 /* We arrive here when we want to parse a list of
771 /* The number can be followed by a - and another number,
772 which means that this is a unit range, a comma
776 if (next_token () != INTEGER
)
779 mark_range (unit1
, unit_num
);
808 } while (continue_ulist
);
813 def
= GFC_CONVERT_NONE
;
817 void init_unformatted (variable
* v
)
820 val
= getenv (v
->name
);
821 def
= GFC_CONVERT_NONE
;
836 elist
= xmallocarray (unit_count
, sizeof (exception_t
));
843 /* Get the default conversion for for an unformatted unit. */
846 get_unformatted_convert (int unit
)
852 else if (search_unit (unit
, &i
))
853 return elist
[i
].conv
;