env.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*!
  2. \file lib/gis/env.c
  3. \brief GIS library - environment routines
  4. (C) 2001-2022 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. \author Updated for GRASS7 by Glynn Clements
  9. */
  10. #include <signal.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <unistd.h> /* for sleep() */
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. struct bind {
  18. int loc;
  19. char *name;
  20. char *value;
  21. };
  22. struct env {
  23. struct bind *binds;
  24. int count;
  25. int size;
  26. };
  27. static struct state {
  28. struct env env;
  29. struct env env2;
  30. char *gisrc;
  31. int varmode;
  32. int init[2];
  33. } state;
  34. static struct state *st = &state;
  35. static int read_env(int);
  36. static int set_env(const char *, const char *, int);
  37. static int unset_env(const char *, int);
  38. static const char *get_env(const char *, int);
  39. static void write_env(int);
  40. static void parse_env(FILE *, int);
  41. static void force_read_env(int);
  42. static FILE *open_env(const char *, int);
  43. /*!
  44. \brief Set where to find/store variables
  45. Modes:
  46. - G_GISRC_MODE_FILE
  47. - G_GISRC_MODE_MEMORY
  48. \param mode mode to find/store variables (G_GISRC_MODE_FILE by default)
  49. */
  50. void G_set_gisrc_mode(int mode)
  51. {
  52. st->varmode = mode;
  53. }
  54. /*!
  55. \brief Get info where variables are stored
  56. \return mode
  57. */
  58. int G_get_gisrc_mode(void)
  59. {
  60. return (st->varmode);
  61. }
  62. /*!
  63. \brief Initialize variables
  64. \return
  65. */
  66. void G_init_env(void)
  67. {
  68. read_env(G_VAR_GISRC);
  69. read_env(G_VAR_MAPSET);
  70. }
  71. /*!
  72. * \brief Force to read the mapset environment file VAR
  73. *
  74. * The mapset specific VAR file of the mapset set with G_setenv()
  75. * will be read into memory, ignoring if it was readed before.
  76. * Existing values will be overwritten, new values appended.
  77. *
  78. * \return
  79. */
  80. void G__read_mapset_env(void)
  81. {
  82. force_read_env(G_VAR_MAPSET);
  83. }
  84. /*!
  85. * \brief Force to read the GISRC environment file
  86. *
  87. * The GISRC file
  88. * will be read into memory, ignoring if it was readed before.
  89. * Existing values will be overwritten, new values appended.
  90. *
  91. * \return
  92. */
  93. void G__read_gisrc_env(void)
  94. {
  95. force_read_env(G_VAR_GISRC);
  96. }
  97. static void parse_env(FILE *fd, int loc)
  98. {
  99. /* Account for long lines up to GPATH_MAX.
  100. E.g. "GISDBASE: GPATH_MAX\n\0" */
  101. char buf[GPATH_MAX + 16];
  102. char *name;
  103. char *value;
  104. while (G_getl2(buf, sizeof buf, fd)) {
  105. for (name = value = buf; *value; value++)
  106. if (*value == ':')
  107. break;
  108. if (*value == 0)
  109. continue;
  110. *value++ = 0;
  111. G_strip(name);
  112. G_strip(value);
  113. if (*name && *value)
  114. set_env(name, value, loc);
  115. }
  116. }
  117. static int read_env(int loc)
  118. {
  119. FILE *fd;
  120. if (loc == G_VAR_GISRC && st->varmode == G_GISRC_MODE_MEMORY)
  121. return 0; /* don't use file for GISRC */
  122. if (G_is_initialized(&st->init[loc]))
  123. return 1;
  124. if ((fd = open_env("r", loc))) {
  125. parse_env(fd, loc);
  126. fclose(fd);
  127. }
  128. G_initialize_done(&st->init[loc]);
  129. return 0;
  130. }
  131. /*!
  132. * \brief Force the reading or the GISRC or MAPSET/VAR files
  133. * and overwrite/append the specified variables
  134. *
  135. */
  136. static void force_read_env(int loc)
  137. {
  138. FILE *fd;
  139. if ((fd = open_env("r", loc))) {
  140. parse_env(fd, loc);
  141. fclose(fd);
  142. }
  143. }
  144. static int set_env(const char *name, const char *value, int loc)
  145. {
  146. int n;
  147. int empty;
  148. char *tv;
  149. /* if value is NULL or empty string, convert into an unsetenv() */
  150. if (!value || !strlen(value)) {
  151. unset_env(name, loc);
  152. return 0;
  153. }
  154. tv = G_store(value);
  155. G_strip(tv);
  156. if (*tv == 0) {
  157. G_free(tv);
  158. unset_env(name, loc);
  159. return 1;
  160. }
  161. /*
  162. * search the array
  163. * keep track of first empty slot
  164. * and look for name in the environment
  165. */
  166. empty = -1;
  167. for (n = 0; n < st->env.count; n++) {
  168. struct bind *b = &st->env.binds[n];
  169. if (!b->name) /* mark empty slot found */
  170. empty = n;
  171. else if (strcmp(b->name, name) == 0 && b->loc == loc) {
  172. b->value = tv;
  173. return 1;
  174. }
  175. }
  176. /* add name to env: to empty slot if any */
  177. if (empty >= 0) {
  178. struct bind *b = &st->env.binds[empty];
  179. b->loc = loc;
  180. b->name = G_store(name);
  181. b->value = tv;
  182. return 0;
  183. }
  184. /* must increase the env list and add in */
  185. if (st->env.count >= st->env.size) {
  186. st->env.size += 20;
  187. st->env.binds = G_realloc(st->env.binds, st->env.size * sizeof(struct bind));
  188. }
  189. {
  190. struct bind *b = &st->env.binds[st->env.count++];
  191. b->loc = loc;
  192. b->name = G_store(name);
  193. b->value = tv;
  194. }
  195. return 0;
  196. }
  197. static int unset_env(const char *name, int loc)
  198. {
  199. int n;
  200. for (n = 0; n < st->env.count; n++) {
  201. struct bind *b = &st->env.binds[n];
  202. if (b->name && strcmp(b->name, name) == 0 && b->loc == loc) {
  203. G_free(b->name);
  204. b->name = 0;
  205. return 1;
  206. }
  207. }
  208. return 0;
  209. }
  210. static const char *get_env(const char *name, int loc)
  211. {
  212. int n;
  213. for (n = 0; n < st->env.count; n++) {
  214. struct bind *b = &st->env.binds[n];
  215. if (b->name && (strcmp(b->name, name) == 0) &&
  216. b->loc == loc)
  217. return b->value;
  218. }
  219. return NULL;
  220. }
  221. static void write_env(int loc)
  222. {
  223. FILE *fd;
  224. int n;
  225. char dummy[2];
  226. void (*sigint)(int);
  227. #ifdef SIGQUIT
  228. void (*sigquit)(int);
  229. #endif
  230. if (loc == G_VAR_GISRC && st->varmode == G_GISRC_MODE_MEMORY)
  231. return; /* don't use file for GISRC */
  232. /*
  233. * THIS CODE NEEDS TO BE PROTECTED FROM INTERRUPTS
  234. * If interrupted, it can wipe out the GISRC file
  235. */
  236. sigint = signal(SIGINT, SIG_IGN);
  237. #ifdef SIGQUIT
  238. sigquit = signal(SIGQUIT, SIG_IGN);
  239. #endif
  240. if ((fd = open_env("w", loc))) {
  241. for (n = 0; n < st->env.count; n++) {
  242. struct bind *b = &st->env.binds[n];
  243. if (b->name && b->value && b->loc == loc
  244. && (sscanf(b->value, "%1s", dummy) == 1))
  245. fprintf(fd, "%s: %s\n", b->name, b->value);
  246. }
  247. fclose(fd);
  248. }
  249. signal(SIGINT, sigint);
  250. #ifdef SIGQUIT
  251. signal(SIGQUIT, sigquit);
  252. #endif
  253. }
  254. static FILE *open_env(const char *mode, int loc)
  255. {
  256. char buf[GPATH_MAX];
  257. if (loc == G_VAR_GISRC) {
  258. if (!st->gisrc)
  259. st->gisrc = getenv("GISRC");
  260. if (!st->gisrc) {
  261. G_fatal_error(_("No active GRASS session: "
  262. "GISRC environment variable not set"));
  263. return NULL;
  264. }
  265. strcpy(buf, st->gisrc);
  266. }
  267. else if (loc == G_VAR_MAPSET) {
  268. /* Warning: G_VAR_GISRC must be previously read -> */
  269. /* TODO: better place ? */
  270. read_env(G_VAR_GISRC);
  271. sprintf(buf, "%s/%s/VAR", G_location_path(), G_mapset());
  272. }
  273. return fopen(buf, mode);
  274. }
  275. /*!
  276. \brief Get environment variable
  277. G_fatal_error() is called when variable is not found.
  278. \param name variable name
  279. \return char pointer to value for name
  280. */
  281. const char *G_getenv(const char *name)
  282. {
  283. const char *value = G_getenv_nofatal(name);
  284. if (value)
  285. return value;
  286. G_fatal_error(_("Incomplete GRASS session: Variable '%s' not set"), name);
  287. return NULL;
  288. }
  289. /*!
  290. \brief Get variable from specific place
  291. Locations:
  292. - G_VAR_GISRC
  293. - G_VAR_MAPSET
  294. G_fatal_error() is called when variable is not found.
  295. \param name variable name
  296. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  297. \return variable value
  298. \return NULL if not found
  299. */
  300. const char *G_getenv2(const char *name, int loc)
  301. {
  302. const char *value = G_getenv_nofatal2(name, loc);
  303. if (value)
  304. return value;
  305. G_fatal_error(_("Incomplete GRASS session: Variable '%s' not set"), name);
  306. return NULL;
  307. }
  308. /*!
  309. \brief Get environment variable
  310. \param name variable name
  311. \return char pointer to value for name
  312. \return NULL if name not set
  313. */
  314. const char *G_getenv_nofatal(const char *name)
  315. {
  316. if (strcmp(name, "GISBASE") == 0)
  317. return getenv(name);
  318. read_env(G_VAR_GISRC);
  319. return get_env(name, G_VAR_GISRC);
  320. }
  321. /*!
  322. \brief Get environment variable from specific place
  323. \param name variable name
  324. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  325. \return char pointer to value for name
  326. \return NULL if name not set
  327. */
  328. const char *G_getenv_nofatal2(const char *name, int loc)
  329. {
  330. if (strcmp(name, "GISBASE") == 0)
  331. return getenv(name);
  332. read_env(loc);
  333. return get_env(name, loc);
  334. }
  335. /*!
  336. \brief Set environment variable (updates .gisrc)
  337. If value is NULL, becomes an G_unsetenv().
  338. \param name variable name
  339. \param value variable value
  340. */
  341. void G_setenv(const char *name, const char *value)
  342. {
  343. read_env(G_VAR_GISRC);
  344. set_env(name, value, G_VAR_GISRC);
  345. write_env(G_VAR_GISRC);
  346. }
  347. /*!
  348. \brief Set environment variable from specific place (updates .gisrc)
  349. If value is NULL, becomes an G_unsetenv().
  350. \param name variable name
  351. \param value variable value
  352. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  353. */
  354. void G_setenv2(const char *name, const char *value, int loc)
  355. {
  356. read_env(loc);
  357. set_env(name, value, loc);
  358. write_env(loc);
  359. }
  360. /*!
  361. \brief Set environment name to value (doesn't update .gisrc)
  362. \param name variable name
  363. \param value variable value
  364. */
  365. void G_setenv_nogisrc(const char *name, const char *value)
  366. {
  367. read_env(G_VAR_GISRC);
  368. set_env(name, value, G_VAR_GISRC);
  369. }
  370. /*!
  371. \brief Set environment name to value from specific place (doesn't update .gisrc)
  372. \param name variable name
  373. \param value variable value
  374. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  375. */
  376. void G_setenv_nogisrc2(const char *name, const char *value, int loc)
  377. {
  378. read_env(loc);
  379. set_env(name, value, loc);
  380. }
  381. /*!
  382. \brief Remove name from environment
  383. Updates .gisrc
  384. \param name variable name
  385. */
  386. void G_unsetenv(const char *name)
  387. {
  388. read_env(G_VAR_GISRC);
  389. unset_env(name, G_VAR_GISRC);
  390. write_env(G_VAR_GISRC);
  391. }
  392. /*!
  393. \brief Remove name from environment from specific place
  394. Updates .gisrc
  395. \param name variable name
  396. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  397. */
  398. void G_unsetenv2(const char *name, int loc)
  399. {
  400. read_env(loc);
  401. unset_env(name, loc);
  402. write_env(loc);
  403. }
  404. /*!
  405. \brief Writes current environment to .gisrc
  406. */
  407. void G__write_env(void)
  408. {
  409. if (st->init[G_VAR_GISRC])
  410. write_env(G_VAR_GISRC);
  411. }
  412. /*!
  413. \brief Get variable name for index n.
  414. For example:
  415. \code
  416. for (n = 0; ; n++)
  417. if ((name = G_get_env_name(n)) == NULL)
  418. break;
  419. \endcode
  420. \param n index of variable
  421. \return pointer to variable name
  422. \return NULL not found
  423. */
  424. const char *G_get_env_name(int n)
  425. {
  426. int i;
  427. read_env(G_VAR_GISRC);
  428. if (n >= 0)
  429. for (i = 0; i < st->env.count; i++)
  430. if (st->env.binds[i].name && *st->env.binds[i].name && (n-- == 0))
  431. return st->env.binds[i].name;
  432. return NULL;
  433. }
  434. /*!
  435. \brief Initialize init array for G_VAR_GISRC.
  436. */
  437. void G__read_env(void)
  438. {
  439. st->init[G_VAR_GISRC] = 0;
  440. }
  441. /*!
  442. \brief Set up alternative environment variables
  443. */
  444. void G_create_alt_env(void)
  445. {
  446. int i;
  447. /* copy env to env2 */
  448. st->env2 = st->env;
  449. st->env.count = 0;
  450. st->env.size = 0;
  451. st->env.binds = NULL;
  452. for (i = 0; i < st->env2.count; i++) {
  453. struct bind *b = &st->env2.binds[i];
  454. if (b->name)
  455. set_env(b->name, b->value, G_VAR_GISRC);
  456. }
  457. }
  458. /*!
  459. \brief Switch environments
  460. */
  461. void G_switch_env(void)
  462. {
  463. struct env tmp;
  464. tmp = st->env;
  465. st->env = st->env2;
  466. st->env2 = tmp;
  467. }