env.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*!
  2. \file lib/gis/env.c
  3. \brief GIS library - environment routines
  4. (C) 2001-2014 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. RETSIGTYPE (*sigint)(int);
  227. #ifdef SIGQUIT
  228. RETSIGTYPE (*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(_("GISRC - variable not set"));
  262. return NULL;
  263. }
  264. strcpy(buf, st->gisrc);
  265. }
  266. else if (loc == G_VAR_MAPSET) {
  267. /* Warning: G_VAR_GISRC must be previously read -> */
  268. /* TODO: better place ? */
  269. read_env(G_VAR_GISRC);
  270. sprintf(buf, "%s/%s/VAR", G_location_path(), G_mapset());
  271. }
  272. return fopen(buf, mode);
  273. }
  274. /*!
  275. \brief Get environment variable
  276. G_fatal_error() is called when variable is not found.
  277. \param name variable name
  278. \return char pointer to value for name
  279. */
  280. const char *G_getenv(const char *name)
  281. {
  282. const char *value = G_getenv_nofatal(name);
  283. if (value)
  284. return value;
  285. G_fatal_error(_("Variable '%s' not set"), name);
  286. return NULL;
  287. }
  288. /*!
  289. \brief Get variable from specific place
  290. Locations:
  291. - G_VAR_GISRC
  292. - G_VAR_MAPSET
  293. G_fatal_error() is called when variable is not found.
  294. \param name variable name
  295. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  296. \return variable value
  297. \return NULL if not found
  298. */
  299. const char *G_getenv2(const char *name, int loc)
  300. {
  301. const char *value = G_getenv_nofatal2(name, loc);
  302. if (value)
  303. return value;
  304. G_fatal_error(_("Variable '%s' not set"), name);
  305. return NULL;
  306. }
  307. /*!
  308. \brief Get environment variable
  309. \param name variable name
  310. \return char pointer to value for name
  311. \return NULL if name not set
  312. */
  313. const char *G_getenv_nofatal(const char *name)
  314. {
  315. if (strcmp(name, "GISBASE") == 0)
  316. return getenv(name);
  317. read_env(G_VAR_GISRC);
  318. return get_env(name, G_VAR_GISRC);
  319. }
  320. /*!
  321. \brief Get environment variable from specific place
  322. \param name variable name
  323. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  324. \return char pointer to value for name
  325. \return NULL if name not set
  326. */
  327. const char *G_getenv_nofatal2(const char *name, int loc)
  328. {
  329. if (strcmp(name, "GISBASE") == 0)
  330. return getenv(name);
  331. read_env(loc);
  332. return get_env(name, loc);
  333. }
  334. /*!
  335. \brief Set environment variable (updates .gisrc)
  336. If value is NULL, becomes an G_unsetenv().
  337. \param name variable name
  338. \param value variable value
  339. */
  340. void G_setenv(const char *name, const char *value)
  341. {
  342. read_env(G_VAR_GISRC);
  343. set_env(name, value, G_VAR_GISRC);
  344. write_env(G_VAR_GISRC);
  345. }
  346. /*!
  347. \brief Set environment variable from specific place (updates .gisrc)
  348. If value is NULL, becomes an G_unsetenv().
  349. \param name variable name
  350. \param value variable value
  351. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  352. */
  353. void G_setenv2(const char *name, const char *value, int loc)
  354. {
  355. read_env(loc);
  356. set_env(name, value, loc);
  357. write_env(loc);
  358. }
  359. /*!
  360. \brief Set environment name to value (doesn't update .gisrc)
  361. \param name variable name
  362. \param value variable value
  363. */
  364. void G_setenv_nogisrc(const char *name, const char *value)
  365. {
  366. read_env(G_VAR_GISRC);
  367. set_env(name, value, G_VAR_GISRC);
  368. }
  369. /*!
  370. \brief Set environment name to value from specific place (doesn't update .gisrc)
  371. \param name variable name
  372. \param value variable value
  373. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  374. */
  375. void G_setenv_nogisrc2(const char *name, const char *value, int loc)
  376. {
  377. read_env(loc);
  378. set_env(name, value, loc);
  379. }
  380. /*!
  381. \brief Remove name from environment
  382. Updates .gisrc
  383. \param name variable name
  384. */
  385. void G_unsetenv(const char *name)
  386. {
  387. read_env(G_VAR_GISRC);
  388. unset_env(name, G_VAR_GISRC);
  389. write_env(G_VAR_GISRC);
  390. }
  391. /*!
  392. \brief Remove name from environment from specific place
  393. Updates .gisrc
  394. \param name variable name
  395. \param loc location (G_VAR_GISRC, G_VAR_MAPSET)
  396. */
  397. void G_unsetenv2(const char *name, int loc)
  398. {
  399. read_env(loc);
  400. unset_env(name, loc);
  401. write_env(loc);
  402. }
  403. /*!
  404. \brief Writes current environment to .gisrc
  405. */
  406. void G__write_env(void)
  407. {
  408. if (st->init[G_VAR_GISRC])
  409. write_env(G_VAR_GISRC);
  410. }
  411. /*!
  412. \brief Get variable name for index n.
  413. For example:
  414. \code
  415. for (n = 0; ; n++)
  416. if ((name = G_get_env_name(n)) == NULL)
  417. break;
  418. \endcode
  419. \param n index of variable
  420. \return pointer to variable name
  421. \return NULL not found
  422. */
  423. const char *G_get_env_name(int n)
  424. {
  425. int i;
  426. read_env(G_VAR_GISRC);
  427. if (n >= 0)
  428. for (i = 0; i < st->env.count; i++)
  429. if (st->env.binds[i].name && *st->env.binds[i].name && (n-- == 0))
  430. return st->env.binds[i].name;
  431. return NULL;
  432. }
  433. /*!
  434. \brief Initialize init array for G_VAR_GISRC.
  435. */
  436. void G__read_env(void)
  437. {
  438. st->init[G_VAR_GISRC] = 0;
  439. }
  440. /*!
  441. \brief Set up alternative environment variables
  442. */
  443. void G_create_alt_env(void)
  444. {
  445. int i;
  446. /* copy env to env2 */
  447. st->env2 = st->env;
  448. st->env.count = 0;
  449. st->env.size = 0;
  450. st->env.binds = NULL;
  451. for (i = 0; i < st->env2.count; i++) {
  452. struct bind *b = &st->env2.binds[i];
  453. if (b->name)
  454. set_env(b->name, b->value, G_VAR_GISRC);
  455. }
  456. }
  457. /*!
  458. \brief Switch environments
  459. */
  460. void G_switch_env(void)
  461. {
  462. struct env tmp;
  463. tmp = st->env;
  464. st->env = st->env2;
  465. st->env2 = tmp;
  466. }