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);