set_data.c 9.0 KB

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