main.c 5.9 KB

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