env.c

Go to the documentation of this file.
00001 /*
00002  ****************************************************************
00003  * GIS environment routines
00004  *
00005  ***************************************************************
00006  * char *
00007  * G_getenv (name)
00008  *
00009  *   returns char pointer to value for name
00010  *   dies if name not set
00011  *
00012  ***************************************************************
00013  * G_setenv (name, value)
00014  *
00015  *   sets environment name to value
00016  *     if value is NULL, becomes an G_unsetenv (name)
00017  *   updates .gisrc
00018  *
00019  ***************************************************************
00020  * G_unsetenv (name)
00021  *
00022  *   remove name from environment
00023  *   updates .gisrc
00024  *
00025  ***************************************************************
00026  * char *
00027  * G__getenv (name)
00028  *
00029  *   returns char pointer to value for name
00030  *   returns NULL if name not set
00031  *
00032  ***************************************************************
00033  * G__setenv (name, value)
00034  *
00035  *   sets environment name to value
00036  *   does NOT update .gisrc
00037  *
00038  ***************************************************************
00039  * G__write_env()
00040  *
00041  *   writes current environment to .gisrc
00042  ***************************************************************
00043  * char *
00044  * G__env_name (n)
00045  *
00046  *   returns environment variable name n
00047  *   for eample:
00048  *     for (n = 0; ; n++)
00049  *       if ((name = G__env_name(n)) == NULL)
00050  *          break;
00051  ***************************************************************/
00052 
00053 #include <signal.h>
00054 #include <unistd.h>
00055 #include <stdlib.h>
00056 #include <unistd.h>   /* for sleep() */
00057 #include <string.h>
00058 #include "gis.h"
00059 #include "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; /* where find/store variables */ 
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 /* Set where to find/store variables */
00085 void G_set_gisrc_mode ( int mode )
00086 {
00087     varmode = mode; 
00088 }
00089 
00090 /* Get info where variables are stored */
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; /* don't use file for GISRC */
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 /* if value is NULL or empty string, convert into an unsetenv() */
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         free (tv);
00152         unset_env (name, loc);
00153         return 1;
00154     }
00155 
00156 /*
00157 * search the array
00158 *   keep track of first empty slot
00159 *   and look for name in the environment
00160 */
00161     empty = -1;
00162     for (n = 0; n < count; n++)
00163         if (!env[n].name)       /* mark empty slot found */
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 /* add name to env: to empty slot if any */
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 /* must increase the env list and add in */
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             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; /* don't use file for GISRC */
00232 
00233 /*
00234  * THIS CODE NEEDS TO BE PROTECTED FROM INTERRUPTS
00235  * If interrupted, it can wipe out the GISRC file
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         sprintf ( buf, "%s/%s/VAR", G_location_path(), G_mapset() );
00274     }
00275                 
00276     return fopen (buf, mode);
00277 }
00278 
00279 char *G_getenv( char *name)
00280 {
00281     char *value;
00282 
00283     if ((value = G__getenv(name)))
00284         return value;
00285 
00286     G_fatal_error(_("%s not set"), name);
00287     return NULL;
00288 }
00289 
00290 /* Read variable from specific place */
00291 char *G_getenv2( char *name, int loc )
00292 {
00293     char *value;
00294 
00295     if ((value = G__getenv2(name, loc)))
00296         return value;
00297 
00298     G_fatal_error(_("%s not set"), name);
00299     return NULL;
00300 }
00301 
00302 char *G__getenv ( char *name)
00303 {
00304     if (strcmp (name, "GISBASE") == 0)
00305        return getenv (name);
00306 
00307     read_env(G_VAR_GISRC);
00308 
00309     return get_env (name, G_VAR_GISRC);
00310 }
00311 
00312 char *G__getenv2 ( char *name, int loc)
00313 {
00314     if (strcmp (name, "GISBASE") == 0)
00315         return getenv (name);
00316 
00317     read_env( loc );
00318 
00319     return get_env (name, loc);
00320 }
00321 
00322 int G_setenv (char *name, char *value)
00323 {
00324     read_env(G_VAR_GISRC);
00325     set_env (name, value, G_VAR_GISRC );
00326     write_env(G_VAR_GISRC);
00327     return 0;
00328 }
00329 
00330 int G_setenv2 (char *name, char *value, int loc)
00331 {
00332     read_env(loc);
00333     set_env (name, value, loc);
00334     write_env(loc);
00335     return 0;
00336 }
00337 
00338 int G__setenv ( char *name, char *value)
00339 {
00340     read_env(G_VAR_GISRC);
00341     set_env (name, value, G_VAR_GISRC );
00342     return 0;
00343 }
00344 
00345 int G__setenv2 (char *name, char *value, int loc)
00346 {
00347     read_env(loc);
00348     set_env (name, value, loc);
00349     return 0;
00350 }
00351 
00352 int G_unsetenv ( char *name)
00353 {
00354     read_env(G_VAR_GISRC);
00355     unset_env (name, G_VAR_GISRC);
00356     write_env(G_VAR_GISRC);
00357 
00358     return 0;
00359 }
00360 
00361 int G_unsetenv2 ( char *name, int loc)
00362 {
00363     read_env(loc);
00364     unset_env (name, loc);
00365     write_env(loc);
00366 
00367     return 0;
00368 }
00369 
00370 int 
00371 G__write_env (void)
00372 {
00373     if (init[G_VAR_GISRC])
00374         write_env(G_VAR_GISRC);
00375 
00376     return 0;
00377 }
00378 
00379 char *G__env_name (int n)
00380 {
00381     int i;
00382 
00383     read_env(G_VAR_GISRC);
00384     if (n >= 0)
00385         for (i = 0; i < count; i++)
00386             if (env[i].name && *env[i].name && (n-- == 0))
00387                 return env[i].name;
00388     return NULL;
00389 }
00390 
00391 int G__read_env (void)
00392 {
00393     init[G_VAR_GISRC] = 0;
00394 
00395     return 0;
00396 }
00397 
00398 int G__set_gisrc_file( char *name)
00399 {
00400     gisrc = NULL;
00401     if (name && *name)
00402         gisrc = G_store(name);
00403 
00404     return 0;
00405 }
00406 
00407 char *G__get_gisrc_file (void)
00408 {
00409     return gisrc;
00410 }
00411 
00412 int G__create_alt_env (void)
00413 {
00414     int i;
00415 
00416 /* copy env to env2 */
00417     env2 = env;
00418     count2 = count;
00419     env = NULL;
00420     count = 0;
00421 
00422     for (i=0; i < count2; i++)
00423         if (env2[count].name)
00424             set_env (env2[count].name, env2[count].value, G_VAR_GISRC);
00425 
00426     return 0;
00427 }
00428 
00429 int G__switch_env (void)
00430 {
00431     ENV *tmp;
00432     int n;
00433 
00434     n   = count;
00435     tmp = env;
00436 
00437     env   = env2;
00438     count = count2;
00439 
00440     env2   = tmp;
00441     count2 = n;
00442 
00443     return 0;
00444 }

Generated on Mon Jan 1 19:49:25 2007 for GRASS by  doxygen 1.5.1