1 /* Copyright (C) 2002-2024 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"
35 /* Implementation of secure_getenv() for targets where it is not
38 #ifdef FALLBACK_SECURE_GETENV
40 #if SUPPORTS_WEAKREF && defined(HAVE___SECURE_GETENV)
41 static char* weak_secure_getenv (const char*)
42 __attribute__((__weakref__("__secure_getenv")));
46 secure_getenv (const char *name
)
48 #if SUPPORTS_WEAKREF && defined(HAVE___SECURE_GETENV)
49 if (weak_secure_getenv
)
50 return weak_secure_getenv (name
);
53 if ((getuid () == geteuid ()) && (getgid () == getegid ()))
62 /* Examine the environment for controlling aspects of the program's
63 execution. Our philosophy here that the environment should not prevent
64 the program from running, so any invalid value will be ignored. */
69 typedef struct variable
74 void (*init
) (struct variable
*);
78 static void init_unformatted (variable
*);
81 /* Initialize an integer environment variable. */
84 init_integer (variable
* v
)
93 if (!safe_isdigit (*q
) && (p
!= q
|| *q
!= '-'))
100 /* Initialize a boolean environment variable. We only look at the first
101 letter of the value. */
104 init_boolean (variable
* v
)
108 p
= getenv (v
->name
);
112 if (*p
== '1' || *p
== 'Y' || *p
== 'y')
114 else if (*p
== '0' || *p
== 'N' || *p
== 'n')
119 /* Initialize a list output separator. It may contain any number of spaces
120 and at most one comma. */
123 init_sep (variable
* v
)
128 p
= getenv (v
->name
);
132 options
.separator
= p
;
133 options
.separator_len
= strlen (p
);
135 /* Make sure the separator is valid */
137 if (options
.separator_len
== 0)
159 options
.separator
= " ";
160 options
.separator_len
= 1;
164 static variable variable_table
[] = {
166 /* Unit number that will be preconnected to standard input */
167 { "GFORTRAN_STDIN_UNIT", GFC_STDIN_UNIT_NUMBER
, &options
.stdin_unit
,
170 /* Unit number that will be preconnected to standard output */
171 { "GFORTRAN_STDOUT_UNIT", GFC_STDOUT_UNIT_NUMBER
, &options
.stdout_unit
,
174 /* Unit number that will be preconnected to standard error */
175 { "GFORTRAN_STDERR_UNIT", GFC_STDERR_UNIT_NUMBER
, &options
.stderr_unit
,
178 /* If TRUE, all output will be unbuffered */
179 { "GFORTRAN_UNBUFFERED_ALL", 0, &options
.all_unbuffered
, init_boolean
},
181 /* If TRUE, output to preconnected units will be unbuffered */
182 { "GFORTRAN_UNBUFFERED_PRECONNECTED", 0, &options
.unbuffered_preconnected
,
185 /* Whether to print filename and line number on runtime error */
186 { "GFORTRAN_SHOW_LOCUS", 1, &options
.locus
, init_boolean
},
188 /* Print optional plus signs in numbers where permitted */
189 { "GFORTRAN_OPTIONAL_PLUS", 0, &options
.optional_plus
, init_boolean
},
191 /* Separator to use when writing list output */
192 { "GFORTRAN_LIST_SEPARATOR", 0, NULL
, init_sep
},
194 /* Set the default data conversion for unformatted I/O */
195 { "GFORTRAN_CONVERT_UNIT", 0, 0, init_unformatted
},
197 /* Print out a backtrace if possible on runtime error */
198 { "GFORTRAN_ERROR_BACKTRACE", -1, &options
.backtrace
, init_boolean
},
200 /* Buffer size for unformatted files. */
201 { "GFORTRAN_UNFORMATTED_BUFFER_SIZE", 0, &options
.unformatted_buffer_size
,
204 /* Buffer size for formatted files. */
205 { "GFORTRAN_FORMATTED_BUFFER_SIZE", 0, &options
.formatted_buffer_size
,
208 { NULL
, 0, NULL
, NULL
}
212 /* Initialize most runtime variables from
213 * environment variables. */
216 init_variables (void)
220 for (v
= variable_table
; v
->name
; v
++)
223 *v
->var
= v
->default_value
;
229 /* This is the handling of the GFORTRAN_CONVERT_UNITS environment variable.
230 It is called from environ.c to parse this variable, and from
231 open.c to determine if the user specified a default for an
233 The syntax of the environment variable is, in bison grammar:
235 GFORTRAN_CONVERT_UNITS: mode | mode ';' exception ;
236 mode: 'native' | 'swap' | 'big_endian' | 'little_endian' ;
237 exception: mode ':' unit_list | unit_list ;
238 unit_list: unit_spec | unit_list unit_spec ;
239 unit_spec: INTEGER | INTEGER '-' INTEGER ;
242 /* Defines for the tokens. Other valid tokens are ',', ':', '-'. */
249 #ifdef HAVE_GFC_REAL_17
254 /* Some space for additional tokens later. */
266 static char *p
; /* Main character pointer for parsing. */
267 static char *lastpos
; /* Auxiliary pointer, for backing up. */
268 static int unit_num
; /* The last unit number read. */
269 static int unit_count
; /* The number of units found. */
270 static int do_count
; /* Parsing is done twice - first to count the number
271 of units, then to fill in the table. This
272 variable controls what to do. */
273 static exception_t
*elist
; /* The list of exceptions to the default. This is
274 sorted according to unit number. */
275 static int n_elist
; /* Number of exceptions to the default. */
277 static unit_convert endian
; /* Current endianness. */
279 static unit_convert def
; /* Default as specified (if any). */
281 /* Search for a unit number, using a binary search. The
282 first argument is the unit number to search for. The second argument
283 is a pointer to an index.
284 If the unit number is found, the function returns 1, and the index
285 is that of the element.
286 If the unit number is not found, the function returns 0, and the
287 index is the one where the element would be inserted. */
290 search_unit (int unit
, int *ip
)
305 mid
= (low
+ high
) / 2;
306 if (unit
== elist
[mid
].unit
)
311 else if (unit
> elist
[mid
].unit
)
315 } while (low
<= high
);
317 if (unit
> elist
[mid
].unit
)
325 /* This matches a keyword. If it is found, return the token supplied,
326 otherwise return ILLEGAL. */
329 match_word (const char *word
, int tok
)
333 if (strncasecmp (p
, word
, strlen (word
)) == 0)
343 /* Match an integer and store its value in unit_num. This only works
344 if p actually points to the start of an integer. The caller has
351 while (safe_isdigit (*p
))
352 unit_num
= unit_num
* 10 + (*p
++ - '0');
356 /* This reads the next token from the GFORTRAN_CONVERT_UNITS variable.
357 Returned values are the different tokens. */
381 result
= match_word ("big_endian", BIG
);
386 result
= match_word ("little_endian", LITTLE
);
391 result
= match_word ("native", NATIVE
);
396 result
= match_word ("swap", SWAP
);
399 #ifdef HAVE_GFC_REAL_17
402 result
= match_word ("r16_ieee", R16_IEEE
);
403 if (result
== ILLEGAL
)
404 result
= match_word ("r16_ibm", R16_IBM
);
408 case '1': case '2': case '3': case '4': case '5':
409 case '6': case '7': case '8': case '9':
410 result
= match_integer ();
420 /* Back up the last token by setting back the character pointer. */
428 /* This is called when a unit is identified. If do_count is nonzero,
429 increment the number of units by one. If do_count is zero,
430 put the unit into the table. For POWER, we have to make sure that
431 we can also put in the conversion btween IBM and IEEE long double. */
434 mark_single (int unit
)
443 if (search_unit (unit
, &i
))
445 #ifdef HAVE_GFC_REAL_17
446 elist
[i
].conv
|= endian
;
448 elist
[i
].conv
= endian
;
453 for (j
=n_elist
-1; j
>=i
; j
--)
454 elist
[j
+1] = elist
[j
];
457 elist
[i
].unit
= unit
;
458 #ifdef HAVE_GFC_REAL_17
459 elist
[i
].conv
|= endian
;
461 elist
[i
].conv
= endian
;
466 /* This is called when a unit range is identified. If do_count is
467 nonzero, increase the number of units. If do_count is zero,
468 put the unit into the table. */
471 mark_range (int unit1
, int unit2
)
475 unit_count
+= abs (unit2
- unit1
) + 1;
479 for (i
=unit2
; i
<=unit1
; i
++)
482 for (i
=unit1
; i
<=unit2
; i
++)
487 /* Parse the GFORTRAN_CONVERT_UNITS variable. This is called
488 twice, once to count the units and once to actually mark them in
489 the table. When counting, we don't check for double occurrences
502 /* Parse the string. First, let's look for a default. */
511 endian
= GFC_CONVERT_NATIVE
;
515 endian
= GFC_CONVERT_SWAP
;
519 endian
= GFC_CONVERT_BIG
;
523 endian
= GFC_CONVERT_LITTLE
;
526 #ifdef HAVE_GFC_REAL_17
528 endian
= GFC_CONVERT_R16_IEEE
;
532 endian
= GFC_CONVERT_R16_IBM
;
536 /* A leading digit means that we are looking at an exception.
537 Reset the position to the beginning, and continue processing
538 at the exception list. */
556 def
= def
== GFC_CONVERT_NONE
? endian
: def
| endian
;
560 /* This isn't a default after all. Reset the position to the
561 beginning, and continue processing at the exception list. */
567 def
= def
== GFC_CONVERT_NONE
? endian
: def
| endian
;
579 /* Loop over all exceptions. */
586 if (next_token () != ':')
588 endian
= GFC_CONVERT_NATIVE
;
592 if (next_token () != ':')
594 endian
= GFC_CONVERT_SWAP
;
598 if (next_token () != ':')
600 endian
= GFC_CONVERT_LITTLE
;
604 if (next_token () != ':')
606 endian
= GFC_CONVERT_BIG
;
608 #ifdef HAVE_GFC_REAL_17
610 if (next_token () != ':')
612 endian
= GFC_CONVERT_R16_IEEE
;
616 if (next_token () != ':')
618 endian
= GFC_CONVERT_R16_IBM
;
634 /* We arrive here when we want to parse a list of
645 /* The number can be followed by a - and another number,
646 which means that this is a unit range, a comma
650 if (next_token () != INTEGER
)
653 mark_range (unit1
, unit_num
);
682 } while (continue_ulist
);
687 def
= GFC_CONVERT_NONE
;
691 void init_unformatted (variable
* v
)
694 val
= getenv (v
->name
);
695 def
= GFC_CONVERT_NONE
;
710 elist
= xmallocarray (unit_count
, sizeof (exception_t
));
717 /* Get the default conversion for for an unformatted unit. */
720 get_unformatted_convert (int unit
)
726 else if (search_unit (unit
, &i
))
727 return elist
[i
].conv
;