1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
/*
****************************************************************
* GIS environment routines
*
***************************************************************
* char *
* G_getenv (name)
*
* returns char pointer to value for name
* dies if name not set
*
***************************************************************
* G_setenv (name, value)
*
* sets environment name to value
* if value is NULL, becomes an G_unsetenv (name)
* updates .gisrc
*
***************************************************************
* G_unsetenv (name)
*
* remove name from environment
* updates .gisrc
*
***************************************************************
* char *
* G__getenv (name)
*
* returns char pointer to value for name
* returns NULL if name not set
*
***************************************************************
* G__setenv (name, value)
*
* sets environment name to value
* does NOT update .gisrc
*
***************************************************************
* G__write_env()
*
* writes current environment to .gisrc
***************************************************************
* char *
* G__env_name (n)
*
* returns environment variable name n
* for eample:
* for (n = 0; ; n++)
* if ((name = G__env_name(n)) == NULL)
* break;
***************************************************************/
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h> /* for sleep() */
#include <string.h>
#include "gis.h"
#include "glocale.h"
#define ENV struct env
ENV
{
int loc;
char *name;
char *value;
} ;
static ENV *env = NULL;
static ENV *env2 = NULL;
static int count = 0;
static int count2 = 0;
static int init[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static char *gisrc = NULL;
static int varmode = G_GISRC_MODE_FILE; /* where find/store variables */
static int read_env( int );
static int set_env ( char *, char *, int);
static int unset_env ( char *, int);
static char *get_env( char *, int);
static int write_env ( int );
static FILE *open_env ( char *, int);
/* Set where to find/store variables */
void G_set_gisrc_mode ( int mode )
{
varmode = mode;
}
/* Get info where variables are stored */
int G_get_gisrc_mode ( void )
{
return ( varmode );
}
static int
read_env ( int loc )
{
char buf[200];
char *name;
char *value;
FILE *fd;
if ( loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY ) return 0; /* don't use file for GISRC */
if (init[loc])
return 1;
init[loc] = 1;
if ((fd = open_env ("r", loc)))
{
while (G_getl (buf, sizeof buf, fd))
{
for (name = value = buf; *value; value++)
if (*value == ':')
break;
if (*value == 0)
continue;
*value++ = 0;
G_strip (name);
G_strip (value);
if (*name && *value)
set_env (name, value, loc);
}
fclose (fd);
}
return 0;
}
static int set_env ( char *name, char *value, int loc)
{
int n;
int empty;
char *tv;
/* if value is NULL or empty string, convert into an unsetenv() */
if(!value || !strlen(value))
{
unset_env (name, loc);
return 0;
}
tv = G_store (value);
G_strip (tv);
if (*tv == 0)
{
free (tv);
unset_env (name, loc);
return 1;
}
/*
* search the array
* keep track of first empty slot
* and look for name in the environment
*/
empty = -1;
for (n = 0; n < count; n++)
if (!env[n].name) /* mark empty slot found */
empty = n;
else if (strcmp (env[n].name, name) == 0 && env[n].loc == loc)
{
env[n].value = tv;
return 1;
}
/* add name to env: to empty slot if any */
if (empty >= 0)
{
env[empty].loc = loc;
env[empty].name = G_store (name);
env[empty].value = tv;
return 0;
}
/* must increase the env list and add in */
if ((n = count++))
env = (ENV *) G_realloc ((char *) env, count * sizeof (ENV));
else
env = (ENV *) G_malloc (sizeof (ENV));
env[n].loc = loc;
env[n].name = G_store (name);
env[n].value = tv;
return 0;
}
static int unset_env (char *name, int loc)
{
int n;
for (n = 0; n < count; n++)
if (env[n].name && (strcmp(env[n].name, name)==0) && env[n].loc == loc )
{
free (env[n].name);
env[n].name = 0;
return 1;
}
return 0;
}
static char *get_env( char *name, int loc)
{
int n;
for (n = 0; n < count; n++) {
if (env[n].name && (strcmp(env[n].name, name)==0) && env[n].loc == loc)
return env[n].value;
}
return NULL;
}
static int write_env ( int loc )
{
FILE *fd;
int n;
char dummy[2];
void (*sigint)()
#ifdef SIGQUIT
, (*sigquit)()
#endif
;
if ( loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY ) return 0; /* don't use file for GISRC */
/*
* THIS CODE NEEDS TO BE PROTECTED FROM INTERRUPTS
* If interrupted, it can wipe out the GISRC file
*/
sigint = signal (SIGINT, SIG_IGN);
#ifdef SIGQUIT
sigquit = signal (SIGQUIT, SIG_IGN);
#endif
if((fd = open_env ("w", loc)))
{
for (n = 0; n < count; n++)
if (env[n].name && env[n].value && env[n].loc == loc
&& (sscanf (env[n].value,"%1s", dummy) == 1))
fprintf(fd,"%s: %s\n", env[n].name, env[n].value);
fclose (fd);
}
signal (SIGINT, sigint);
#ifdef SIGQUIT
signal (SIGQUIT, sigquit);
#endif
return 0;
}
static FILE *open_env ( char *mode, int loc)
{
char buf[1000];
if ( loc == G_VAR_GISRC ) {
if (!gisrc)
gisrc = getenv ("GISRC");
if (!gisrc)
{
G_fatal_error(_("GISRC - variable not set"));
return(NULL);
}
strcpy (buf, gisrc);
} else if ( loc == G_VAR_MAPSET ) {
sprintf ( buf, "%s/%s/VAR", G_location_path(), G_mapset() );
}
return fopen (buf, mode);
}
char *G_getenv( char *name)
{
char *value;
if ((value = G__getenv(name)))
return value;
G_fatal_error(_("%s not set"), name);
return NULL;
}
/* Read variable from specific place */
char *G_getenv2( char *name, int loc )
{
char *value;
if ((value = G__getenv2(name, loc)))
return value;
G_fatal_error(_("%s not set"), name);
return NULL;
}
char *G__getenv ( char *name)
{
if (strcmp (name, "GISBASE") == 0)
return getenv (name);
read_env(G_VAR_GISRC);
return get_env (name, G_VAR_GISRC);
}
char *G__getenv2 ( char *name, int loc)
{
if (strcmp (name, "GISBASE") == 0)
return getenv (name);
read_env( loc );
return get_env (name, loc);
}
int G_setenv (char *name, char *value)
{
read_env(G_VAR_GISRC);
set_env (name, value, G_VAR_GISRC );
write_env(G_VAR_GISRC);
return 0;
}
int G_setenv2 (char *name, char *value, int loc)
{
read_env(loc);
set_env (name, value, loc);
write_env(loc);
return 0;
}
int G__setenv ( char *name, char *value)
{
read_env(G_VAR_GISRC);
set_env (name, value, G_VAR_GISRC );
return 0;
}
int G__setenv2 (char *name, char *value, int loc)
{
read_env(loc);
set_env (name, value, loc);
return 0;
}
int G_unsetenv ( char *name)
{
read_env(G_VAR_GISRC);
unset_env (name, G_VAR_GISRC);
write_env(G_VAR_GISRC);
return 0;
}
int G_unsetenv2 ( char *name, int loc)
{
read_env(loc);
unset_env (name, loc);
write_env(loc);
return 0;
}
int
G__write_env (void)
{
if (init[G_VAR_GISRC])
write_env(G_VAR_GISRC);
return 0;
}
char *G__env_name (int n)
{
int i;
read_env(G_VAR_GISRC);
if (n >= 0)
for (i = 0; i < count; i++)
if (env[i].name && *env[i].name && (n-- == 0))
return env[i].name;
return NULL;
}
int G__read_env (void)
{
init[G_VAR_GISRC] = 0;
return 0;
}
int G__set_gisrc_file( char *name)
{
gisrc = NULL;
if (name && *name)
gisrc = G_store(name);
return 0;
}
char *G__get_gisrc_file (void)
{
return gisrc;
}
int G__create_alt_env (void)
{
int i;
/* copy env to env2 */
env2 = env;
count2 = count;
env = NULL;
count = 0;
for (i=0; i < count2; i++)
if (env2[count].name)
set_env (env2[count].name, env2[count].value, G_VAR_GISRC);
return 0;
}
int G__switch_env (void)
{
ENV *tmp;
int n;
n = count;
tmp = env;
env = env2;
count = count2;
env2 = tmp;
count2 = n;
return 0;
}
|