main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /****************************************************************************
  2. *
  3. * MODULE: g.remove
  4. *
  5. * AUTHOR(S): Huidae Cho <grass4u gmail.com>
  6. *
  7. * Based on general/manage/cmd/remove.c by
  8. * CERL (original contributor),
  9. * Radim Blazek <radim.blazek gmail.com>,
  10. * Cedric Shock <cedricgrass shockfamily.net>,
  11. * Glynn Clements <glynn gclements.plus.com>,
  12. * Jachym Cepicky <jachym les-ejk.cz>,
  13. * Markus Neteler <neteler itc.it>,
  14. * Martin Landa <landa.martin gmail.com>
  15. *
  16. * PURPOSE: Lets users remove GRASS database files
  17. *
  18. * COPYRIGHT: (C) 1999-2014 by the GRASS Development Team
  19. *
  20. * This program is free software under the GNU General
  21. * Public License (>=v2). Read the file COPYING that
  22. * comes with GRASS for details.
  23. *
  24. *****************************************************************************/
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <grass/gis.h>
  29. #include <grass/manage.h>
  30. #include <grass/glocale.h>
  31. /* check_reclass.c */
  32. int check_reclass(const char *, const char *, int);
  33. int main(int argc, char *argv[])
  34. {
  35. struct GModule *module;
  36. struct
  37. {
  38. struct Option *type;
  39. struct Option *pattern;
  40. struct Option *exclude;
  41. } opt;
  42. struct
  43. {
  44. struct Flag *regex;
  45. struct Flag *extended;
  46. struct Flag *force;
  47. struct Flag *basemap;
  48. } flag;
  49. const char *mapset;
  50. int result;
  51. int i, all, num_types, nlist;
  52. void *filter, *exclude;
  53. G_gisinit(argv[0]);
  54. result = EXIT_SUCCESS;
  55. module = G_define_module();
  56. G_add_keyword(_("general"));
  57. G_add_keyword(_("map management"));
  58. G_add_keyword(_("remove"));
  59. module->description =
  60. _("Removes data base element files from "
  61. "the user's current mapset using the search pattern.");
  62. M_read_list(FALSE, &nlist);
  63. opt.type = G_define_standard_option(G_OPT_M_DATATYPE);
  64. opt.type->multiple = YES;
  65. opt.type->options = M_get_options(TRUE);
  66. opt.type->descriptions = M_get_option_desc(TRUE);
  67. opt.type->guidependency = "pattern,exclude";
  68. opt.pattern = G_define_option();
  69. opt.pattern->key = "pattern";
  70. opt.pattern->type = TYPE_STRING;
  71. opt.pattern->required = YES;
  72. opt.pattern->gisprompt = "old,element,element";
  73. opt.pattern->description = _("Map name search pattern or map names separated by a comma");
  74. opt.pattern->guisection = _("Pattern");
  75. opt.exclude = G_define_option();
  76. opt.exclude->key = "exclude";
  77. opt.exclude->type = TYPE_STRING;
  78. opt.exclude->required = NO;
  79. opt.exclude->gisprompt = "old,element,element";
  80. opt.exclude->description = _("Map name exclusion pattern (default: none)");
  81. opt.exclude->guisection = _("Pattern");
  82. flag.regex = G_define_flag();
  83. flag.regex->key = 'r';
  84. flag.regex->description =
  85. _("Use basic regular expressions instead of wildcards");
  86. flag.extended = G_define_flag();
  87. flag.extended->key = 'e';
  88. flag.extended->description =
  89. _("Use extended regular expressions instead of wildcards");
  90. flag.force = G_define_flag();
  91. flag.force->key = 'f';
  92. flag.force->description =
  93. _("Force removal (required for actual deletion of files)");
  94. flag.basemap = G_define_flag();
  95. flag.basemap->key = 'b';
  96. flag.basemap->description = _("Remove base raster maps");
  97. flag.basemap->guisection = _("Raster");
  98. if (G_parser(argc, argv))
  99. exit(EXIT_FAILURE);
  100. if (flag.regex->answer && flag.extended->answer)
  101. G_fatal_error(_("-%c and -%c are mutually exclusive"),
  102. flag.regex->key, flag.extended->key);
  103. if (flag.regex->answer || flag.extended->answer)
  104. filter = G_ls_regex_filter(opt.pattern->answer, 0,
  105. (int)flag.extended->answer);
  106. else {
  107. /* handle individual map names */
  108. if (strchr(opt.pattern->answer, ',')) {
  109. char *pattern;
  110. pattern = (char *)G_malloc(strlen(opt.pattern->answer) + 3);
  111. sprintf(pattern, "{%s}", opt.pattern->answer);
  112. filter = G_ls_glob_filter(pattern, 0);
  113. }
  114. else
  115. filter = G_ls_glob_filter(opt.pattern->answer, 0);
  116. }
  117. if (!filter)
  118. G_fatal_error(_("Unable to compile pattern <%s>"), opt.pattern->answer);
  119. if (opt.exclude->answer) {
  120. if (flag.regex->answer || flag.extended->answer)
  121. exclude = G_ls_regex_filter(opt.exclude->answer, 1,
  122. (int)flag.extended->answer);
  123. else {
  124. /* handle individual map names */
  125. if (strchr(opt.exclude->answer, ',')) {
  126. char *pattern;
  127. pattern = (char *)G_malloc(strlen(opt.exclude->answer) + 3);
  128. sprintf(pattern, "{%s}", opt.exclude->answer);
  129. exclude = G_ls_glob_filter(pattern, 1);
  130. }
  131. else
  132. exclude = G_ls_glob_filter(opt.exclude->answer, 1);
  133. }
  134. if (!exclude)
  135. G_fatal_error(_("Unable to compile pattern <%s>"),
  136. opt.exclude->answer);
  137. }
  138. else
  139. exclude = NULL;
  140. if (!flag.force->answer)
  141. G_message(_("The following data base element files would be deleted:"));
  142. mapset = G_mapset();
  143. for (i = 0; opt.type->answers[i]; i++) {
  144. if (strcmp(opt.type->answers[i], "all") == 0)
  145. break;
  146. }
  147. if (opt.type->answers[i]) {
  148. all = 1;
  149. num_types = nlist;
  150. }
  151. else {
  152. all = 0;
  153. num_types = i;
  154. }
  155. for (i = 0; i < num_types; i++) {
  156. int n, rast, num_files, j;
  157. const struct list *elem;
  158. char path[GPATH_MAX];
  159. char **files;
  160. n = all ? i : M_get_element(opt.type->answers[i]);
  161. elem = M_get_list(n);
  162. G_file_name(path, elem->element[0], "", mapset);
  163. if (access(path, 0) != 0)
  164. continue;
  165. rast = !G_strcasecmp(elem->alias, "rast");
  166. files = G__ls(path, &num_files);
  167. for (j = 0; j < num_files; j++) {
  168. if (!flag.force->answer) {
  169. fprintf(stdout, "%s/%s@%s\n", elem->alias, files[j], mapset);
  170. continue;
  171. }
  172. if (rast && check_reclass(files[j], mapset, flag.basemap->answer))
  173. continue;
  174. if (M_do_remove(n, (char *)files[j]) == 1)
  175. result = EXIT_FAILURE;
  176. }
  177. }
  178. G_free_ls_filter(filter);
  179. if (exclude)
  180. G_free_ls_filter(exclude);
  181. if (!flag.force->answer)
  182. G_important_message(_("You must use the force flag (-%c) to actually "
  183. "remove them. Exiting."), flag.force->key);
  184. exit(result);
  185. }