environ.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libgfortran.h"
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. /* Environment scanner. Examine the environment for controlling minor
  27. * aspects of the program's execution. Our philosophy here that the
  28. * environment should not prevent the program from running, so an
  29. * environment variable with a messed-up value will be interpreted in
  30. * the default way.
  31. *
  32. * Most of the environment is checked early in the startup sequence,
  33. * but other variables are checked during execution of the user's
  34. * program. */
  35. options_t options;
  36. typedef struct variable
  37. {
  38. const char *name;
  39. int value, *var;
  40. void (*init) (struct variable *);
  41. void (*show) (struct variable *);
  42. const char *desc;
  43. int bad;
  44. }
  45. variable;
  46. static void init_unformatted (variable *);
  47. #ifdef FALLBACK_SECURE_GETENV
  48. char *
  49. secure_getenv (const char *name)
  50. {
  51. if ((getuid () == geteuid ()) && (getgid () == getegid ()))
  52. return getenv (name);
  53. else
  54. return NULL;
  55. }
  56. #endif
  57. /* print_spaces()-- Print a particular number of spaces. */
  58. static void
  59. print_spaces (int n)
  60. {
  61. char buffer[80];
  62. int i;
  63. if (n <= 0)
  64. return;
  65. for (i = 0; i < n; i++)
  66. buffer[i] = ' ';
  67. buffer[i] = '\0';
  68. estr_write (buffer);
  69. }
  70. /* var_source()-- Return a string that describes where the value of a
  71. * variable comes from */
  72. static const char *
  73. var_source (variable * v)
  74. {
  75. if (getenv (v->name) == NULL)
  76. return "Default";
  77. if (v->bad)
  78. return "Bad ";
  79. return "Set ";
  80. }
  81. /* init_integer()-- Initialize an integer environment variable. */
  82. static void
  83. init_integer (variable * v)
  84. {
  85. char *p, *q;
  86. p = getenv (v->name);
  87. if (p == NULL)
  88. goto set_default;
  89. for (q = p; *q; q++)
  90. if (!isdigit (*q) && (p != q || *q != '-'))
  91. {
  92. v->bad = 1;
  93. goto set_default;
  94. }
  95. *v->var = atoi (p);
  96. return;
  97. set_default:
  98. *v->var = v->value;
  99. return;
  100. }
  101. /* init_unsigned_integer()-- Initialize an integer environment variable
  102. which has to be positive. */
  103. static void
  104. init_unsigned_integer (variable * v)
  105. {
  106. char *p, *q;
  107. p = getenv (v->name);
  108. if (p == NULL)
  109. goto set_default;
  110. for (q = p; *q; q++)
  111. if (!isdigit (*q))
  112. {
  113. v->bad = 1;
  114. goto set_default;
  115. }
  116. *v->var = atoi (p);
  117. return;
  118. set_default:
  119. *v->var = v->value;
  120. return;
  121. }
  122. /* show_integer()-- Show an integer environment variable */
  123. static void
  124. show_integer (variable * v)
  125. {
  126. st_printf ("%s %d\n", var_source (v), *v->var);
  127. }
  128. /* init_boolean()-- Initialize a boolean environment variable. We
  129. * only look at the first letter of the variable. */
  130. static void
  131. init_boolean (variable * v)
  132. {
  133. char *p;
  134. p = getenv (v->name);
  135. if (p == NULL)
  136. goto set_default;
  137. if (*p == '1' || *p == 'Y' || *p == 'y')
  138. {
  139. *v->var = 1;
  140. return;
  141. }
  142. if (*p == '0' || *p == 'N' || *p == 'n')
  143. {
  144. *v->var = 0;
  145. return;
  146. }
  147. v->bad = 1;
  148. set_default:
  149. *v->var = v->value;
  150. return;
  151. }
  152. /* show_boolean()-- Show a boolean environment variable */
  153. static void
  154. show_boolean (variable * v)
  155. {
  156. st_printf ("%s %s\n", var_source (v), *v->var ? "Yes" : "No");
  157. }
  158. static void
  159. init_sep (variable * v)
  160. {
  161. int seen_comma;
  162. char *p;
  163. p = getenv (v->name);
  164. if (p == NULL)
  165. goto set_default;
  166. v->bad = 1;
  167. options.separator = p;
  168. options.separator_len = strlen (p);
  169. /* Make sure the separator is valid */
  170. if (options.separator_len == 0)
  171. goto set_default;
  172. seen_comma = 0;
  173. while (*p)
  174. {
  175. if (*p == ',')
  176. {
  177. if (seen_comma)
  178. goto set_default;
  179. seen_comma = 1;
  180. p++;
  181. continue;
  182. }
  183. if (*p++ != ' ')
  184. goto set_default;
  185. }
  186. v->bad = 0;
  187. return;
  188. set_default:
  189. options.separator = " ";
  190. options.separator_len = 1;
  191. }
  192. static void
  193. show_sep (variable * v)
  194. {
  195. st_printf ("%s \"%s\"\n", var_source (v), options.separator);
  196. }
  197. static void
  198. init_string (variable * v __attribute__ ((unused)))
  199. {
  200. }
  201. static void
  202. show_string (variable * v)
  203. {
  204. const char *p;
  205. p = getenv (v->name);
  206. if (p == NULL)
  207. p = "";
  208. estr_write (var_source (v));
  209. estr_write (" \"");
  210. estr_write (p);
  211. estr_write ("\"\n");
  212. }
  213. static variable variable_table[] = {
  214. {"GFORTRAN_STDIN_UNIT", GFC_STDIN_UNIT_NUMBER, &options.stdin_unit,
  215. init_integer, show_integer,
  216. "Unit number that will be preconnected to standard input\n"
  217. "(No preconnection if negative)", 0},
  218. {"GFORTRAN_STDOUT_UNIT", GFC_STDOUT_UNIT_NUMBER, &options.stdout_unit,
  219. init_integer, show_integer,
  220. "Unit number that will be preconnected to standard output\n"
  221. "(No preconnection if negative)", 0},
  222. {"GFORTRAN_STDERR_UNIT", GFC_STDERR_UNIT_NUMBER, &options.stderr_unit,
  223. init_integer, show_integer,
  224. "Unit number that will be preconnected to standard error\n"
  225. "(No preconnection if negative)", 0},
  226. {"TMPDIR", 0, NULL, init_string, show_string,
  227. "Directory for scratch files.", 0},
  228. {"GFORTRAN_UNBUFFERED_ALL", 0, &options.all_unbuffered, init_boolean,
  229. show_boolean,
  230. "If TRUE, all output is unbuffered. This will slow down large writes "
  231. "but can be\nuseful for forcing data to be displayed immediately.", 0},
  232. {"GFORTRAN_UNBUFFERED_PRECONNECTED", 0, &options.unbuffered_preconnected,
  233. init_boolean, show_boolean,
  234. "If TRUE, output to preconnected units is unbuffered.", 0},
  235. {"GFORTRAN_SHOW_LOCUS", 1, &options.locus, init_boolean, show_boolean,
  236. "If TRUE, print filename and line number where runtime errors happen.", 0},
  237. {"GFORTRAN_OPTIONAL_PLUS", 0, &options.optional_plus, init_boolean, show_boolean,
  238. "Print optional plus signs in numbers where permitted. Default FALSE.", 0},
  239. {"GFORTRAN_DEFAULT_RECL", DEFAULT_RECL, &options.default_recl,
  240. init_unsigned_integer, show_integer,
  241. "Default maximum record length for sequential files. Most useful for\n"
  242. "adjusting line length of preconnected units. Default "
  243. stringize (DEFAULT_RECL), 0},
  244. {"GFORTRAN_LIST_SEPARATOR", 0, NULL, init_sep, show_sep,
  245. "Separator to use when writing list output. May contain any number of "
  246. "spaces\nand at most one comma. Default is a single space.", 0},
  247. /* GFORTRAN_CONVERT_UNIT - Set the default data conversion for
  248. unformatted I/O. */
  249. {"GFORTRAN_CONVERT_UNIT", 0, 0, init_unformatted, show_string,
  250. "Set format for unformatted files", 0},
  251. {"GFORTRAN_ERROR_BACKTRACE", -1, &options.backtrace,
  252. init_boolean, show_boolean,
  253. "Print out a backtrace (if possible) on runtime error", -1},
  254. {NULL, 0, NULL, NULL, NULL, NULL, 0}
  255. };
  256. /* init_variables()-- Initialize most runtime variables from
  257. * environment variables. */
  258. void
  259. init_variables (void)
  260. {
  261. variable *v;
  262. for (v = variable_table; v->name; v++)
  263. v->init (v);
  264. }
  265. void
  266. show_variables (void)
  267. {
  268. variable *v;
  269. int n;
  270. /* TODO: print version number. */
  271. estr_write ("GNU Fortran runtime library version "
  272. "UNKNOWN" "\n\n");
  273. estr_write ("Environment variables:\n");
  274. estr_write ("----------------------\n");
  275. for (v = variable_table; v->name; v++)
  276. {
  277. n = estr_write (v->name);
  278. print_spaces (25 - n);
  279. if (v->show == show_integer)
  280. estr_write ("Integer ");
  281. else if (v->show == show_boolean)
  282. estr_write ("Boolean ");
  283. else
  284. estr_write ("String ");
  285. v->show (v);
  286. estr_write (v->desc);
  287. estr_write ("\n\n");
  288. }
  289. /* System error codes */
  290. estr_write ("\nRuntime error codes:");
  291. estr_write ("\n--------------------\n");
  292. for (n = LIBERROR_FIRST + 1; n < LIBERROR_LAST; n++)
  293. if (n < 0 || n > 9)
  294. st_printf ("%d %s\n", n, translate_error (n));
  295. else
  296. st_printf (" %d %s\n", n, translate_error (n));
  297. estr_write ("\nCommand line arguments:\n");
  298. estr_write (" --help Print this list\n");
  299. exit (0);
  300. }
  301. /* This is the handling of the GFORTRAN_CONVERT_UNITS environment variable.
  302. It is called from environ.c to parse this variable, and from
  303. open.c to determine if the user specified a default for an
  304. unformatted file.
  305. The syntax of the environment variable is, in bison grammar:
  306. GFORTRAN_CONVERT_UNITS: mode | mode ';' exception ;
  307. mode: 'native' | 'swap' | 'big_endian' | 'little_endian' ;
  308. exception: mode ':' unit_list | unit_list ;
  309. unit_list: unit_spec | unit_list unit_spec ;
  310. unit_spec: INTEGER | INTEGER '-' INTEGER ;
  311. */
  312. /* Defines for the tokens. Other valid tokens are ',', ':', '-'. */
  313. #define NATIVE 257
  314. #define SWAP 258
  315. #define BIG 259
  316. #define LITTLE 260
  317. /* Some space for additional tokens later. */
  318. #define INTEGER 273
  319. #define END (-1)
  320. #define ILLEGAL (-2)
  321. typedef struct
  322. {
  323. int unit;
  324. unit_convert conv;
  325. } exception_t;
  326. static char *p; /* Main character pointer for parsing. */
  327. static char *lastpos; /* Auxiliary pointer, for backing up. */
  328. static int unit_num; /* The last unit number read. */
  329. static int unit_count; /* The number of units found. */
  330. static int do_count; /* Parsing is done twice - first to count the number
  331. of units, then to fill in the table. This
  332. variable controls what to do. */
  333. static exception_t *elist; /* The list of exceptions to the default. This is
  334. sorted according to unit number. */
  335. static int n_elist; /* Number of exceptions to the default. */
  336. static unit_convert endian; /* Current endianness. */
  337. static unit_convert def; /* Default as specified (if any). */
  338. /* Search for a unit number, using a binary search. The
  339. first argument is the unit number to search for. The second argument
  340. is a pointer to an index.
  341. If the unit number is found, the function returns 1, and the index
  342. is that of the element.
  343. If the unit number is not found, the function returns 0, and the
  344. index is the one where the element would be inserted. */
  345. static int
  346. search_unit (int unit, int *ip)
  347. {
  348. int low, high, mid;
  349. if (n_elist == 0)
  350. {
  351. *ip = 0;
  352. return 0;
  353. }
  354. low = 0;
  355. high = n_elist - 1;
  356. do
  357. {
  358. mid = (low + high) / 2;
  359. if (unit == elist[mid].unit)
  360. {
  361. *ip = mid;
  362. return 1;
  363. }
  364. else if (unit > elist[mid].unit)
  365. low = mid + 1;
  366. else
  367. high = mid - 1;
  368. } while (low <= high);
  369. if (unit > elist[mid].unit)
  370. *ip = mid + 1;
  371. else
  372. *ip = mid;
  373. return 0;
  374. }
  375. /* This matches a keyword. If it is found, return the token supplied,
  376. otherwise return ILLEGAL. */
  377. static int
  378. match_word (const char *word, int tok)
  379. {
  380. int res;
  381. if (strncasecmp (p, word, strlen (word)) == 0)
  382. {
  383. p += strlen (word);
  384. res = tok;
  385. }
  386. else
  387. res = ILLEGAL;
  388. return res;
  389. }
  390. /* Match an integer and store its value in unit_num. This only works
  391. if p actually points to the start of an integer. The caller has
  392. to ensure this. */
  393. static int
  394. match_integer (void)
  395. {
  396. unit_num = 0;
  397. while (isdigit (*p))
  398. unit_num = unit_num * 10 + (*p++ - '0');
  399. return INTEGER;
  400. }
  401. /* This reads the next token from the GFORTRAN_CONVERT_UNITS variable.
  402. Returned values are the different tokens. */
  403. static int
  404. next_token (void)
  405. {
  406. int result;
  407. lastpos = p;
  408. switch (*p)
  409. {
  410. case '\0':
  411. result = END;
  412. break;
  413. case ':':
  414. case ',':
  415. case '-':
  416. case ';':
  417. result = *p;
  418. p++;
  419. break;
  420. case 'b':
  421. case 'B':
  422. result = match_word ("big_endian", BIG);
  423. break;
  424. case 'l':
  425. case 'L':
  426. result = match_word ("little_endian", LITTLE);
  427. break;
  428. case 'n':
  429. case 'N':
  430. result = match_word ("native", NATIVE);
  431. break;
  432. case 's':
  433. case 'S':
  434. result = match_word ("swap", SWAP);
  435. break;
  436. case '1': case '2': case '3': case '4': case '5':
  437. case '6': case '7': case '8': case '9':
  438. result = match_integer ();
  439. break;
  440. default:
  441. result = ILLEGAL;
  442. break;
  443. }
  444. return result;
  445. }
  446. /* Back up the last token by setting back the character pointer. */
  447. static void
  448. push_token (void)
  449. {
  450. p = lastpos;
  451. }
  452. /* This is called when a unit is identified. If do_count is nonzero,
  453. increment the number of units by one. If do_count is zero,
  454. put the unit into the table. */
  455. static void
  456. mark_single (int unit)
  457. {
  458. int i,j;
  459. if (do_count)
  460. {
  461. unit_count++;
  462. return;
  463. }
  464. if (search_unit (unit, &i))
  465. {
  466. elist[i].conv = endian;
  467. }
  468. else
  469. {
  470. for (j=n_elist-1; j>=i; j--)
  471. elist[j+1] = elist[j];
  472. n_elist += 1;
  473. elist[i].unit = unit;
  474. elist[i].conv = endian;
  475. }
  476. }
  477. /* This is called when a unit range is identified. If do_count is
  478. nonzero, increase the number of units. If do_count is zero,
  479. put the unit into the table. */
  480. static void
  481. mark_range (int unit1, int unit2)
  482. {
  483. int i;
  484. if (do_count)
  485. unit_count += abs (unit2 - unit1) + 1;
  486. else
  487. {
  488. if (unit2 < unit1)
  489. for (i=unit2; i<=unit1; i++)
  490. mark_single (i);
  491. else
  492. for (i=unit1; i<=unit2; i++)
  493. mark_single (i);
  494. }
  495. }
  496. /* Parse the GFORTRAN_CONVERT_UNITS variable. This is called
  497. twice, once to count the units and once to actually mark them in
  498. the table. When counting, we don't check for double occurrences
  499. of units. */
  500. static int
  501. do_parse (void)
  502. {
  503. int tok;
  504. int unit1;
  505. int continue_ulist;
  506. char *start;
  507. unit_count = 0;
  508. start = p;
  509. /* Parse the string. First, let's look for a default. */
  510. tok = next_token ();
  511. switch (tok)
  512. {
  513. case NATIVE:
  514. endian = GFC_CONVERT_NATIVE;
  515. break;
  516. case SWAP:
  517. endian = GFC_CONVERT_SWAP;
  518. break;
  519. case BIG:
  520. endian = GFC_CONVERT_BIG;
  521. break;
  522. case LITTLE:
  523. endian = GFC_CONVERT_LITTLE;
  524. break;
  525. case INTEGER:
  526. /* A leading digit means that we are looking at an exception.
  527. Reset the position to the beginning, and continue processing
  528. at the exception list. */
  529. p = start;
  530. goto exceptions;
  531. break;
  532. case END:
  533. goto end;
  534. break;
  535. default:
  536. goto error;
  537. break;
  538. }
  539. tok = next_token ();
  540. switch (tok)
  541. {
  542. case ';':
  543. def = endian;
  544. break;
  545. case ':':
  546. /* This isn't a default after all. Reset the position to the
  547. beginning, and continue processing at the exception list. */
  548. p = start;
  549. goto exceptions;
  550. break;
  551. case END:
  552. def = endian;
  553. goto end;
  554. break;
  555. default:
  556. goto error;
  557. break;
  558. }
  559. exceptions:
  560. /* Loop over all exceptions. */
  561. while(1)
  562. {
  563. tok = next_token ();
  564. switch (tok)
  565. {
  566. case NATIVE:
  567. if (next_token () != ':')
  568. goto error;
  569. endian = GFC_CONVERT_NATIVE;
  570. break;
  571. case SWAP:
  572. if (next_token () != ':')
  573. goto error;
  574. endian = GFC_CONVERT_SWAP;
  575. break;
  576. case LITTLE:
  577. if (next_token () != ':')
  578. goto error;
  579. endian = GFC_CONVERT_LITTLE;
  580. break;
  581. case BIG:
  582. if (next_token () != ':')
  583. goto error;
  584. endian = GFC_CONVERT_BIG;
  585. break;
  586. case INTEGER:
  587. push_token ();
  588. break;
  589. case END:
  590. goto end;
  591. break;
  592. default:
  593. goto error;
  594. break;
  595. }
  596. /* We arrive here when we want to parse a list of
  597. numbers. */
  598. continue_ulist = 1;
  599. do
  600. {
  601. tok = next_token ();
  602. if (tok != INTEGER)
  603. goto error;
  604. unit1 = unit_num;
  605. tok = next_token ();
  606. /* The number can be followed by a - and another number,
  607. which means that this is a unit range, a comma
  608. or a semicolon. */
  609. if (tok == '-')
  610. {
  611. if (next_token () != INTEGER)
  612. goto error;
  613. mark_range (unit1, unit_num);
  614. tok = next_token ();
  615. if (tok == END)
  616. goto end;
  617. else if (tok == ';')
  618. continue_ulist = 0;
  619. else if (tok != ',')
  620. goto error;
  621. }
  622. else
  623. {
  624. mark_single (unit1);
  625. switch (tok)
  626. {
  627. case ';':
  628. continue_ulist = 0;
  629. break;
  630. case ',':
  631. break;
  632. case END:
  633. goto end;
  634. break;
  635. default:
  636. goto error;
  637. }
  638. }
  639. } while (continue_ulist);
  640. }
  641. end:
  642. return 0;
  643. error:
  644. def = GFC_CONVERT_NONE;
  645. return -1;
  646. }
  647. void init_unformatted (variable * v)
  648. {
  649. char *val;
  650. val = getenv (v->name);
  651. def = GFC_CONVERT_NONE;
  652. n_elist = 0;
  653. if (val == NULL)
  654. return;
  655. do_count = 1;
  656. p = val;
  657. do_parse ();
  658. if (do_count <= 0)
  659. {
  660. n_elist = 0;
  661. elist = NULL;
  662. }
  663. else
  664. {
  665. elist = xmallocarray (unit_count, sizeof (exception_t));
  666. do_count = 0;
  667. p = val;
  668. do_parse ();
  669. }
  670. }
  671. /* Get the default conversion for for an unformatted unit. */
  672. unit_convert
  673. get_unformatted_convert (int unit)
  674. {
  675. int i;
  676. if (elist == NULL)
  677. return def;
  678. else if (search_unit (unit, &i))
  679. return elist[i].conv;
  680. else
  681. return def;
  682. }