GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
env.c
Go to the documentation of this file.
1 
17 #include <signal.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <unistd.h> /* for sleep() */
21 #include <string.h>
22 #include <grass/gis.h>
23 #include <grass/glocale.h>
24 
25 #define ENV struct env
26 
27 ENV {
28  int loc;
29  char *name;
30  char *value;
31 };
32 
33 static ENV *env = NULL;
34 static ENV *env2 = NULL;
35 static int count = 0;
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; /* where find/store variables */
40 
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);
47 
60 {
61  varmode = mode;
62 }
63 
72 {
73  return (varmode);
74 }
75 
76 static int read_env(int loc)
77 {
78  char buf[200];
79  char *name;
80  char *value;
81 
82  FILE *fd;
83 
84  if (loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY)
85  return 0; /* don't use file for GISRC */
86 
87  if (init[loc])
88  return 1;
89 
90  init[loc] = 1;
91 
92  if ((fd = open_env("r", loc))) {
93  while (G_getl2(buf, sizeof buf, fd)) {
94  for (name = value = buf; *value; value++)
95  if (*value == ':')
96  break;
97  if (*value == 0)
98  continue;
99 
100  *value++ = 0;
101  G_strip(name);
102  G_strip(value);
103  if (*name && *value)
104  set_env(name, value, loc);
105  }
106  fclose(fd);
107  }
108 
109  return 0;
110 }
111 
112 static int set_env(const char *name, const char *value, int loc)
113 {
114  int n;
115  int empty;
116  char *tv;
117 
118  /* if value is NULL or empty string, convert into an unsetenv() */
119  if (!value || !strlen(value)) {
120  unset_env(name, loc);
121  return 0;
122  }
123 
124  tv = G_store(value);
125  G_strip(tv);
126  if (*tv == 0) {
127  G_free(tv);
128  unset_env(name, loc);
129  return 1;
130  }
131 
132  /*
133  * search the array
134  * keep track of first empty slot
135  * and look for name in the environment
136  */
137  empty = -1;
138  for (n = 0; n < count; n++)
139  if (!env[n].name) /* mark empty slot found */
140  empty = n;
141  else if (strcmp(env[n].name, name) == 0 && env[n].loc == loc) {
142  env[n].value = tv;
143  return 1;
144  }
145 
146  /* add name to env: to empty slot if any */
147  if (empty >= 0) {
148  env[empty].loc = loc;
149  env[empty].name = G_store(name);
150  env[empty].value = tv;
151  return 0;
152  }
153 
154  /* must increase the env list and add in */
155  if ((n = count++))
156  env = (ENV *) G_realloc((char *)env, count * sizeof(ENV));
157  else
158  env = (ENV *) G_malloc(sizeof(ENV));
159 
160  env[n].loc = loc;
161  env[n].name = G_store(name);
162  env[n].value = tv;
163 
164  return 0;
165 }
166 
167 static int unset_env(const char *name, int loc)
168 {
169  int n;
170 
171  for (n = 0; n < count; n++)
172  if (env[n].name && (strcmp(env[n].name, name) == 0) &&
173  env[n].loc == loc) {
174  G_free(env[n].name);
175  env[n].name = 0;
176  return 1;
177  }
178 
179  return 0;
180 }
181 
182 static char *get_env(const char *name, int loc)
183 {
184  int n;
185 
186  for (n = 0; n < count; n++) {
187  if (env[n].name && (strcmp(env[n].name, name) == 0) &&
188  env[n].loc == loc)
189  return env[n].value;
190  }
191 
192  return NULL;
193 }
194 
195 static int write_env(int loc)
196 {
197  FILE *fd;
198  int n;
199  char dummy[2];
200  void (*sigint) ()
201 #ifdef SIGQUIT
202  , (*sigquit) ()
203 #endif
204  ;
205 
206  if (loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY)
207  return 0; /* don't use file for GISRC */
208 
209  /*
210  * THIS CODE NEEDS TO BE PROTECTED FROM INTERRUPTS
211  * If interrupted, it can wipe out the GISRC file
212  */
213  sigint = signal(SIGINT, SIG_IGN);
214 #ifdef SIGQUIT
215  sigquit = signal(SIGQUIT, SIG_IGN);
216 #endif
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);
222  fclose(fd);
223  }
224 
225  signal(SIGINT, sigint);
226 #ifdef SIGQUIT
227  signal(SIGQUIT, sigquit);
228 #endif
229 
230  return 0;
231 }
232 
233 static FILE *open_env(const char *mode, int loc)
234 {
235  char buf[1000];
236 
237  if (loc == G_VAR_GISRC) {
238  if (!gisrc)
239  gisrc = getenv("GISRC");
240 
241  if (!gisrc) {
242  G_fatal_error(_("GISRC - variable not set"));
243  return (NULL);
244  }
245  strcpy(buf, gisrc);
246  }
247  else if (loc == G_VAR_MAPSET) {
248  /* Warning: G_VAR_GISRC must be previously read -> */
249  /* TODO: better place ? */
250  read_env(G_VAR_GISRC);
251 
252  sprintf(buf, "%s/%s/VAR", G_location_path(), G_mapset());
253  }
254 
255  return fopen(buf, mode);
256 }
257 
267 char *G_getenv(const char *name)
268 {
269  char *value;
270 
271  if ((value = G__getenv(name)))
272  return value;
273 
274  G_fatal_error(_("G_getenv(): Variable %s not set"), name);
275  return NULL;
276 }
277 
293 char *G_getenv2(const char *name, int loc)
294 {
295  char *value;
296 
297  if ((value = G__getenv2(name, loc)))
298  return value;
299 
300  G_fatal_error(_("%s not set"), name);
301  return NULL;
302 }
303 
312 char *G__getenv(const char *name)
313 {
314  if (strcmp(name, "GISBASE") == 0)
315  return getenv(name);
316 
317  read_env(G_VAR_GISRC);
318 
319  return get_env(name, G_VAR_GISRC);
320 }
321 
331 char *G__getenv2(const char *name, int loc)
332 {
333  if (strcmp(name, "GISBASE") == 0)
334  return getenv(name);
335 
336  read_env(loc);
337 
338  return get_env(name, loc);
339 }
340 
352 int G_setenv(const char *name, const char *value)
353 {
354  read_env(G_VAR_GISRC);
355  set_env(name, value, G_VAR_GISRC);
356  write_env(G_VAR_GISRC);
357  return 0;
358 }
359 
372 int G_setenv2(const char *name, const char *value, int loc)
373 {
374  read_env(loc);
375  set_env(name, value, loc);
376  write_env(loc);
377  return 0;
378 }
379 
388 int G__setenv(const char *name, const char *value)
389 {
390  read_env(G_VAR_GISRC);
391  set_env(name, value, G_VAR_GISRC);
392  return 0;
393 }
394 
404 int G__setenv2(const char *name, const char *value, int loc)
405 {
406  read_env(loc);
407  set_env(name, value, loc);
408  return 0;
409 }
410 
420 int G_unsetenv(const char *name)
421 {
422  read_env(G_VAR_GISRC);
423  unset_env(name, G_VAR_GISRC);
424  write_env(G_VAR_GISRC);
425 
426  return 0;
427 }
428 
438 int G_unsetenv2(const char *name, int loc)
439 {
440  read_env(loc);
441  unset_env(name, loc);
442  write_env(loc);
443 
444  return 0;
445 }
446 
454 int G__write_env(void)
455 {
456  if (init[G_VAR_GISRC])
457  write_env(G_VAR_GISRC);
458 
459  return 0;
460 }
461 
476 char *G__env_name(int n)
477 {
478  int i;
479 
480  read_env(G_VAR_GISRC);
481  if (n >= 0)
482  for (i = 0; i < count; i++)
483  if (env[i].name && *env[i].name && (n-- == 0))
484  return env[i].name;
485  return NULL;
486 }
487 
495 int G__read_env(void)
496 {
497  init[G_VAR_GISRC] = 0;
498 
499  return 0;
500 }
501 
509 int G__set_gisrc_file(const char *name)
510 {
511  gisrc = NULL;
512  if (name && *name)
513  gisrc = G_store(name);
514 
515  return 0;
516 }
517 
525 char *G__get_gisrc_file(void)
526 {
527  return gisrc;
528 }
529 
538 {
539  int i;
540 
541  /* copy env to env2 */
542  env2 = env;
543  count2 = count;
544  env = NULL;
545  count = 0;
546 
547  for (i = 0; i < count2; i++)
548  if (env2[count].name)
549  set_env(env2[count].name, env2[count].value, G_VAR_GISRC);
550 
551  return 0;
552 }
553 
561 int G__switch_env(void)
562 {
563  ENV *tmp;
564  int n;
565 
566  n = count;
567  tmp = env;
568 
569  env = env2;
570  count = count2;
571 
572  env2 = tmp;
573  count2 = n;
574 
575  return 0;
576 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G__set_gisrc_file(const char *name)
Sets filename for gisrc.
Definition: env.c:509
char * G__getenv(const char *name)
Get environment variable.
Definition: env.c:312
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
char * G__env_name(int n)
Get variable name for index n.
Definition: env.c:476
#define ENV
Definition: env.c:25
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
int G__setenv2(const char *name, const char *value, int loc)
Set environment name to value from specific place.
Definition: env.c:404
int count
char * G__get_gisrc_file(void)
Get gisrc filename.
Definition: env.c:525
int G_unsetenv(const char *name)
Remove name from environment.
Definition: env.c:420
int G_unsetenv2(const char *name, int loc)
Remove name from environment from specific place.
Definition: env.c:438
tuple env
char * G_getenv(const char *name)
Get environment variable.
Definition: env.c:267
char * getenv()
int G_setenv2(const char *name, const char *value, int loc)
Set environment variable from specific place.
Definition: env.c:372
char * G__getenv2(const char *name, int loc)
Get environment variable from specific place.
Definition: env.c:331
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
Definition: getl.c:52
char * G_location_path(void)
Get current location directory.
Definition: location.c:37
int G_get_gisrc_mode(void)
Get info where variables are stored.
Definition: env.c:71
void G_set_gisrc_mode(int mode)
Set where to find/store variables.
Definition: env.c:59
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
char * value
Definition: env.c:30
int G__write_env(void)
Writes current environment to .gisrc.
Definition: env.c:454
int G_setenv(const char *name, const char *value)
Set environment variable.
Definition: env.c:352
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
char * G_getenv2(const char *name, int loc)
Read variable from specific place.
Definition: env.c:293
int G__setenv(const char *name, const char *value)
Set environment name to value.
Definition: env.c:388
fclose(fd)
int G__create_alt_env(void)
Set up alternative environment variables.
Definition: env.c:537
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: spawn.c:69
int G__switch_env(void)
Switch environments.
Definition: env.c:561
int n
Definition: dataquad.c:291
int G__read_env(void)
Initialize init array for G_VAR_GISRC.
Definition: env.c:495
tuple mode
Definition: tools.py:1481
void init(double work[])
Definition: as177.c:65