22 #include <grass/gis.h>
23 #include <grass/glocale.h>
25 #define ENV struct env
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;
41 static int read_env(
int);
42 static int set_env(
const char *,
const char *,
int);
43 static int unset_env(
const char *,
int);
44 static char *get_env(
const char *,
int);
45 static int write_env(
int);
46 static FILE *open_env(
const char *,
int);
76 static int read_env(
int loc)
84 if (loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY)
92 if ((fd = open_env(
"r", loc))) {
93 while (
G_getl2(buf,
sizeof buf, fd)) {
94 for (name = value = buf; *
value; value++)
104 set_env(name, value, loc);
112 static int set_env(
const char *name,
const char *value,
int loc)
119 if (!value || !strlen(value)) {
120 unset_env(name, loc);
128 unset_env(name, loc);
138 for (n = 0; n <
count; n++)
141 else if (strcmp(
env[n].name, name) == 0 &&
env[n].loc == loc) {
148 env[empty].loc = loc;
150 env[empty].value = tv;
156 env = (
ENV *) G_realloc((
char *)
env, count *
sizeof(
ENV));
167 static int unset_env(
const char *name,
int loc)
171 for (n = 0; n <
count; n++)
172 if (
env[n].name && (strcmp(
env[n].name, name) == 0) &&
182 static 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) &&
195 static int write_env(
int loc)
206 if (loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY)
213 sigint =
signal(SIGINT, SIG_IGN);
215 sigquit =
signal(SIGQUIT, SIG_IGN);
217 if ((fd = open_env(
"w", loc))) {
218 for (n = 0; n <
count; n++)
219 if (
env[n].name &&
env[n].value &&
env[n].loc == loc
220 && (sscanf(
env[n].value,
"%1s", dummy) == 1))
221 fprintf(fd,
"%s: %s\n",
env[n].name,
env[n].value);
233 static FILE *open_env(
const char *
mode,
int loc)
237 if (loc == G_VAR_GISRC) {
247 else if (loc == G_VAR_MAPSET) {
250 read_env(G_VAR_GISRC);
255 return fopen(buf, mode);
314 if (strcmp(name,
"GISBASE") == 0)
317 read_env(G_VAR_GISRC);
319 return get_env(name, G_VAR_GISRC);
333 if (strcmp(name,
"GISBASE") == 0)
338 return get_env(name, loc);
354 read_env(G_VAR_GISRC);
355 set_env(name, value, G_VAR_GISRC);
356 write_env(G_VAR_GISRC);
372 int G_setenv2(
const char *name,
const char *value,
int loc)
375 set_env(name, value, loc);
390 read_env(G_VAR_GISRC);
391 set_env(name, value, G_VAR_GISRC);
407 set_env(name, value, loc);
422 read_env(G_VAR_GISRC);
423 unset_env(name, G_VAR_GISRC);
424 write_env(G_VAR_GISRC);
441 unset_env(name, loc);
456 if (
init[G_VAR_GISRC])
457 write_env(G_VAR_GISRC);
480 read_env(G_VAR_GISRC);
482 for (i = 0; i <
count; i++)
483 if (
env[i].name && *
env[i].name && (n-- == 0))
497 init[G_VAR_GISRC] = 0;
547 for (i = 0; i < count2; i++)
548 if (env2[count].name)
549 set_env(env2[count].name, env2[count].value, G_VAR_GISRC);
char * G_mapset(void)
current mapset name
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
int G__set_gisrc_file(const char *name)
Sets filename for gisrc.
char * G__getenv(const char *name)
Get environment variable.
char * G_store(const char *s)
Copy string to allocated memory.
char * G__env_name(int n)
Get variable name for index n.
int G__setenv2(const char *name, const char *value, int loc)
Set environment name to value from specific place.
char * G__get_gisrc_file(void)
Get gisrc filename.
int G_unsetenv(const char *name)
Remove name from environment.
int G_unsetenv2(const char *name, int loc)
Remove name from environment from specific place.
char * G_getenv(const char *name)
Get environment variable.
int G_setenv2(const char *name, const char *value, int loc)
Set environment variable from specific place.
char * G__getenv2(const char *name, int loc)
Get environment variable from specific place.
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
char * G_location_path(void)
Get current location directory.
int G_get_gisrc_mode(void)
Get info where variables are stored.
void G_set_gisrc_mode(int mode)
Set where to find/store variables.
int G_strip(char *buf)
Removes all leading and trailing white space from string.
int G__write_env(void)
Writes current environment to .gisrc.
int G_setenv(const char *name, const char *value)
Set environment variable.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
char * G_getenv2(const char *name, int loc)
Read variable from specific place.
int G__setenv(const char *name, const char *value)
Set environment name to value.
int G__create_alt_env(void)
Set up alternative environment variables.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G__switch_env(void)
Switch environments.
int G__read_env(void)
Initialize init array for G_VAR_GISRC.