1 /* Copyright (C) 2002-2015 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"
36 /* Environment scanner. Examine the environment for controlling minor
37 * aspects of the program's execution. Our philosophy here that the
38 * environment should not prevent the program from running, so an
39 * environment variable with a messed-up value will be interpreted in
42 * Most of the environment is checked early in the startup sequence,
43 * but other variables are checked during execution of the user's
49 typedef struct variable
53 void (*init
) (struct variable
*);
54 void (*show
) (struct variable
*);
60 static void init_unformatted (variable
*);
63 #ifdef FALLBACK_SECURE_GETENV
65 secure_getenv (const char *name
)
67 if ((getuid () == geteuid ()) && (getgid () == getegid ()))
75 /* print_spaces()-- Print a particular number of spaces. */
86 for (i
= 0; i
< n
; i
++)
95 /* var_source()-- Return a string that describes where the value of a
96 * variable comes from */
99 var_source (variable
* v
)
101 if (getenv (v
->name
) == NULL
)
111 /* init_integer()-- Initialize an integer environment variable. */
114 init_integer (variable
* v
)
118 p
= getenv (v
->name
);
123 if (!isdigit (*q
) && (p
!= q
|| *q
!= '-'))
138 /* init_unsigned_integer()-- Initialize an integer environment variable
139 which has to be positive. */
142 init_unsigned_integer (variable
* v
)
146 p
= getenv (v
->name
);
166 /* show_integer()-- Show an integer environment variable */
169 show_integer (variable
* v
)
171 st_printf ("%s %d\n", var_source (v
), *v
->var
);
175 /* init_boolean()-- Initialize a boolean environment variable. We
176 * only look at the first letter of the variable. */
179 init_boolean (variable
* v
)
183 p
= getenv (v
->name
);
187 if (*p
== '1' || *p
== 'Y' || *p
== 'y')
193 if (*p
== '0' || *p
== 'N' || *p
== 'n')
207 /* show_boolean()-- Show a boolean environment variable */
210 show_boolean (variable
* v
)
212 st_printf ("%s %s\n", var_source (v
), *v
->var
? "Yes" : "No");
217 init_sep (variable
* v
)
222 p
= getenv (v
->name
);
227 options
.separator
= p
;
228 options
.separator_len
= strlen (p
);
230 /* Make sure the separator is valid */
232 if (options
.separator_len
== 0)
255 options
.separator
= " ";
256 options
.separator_len
= 1;
261 show_sep (variable
* v
)
263 st_printf ("%s \"%s\"\n", var_source (v
), options
.separator
);
268 init_string (variable
* v
__attribute__ ((unused
)))
273 show_string (variable
* v
)
277 p
= getenv (v
->name
);
281 estr_write (var_source (v
));
288 static variable variable_table
[] = {
289 {"GFORTRAN_STDIN_UNIT", GFC_STDIN_UNIT_NUMBER
, &options
.stdin_unit
,
290 init_integer
, show_integer
,
291 "Unit number that will be preconnected to standard input\n"
292 "(No preconnection if negative)", 0},
294 {"GFORTRAN_STDOUT_UNIT", GFC_STDOUT_UNIT_NUMBER
, &options
.stdout_unit
,
295 init_integer
, show_integer
,
296 "Unit number that will be preconnected to standard output\n"
297 "(No preconnection if negative)", 0},
299 {"GFORTRAN_STDERR_UNIT", GFC_STDERR_UNIT_NUMBER
, &options
.stderr_unit
,
300 init_integer
, show_integer
,
301 "Unit number that will be preconnected to standard error\n"
302 "(No preconnection if negative)", 0},
304 {"TMPDIR", 0, NULL
, init_string
, show_string
,
305 "Directory for scratch files.", 0},
307 {"GFORTRAN_UNBUFFERED_ALL", 0, &options
.all_unbuffered
, init_boolean
,
309 "If TRUE, all output is unbuffered. This will slow down large writes "
310 "but can be\nuseful for forcing data to be displayed immediately.", 0},
312 {"GFORTRAN_UNBUFFERED_PRECONNECTED", 0, &options
.unbuffered_preconnected
,
313 init_boolean
, show_boolean
,
314 "If TRUE, output to preconnected units is unbuffered.", 0},
316 {"GFORTRAN_SHOW_LOCUS", 1, &options
.locus
, init_boolean
, show_boolean
,
317 "If TRUE, print filename and line number where runtime errors happen.", 0},
319 {"GFORTRAN_OPTIONAL_PLUS", 0, &options
.optional_plus
, init_boolean
, show_boolean
,
320 "Print optional plus signs in numbers where permitted. Default FALSE.", 0},
322 {"GFORTRAN_DEFAULT_RECL", DEFAULT_RECL
, &options
.default_recl
,
323 init_unsigned_integer
, show_integer
,
324 "Default maximum record length for sequential files. Most useful for\n"
325 "adjusting line length of preconnected units. Default "
326 stringize (DEFAULT_RECL
), 0},
328 {"GFORTRAN_LIST_SEPARATOR", 0, NULL
, init_sep
, show_sep
,
329 "Separator to use when writing list output. May contain any number of "
330 "spaces\nand at most one comma. Default is a single space.", 0},
332 /* GFORTRAN_CONVERT_UNIT - Set the default data conversion for
334 {"GFORTRAN_CONVERT_UNIT", 0, 0, init_unformatted
, show_string
,
335 "Set format for unformatted files", 0},
337 {"GFORTRAN_ERROR_BACKTRACE", -1, &options
.backtrace
,
338 init_boolean
, show_boolean
,
339 "Print out a backtrace (if possible) on runtime error", -1},
341 {NULL
, 0, NULL
, NULL
, NULL
, NULL
, 0}
345 /* init_variables()-- Initialize most runtime variables from
346 * environment variables. */
349 init_variables (void)
353 for (v
= variable_table
; v
->name
; v
++)
359 show_variables (void)
364 /* TODO: print version number. */
365 estr_write ("GNU Fortran runtime library version "
368 estr_write ("Environment variables:\n");
369 estr_write ("----------------------\n");
371 for (v
= variable_table
; v
->name
; v
++)
373 n
= estr_write (v
->name
);
374 print_spaces (25 - n
);
376 if (v
->show
== show_integer
)
377 estr_write ("Integer ");
378 else if (v
->show
== show_boolean
)
379 estr_write ("Boolean ");
381 estr_write ("String ");
384 estr_write (v
->desc
);
388 /* System error codes */
390 estr_write ("\nRuntime error codes:");
391 estr_write ("\n--------------------\n");
393 for (n
= LIBERROR_FIRST
+ 1; n
< LIBERROR_LAST
; n
++)
395 st_printf ("%d %s\n", n
, translate_error (n
));
397 st_printf (" %d %s\n", n
, translate_error (n
));
399 estr_write ("\nCommand line arguments:\n");
400 estr_write (" --help Print this list\n");
405 /* This is the handling of the GFORTRAN_CONVERT_UNITS environment variable.
406 It is called from environ.c to parse this variable, and from
407 open.c to determine if the user specified a default for an
409 The syntax of the environment variable is, in bison grammar:
411 GFORTRAN_CONVERT_UNITS: mode | mode ';' exception ;
412 mode: 'native' | 'swap' | 'big_endian' | 'little_endian' ;
413 exception: mode ':' unit_list | unit_list ;
414 unit_list: unit_spec | unit_list unit_spec ;
415 unit_spec: INTEGER | INTEGER '-' INTEGER ;
418 /* Defines for the tokens. Other valid tokens are ',', ':', '-'. */
425 /* Some space for additional tokens later. */
437 static char *p
; /* Main character pointer for parsing. */
438 static char *lastpos
; /* Auxiliary pointer, for backing up. */
439 static int unit_num
; /* The last unit number read. */
440 static int unit_count
; /* The number of units found. */
441 static int do_count
; /* Parsing is done twice - first to count the number
442 of units, then to fill in the table. This
443 variable controls what to do. */
444 static exception_t
*elist
; /* The list of exceptions to the default. This is
445 sorted according to unit number. */
446 static int n_elist
; /* Number of exceptions to the default. */
448 static unit_convert endian
; /* Current endianness. */
450 static unit_convert def
; /* Default as specified (if any). */
452 /* Search for a unit number, using a binary search. The
453 first argument is the unit number to search for. The second argument
454 is a pointer to an index.
455 If the unit number is found, the function returns 1, and the index
456 is that of the element.
457 If the unit number is not found, the function returns 0, and the
458 index is the one where the element would be inserted. */
461 search_unit (int unit
, int *ip
)
476 mid
= (low
+ high
) / 2;
477 if (unit
== elist
[mid
].unit
)
482 else if (unit
> elist
[mid
].unit
)
486 } while (low
<= high
);
488 if (unit
> elist
[mid
].unit
)
496 /* This matches a keyword. If it is found, return the token supplied,
497 otherwise return ILLEGAL. */
500 match_word (const char *word
, int tok
)
504 if (strncasecmp (p
, word
, strlen (word
)) == 0)
515 /* Match an integer and store its value in unit_num. This only works
516 if p actually points to the start of an integer. The caller has
524 unit_num
= unit_num
* 10 + (*p
++ - '0');
529 /* This reads the next token from the GFORTRAN_CONVERT_UNITS variable.
530 Returned values are the different tokens. */
554 result
= match_word ("big_endian", BIG
);
559 result
= match_word ("little_endian", LITTLE
);
564 result
= match_word ("native", NATIVE
);
569 result
= match_word ("swap", SWAP
);
572 case '1': case '2': case '3': case '4': case '5':
573 case '6': case '7': case '8': case '9':
574 result
= match_integer ();
584 /* Back up the last token by setting back the character pointer. */
592 /* This is called when a unit is identified. If do_count is nonzero,
593 increment the number of units by one. If do_count is zero,
594 put the unit into the table. */
597 mark_single (int unit
)
606 if (search_unit (unit
, &i
))
608 elist
[i
].conv
= endian
;
612 for (j
=n_elist
-1; j
>=i
; j
--)
613 elist
[j
+1] = elist
[j
];
616 elist
[i
].unit
= unit
;
617 elist
[i
].conv
= endian
;
621 /* This is called when a unit range is identified. If do_count is
622 nonzero, increase the number of units. If do_count is zero,
623 put the unit into the table. */
626 mark_range (int unit1
, int unit2
)
630 unit_count
+= abs (unit2
- unit1
) + 1;
634 for (i
=unit2
; i
<=unit1
; i
++)
637 for (i
=unit1
; i
<=unit2
; i
++)
642 /* Parse the GFORTRAN_CONVERT_UNITS variable. This is called
643 twice, once to count the units and once to actually mark them in
644 the table. When counting, we don't check for double occurrences
659 /* Parse the string. First, let's look for a default. */
664 endian
= GFC_CONVERT_NATIVE
;
668 endian
= GFC_CONVERT_SWAP
;
672 endian
= GFC_CONVERT_BIG
;
676 endian
= GFC_CONVERT_LITTLE
;
680 /* A leading digit means that we are looking at an exception.
681 Reset the position to the beginning, and continue processing
682 at the exception list. */
704 /* This isn't a default after all. Reset the position to the
705 beginning, and continue processing at the exception list. */
722 /* Loop over all exceptions. */
729 if (next_token () != ':')
731 endian
= GFC_CONVERT_NATIVE
;
735 if (next_token () != ':')
737 endian
= GFC_CONVERT_SWAP
;
741 if (next_token () != ':')
743 endian
= GFC_CONVERT_LITTLE
;
747 if (next_token () != ':')
749 endian
= GFC_CONVERT_BIG
;
764 /* We arrive here when we want to parse a list of
775 /* The number can be followed by a - and another number,
776 which means that this is a unit range, a comma
780 if (next_token () != INTEGER
)
783 mark_range (unit1
, unit_num
);
812 } while (continue_ulist
);
817 def
= GFC_CONVERT_NONE
;
821 void init_unformatted (variable
* v
)
824 val
= getenv (v
->name
);
825 def
= GFC_CONVERT_NONE
;
840 elist
= xmallocarray (unit_count
, sizeof (exception_t
));
847 /* Get the default conversion for for an unformatted unit. */
850 get_unformatted_convert (int unit
)
856 else if (search_unit (unit
, &i
))
857 return elist
[i
].conv
;