00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include <signal.h>
00054 #include <unistd.h>
00055 #include <stdlib.h>
00056 #include <unistd.h>
00057 #include <string.h>
00058 #include <grass/gis.h>
00059 #include <grass/glocale.h>
00060
00061 #define ENV struct env
00062 ENV
00063 {
00064 int loc;
00065 char *name;
00066 char *value;
00067 } ;
00068
00069 static ENV *env = NULL;
00070 static ENV *env2 = NULL;
00071 static int count = 0;
00072 static int count2 = 0;
00073 static int init[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00074 static char *gisrc = NULL;
00075 static int varmode = G_GISRC_MODE_FILE;
00076
00077 static int read_env( int );
00078 static int set_env ( char *, char *, int);
00079 static int unset_env ( char *, int);
00080 static char *get_env( char *, int);
00081 static int write_env ( int );
00082 static FILE *open_env ( char *, int);
00083
00084
00085 void G_set_gisrc_mode ( int mode )
00086 {
00087 varmode = mode;
00088 }
00089
00090
00091 int G_get_gisrc_mode ( void )
00092 {
00093 return ( varmode );
00094 }
00095
00096 static int
00097 read_env ( int loc )
00098 {
00099 char buf[200];
00100 char *name;
00101 char *value;
00102
00103 FILE *fd;
00104
00105 if ( loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY ) return 0;
00106
00107 if (init[loc])
00108 return 1;
00109
00110 init[loc] = 1;
00111
00112 if ((fd = open_env ("r", loc)))
00113 {
00114 while (G_getl (buf, sizeof buf, fd))
00115 {
00116 for (name = value = buf; *value; value++)
00117 if (*value == ':')
00118 break;
00119 if (*value == 0)
00120 continue;
00121
00122 *value++ = 0;
00123 G_strip (name);
00124 G_strip (value);
00125 if (*name && *value)
00126 set_env (name, value, loc);
00127 }
00128 fclose (fd);
00129 }
00130
00131 return 0;
00132 }
00133
00134 static int set_env ( char *name, char *value, int loc)
00135 {
00136 int n;
00137 int empty;
00138 char *tv;
00139
00140
00141 if(!value || !strlen(value))
00142 {
00143 unset_env (name, loc);
00144 return 0;
00145 }
00146
00147 tv = G_store (value);
00148 G_strip (tv);
00149 if (*tv == 0)
00150 {
00151 G_free (tv);
00152 unset_env (name, loc);
00153 return 1;
00154 }
00155
00156
00157
00158
00159
00160
00161 empty = -1;
00162 for (n = 0; n < count; n++)
00163 if (!env[n].name)
00164 empty = n;
00165 else if (strcmp (env[n].name, name) == 0 && env[n].loc == loc)
00166 {
00167 env[n].value = tv;
00168 return 1;
00169 }
00170
00171
00172 if (empty >= 0)
00173 {
00174 env[empty].loc = loc;
00175 env[empty].name = G_store (name);
00176 env[empty].value = tv;
00177 return 0;
00178 }
00179
00180
00181 if ((n = count++))
00182 env = (ENV *) G_realloc ((char *) env, count * sizeof (ENV));
00183 else
00184 env = (ENV *) G_malloc (sizeof (ENV));
00185
00186 env[n].loc = loc;
00187 env[n].name = G_store (name);
00188 env[n].value = tv;
00189
00190 return 0;
00191 }
00192
00193 static int unset_env (char *name, int loc)
00194 {
00195 int n;
00196
00197 for (n = 0; n < count; n++)
00198 if (env[n].name && (strcmp(env[n].name, name)==0) && env[n].loc == loc )
00199 {
00200 G_free (env[n].name);
00201 env[n].name = 0;
00202 return 1;
00203 }
00204
00205 return 0;
00206 }
00207
00208 static char *get_env( char *name, int loc)
00209 {
00210 int n;
00211
00212 for (n = 0; n < count; n++) {
00213 if (env[n].name && (strcmp(env[n].name, name)==0) && env[n].loc == loc)
00214 return env[n].value;
00215 }
00216
00217 return NULL;
00218 }
00219
00220 static int write_env ( int loc )
00221 {
00222 FILE *fd;
00223 int n;
00224 char dummy[2];
00225 void (*sigint)()
00226 #ifdef SIGQUIT
00227 , (*sigquit)()
00228 #endif
00229 ;
00230
00231 if ( loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY ) return 0;
00232
00233
00234
00235
00236
00237 sigint = signal (SIGINT, SIG_IGN);
00238 #ifdef SIGQUIT
00239 sigquit = signal (SIGQUIT, SIG_IGN);
00240 #endif
00241 if((fd = open_env ("w", loc)))
00242 {
00243 for (n = 0; n < count; n++)
00244 if (env[n].name && env[n].value && env[n].loc == loc
00245 && (sscanf (env[n].value,"%1s", dummy) == 1))
00246 fprintf(fd,"%s: %s\n", env[n].name, env[n].value);
00247 fclose (fd);
00248 }
00249
00250 signal (SIGINT, sigint);
00251 #ifdef SIGQUIT
00252 signal (SIGQUIT, sigquit);
00253 #endif
00254
00255 return 0;
00256 }
00257
00258 static FILE *open_env ( char *mode, int loc)
00259 {
00260 char buf[1000];
00261
00262 if ( loc == G_VAR_GISRC ) {
00263 if (!gisrc)
00264 gisrc = getenv ("GISRC");
00265
00266 if (!gisrc)
00267 {
00268 G_fatal_error(_("GISRC - variable not set"));
00269 return(NULL);
00270 }
00271 strcpy (buf, gisrc);
00272 } else if ( loc == G_VAR_MAPSET ) {
00273
00274
00275 read_env ( G_VAR_GISRC );
00276
00277 #ifdef __MINGW32__
00278 sprintf ( buf, "%s\\%s\\VAR", G_location_path(), G_mapset() );
00279 #else
00280 sprintf ( buf, "%s/%s/VAR", G_location_path(), G_mapset() );
00281 #endif
00282 }
00283
00284 return fopen (buf, mode);
00285 }
00286
00287 char *G_getenv( char *name)
00288 {
00289 char *value;
00290
00291 if ((value = G__getenv(name)))
00292 return value;
00293
00294 G_fatal_error(_("%s not set"), name);
00295 return NULL;
00296 }
00297
00298
00299 char *G_getenv2( char *name, int loc )
00300 {
00301 char *value;
00302
00303 if ((value = G__getenv2(name, loc)))
00304 return value;
00305
00306 G_fatal_error(_("%s not set"), name);
00307 return NULL;
00308 }
00309
00310 char *G__getenv ( char *name)
00311 {
00312 if (strcmp (name, "GISBASE") == 0)
00313 return getenv (name);
00314
00315 read_env(G_VAR_GISRC);
00316
00317 return get_env (name, G_VAR_GISRC);
00318 }
00319
00320 char *G__getenv2 ( char *name, int loc)
00321 {
00322 if (strcmp (name, "GISBASE") == 0)
00323 return getenv (name);
00324
00325 read_env( loc );
00326
00327 return get_env (name, loc);
00328 }
00329
00330 int G_setenv (char *name, char *value)
00331 {
00332 read_env(G_VAR_GISRC);
00333 set_env (name, value, G_VAR_GISRC );
00334 write_env(G_VAR_GISRC);
00335 return 0;
00336 }
00337
00338 int G_setenv2 (char *name, char *value, int loc)
00339 {
00340 read_env(loc);
00341 set_env (name, value, loc);
00342 write_env(loc);
00343 return 0;
00344 }
00345
00346 int G__setenv ( char *name, char *value)
00347 {
00348 read_env(G_VAR_GISRC);
00349 set_env (name, value, G_VAR_GISRC );
00350 return 0;
00351 }
00352
00353 int G__setenv2 (char *name, char *value, int loc)
00354 {
00355 read_env(loc);
00356 set_env (name, value, loc);
00357 return 0;
00358 }
00359
00360 int G_unsetenv ( char *name)
00361 {
00362 read_env(G_VAR_GISRC);
00363 unset_env (name, G_VAR_GISRC);
00364 write_env(G_VAR_GISRC);
00365
00366 return 0;
00367 }
00368
00369 int G_unsetenv2 ( char *name, int loc)
00370 {
00371 read_env(loc);
00372 unset_env (name, loc);
00373 write_env(loc);
00374
00375 return 0;
00376 }
00377
00378 int
00379 G__write_env (void)
00380 {
00381 if (init[G_VAR_GISRC])
00382 write_env(G_VAR_GISRC);
00383
00384 return 0;
00385 }
00386
00387 char *G__env_name (int n)
00388 {
00389 int i;
00390
00391 read_env(G_VAR_GISRC);
00392 if (n >= 0)
00393 for (i = 0; i < count; i++)
00394 if (env[i].name && *env[i].name && (n-- == 0))
00395 return env[i].name;
00396 return NULL;
00397 }
00398
00399 int G__read_env (void)
00400 {
00401 init[G_VAR_GISRC] = 0;
00402
00403 return 0;
00404 }
00405
00406 int G__set_gisrc_file( char *name)
00407 {
00408 gisrc = NULL;
00409 if (name && *name)
00410 gisrc = G_store(name);
00411
00412 return 0;
00413 }
00414
00415 char *G__get_gisrc_file (void)
00416 {
00417 return gisrc;
00418 }
00419
00420 int G__create_alt_env (void)
00421 {
00422 int i;
00423
00424
00425 env2 = env;
00426 count2 = count;
00427 env = NULL;
00428 count = 0;
00429
00430 for (i=0; i < count2; i++)
00431 if (env2[count].name)
00432 set_env (env2[count].name, env2[count].value, G_VAR_GISRC);
00433
00434 return 0;
00435 }
00436
00437 int G__switch_env (void)
00438 {
00439 ENV *tmp;
00440 int n;
00441
00442 n = count;
00443 tmp = env;
00444
00445 env = env2;
00446 count = count2;
00447
00448 env2 = tmp;
00449 count2 = n;
00450
00451 return 0;
00452 }