20 #include <grass/gis.h>
21 #include <grass/glocale.h>
45 static int read_env(
int);
46 static int set_env(
const char *,
const char *,
int);
47 static int unset_env(
const char *,
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);
52 static 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);
117 static void parse_env(FILE *fd,
int loc)
123 while (
G_getl2(buf,
sizeof buf, fd)) {
124 for (
name = value = buf; *value; value++)
134 set_env(
name, value, loc);
138 static int read_env(
int loc)
143 if (loc == G_VAR_GISRC &&
st->varmode == G_GISRC_MODE_MEMORY)
149 if ((fd = open_env(
"r", loc))) {
163 static void force_read_env(
int loc)
166 if ((fd = open_env(
"r", loc))) {
173 static int set_env(
const char *
name,
const char *value,
int loc)
180 if (!value || !strlen(value)) {
181 unset_env(
name, loc);
189 unset_env(
name, loc);
199 for (n = 0; n <
st->env.count; n++) {
200 struct bind *
b = &
st->env.binds[n];
203 else if (strcmp(
b->name,
name) == 0 &&
b->loc == loc) {
211 struct bind *
b = &
st->env.binds[empty];
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++];
235 static int unset_env(
const char *
name,
int loc)
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) {
251 static const char *get_env(
const char *
name,
int loc)
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) &&
265 static void write_env(
int loc)
270 RETSIGTYPE (*sigint)(int);
272 RETSIGTYPE (*sigquit)(int);
275 if (loc == G_VAR_GISRC &&
st->varmode == G_GISRC_MODE_MEMORY)
282 sigint = signal(SIGINT, SIG_IGN);
284 sigquit = signal(SIGQUIT, SIG_IGN);
286 if ((fd = open_env(
"w", loc))) {
287 for (n = 0; n <
st->env.count; n++) {
288 struct bind *
b = &
st->env.binds[n];
289 if (
b->name &&
b->value &&
b->loc == loc
290 && (sscanf(
b->value,
"%1s", dummy) == 1))
291 fprintf(fd,
"%s: %s\n",
b->name,
b->value);
296 signal(SIGINT, sigint);
298 signal(SIGQUIT, sigquit);
302 static FILE *open_env(
const char *mode,
int loc)
306 if (loc == G_VAR_GISRC) {
308 st->gisrc = getenv(
"GISRC");
314 strcpy(buf,
st->gisrc);
316 else if (loc == G_VAR_MAPSET) {
319 read_env(G_VAR_GISRC);
324 return fopen(buf, mode);
383 if (strcmp(
name,
"GISBASE") == 0)
386 read_env(G_VAR_GISRC);
388 return get_env(
name, G_VAR_GISRC);
402 if (strcmp(
name,
"GISBASE") == 0)
407 return get_env(
name, loc);
420 read_env(G_VAR_GISRC);
421 set_env(
name, value, G_VAR_GISRC);
422 write_env(G_VAR_GISRC);
438 set_env(
name, value, loc);
450 read_env(G_VAR_GISRC);
451 set_env(
name, value, G_VAR_GISRC);
464 set_env(
name, value, loc);
476 read_env(G_VAR_GISRC);
477 unset_env(
name, G_VAR_GISRC);
478 write_env(G_VAR_GISRC);
492 unset_env(
name, loc);
501 if (
st->init[G_VAR_GISRC])
502 write_env(G_VAR_GISRC);
525 read_env(G_VAR_GISRC);
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;
538 st->init[G_VAR_GISRC] = 0;
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);
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)
void G_switch_env(void)
Switch environments.
const char * G_get_env_name(int n)
Get variable name for index n.
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_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_nofatal2(const char *name, int loc)
Get environment variable from specific place.
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)
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_nofatal(const char *name)
Get environment variable.
void G_init_env(void)
Initialize variables.
const char * G_getenv2(const char *name, int loc)
Get variable from specific place.
const char * G_getenv(const char *name)
Get environment variable.
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.