* runtime/environ.c: Include unistd.h.
[official-gcc.git] / libgfortran / runtime / environ.c
bloba29785bab21c9e2416a99a4b563aba5eabc368a0
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)
9 any later version.
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"
27 #include <string.h>
28 #include <stdlib.h>
29 #include <ctype.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
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
40 * the default way.
42 * Most of the environment is checked early in the startup sequence,
43 * but other variables are checked during execution of the user's
44 * program. */
46 options_t options;
49 typedef struct variable
51 const char *name;
52 int value, *var;
53 void (*init) (struct variable *);
54 void (*show) (struct variable *);
55 const char *desc;
56 int bad;
58 variable;
60 static void init_unformatted (variable *);
63 #ifdef FALLBACK_SECURE_GETENV
64 char *
65 secure_getenv (const char *name)
67 if ((getuid () == geteuid ()) && (getgid () == getegid ()))
68 return getenv (name);
69 else
70 return NULL;
72 #endif
75 /* print_spaces()-- Print a particular number of spaces. */
77 static void
78 print_spaces (int n)
80 char buffer[80];
81 int i;
83 if (n <= 0)
84 return;
86 for (i = 0; i < n; i++)
87 buffer[i] = ' ';
89 buffer[i] = '\0';
91 estr_write (buffer);
95 /* var_source()-- Return a string that describes where the value of a
96 * variable comes from */
98 static const char *
99 var_source (variable * v)
101 if (getenv (v->name) == NULL)
102 return "Default";
104 if (v->bad)
105 return "Bad ";
107 return "Set ";
111 /* init_integer()-- Initialize an integer environment variable. */
113 static void
114 init_integer (variable * v)
116 char *p, *q;
118 p = getenv (v->name);
119 if (p == NULL)
120 goto set_default;
122 for (q = p; *q; q++)
123 if (!isdigit (*q) && (p != q || *q != '-'))
125 v->bad = 1;
126 goto set_default;
129 *v->var = atoi (p);
130 return;
132 set_default:
133 *v->var = v->value;
134 return;
138 /* init_unsigned_integer()-- Initialize an integer environment variable
139 which has to be positive. */
141 static void
142 init_unsigned_integer (variable * v)
144 char *p, *q;
146 p = getenv (v->name);
147 if (p == NULL)
148 goto set_default;
150 for (q = p; *q; q++)
151 if (!isdigit (*q))
153 v->bad = 1;
154 goto set_default;
157 *v->var = atoi (p);
158 return;
160 set_default:
161 *v->var = v->value;
162 return;
166 /* show_integer()-- Show an integer environment variable */
168 static void
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. */
178 static void
179 init_boolean (variable * v)
181 char *p;
183 p = getenv (v->name);
184 if (p == NULL)
185 goto set_default;
187 if (*p == '1' || *p == 'Y' || *p == 'y')
189 *v->var = 1;
190 return;
193 if (*p == '0' || *p == 'N' || *p == 'n')
195 *v->var = 0;
196 return;
199 v->bad = 1;
201 set_default:
202 *v->var = v->value;
203 return;
207 /* show_boolean()-- Show a boolean environment variable */
209 static void
210 show_boolean (variable * v)
212 st_printf ("%s %s\n", var_source (v), *v->var ? "Yes" : "No");
216 static void
217 init_sep (variable * v)
219 int seen_comma;
220 char *p;
222 p = getenv (v->name);
223 if (p == NULL)
224 goto set_default;
226 v->bad = 1;
227 options.separator = p;
228 options.separator_len = strlen (p);
230 /* Make sure the separator is valid */
232 if (options.separator_len == 0)
233 goto set_default;
234 seen_comma = 0;
236 while (*p)
238 if (*p == ',')
240 if (seen_comma)
241 goto set_default;
242 seen_comma = 1;
243 p++;
244 continue;
247 if (*p++ != ' ')
248 goto set_default;
251 v->bad = 0;
252 return;
254 set_default:
255 options.separator = " ";
256 options.separator_len = 1;
260 static void
261 show_sep (variable * v)
263 st_printf ("%s \"%s\"\n", var_source (v), options.separator);
267 static void
268 init_string (variable * v __attribute__ ((unused)))
272 static void
273 show_string (variable * v)
275 const char *p;
277 p = getenv (v->name);
278 if (p == NULL)
279 p = "";
281 estr_write (var_source (v));
282 estr_write (" \"");
283 estr_write (p);
284 estr_write ("\"\n");
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,
308 show_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
333 unformatted I/O. */
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. */
348 void
349 init_variables (void)
351 variable *v;
353 for (v = variable_table; v->name; v++)
354 v->init (v);
358 void
359 show_variables (void)
361 variable *v;
362 int n;
364 /* TODO: print version number. */
365 estr_write ("GNU Fortran runtime library version "
366 "UNKNOWN" "\n\n");
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 ");
380 else
381 estr_write ("String ");
383 v->show (v);
384 estr_write (v->desc);
385 estr_write ("\n\n");
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++)
394 if (n < 0 || n > 9)
395 st_printf ("%d %s\n", n, translate_error (n));
396 else
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");
402 exit (0);
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
408 unformatted file.
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 ',', ':', '-'. */
421 #define NATIVE 257
422 #define SWAP 258
423 #define BIG 259
424 #define LITTLE 260
425 /* Some space for additional tokens later. */
426 #define INTEGER 273
427 #define END (-1)
428 #define ILLEGAL (-2)
430 typedef struct
432 int unit;
433 unit_convert conv;
434 } exception_t;
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. */
460 static int
461 search_unit (int unit, int *ip)
463 int low, high, mid;
465 if (n_elist == 0)
467 *ip = 0;
468 return 0;
471 low = 0;
472 high = n_elist - 1;
476 mid = (low + high) / 2;
477 if (unit == elist[mid].unit)
479 *ip = mid;
480 return 1;
482 else if (unit > elist[mid].unit)
483 low = mid + 1;
484 else
485 high = mid - 1;
486 } while (low <= high);
488 if (unit > elist[mid].unit)
489 *ip = mid + 1;
490 else
491 *ip = mid;
493 return 0;
496 /* This matches a keyword. If it is found, return the token supplied,
497 otherwise return ILLEGAL. */
499 static int
500 match_word (const char *word, int tok)
502 int res;
504 if (strncasecmp (p, word, strlen (word)) == 0)
506 p += strlen (word);
507 res = tok;
509 else
510 res = ILLEGAL;
511 return res;
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
517 to ensure this. */
519 static int
520 match_integer (void)
522 unit_num = 0;
523 while (isdigit (*p))
524 unit_num = unit_num * 10 + (*p++ - '0');
525 return INTEGER;
529 /* This reads the next token from the GFORTRAN_CONVERT_UNITS variable.
530 Returned values are the different tokens. */
532 static int
533 next_token (void)
535 int result;
537 lastpos = p;
538 switch (*p)
540 case '\0':
541 result = END;
542 break;
544 case ':':
545 case ',':
546 case '-':
547 case ';':
548 result = *p;
549 p++;
550 break;
552 case 'b':
553 case 'B':
554 result = match_word ("big_endian", BIG);
555 break;
557 case 'l':
558 case 'L':
559 result = match_word ("little_endian", LITTLE);
560 break;
562 case 'n':
563 case 'N':
564 result = match_word ("native", NATIVE);
565 break;
567 case 's':
568 case 'S':
569 result = match_word ("swap", SWAP);
570 break;
572 case '1': case '2': case '3': case '4': case '5':
573 case '6': case '7': case '8': case '9':
574 result = match_integer ();
575 break;
577 default:
578 result = ILLEGAL;
579 break;
581 return result;
584 /* Back up the last token by setting back the character pointer. */
586 static void
587 push_token (void)
589 p = lastpos;
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. */
596 static void
597 mark_single (int unit)
599 int i,j;
601 if (do_count)
603 unit_count++;
604 return;
606 if (search_unit (unit, &i))
608 elist[i].conv = endian;
610 else
612 for (j=n_elist-1; j>=i; j--)
613 elist[j+1] = elist[j];
615 n_elist += 1;
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. */
625 static void
626 mark_range (int unit1, int unit2)
628 int i;
629 if (do_count)
630 unit_count += abs (unit2 - unit1) + 1;
631 else
633 if (unit2 < unit1)
634 for (i=unit2; i<=unit1; i++)
635 mark_single (i);
636 else
637 for (i=unit1; i<=unit2; i++)
638 mark_single (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
645 of units. */
647 static int
648 do_parse (void)
650 int tok;
651 int unit1;
652 int continue_ulist;
653 char *start;
655 unit_count = 0;
657 start = p;
659 /* Parse the string. First, let's look for a default. */
660 tok = next_token ();
661 switch (tok)
663 case NATIVE:
664 endian = GFC_CONVERT_NATIVE;
665 break;
667 case SWAP:
668 endian = GFC_CONVERT_SWAP;
669 break;
671 case BIG:
672 endian = GFC_CONVERT_BIG;
673 break;
675 case LITTLE:
676 endian = GFC_CONVERT_LITTLE;
677 break;
679 case INTEGER:
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. */
683 p = start;
684 goto exceptions;
685 break;
687 case END:
688 goto end;
689 break;
691 default:
692 goto error;
693 break;
696 tok = next_token ();
697 switch (tok)
699 case ';':
700 def = endian;
701 break;
703 case ':':
704 /* This isn't a default after all. Reset the position to the
705 beginning, and continue processing at the exception list. */
706 p = start;
707 goto exceptions;
708 break;
710 case END:
711 def = endian;
712 goto end;
713 break;
715 default:
716 goto error;
717 break;
720 exceptions:
722 /* Loop over all exceptions. */
723 while(1)
725 tok = next_token ();
726 switch (tok)
728 case NATIVE:
729 if (next_token () != ':')
730 goto error;
731 endian = GFC_CONVERT_NATIVE;
732 break;
734 case SWAP:
735 if (next_token () != ':')
736 goto error;
737 endian = GFC_CONVERT_SWAP;
738 break;
740 case LITTLE:
741 if (next_token () != ':')
742 goto error;
743 endian = GFC_CONVERT_LITTLE;
744 break;
746 case BIG:
747 if (next_token () != ':')
748 goto error;
749 endian = GFC_CONVERT_BIG;
750 break;
752 case INTEGER:
753 push_token ();
754 break;
756 case END:
757 goto end;
758 break;
760 default:
761 goto error;
762 break;
764 /* We arrive here when we want to parse a list of
765 numbers. */
766 continue_ulist = 1;
769 tok = next_token ();
770 if (tok != INTEGER)
771 goto error;
773 unit1 = unit_num;
774 tok = next_token ();
775 /* The number can be followed by a - and another number,
776 which means that this is a unit range, a comma
777 or a semicolon. */
778 if (tok == '-')
780 if (next_token () != INTEGER)
781 goto error;
783 mark_range (unit1, unit_num);
784 tok = next_token ();
785 if (tok == END)
786 goto end;
787 else if (tok == ';')
788 continue_ulist = 0;
789 else if (tok != ',')
790 goto error;
792 else
794 mark_single (unit1);
795 switch (tok)
797 case ';':
798 continue_ulist = 0;
799 break;
801 case ',':
802 break;
804 case END:
805 goto end;
806 break;
808 default:
809 goto error;
812 } while (continue_ulist);
814 end:
815 return 0;
816 error:
817 def = GFC_CONVERT_NONE;
818 return -1;
821 void init_unformatted (variable * v)
823 char *val;
824 val = getenv (v->name);
825 def = GFC_CONVERT_NONE;
826 n_elist = 0;
828 if (val == NULL)
829 return;
830 do_count = 1;
831 p = val;
832 do_parse ();
833 if (do_count <= 0)
835 n_elist = 0;
836 elist = NULL;
838 else
840 elist = xmalloc (unit_count * sizeof (exception_t));
841 do_count = 0;
842 p = val;
843 do_parse ();
847 /* Get the default conversion for for an unformatted unit. */
849 unit_convert
850 get_unformatted_convert (int unit)
852 int i;
854 if (elist == NULL)
855 return def;
856 else if (search_unit (unit, &i))
857 return elist[i].conv;
858 else
859 return def;