GRASS 8 Programmer's Manual  8.5.0dev(2025)-e5e60895a1
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 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 #if 0
30 #ifdef GRASS_CMAKE_BUILD
31 #include <export/grass_gis_export.h>
32 #else
33 #define GRASS_GIS_EXPORT
34 #endif
35 #endif
36 
37 GRASS_GIS_EXPORT struct G__ G__;
38 
39 /** initialized is set to 1 when engine is initialized */
40 /* GRASS_GIS_EXPORT static int initialized on windows msvc throws below error.
41 "Error C2201 'initialized': must have external linkage in order to be
42  exported/imported"
43 So we do an ifndef on msvc. without GRASS_GIS_EXPORT it will be exported in DLL.
44 */
45 #ifndef _MSC_VER
46 static int initialized = 0;
47 #else
48 GRASS_GIS_EXPORT int initialized;
49 #endif
50 
51 static int gisinit(void);
52 
53 /*!
54  \brief Initialize GIS Library and ensures a valid mapset is available.
55 
56  \param version
57  \param pgm program (module) name
58 
59  \return always returns 0 on success
60  \return G_fatal_error() is called on error
61  */
62 void G__gisinit(const char *version, const char *pgm)
63 {
64  const char *mapset;
65 
66  if (initialized)
67  return;
68 
69  G_set_program_name(pgm);
70 
71  /* verify version of GRASS headers (and anything else in include) */
72  if (strcmp(version, GIS_H_VERSION) != 0) {
73  char *envstr;
74  char *answer = "0";
75 
76  envstr = getenv("GRASS_COMPATIBILITY_TEST");
77  if (envstr && *envstr && strcmp(envstr, answer) == 0) {
78  G_warning(_("Module built against version %s but "
79  "trying to use version %s. "
80  "In case of errors you need to rebuild the module "
81  "against GRASS version %s."),
83  }
84  else {
85  G_fatal_error(_("Module built against version %s but "
86  "trying to use version %s. "
87  "You need to rebuild GRASS or untangle multiple "
88  "installations."),
89  version, GIS_H_VERSION);
90  }
91  }
92 
93  /* Make sure location and mapset are set */
95  mapset = G_mapset();
96  switch (G_mapset_permissions(mapset)) {
97  case 1:
98  break;
99  case 0:
100  G_fatal_error(_("MAPSET %s - permission denied"), mapset);
101  break;
102  default:
103  G_fatal_error(_("MAPSET %s not found at %s"), mapset,
104  G_location_path());
105  break;
106  }
107 
108  gisinit();
109 }
110 
111 /*!
112  \brief Initialize GIS Library
113 
114  Initializes GIS engine, but does not check for a valid mapset.
115  */
116 void G__no_gisinit(const char *version)
117 {
118  if (initialized)
119  return;
120 
121  /* verify version of GRASS headers (and anything else in include) */
122  if (strcmp(version, GIS_H_VERSION) != 0) {
123  char *envstr;
124  char *answer = "0";
125 
126  envstr = getenv("GRASS_COMPATIBILITY_TEST");
127  if (envstr && *envstr && strcmp(envstr, answer) == 0) {
128  G_warning(_("Module built against version %s but "
129  "trying to use version %s. "
130  "In case of errors you need to rebuild the module "
131  "against GRASS version %s."),
133  }
134  else {
135  G_fatal_error(_("Module built against version %s but "
136  "trying to use version %s. "
137  "You need to rebuild GRASS or untangle multiple "
138  "installations."),
139  version, GIS_H_VERSION);
140  }
141  }
142  gisinit();
143 }
144 
145 /*!
146  \brief Checks to see if GIS engine is initialized.
147  */
149 {
150  if (initialized)
151  return;
152  G_warning(
153  _("System not initialized. Programmer forgot to call G_gisinit()."));
154  G_sleep(3);
155  exit(EXIT_FAILURE);
156 }
157 
158 static int gisinit(void)
159 {
160  char *zlib;
161 
162 #if defined(_MSC_VER) || defined(__MINGW32__)
163  _fmode = O_BINARY;
164 #endif
165  /* Mark window as not set */
166  G__.window_set = 0;
167 
168  /* byte order */
170 
171  zlib = getenv("GRASS_ZLIB_LEVEL");
172  /* Valid zlib compression levels -1 - 9 */
173  /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
174  * level 0 means no compression
175  * as used here, 1 gives the best compromise between speed and compression
176  */
177  G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : 1;
178  if (G__.compression_level < -1 || G__.compression_level > 9)
180 
181  initialized = 1;
182 
183  setlocale(LC_NUMERIC, "C");
184 
185  return 0;
186 }
187 
188 /*!
189  \brief Initialize environment
190  */
191 void G_init_all(void)
192 {
194  G_init_env();
195  G_init_logging();
196  G__init_window();
197  G_init_locale();
198  G_init_debug();
199  G_verbose();
200  G_init_tempfile();
202  G__home();
203  G__machine_name();
204  G_whoami();
207 }
#define GRASS_GIS_EXPORT
Definition: G.h:7
int G_mapset_permissions(const char *)
Check for user mapset permission.
Definition: mapset_msc.c:292
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:34
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
Header file for msvc/fcntl.c.
int _fmode
Definition: fmode.c:4
#define GIS_H_VERSION
Definition: gis.h:65
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:148
void G_init_all(void)
Initialize environment.
Definition: gisinit.c:191
void G__no_gisinit(const char *version)
Initialize GIS Library.
Definition: gisinit.c:116
void G__gisinit(const char *version, const char *pgm)
Initialize GIS Library and ensures a valid mapset is available.
Definition: gisinit.c:62
#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:16
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:11
int compression_level
Definition: G.h:15
int little_endian
Definition: G.h:14
int window_set
Definition: G.h:13
#define GRASS_VERSION_STRING
Definition: version.h:1