set_data.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. " PLEASE SET SESSION INFORMATION",
  18. "",
  19. "DATABASE: A directory (folder) on disk to contain all GRASS maps and data.",
  20. "",
  21. "LOCATION: This is the name of a geographic location. It is defined by a",
  22. " co-ordinate system and a rectangular boundary.",
  23. "",
  24. "MAPSET: Each GRASS session runs under a particular MAPSET. This consists of",
  25. " a rectangular REGION and a set of maps. Every LOCATION contains at",
  26. " least a MAPSET called PERMANENT, which is readable by all sessions.",
  27. "",
  28. " The REGION defaults to the entire area of the chosen LOCATION.",
  29. " You may change it later with the command: g.region",
  30. "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ",
  31. 0
  32. };
  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)
  73. MAPSET = G_whoami();
  74. if (!LOCATION_NAME)
  75. LOCATION_NAME = D_LOCATION_NAME;
  76. if (!GISDBASE)
  77. GISDBASE = D_GISDBASE;
  78. strcpy(mapset, MAPSET);
  79. strcpy(location_name, LOCATION_NAME);
  80. strcpy(gisdbase, GISDBASE);
  81. G__setenv("GISDBASE", gisdbase);
  82. sprintf(version, " GRASS %s",
  83. GRASS_VERSION_NUMBER);
  84. for (repeat = 1; repeat; hit_return()) {
  85. V_clear();
  86. V_line(0, version);
  87. for (line = 1; intro[line]; line++)
  88. V_line(line, intro[line]);
  89. line++;
  90. V_line(line, loc_text);
  91. V_ques(location_name, 's', line++, 12, 25);
  92. V_line(line, map_text);
  93. V_ques(mapset, 's', line++, 12, 25);
  94. line++;
  95. V_line(line, "DATABASE:");
  96. V_ques(gisdbase, 's', line++, 10, sizeof(gisdbase) - 1);
  97. V_intrpt_ok();
  98. if (!V_call())
  99. exit(1);
  100. G_strip(gisdbase);
  101. if (*gisdbase == 0) {
  102. fprintf(stderr, "No DATABASE specified\n");
  103. strcpy(gisdbase, D_GISDBASE);
  104. continue;
  105. }
  106. #ifdef __MINGW32__
  107. if (*gisdbase == '/') {
  108. char tmp[200], *p;
  109. sprintf(tmp, "%s", getenv("WD"));
  110. for (p = tmp + strlen(tmp); --p >= tmp && *p == '\\';) ;
  111. for (; --p >= tmp && *p != '\\';) ;
  112. for (; --p >= tmp && *p == '\\';) ;
  113. *(p + 1) = 0;
  114. for (p = tmp; *p; p++)
  115. if (*p == '\\')
  116. *p = '/';
  117. strcat(tmp, gisdbase);
  118. strcpy(gisdbase, tmp);
  119. }
  120. if (!gisdbase[1] || gisdbase[1] != ':')
  121. #else
  122. if (*gisdbase != '/')
  123. #endif
  124. {
  125. char temp[200];
  126. fprintf(stderr, "DATABASE <%s> - must start with /\n", gisdbase);
  127. sprintf(temp, " '%s'", gisdbase);
  128. strcpy(gisdbase, temp);
  129. continue;
  130. }
  131. if (access(gisdbase, 0) != 0) {
  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. fprintf(stderr, "LOCATION <%s> - illegal name\n", location_name);
  141. continue;
  142. }
  143. if (*mapset && (G_legal_filename(mapset) < 0)) {
  144. fprintf(stderr, "MAPSET <%s> - illegal name\n", mapset);
  145. continue;
  146. }
  147. if (*location_name == 0 || strcmp(location_name, "list") == 0) {
  148. list_locations(gisdbase);
  149. *location_name = 0;
  150. continue;
  151. }
  152. sprintf(location, "%s/%s", gisdbase, location_name);
  153. if (access(location, 0) != 0) {
  154. fprintf(stderr, "LOCATION <%s> - doesn't exist\n", location_name);
  155. list_locations(gisdbase);
  156. if (!can_make_location(gisdbase, location_name))
  157. continue;
  158. fprintf(stderr, "\nWould you like to create location <%s> ? ",
  159. location_name);
  160. if (G_yes("", 1)) {
  161. if (make_location(gisdbase, location_name)) {
  162. G__setenv("LOCATION_NAME", location_name);
  163. G__setenv("MAPSET", "PERMANENT");
  164. G__write_env();
  165. if (system("g.setproj")) {
  166. G_get_default_window(&window);
  167. if (E_edit_cellhd(&window, -1) < 0)
  168. fprintf(stderr,
  169. "WARNING: You did not provide default region for %s!\n",
  170. location_name);
  171. G__put_window(&window, "", "DEFAULT_WIND");
  172. G__put_window(&window, "", "WIND");
  173. fprintf(stderr, "LOCATION <%s> created\n",
  174. location_name);
  175. fprintf(stderr,
  176. "\nBut the PROJECTION information files were not created!\n");
  177. fprintf(stderr,
  178. "You must run g.setproj successfully before projection software will work%c%c%c\n",
  179. 7, 7, 7);
  180. continue;
  181. }
  182. else {
  183. G_get_default_window(&window);
  184. if (E_edit_cellhd(&window, -1) < 0)
  185. fprintf(stderr,
  186. "WARNING: You did not provide default region for %s!\n",
  187. location_name);
  188. G__put_window(&window, "", "DEFAULT_WIND");
  189. G__put_window(&window, "", "WIND");
  190. fprintf(stderr, "LOCATION <%s> created!\n",
  191. location_name);
  192. continue;
  193. }
  194. }
  195. fprintf(stderr, "LOCATION <%s> NOT created\n", location_name);
  196. }
  197. continue;
  198. }
  199. G__setenv("LOCATION_NAME", location_name);
  200. if (*mapset == 0 || strcmp(mapset, "list") == 0) {
  201. list_mapsets(location_name, location);
  202. *mapset = 0;
  203. continue;
  204. }
  205. G__setenv("MAPSET", mapset);
  206. repeat = 0;
  207. switch (mapset_permissions(mapset)) {
  208. case -1:
  209. if (strcmp(mapset, G_whoami()) == 0) {
  210. yes = 1;
  211. }
  212. else {
  213. repeat = 1;
  214. fprintf(stderr, "\n\nMapset <<%s>> is not available\n",
  215. mapset);
  216. list_mapsets(location_name, location);
  217. fprintf(stderr,
  218. "\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))
  233. repeat = 1;
  234. break;
  235. }
  236. if (!repeat)
  237. break;
  238. }
  239. G__write_env();
  240. exit(0);
  241. }
  242. int list_locations(const char *gisdbase)
  243. {
  244. fprintf(stderr, "\nAvailable locations:\n");
  245. fprintf(stderr, "----------------------\n");
  246. G_ls(gisdbase, stderr);
  247. fprintf(stderr, "----------------------\n");
  248. return 0;
  249. }
  250. int 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. for (i = 0; i < num_mapsets; i++) {
  264. any = 1;
  265. len = strlen(mapsets[i]) + 1;
  266. len /= 20;
  267. len = (len + 1) * 20;
  268. tot_len += len;
  269. if (tot_len > 75) {
  270. fprintf(stderr, "\n");
  271. tot_len = len;
  272. }
  273. if ((ok = (mapset_permissions(mapsets[i]) == 1)))
  274. any_ok = 1;
  275. fprintf(stderr, "%s%-*s", ok ? "(+)" : " ", len, mapsets[i]);
  276. }
  277. if (tot_len)
  278. fprintf(stderr, "\n");
  279. if (any_ok)
  280. fprintf(stderr,
  281. "\nnote: you only have access to mapsets marked with (+)\n");
  282. else if (any)
  283. fprintf(stderr,
  284. "\nnote: you do not have access to any of these mapsets\n");
  285. }
  286. fprintf(stderr, "----------------------\n");
  287. return 0;
  288. }
  289. int first_word(char *buf)
  290. {
  291. char temp[GNAME_MAX];
  292. *temp = 0;
  293. sscanf(buf, "%s", temp);
  294. strcpy(buf, temp);
  295. return 0;
  296. }
  297. int hit_return(void)
  298. {
  299. char buf[80];
  300. fprintf(stderr, "\nHit RETURN -->");
  301. G_gets(buf);
  302. return 0;
  303. }