GRASS Programmer's Manual
6.4.2(2012)
|
00001 /* setup: Program for GRASS user to first use. Sets environment 00002 * variables: LOCATION_NAME MAPSET GISDBASE 00003 */ 00004 00005 00006 /* these defines come from the Gmakefile, but are defined here to allow 00007 * debugging with saber 00008 */ 00009 #ifndef D_LOCATION_NAME 00010 #define D_LOCATION_NAME "spearfish" 00011 #endif 00012 #ifndef D_GISDBASE 00013 #define D_GISDBASE "/data" 00014 #endif 00015 #ifndef GRASS_VERSION_NUMBER 00016 #define GRASS_VERSION_NUMBER "" 00017 #endif 00018 00019 00020 00021 static char *intro[] = { 00022 " PLEASE SET SESSION INFORMATION", 00023 "", 00024 "DATABASE: A directory (folder) on disk to contain all GRASS maps and data.", 00025 "", 00026 "LOCATION: This is the name of a geographic location. It is defined by a", 00027 " co-ordinate system and a rectangular boundary.", 00028 "", 00029 "MAPSET: Each GRASS session runs under a particular MAPSET. This consists of", 00030 " a rectangular REGION and a set of maps. Every LOCATION contains at", 00031 " least a MAPSET called PERMANENT, which is readable by all sessions.", 00032 "", 00033 " The REGION defaults to the entire area of the chosen LOCATION.", 00034 " You may change it later with the command: g.region", 00035 "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ", 00036 0 00037 }; 00038 static char *loc_text = 00039 "LOCATION: (enter list for a list of locations)"; 00040 static char *map_text = 00041 "MAPSET: (or mapsets within a location)"; 00042 00043 #include <string.h> 00044 #include <sys/types.h> 00045 #include <sys/stat.h> 00046 #include <unistd.h> 00047 #include <stdlib.h> 00048 #include <grass/gis.h> 00049 #include <grass/vask.h> 00050 #include <grass/edit.h> 00051 #include "local_proto.h" 00052 00053 int main(int argc, char *argv[]) 00054 { 00055 char version[80]; 00056 char gisdbase[70]; 00057 char location_name[41]; 00058 char location[1024]; 00059 char mapset[41]; 00060 int line; 00061 int yes; 00062 struct Cell_head window; 00063 00064 char *GISDBASE; 00065 char *LOCATION_NAME; 00066 char *MAPSET; 00067 00068 int repeat; 00069 00070 /* GISBASE 00071 * comes from the unix environment and must be set 00072 * make sure it is NOT in the .gisrc file 00073 * note: G_getenv() make a special case of this variable 00074 */ 00075 G_no_gisinit(); 00076 00077 G_unsetenv("GISBASE"); /* this cleans the variable */ 00078 G_getenv("GISBASE"); /* this reads it from the unix environment */ 00079 00080 /* Gather all existing variables ********************************************* */ 00081 GISDBASE = G__getenv("GISDBASE"); 00082 LOCATION_NAME = G__getenv("LOCATION_NAME"); 00083 MAPSET = G__getenv("MAPSET"); 00084 00085 if (!MAPSET) 00086 MAPSET = G_whoami(); 00087 if (!LOCATION_NAME) 00088 LOCATION_NAME = D_LOCATION_NAME; 00089 if (!GISDBASE) 00090 GISDBASE = D_GISDBASE; 00091 00092 strcpy(mapset, MAPSET); 00093 strcpy(location_name, LOCATION_NAME); 00094 strcpy(gisdbase, GISDBASE); 00095 G__setenv("GISDBASE", gisdbase); 00096 00097 sprintf(version, " GRASS %s", 00098 GRASS_VERSION_NUMBER); 00099 00100 for (repeat = 1; repeat; hit_return()) { 00101 V_clear(); 00102 V_line(0, version); 00103 for (line = 1; intro[line]; line++) 00104 V_line(line, intro[line]); 00105 00106 line++; 00107 V_line(line, loc_text); 00108 V_ques(location_name, 's', line++, 12, 25); 00109 00110 V_line(line, map_text); 00111 V_ques(mapset, 's', line++, 12, 25); 00112 00113 line++; 00114 V_line(line, "DATABASE:"); 00115 V_ques(gisdbase, 's', line++, 10, sizeof(gisdbase) - 1); 00116 00117 V_intrpt_ok(); 00118 if (!V_call()) 00119 exit(1); 00120 00121 G_strip(gisdbase); 00122 if (*gisdbase == 0) { 00123 fprintf(stderr, "No DATABASE specified\n"); 00124 strcpy(gisdbase, D_GISDBASE); 00125 continue; 00126 } 00127 #ifdef __MINGW32__ 00128 if (*gisdbase == '/') { 00129 char tmp[200], *p; 00130 00131 sprintf(tmp, "%s", getenv("WD")); 00132 for (p = tmp + strlen(tmp); --p >= tmp && *p == '\\';) ; 00133 for (; --p >= tmp && *p != '\\';) ; 00134 for (; --p >= tmp && *p == '\\';) ; 00135 *(p + 1) = 0; 00136 for (p = tmp; *p; p++) 00137 if (*p == '\\') 00138 *p = '/'; 00139 strcat(tmp, gisdbase); 00140 strcpy(gisdbase, tmp); 00141 } 00142 if (!gisdbase[1] || gisdbase[1] != ':') 00143 #else 00144 if (*gisdbase != '/') 00145 #endif 00146 { 00147 char temp[200]; 00148 00149 fprintf(stderr, "DATABASE <%s> - must start with /\n", gisdbase); 00150 sprintf(temp, " '%s'", gisdbase); 00151 strcpy(gisdbase, temp); 00152 continue; 00153 } 00154 if (access(gisdbase, 0) != 0) { 00155 fprintf(stderr, "DATABASE <%s> - not found\n", gisdbase); 00156 continue; 00157 } 00158 G__setenv("GISDBASE", gisdbase); 00159 00160 /* take only first word of responses */ 00161 first_word(location_name); 00162 first_word(mapset); 00163 00164 if (*location_name && (G_legal_filename(location_name) < 0)) { 00165 fprintf(stderr, "LOCATION <%s> - illegal name\n", location_name); 00166 continue; 00167 } 00168 if (*mapset && (G_legal_filename(mapset) < 0)) { 00169 fprintf(stderr, "MAPSET <%s> - illegal name\n", mapset); 00170 continue; 00171 } 00172 00173 if (*location_name == 0 || strcmp(location_name, "list") == 0) { 00174 list_locations(gisdbase); 00175 *location_name = 0; 00176 continue; 00177 } 00178 sprintf(location, "%s/%s", gisdbase, location_name); 00179 if (access(location, 0) != 0) { 00180 fprintf(stderr, "LOCATION <%s> - doesn't exist\n", location_name); 00181 list_locations(gisdbase); 00182 if (!can_make_location(gisdbase, location_name)) 00183 continue; 00184 00185 fprintf(stderr, "\nWould you like to create location <%s> ? ", 00186 location_name); 00187 if (G_yes("", 1)) { 00188 if (make_location(gisdbase, location_name)) { 00189 00190 G__setenv("LOCATION_NAME", location_name); 00191 G__setenv("MAPSET", "PERMANENT"); 00192 G__write_env(); 00193 00194 00195 if (system("g.setproj")) { 00196 G_get_default_window(&window); 00197 if (E_edit_cellhd(&window, -1) < 0) 00198 fprintf(stderr, 00199 "WARNING: You did not provide default region for %s!\n", 00200 location_name); 00201 00202 G__put_window(&window, "", "DEFAULT_WIND"); 00203 G__put_window(&window, "", "WIND"); 00204 fprintf(stderr, "LOCATION <%s> created\n", 00205 location_name); 00206 fprintf(stderr, 00207 "\nBut the PROJECTION information files were not created!\n"); 00208 fprintf(stderr, 00209 "You must run g.setproj successfully before projection software will work%c%c%c\n", 00210 7, 7, 7); 00211 continue; 00212 } 00213 else { 00214 G_get_default_window(&window); 00215 if (E_edit_cellhd(&window, -1) < 0) 00216 fprintf(stderr, 00217 "WARNING: You did not provide default region for %s!\n", 00218 location_name); 00219 G__put_window(&window, "", "DEFAULT_WIND"); 00220 G__put_window(&window, "", "WIND"); 00221 fprintf(stderr, "LOCATION <%s> created!\n", 00222 location_name); 00223 continue; 00224 } 00225 } 00226 fprintf(stderr, "LOCATION <%s> NOT created\n", location_name); 00227 } 00228 continue; 00229 } 00230 G__setenv("LOCATION_NAME", location_name); 00231 if (*mapset == 0 || strcmp(mapset, "list") == 0) { 00232 list_mapsets(location_name, location); 00233 *mapset = 0; 00234 continue; 00235 } 00236 G__setenv("MAPSET", mapset); 00237 00238 repeat = 0; 00239 00240 switch (mapset_permissions(mapset)) { 00241 case -1: 00242 if (strcmp(mapset, G_whoami()) == 0) { 00243 yes = 1; 00244 } 00245 else { 00246 repeat = 1; 00247 fprintf(stderr, "\n\nMapset <<%s>> is not available\n", 00248 mapset); 00249 list_mapsets(location_name, location); 00250 fprintf(stderr, 00251 "\nWould you like to create < %s > as a new mapset? ", 00252 mapset); 00253 yes = G_yes("", 1); 00254 } 00255 00256 if (yes && (make_mapset(location, mapset) == 0)) 00257 repeat = 0; 00258 break; 00259 case 0: 00260 fprintf(stderr, "\n\nSorry, no access to <<%s>>.\n", mapset); 00261 list_mapsets(location_name, location); 00262 repeat = 1; 00263 break; 00264 case 1: 00265 mapset_message(mapset); 00266 if (!mapset_question(mapset)) 00267 repeat = 1; 00268 break; 00269 } 00270 if (!repeat) 00271 break; 00272 } 00273 00274 G__write_env(); 00275 exit(0); 00276 } 00277 00278 int list_locations(const char *gisdbase) 00279 { 00280 fprintf(stderr, "\nAvailable locations:\n"); 00281 fprintf(stderr, "----------------------\n"); 00282 G_ls(gisdbase, stderr); 00283 fprintf(stderr, "----------------------\n"); 00284 return 0; 00285 } 00286 00287 int list_mapsets(const char *location_name, const char *location) 00288 { 00289 char **mapsets; 00290 int i, num_mapsets; 00291 int any, ok, any_ok; 00292 int len, tot_len; 00293 00294 fprintf(stderr, "\nMapsets in location <%s>\n", location_name); 00295 fprintf(stderr, "----------------------\n"); 00296 mapsets = G__ls(location, &num_mapsets); 00297 any = 0; 00298 any_ok = 0; 00299 tot_len = 0; 00300 if (num_mapsets > 0) { 00301 for (i = 0; i < num_mapsets; i++) { 00302 any = 1; 00303 len = strlen(mapsets[i]) + 1; 00304 len /= 20; 00305 len = (len + 1) * 20; 00306 tot_len += len; 00307 if (tot_len > 75) { 00308 fprintf(stderr, "\n"); 00309 tot_len = len; 00310 } 00311 if ((ok = (mapset_permissions(mapsets[i]) == 1))) 00312 any_ok = 1; 00313 fprintf(stderr, "%s%-*s", ok ? "(+)" : " ", len, mapsets[i]); 00314 } 00315 if (tot_len) 00316 fprintf(stderr, "\n"); 00317 if (any_ok) 00318 fprintf(stderr, 00319 "\nnote: you only have access to mapsets marked with (+)\n"); 00320 else if (any) 00321 fprintf(stderr, 00322 "\nnote: you do not have access to any of these mapsets\n"); 00323 } 00324 fprintf(stderr, "----------------------\n"); 00325 00326 return 0; 00327 } 00328 00329 int first_word(char *buf) 00330 { 00331 char temp[GNAME_MAX]; 00332 00333 *temp = 0; 00334 sscanf(buf, "%s", temp); 00335 strcpy(buf, temp); 00336 00337 return 0; 00338 } 00339 00340 int hit_return(void) 00341 { 00342 char buf[80]; 00343 00344 fprintf(stderr, "\nHit RETURN -->"); 00345 G_gets(buf); 00346 00347 return 0; 00348 }