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