GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-761b3a08c9
gisinit.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/gisinit.c
3 
4  \brief GIS Library - Handles program initialization.
5 
6  (C) 2001-2008, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author GRASS GIS Development Team
12  */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <locale.h>
22 
23 #include <grass/gis.h>
24 #include <grass/glocale.h>
25 
26 #include "G.h"
27 #include "gis_local_proto.h"
28 
29 struct G__ G__;
30 
31 static int initialized = 0; /** Is set when engine is initialized */
32 static int gisinit(void);
33 
34 /*!
35  \brief Initialize GIS Library and ensures a valid mapset is available.
36 
37  \param version
38  \param pgm program (module) name
39 
40  \return always returns 0 on success
41  \return G_fatal_error() is called on error
42  */
43 void G__gisinit(const char *version, const char *pgm)
44 {
45  const char *mapset;
46 
47  if (initialized)
48  return;
49 
50  G_set_program_name(pgm);
51 
52  /* verify version of GRASS headers (and anything else in include) */
53  if (strcmp(version, GIS_H_VERSION) != 0) {
54  char *envstr;
55  char *answer = "0";
56 
57  envstr = getenv("GRASS_COMPATIBILITY_TEST");
58  if (envstr && *envstr && strcmp(envstr, answer) == 0) {
59  G_warning(_("Module built against version %s but "
60  "trying to use version %s. "
61  "In case of errors you need to rebuild the module "
62  "against GRASS GIS version %s."),
64  }
65  else {
67  _("Module built against version %s but "
68  "trying to use version %s. "
69  "You need to rebuild GRASS GIS or untangle multiple "
70  "installations."),
71  version, GIS_H_VERSION);
72  }
73  }
74 
75  /* Make sure location and mapset are set */
77  mapset = G_mapset();
78  switch (G_mapset_permissions(mapset)) {
79  case 1:
80  break;
81  case 0:
82  G_fatal_error(_("MAPSET %s - permission denied"), mapset);
83  break;
84  default:
85  G_fatal_error(_("MAPSET %s not found at %s"), mapset,
86  G_location_path());
87  break;
88  }
89 
90  gisinit();
91 }
92 
93 /*!
94  \brief Initialize GIS Library
95 
96  Initializes GIS engine, but does not check for a valid mapset.
97  */
98 void G__no_gisinit(const char *version)
99 {
100  if (initialized)
101  return;
102 
103  /* verify version of GRASS headers (and anything else in include) */
104  if (strcmp(version, GIS_H_VERSION) != 0) {
105  char *envstr;
106  char *answer = "0";
107 
108  envstr = getenv("GRASS_COMPATIBILITY_TEST");
109  if (envstr && *envstr && strcmp(envstr, answer) == 0) {
110  G_warning(_("Module built against version %s but "
111  "trying to use version %s. "
112  "In case of errors you need to rebuild the module "
113  "against GRASS GIS version %s."),
115  }
116  else {
118  _("Module built against version %s but "
119  "trying to use version %s. "
120  "You need to rebuild GRASS GIS or untangle multiple "
121  "installations."),
122  version, GIS_H_VERSION);
123  }
124  }
125  gisinit();
126 }
127 
128 /*!
129  \brief Checks to see if GIS engine is initialized.
130  */
132 {
133  if (initialized)
134  return;
135  G_warning(
136  _("System not initialized. Programmer forgot to call G_gisinit()."));
137  G_sleep(3);
138  exit(EXIT_FAILURE);
139 }
140 
141 static int gisinit(void)
142 {
143  char *zlib;
144 
145 #ifdef __MINGW32__
146  _fmode = O_BINARY;
147 #endif
148  /* Mark window as not set */
149  G__.window_set = 0;
150 
151  /* byte order */
153 
154  zlib = getenv("GRASS_ZLIB_LEVEL");
155  /* Valid zlib compression levels -1 - 9 */
156  /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
157  * level 0 means no compression
158  * as used here, 1 gives the best compromise between speed and compression
159  */
160  G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : 1;
161  if (G__.compression_level < -1 || G__.compression_level > 9)
163 
164  initialized = 1;
165 
166  setlocale(LC_NUMERIC, "C");
167 
168  return 0;
169 }
170 
171 /*!
172  \brief Initialize environment
173  */
174 void G_init_all(void)
175 {
177  G_init_env();
178  G_init_logging();
179  G__init_window();
180  G_init_locale();
181  G_init_debug();
182  G_verbose();
183  G_init_tempfile();
185  G__home();
186  G__machine_name();
187  G_whoami();
190 }
int G_mapset_permissions(const char *)
Check for user mapset permission.
Definition: mapset_msc.c:291
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void int G_is_little_endian(void)
Tests for little ENDIAN.
Definition: endian.c:24
const char * G_whoami(void)
Gets user's name.
Definition: gis/whoami.c:35
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
void G_read_datum_table(void)
Definition: gis/datum.c:141
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:60
void G_sleep(unsigned int)
Definition: sleep.c:11
void G_set_program_name(const char *)
Set program name.
Definition: progrm_nme.c:61
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
void G__init_window(void)
Initialize window (region).
int G_read_ellipsoid_table(int)
Read ellipsoid table.
Definition: get_ellipse.c:246
void G_init_env(void)
Initialize variables.
Definition: env.c:83
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition: tempfile.c:29
void G_init_logging(void)
Definition: gis/error.c:355
void G_init_debug(void)
Initiate debugging.
Definition: debug.c:27
void G_init_locale(void)
Definition: locale.c:29
int _fmode
Definition: fmode.c:4
#define GIS_H_VERSION
Definition: gis.h:66
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:131
void G_init_all(void)
Initialize environment.
Definition: gisinit.c:174
void G__no_gisinit(const char *version)
Initialize GIS Library.
Definition: gisinit.c:98
void G__gisinit(const char *version, const char *pgm)
Initialize GIS Library and ensures a valid mapset is available.
Definition: gisinit.c:43
#define _(str)
Definition: glocale.h:10
const char * G__home(void)
Get user's home directory (internal use only)
Definition: home.c:53
const char * G__machine_name(void)
Definition: mach_name.c:17
void G__get_list_of_mapsets(void)
Fill list of mapsets from search path (internal use only)
Definition: mapset_nme.c:57
Definition: G.h:5
int compression_level
Definition: G.h:9
int little_endian
Definition: G.h:8
int window_set
Definition: G.h:7
#define GRASS_VERSION_STRING
Definition: version.h:1