21#include <grass/glocale.h>
45static int read_env(
int);
46static int set_env(
const char *,
const char *,
int);
47static int unset_env(
const char *,
int);
48static const char *get_env(
const char *,
int);
49static void write_env(
int);
50static void parse_env(FILE *,
int);
51static void force_read_env(
int);
52static FILE *open_env(
const char *,
int);
85 read_env(G_VAR_GISRC);
86 read_env(G_VAR_MAPSET);
100 force_read_env(G_VAR_MAPSET);
114 force_read_env(G_VAR_GISRC);
126 st->gisrc = getenv(
"GISRC");
129 "GISRC environment variable not set"));
133static void parse_env(FILE *fd,
int loc)
137 char buf[GPATH_MAX + 16];
141 while (
G_getl2(buf,
sizeof buf, fd)) {
142 for (
name = value = buf; *value; value++)
152 set_env(
name, value, loc);
156static int read_env(
int loc)
161 if (loc == G_VAR_GISRC &&
st->varmode == G_GISRC_MODE_MEMORY)
167 if ((fd = open_env(
"r", loc))) {
181static void force_read_env(
int loc)
184 if ((fd = open_env(
"r", loc))) {
191static int set_env(
const char *
name,
const char *value,
int loc)
198 if (!value || !strlen(value)) {
199 unset_env(
name, loc);
207 unset_env(
name, loc);
217 for (n = 0; n <
st->env.count; n++) {
218 struct bind *
b = &
st->env.binds[n];
221 else if (strcmp(
b->name,
name) == 0 &&
b->loc == loc) {
229 struct bind *
b = &
st->env.binds[empty];
237 if (
st->env.count >=
st->env.size) {
239 st->env.binds = G_realloc(
st->env.binds,
st->env.size *
sizeof(
struct bind));
243 struct bind *
b = &
st->env.binds[
st->env.count++];
253static int unset_env(
const char *
name,
int loc)
257 for (n = 0; n <
st->env.count; n++) {
258 struct bind *
b = &
st->env.binds[n];
259 if (
b->name && strcmp(
b->name,
name) == 0 &&
b->loc == loc) {
269static const char *get_env(
const char *
name,
int loc)
273 for (n = 0; n <
st->env.count; n++) {
274 struct bind *
b = &
st->env.binds[n];
275 if (
b->name && (strcmp(
b->name,
name) == 0) &&
283static void write_env(
int loc)
290 void (*sigquit)(int);
293 if (loc == G_VAR_GISRC &&
st->varmode == G_GISRC_MODE_MEMORY)
300 sigint = signal(SIGINT, SIG_IGN);
302 sigquit = signal(SIGQUIT, SIG_IGN);
304 if ((fd = open_env(
"w", loc))) {
305 for (n = 0; n <
st->env.count; n++) {
306 struct bind *
b = &
st->env.binds[n];
307 if (
b->name &&
b->value &&
b->loc == loc
308 && (sscanf(
b->value,
"%1s", dummy) == 1))
309 fprintf(fd,
"%s: %s\n",
b->name,
b->value);
314 signal(SIGINT, sigint);
316 signal(SIGQUIT, sigquit);
320static FILE *open_env(
const char *mode,
int loc)
324 if (loc == G_VAR_GISRC) {
331 strcpy(buf,
st->gisrc);
333 else if (loc == G_VAR_MAPSET) {
336 read_env(G_VAR_GISRC);
341 return fopen(buf, mode);
400 if (strcmp(
name,
"GISBASE") == 0)
403 read_env(G_VAR_GISRC);
405 return get_env(
name, G_VAR_GISRC);
419 if (strcmp(
name,
"GISBASE") == 0)
424 return get_env(
name, loc);
437 read_env(G_VAR_GISRC);
438 set_env(
name, value, G_VAR_GISRC);
439 write_env(G_VAR_GISRC);
455 set_env(
name, value, loc);
467 read_env(G_VAR_GISRC);
468 set_env(
name, value, G_VAR_GISRC);
481 set_env(
name, value, loc);
493 read_env(G_VAR_GISRC);
494 unset_env(
name, G_VAR_GISRC);
495 write_env(G_VAR_GISRC);
509 unset_env(
name, loc);
518 if (
st->init[G_VAR_GISRC])
519 write_env(G_VAR_GISRC);
542 read_env(G_VAR_GISRC);
544 for (i = 0; i <
st->env.count; i++)
545 if (
st->env.binds[i].name && *
st->env.binds[i].name && (n-- == 0))
546 return st->env.binds[i].name;
555 st->init[G_VAR_GISRC] = 0;
572 for (i = 0; i <
st->env2.count; i++) {
573 struct bind *
b = &
st->env2.binds[i];
575 set_env(
b->name,
b->value, G_VAR_GISRC);
void G_free(void *buf)
Free allocated memory.
void G_initialize_done(int *p)
int G_is_initialized(int *p)
void G_setenv(const char *name, const char *value)
Set environment variable (updates .gisrc)
const char * G_getenv_nofatal2(const char *name, int loc)
Get environment variable from specific place.
void G_switch_env(void)
Switch environments.
const char * G_getenv2(const char *name, int loc)
Get variable from specific place.
void G__read_env(void)
Initialize init array for G_VAR_GISRC.
void G_setenv_nogisrc2(const char *name, const char *value, int loc)
Set environment name to value from specific place (doesn't update .gisrc)
void G_unsetenv(const char *name)
Remove name from environment.
void G__read_gisrc_path()
Read or read again the GISRC (session) environment variable.
void G_setenv2(const char *name, const char *value, int loc)
Set environment variable from specific place (updates .gisrc)
void G_unsetenv2(const char *name, int loc)
Remove name from environment from specific place.
void G__write_env(void)
Writes current environment to .gisrc.
void G__read_gisrc_env(void)
Force to read the GISRC environment file.
void G_set_gisrc_mode(int mode)
Set where to find/store variables.
const char * G_getenv_nofatal(const char *name)
Get environment variable.
void G__read_mapset_env(void)
Force to read the mapset environment file VAR.
void G_setenv_nogisrc(const char *name, const char *value)
Set environment name to value (doesn't update .gisrc)
const char * G_get_env_name(int n)
Get variable name for index n.
int G_get_gisrc_mode(void)
Get info where variables are stored.
void G_create_alt_env(void)
Set up alternative environment variables.
const char * G_getenv(const char *name)
Get environment variable.
void G_init_env(void)
Initialize variables.
int G_getl2(char *buf, int n, FILE *fd)
Gets a line of text from a file of any pedigree.
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
char * G_location_path(void)
Get current location UNIX-like path.
const char * G_mapset(void)
Get current mapset name.
char * G_store(const char *s)
Copy string to allocated memory.
void G_strip(char *buf)
Removes all leading and trailing white space from string.