22
20
#include <grass/gis.h>
23
21
#include <grass/glocale.h>
25
#define ENV struct env
33
static ENV *env = NULL;
34
static ENV *env2 = NULL;
36
static int count2 = 0;
37
static int init[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
38
static char *gisrc = NULL;
39
static int varmode = G_GISRC_MODE_FILE; /* where find/store variables */
43
static struct state *st = &state;
41
45
static int read_env(int);
42
46
static int set_env(const char *, const char *, int);
43
47
static int unset_env(const char *, int);
44
static char *get_env(const char *, int);
45
static int write_env(int);
48
static const char *get_env(const char *, int);
49
static void write_env(int);
50
static void parse_env(FILE *, int);
51
static void force_read_env(int);
46
52
static FILE *open_env(const char *, int);
49
\brief Set where to find/store variables
55
\brief Set where to find/store variables
55
61
\param mode mode to find/store variables (G_GISRC_MODE_FILE by default)
59
63
void G_set_gisrc_mode(int mode)
65
\brief Get info where variables are stored
69
\brief Get info where variables are stored
71
73
int G_get_gisrc_mode(void)
76
static int read_env(int loc)
79
\brief Initialize variables
85
read_env(G_VAR_GISRC);
86
read_env(G_VAR_MAPSET);
90
* \brief Force to read the mapset environment file VAR
92
* The mapset specific VAR file of the mapset set with G_setenv()
93
* will be read into memory, ignoring if it was readed before.
94
* Existing values will be overwritten, new values appended.
98
void G__read_mapset_env(void)
100
force_read_env(G_VAR_MAPSET);
104
* \brief Force to read the GISRC environment file
107
* will be read into memory, ignoring if it was readed before.
108
* Existing values will be overwritten, new values appended.
112
void G__read_gisrc_env(void)
114
force_read_env(G_VAR_GISRC);
117
static void parse_env(FILE *fd, int loc)
84
if (loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY)
85
return 0; /* don't use file for GISRC */
92
if ((fd = open_env("r", loc))) {
93
while (G_getl2(buf, sizeof buf, fd)) {
123
while (G_getl2(buf, sizeof buf, fd)) {
94
124
for (name = value = buf; *value; value++)
95
125
if (*value == ':')
135
196
* and look for name in the environment
138
for (n = 0; n < count; n++)
139
if (!env[n].name) /* mark empty slot found */
199
for (n = 0; n < st->env.count; n++) {
200
struct bind *b = &st->env.binds[n];
201
if (!b->name) /* mark empty slot found */
141
else if (strcmp(env[n].name, name) == 0 && env[n].loc == loc) {
203
else if (strcmp(b->name, name) == 0 && b->loc == loc) {
146
209
/* add name to env: to empty slot if any */
147
210
if (empty >= 0) {
148
env[empty].loc = loc;
149
env[empty].name = G_store(name);
150
env[empty].value = tv;
211
struct bind *b = &st->env.binds[empty];
213
b->name = G_store(name);
154
218
/* must increase the env list and add in */
156
env = (ENV *) G_realloc((char *)env, count * sizeof(ENV));
158
env = (ENV *) G_malloc(sizeof(ENV));
161
env[n].name = G_store(name);
219
if (st->env.count >= st->env.size) {
221
st->env.binds = G_realloc(st->env.binds, st->env.size * sizeof(struct bind));
225
struct bind *b = &st->env.binds[st->env.count++];
228
b->name = G_store(name);
171
for (n = 0; n < count; n++)
172
if (env[n].name && (strcmp(env[n].name, name) == 0) &&
239
for (n = 0; n < st->env.count; n++) {
240
struct bind *b = &st->env.binds[n];
241
if (b->name && strcmp(b->name, name) == 0 && b->loc == loc) {
182
static char *get_env(const char *name, int loc)
251
static const char *get_env(const char *name, int loc)
186
for (n = 0; n < count; n++) {
187
if (env[n].name && (strcmp(env[n].name, name) == 0) &&
255
for (n = 0; n < st->env.count; n++) {
256
struct bind *b = &st->env.binds[n];
257
if (b->name && (strcmp(b->name, name) == 0) &&
195
static int write_env(int loc)
265
static void write_env(int loc)
270
RETSIGTYPE (*sigint)(int);
272
RETSIGTYPE (*sigquit)(int);
206
if (loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY)
207
return 0; /* don't use file for GISRC */
275
if (loc == G_VAR_GISRC && st->varmode == G_GISRC_MODE_MEMORY)
276
return; /* don't use file for GISRC */
210
279
* THIS CODE NEEDS TO BE PROTECTED FROM INTERRUPTS
255
324
return fopen(buf, mode);
259
\brief Get environment variable
261
Calls G_fatal_error() if name not set.
263
\param name variable name
265
\return char pointer to value for name
267
char *G_getenv(const char *name)
271
if ((value = G__getenv(name)))
274
G_fatal_error(_("G_getenv(): Variable %s not set"), name);
279
\brief Read variable from specific place
285
G_fatal_error() is called when variable is not found.
287
\param name variable name
288
\param loc location id
290
\return variable value
291
\return NULL if not found
293
char *G_getenv2(const char *name, int loc)
297
if ((value = G__getenv2(name, loc)))
300
G_fatal_error(_("%s not set"), name);
305
\brief Get environment variable
307
\param name variable name
328
\brief Get environment variable
330
G_fatal_error() is called when variable is not found.
332
\param name variable name
334
\return char pointer to value for name
336
const char *G_getenv(const char *name)
338
const char *value = G_getenv_nofatal(name);
343
G_fatal_error(_("Variable '%s' not set"), name);
348
\brief Get variable from specific place
354
G_fatal_error() is called when variable is not found.
356
\param name variable name
357
\param loc location (G_VAR_GISRC, G_VAR_MAPSET)
309
\return char pointer to value for name
310
\return NULL if name not set
312
char *G__getenv(const char *name)
359
\return variable value
360
\return NULL if not found
362
const char *G_getenv2(const char *name, int loc)
364
const char *value = G_getenv_nofatal2(name, loc);
369
G_fatal_error(_("Variable '%s' not set"), name);
374
\brief Get environment variable
376
\param name variable name
378
\return char pointer to value for name
379
\return NULL if name not set
381
const char *G_getenv_nofatal(const char *name)
314
383
if (strcmp(name, "GISBASE") == 0)
315
384
return getenv(name);
338
407
return get_env(name, loc);
342
\brief Set environment variable
344
If value is NULL, becomes an G_unsetenv().
347
\param name variable name
348
\param value variable value
411
\brief Set environment variable (updates .gisrc)
413
If value is NULL, becomes an G_unsetenv().
415
\param name variable name
416
\param value variable value
352
int G_setenv(const char *name, const char *value)
418
void G_setenv(const char *name, const char *value)
354
420
read_env(G_VAR_GISRC);
355
421
set_env(name, value, G_VAR_GISRC);
356
422
write_env(G_VAR_GISRC);
361
\brief Set environment variable from specific place
363
If value is NULL, becomes an G_unsetenv().
366
\param name variable name
367
\param value variable value
368
\param loc location id
426
\brief Set environment variable from specific place (updates .gisrc)
428
If value is NULL, becomes an G_unsetenv().
430
\param name variable name
431
\param value variable value
432
\param loc location (G_VAR_GISRC, G_VAR_MAPSET)
372
int G_setenv2(const char *name, const char *value, int loc)
435
void G_setenv2(const char *name, const char *value, int loc)
375
438
set_env(name, value, loc);
381
\brief Set environment name to value
383
\param name variable name
384
\param value variable value
443
\brief Set environment name to value (doesn't update .gisrc)
445
\param name variable name
446
\param value variable value
388
int G__setenv(const char *name, const char *value)
448
void G_setenv_nogisrc(const char *name, const char *value)
390
450
read_env(G_VAR_GISRC);
391
451
set_env(name, value, G_VAR_GISRC);
396
\brief Set environment name to value from specific place
398
\param name variable name
399
\param value variable value
400
\param loc location id
455
\brief Set environment name to value from specific place (doesn't update .gisrc)
457
\param name variable name
458
\param value variable value
459
\param loc location (G_VAR_GISRC, G_VAR_MAPSET)
404
int G__setenv2(const char *name, const char *value, int loc)
461
void G_setenv_nogisrc2(const char *name, const char *value, int loc)
407
464
set_env(name, value, loc);
412
\brief Remove name from environment
416
\param name variable name
468
\brief Remove name from environment
472
\param name variable name
420
int G_unsetenv(const char *name)
474
void G_unsetenv(const char *name)
422
476
read_env(G_VAR_GISRC);
423
477
unset_env(name, G_VAR_GISRC);
424
478
write_env(G_VAR_GISRC);
430
\brief Remove name from environment from specific place
434
\param name variable name
482
\brief Remove name from environment from specific place
486
\param name variable name
487
\param loc location (G_VAR_GISRC, G_VAR_MAPSET)
438
int G_unsetenv2(const char *name, int loc)
489
void G_unsetenv2(const char *name, int loc)
441
492
unset_env(name, loc);
448
\brief Writes current environment to .gisrc
497
\brief Writes current environment to .gisrc
454
int G__write_env(void)
499
void G__write_env(void)
456
if (init[G_VAR_GISRC])
501
if (st->init[G_VAR_GISRC])
457
502
write_env(G_VAR_GISRC);
463
\brief Get variable name for index n.
506
\brief Get variable name for index n.
467
511
for (n = 0; ; n++)
468
if ((name = G__env_name(n)) == NULL)
512
if ((name = G_get_env_name(n)) == NULL)
472
\param n index of variable
474
\return pointer to variable name
516
\param n index of variable
518
\return pointer to variable name
519
\return NULL not found
476
char *G__env_name(int n)
521
const char *G_get_env_name(int n)
480
525
read_env(G_VAR_GISRC);
482
for (i = 0; i < count; i++)
483
if (env[i].name && *env[i].name && (n-- == 0))
527
for (i = 0; i < st->env.count; i++)
528
if (st->env.binds[i].name && *st->env.binds[i].name && (n-- == 0))
529
return st->env.binds[i].name;
489
\brief Initialize init array for G_VAR_GISRC.
495
int G__read_env(void)
497
init[G_VAR_GISRC] = 0;
503
\brief Sets filename for gisrc
509
int G__set_gisrc_file(const char *name)
513
gisrc = G_store(name);
519
\brief Get gisrc filename
523
\return char pointer to filename
525
char *G__get_gisrc_file(void)
531
\brief Set up alternative environment variables
537
int G__create_alt_env(void)
534
\brief Initialize init array for G_VAR_GISRC.
536
void G__read_env(void)
538
st->init[G_VAR_GISRC] = 0;
542
\brief Set up alternative environment variables
544
void G_create_alt_env(void)
541
548
/* copy env to env2 */
547
for (i = 0; i < count2; i++)
548
if (env2[count].name)
549
set_env(env2[count].name, env2[count].value, G_VAR_GISRC);
553
st->env.binds = NULL;
555
for (i = 0; i < st->env2.count; i++) {
556
struct bind *b = &st->env2.binds[i];
558
set_env(b->name, b->value, G_VAR_GISRC);
555
\brief Switch environments
563
\brief Switch environments
561
int G__switch_env(void)
565
void G_switch_env(void)