GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
set_data.c
Go to the documentation of this file.
1 /* setup: Program for GRASS user to first use. Sets environment
2  * variables: LOCATION_NAME MAPSET GISDBASE
3  */
4 
5 
6 /* these defines come from the Gmakefile, but are defined here to allow
7  * debugging with saber
8  */
9 #ifndef D_LOCATION_NAME
10 #define D_LOCATION_NAME "spearfish"
11 #endif
12 #ifndef D_GISDBASE
13 #define D_GISDBASE "/data"
14 #endif
15 #ifndef GRASS_VERSION_NUMBER
16 #define GRASS_VERSION_NUMBER ""
17 #endif
18 
19 
20 
21 static char *intro[] = {
22  " PLEASE SET SESSION INFORMATION",
23  "",
24  "DATABASE: A directory (folder) on disk to contain all GRASS maps and data.",
25  "",
26  "LOCATION: This is the name of a geographic location. It is defined by a",
27  " co-ordinate system and a rectangular boundary.",
28  "",
29  "MAPSET: Each GRASS session runs under a particular MAPSET. This consists of",
30  " a rectangular REGION and a set of maps. Every LOCATION contains at",
31  " least a MAPSET called PERMANENT, which is readable by all sessions.",
32  "",
33  " The REGION defaults to the entire area of the chosen LOCATION.",
34  " You may change it later with the command: g.region",
35  "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ",
36  0
37 };
38 static char *loc_text =
39  "LOCATION: (enter list for a list of locations)";
40 static char *map_text =
41  "MAPSET: (or mapsets within a location)";
42 
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <grass/gis.h>
49 #include <grass/vask.h>
50 #include <grass/edit.h>
51 #include "local_proto.h"
52 
53 int main(int argc, char *argv[])
54 {
55  char version[80];
56  char gisdbase[GPATH_MAX];
57  char location_name[GMAPSET_MAX];
58  char location[GPATH_MAX + GMAPSET_MAX];
59  char mapset[GMAPSET_MAX];
60  int line;
61  int yes;
62  struct Cell_head window;
63 
64  char *GISDBASE;
65  char *LOCATION_NAME;
66  char *MAPSET;
67 
68  int repeat;
69 
70  /* GISBASE
71  * comes from the unix environment and must be set
72  * make sure it is NOT in the .gisrc file
73  * note: G_getenv() make a special case of this variable
74  */
75  G_no_gisinit();
76 
77  G_unsetenv("GISBASE"); /* this cleans the variable */
78  G_getenv("GISBASE"); /* this reads it from the unix environment */
79 
80  /* Gather all existing variables ********************************************* */
81  GISDBASE = G__getenv("GISDBASE");
82  LOCATION_NAME = G__getenv("LOCATION_NAME");
83  MAPSET = G__getenv("MAPSET");
84 
85  if (!MAPSET)
86  MAPSET = G_whoami();
87  if (!LOCATION_NAME)
88  LOCATION_NAME = D_LOCATION_NAME;
89  if (!GISDBASE)
90  GISDBASE = D_GISDBASE;
91 
92  strcpy(mapset, MAPSET);
93  strcpy(location_name, LOCATION_NAME);
94  strcpy(gisdbase, GISDBASE);
95  G__setenv("GISDBASE", gisdbase);
96 
97  sprintf(version, " GRASS %s",
99 
100  for (repeat = 1; repeat; hit_return()) {
101  V_clear();
102  V_line(0, version);
103  for (line = 1; intro[line]; line++)
104  V_line(line, intro[line]);
105 
106  line++;
107  V_line(line, loc_text);
108  V_ques(location_name, 's', line++, 12, 25);
109 
110  V_line(line, map_text);
111  V_ques(mapset, 's', line++, 12, 25);
112 
113  line++;
114  V_line(line, "DATABASE:");
115  V_ques(gisdbase, 's', line++, 10, 70 - 1);
116 
117  V_intrpt_ok();
118  if (!V_call())
119  exit(1);
120 
121  G_strip(gisdbase);
122  if (*gisdbase == 0) {
123  fprintf(stderr, "No DATABASE specified\n");
124  strcpy(gisdbase, D_GISDBASE);
125  continue;
126  }
127 #ifdef __MINGW32__
128  if (*gisdbase == '/') {
129  char tmp[GPATH_MAX], *p;
130 
131  sprintf(tmp, "%s", getenv("WD"));
132  for (p = tmp + strlen(tmp); --p >= tmp && *p == '\\';) ;
133  for (; --p >= tmp && *p != '\\';) ;
134  for (; --p >= tmp && *p == '\\';) ;
135  *(p + 1) = 0;
136  for (p = tmp; *p; p++)
137  if (*p == '\\')
138  *p = '/';
139  strcat(tmp, gisdbase);
140  strcpy(gisdbase, tmp);
141  }
142  if (!gisdbase[1] || gisdbase[1] != ':')
143 #else
144  if (*gisdbase != '/')
145 #endif
146  {
147  char temp[GPATH_MAX];
148 
149  fprintf(stderr, "DATABASE <%s> - must start with /\n", gisdbase);
150  sprintf(temp, " '%s'", gisdbase);
151  strcpy(gisdbase, temp);
152  continue;
153  }
154  if (access(gisdbase, 0) != 0) {
155  fprintf(stderr, "DATABASE <%s> - not found\n", gisdbase);
156  continue;
157  }
158  G__setenv("GISDBASE", gisdbase);
159 
160  /* take only first word of responses */
161  first_word(location_name);
162  first_word(mapset);
163 
164  if (*location_name && (G_legal_filename(location_name) < 0)) {
165  fprintf(stderr, "LOCATION <%s> - illegal name\n", location_name);
166  continue;
167  }
168  if (*mapset && (G_legal_filename(mapset) < 0)) {
169  fprintf(stderr, "MAPSET <%s> - illegal name\n", mapset);
170  continue;
171  }
172 
173  if (*location_name == 0 || strcmp(location_name, "list") == 0) {
174  list_locations(gisdbase);
175  *location_name = 0;
176  continue;
177  }
178  sprintf(location, "%s/%s", gisdbase, location_name);
179  if (access(location, 0) != 0) {
180  fprintf(stderr, "LOCATION <%s> - doesn't exist\n", location_name);
181  list_locations(gisdbase);
182  if (!can_make_location(gisdbase, location_name))
183  continue;
184 
185  fprintf(stderr, "\nWould you like to create location <%s> ? ",
186  location_name);
187  if (G_yes("", 1)) {
188  if (make_location(gisdbase, location_name)) {
189 
190  G__setenv("LOCATION_NAME", location_name);
191  G__setenv("MAPSET", "PERMANENT");
192  G__write_env();
193 
194 
195  if (system("g.setproj")) {
196  G_get_default_window(&window);
197  if (E_edit_cellhd(&window, -1) < 0)
198  fprintf(stderr,
199  "WARNING: You did not provide default region for %s!\n",
200  location_name);
201 
202  G__put_window(&window, "", "DEFAULT_WIND");
203  G__put_window(&window, "", "WIND");
204  fprintf(stderr, "LOCATION <%s> created\n",
205  location_name);
206  fprintf(stderr,
207  "\nBut the PROJECTION information files were not created!\n");
208  fprintf(stderr,
209  "You must run g.setproj successfully before projection software will work%c%c%c\n",
210  7, 7, 7);
211  continue;
212  }
213  else {
214  G_get_default_window(&window);
215  if (E_edit_cellhd(&window, -1) < 0)
216  fprintf(stderr,
217  "WARNING: You did not provide default region for %s!\n",
218  location_name);
219  G__put_window(&window, "", "DEFAULT_WIND");
220  G__put_window(&window, "", "WIND");
221  fprintf(stderr, "LOCATION <%s> created!\n",
222  location_name);
223  continue;
224  }
225  }
226  fprintf(stderr, "LOCATION <%s> NOT created\n", location_name);
227  }
228  continue;
229  }
230  G__setenv("LOCATION_NAME", location_name);
231  if (*mapset == 0 || strcmp(mapset, "list") == 0) {
232  list_mapsets(location_name, location);
233  *mapset = 0;
234  continue;
235  }
236  G__setenv("MAPSET", mapset);
237 
238  repeat = 0;
239 
240  switch (mapset_permissions(mapset)) {
241  case -1:
242  if (strcmp(mapset, G_whoami()) == 0) {
243  yes = 1;
244  }
245  else {
246  repeat = 1;
247  fprintf(stderr, "\n\nMapset <<%s>> is not available\n",
248  mapset);
249  list_mapsets(location_name, location);
250  fprintf(stderr,
251  "\nWould you like to create < %s > as a new mapset? ",
252  mapset);
253  yes = G_yes("", 1);
254  }
255 
256  if (yes && (make_mapset(location, mapset) == 0))
257  repeat = 0;
258  break;
259  case 0:
260  fprintf(stderr, "\n\nSorry, no access to <<%s>>.\n", mapset);
261  list_mapsets(location_name, location);
262  repeat = 1;
263  break;
264  case 1:
265  mapset_message(mapset);
266  if (!mapset_question(mapset))
267  repeat = 1;
268  break;
269  }
270  if (!repeat)
271  break;
272  }
273 
274  G__write_env();
275  exit(0);
276 }
277 
278 int list_locations(const char *gisdbase)
279 {
280  fprintf(stderr, "\nAvailable locations:\n");
281  fprintf(stderr, "----------------------\n");
282  G_ls(gisdbase, stderr);
283  fprintf(stderr, "----------------------\n");
284  return 0;
285 }
286 
287 int list_mapsets(const char *location_name, const char *location)
288 {
289  char **mapsets;
290  int i, num_mapsets;
291  int any, ok, any_ok;
292  int len, tot_len;
293 
294  fprintf(stderr, "\nMapsets in location <%s>\n", location_name);
295  fprintf(stderr, "----------------------\n");
296  mapsets = G__ls(location, &num_mapsets);
297  any = 0;
298  any_ok = 0;
299  tot_len = 0;
300  if (num_mapsets > 0) {
301  for (i = 0; i < num_mapsets; i++) {
302  any = 1;
303  len = strlen(mapsets[i]) + 1;
304  len /= 20;
305  len = (len + 1) * 20;
306  tot_len += len;
307  if (tot_len > 75) {
308  fprintf(stderr, "\n");
309  tot_len = len;
310  }
311  if ((ok = (mapset_permissions(mapsets[i]) == 1)))
312  any_ok = 1;
313  fprintf(stderr, "%s%-*s", ok ? "(+)" : " ", len, mapsets[i]);
314  }
315  if (tot_len)
316  fprintf(stderr, "\n");
317  if (any_ok)
318  fprintf(stderr,
319  "\nnote: you only have access to mapsets marked with (+)\n");
320  else if (any)
321  fprintf(stderr,
322  "\nnote: you do not have access to any of these mapsets\n");
323  }
324  fprintf(stderr, "----------------------\n");
325 
326  return 0;
327 }
328 
329 int first_word(char *buf)
330 {
331  char temp[GNAME_MAX];
332 
333  *temp = 0;
334  sscanf(buf, "%s", temp);
335  strcpy(buf, temp);
336 
337  return 0;
338 }
339 
340 int hit_return(void)
341 {
342  char buf[80];
343 
344  fprintf(stderr, "\nHit RETURN -->");
345  G_gets(buf);
346 
347  return 0;
348 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int mapset_message(const char *)
Definition: other.c:29
char * G__getenv(const char *name)
Get environment variable.
Definition: env.c:312
#define D_GISDBASE
Definition: set_data.c:13
int mapset_question(const char *)
Definition: other.c:37
#define GRASS_VERSION_NUMBER
Definition: set_data.c:16
#define D_LOCATION_NAME
Definition: set_data.c:10
int G_gets(char *buf)
Definition: gets.c:39
char ** G__ls(const char *dir, int *num_files)
Stores a sorted directory listing in an array.
Definition: ls.c:88
void V_clear(void)
Zero out prompt and answer arrays.
Definition: V_clear.c:44
int V_line(int linenumber, const char *text)
Definition: V_line.c:50
int make_mapset(const char *, const char *)
Definition: mke_mapset.c:13
int G_yes(const char *question, int dflt)
Ask a yes/no question.
Definition: yes.c:39
int G_get_default_window(struct Cell_head *window)
read the default region
Definition: get_window.c:115
int hit_return(void)
Definition: set_data.c:340
void G_ls(const char *dir, FILE *stream)
Prints a directory listing to a stream, in prettified column format.
Definition: ls.c:129
def version
Get GRASS version as dictionary.
Definition: core.py:1199
int G_unsetenv(const char *name)
Remove name from environment.
Definition: env.c:420
int V_call(void)
Interact with the user.
Definition: V_call.c:149
int list_mapsets(const char *, const char *)
Definition: set_data.c:287
char * G_getenv(const char *name)
Get environment variable.
Definition: env.c:267
int E_edit_cellhd(struct Cell_head *cellhd, int type)
Definition: edit_cellhd.c:224
char * getenv()
int can_make_location(char *gisdbase, char *location)
Definition: chk_dbase.c:9
int main(int argc, char *argv[])
Definition: gem/main.c:302
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
def mapsets
List available mapsets.
Definition: core.py:1045
int mapset_permissions(const char *)
Definition: other.c:17
int G__write_env(void)
Writes current environment to .gisrc.
Definition: env.c:454
char * G_whoami(void)
Gets user&#39;s name.
Definition: gis/whoami.c:40
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int first_word(char *)
Definition: set_data.c:329
int G__setenv(const char *name, const char *value)
Set environment name to value.
Definition: env.c:388
int G__put_window(const struct Cell_head *window, char *dir, char *name)
Definition: put_window.c:40
int list_locations(const char *)
Definition: set_data.c:278
int V_ques(void *src, int var_type, int row, int col, int length)
Definition: V_ques.c:80
int make_location(const char *, const char *)
Definition: mke_loc.c:9
void V_intrpt_ok(void)
Allow CTRL-C.
Definition: V_call.c:455