main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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-2008 by the GRASS Development Team
  20. *
  21. * This program is free software under the GNU General Public
  22. * License (>=v2). Read the file COPYING that comes with GRASS
  23. * for details.
  24. *
  25. *****************************************************************************/
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <regex.h>
  29. #include "global.h"
  30. static int ls_filter(const char *, void *);
  31. int main(int argc, char *argv[])
  32. {
  33. struct GModule *module;
  34. struct Option **opt, *o;
  35. struct
  36. {
  37. struct Flag *regex;
  38. struct Flag *extended;
  39. struct Flag *force;
  40. struct Flag *basemap;
  41. } flag;
  42. const char *mapset, *location_path;
  43. char *name, path[GPATH_MAX], **files;
  44. char *buf, *buf2;
  45. int num_files, rast, result = EXIT_SUCCESS;
  46. int i, j, n;
  47. regex_t regex;
  48. G_gisinit(argv[0]);
  49. module = G_define_module();
  50. G_add_keyword(_("general"));
  51. G_add_keyword(_("map management"));
  52. module->description =
  53. _("Removes data base element files from "
  54. "the user's current mapset.");
  55. flag.regex = G_define_flag();
  56. flag.regex->key = 'r';
  57. flag.regex->description =
  58. _("Use basic regular expressions instead of wildcards");
  59. flag.extended = G_define_flag();
  60. flag.extended->key = 'e';
  61. flag.extended->description =
  62. _("Use extended regular expressions instead of wildcards");
  63. flag.force = G_define_flag();
  64. flag.force->key = 'f';
  65. flag.force->description =
  66. _("Force removal (required for actual deletion of files)");
  67. flag.basemap = G_define_flag();
  68. flag.basemap->key = 'b';
  69. flag.basemap->description = _("Remove base maps");
  70. read_list(0);
  71. opt = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
  72. for (n = 0; n < nlist; n++) {
  73. o = opt[n] = G_define_option();
  74. o->key = list[n].alias;
  75. o->type = TYPE_STRING;
  76. o->required = NO;
  77. o->multiple = YES;
  78. buf = G_malloc(64);
  79. sprintf(buf, "old,%s,%s", list[n].mainelem, list[n].maindesc);
  80. o->gisprompt = buf;
  81. buf2 = G_malloc(64);
  82. sprintf(buf2, _("%s file(s) to be removed"), list[n].alias);
  83. o->description = buf2;
  84. }
  85. if (G_parser(argc, argv))
  86. exit(EXIT_FAILURE);
  87. if (flag.regex->answer && flag.extended->answer)
  88. G_fatal_error(_("-r and -e are mutually exclusive"));
  89. if (!flag.force->answer)
  90. G_message(_("The following files would be deleted:"));
  91. for (n = 0; n < nlist; n++) {
  92. o = opt[n];
  93. G_free((char *)o->gisprompt);
  94. G_free((char *)o->description);
  95. }
  96. location_path = G_location_path();
  97. mapset = G_mapset();
  98. for (n = 0; n < nlist; n++) {
  99. if (opt[n]->answers) {
  100. G__file_name(path, list[n].element[0], "", mapset);
  101. if (access(path, 0) != 0)
  102. continue;
  103. rast = !G_strcasecmp(list[n].alias, "rast");
  104. for (i = 0; (name = opt[n]->answers[i]); i++) {
  105. if (!flag.regex->answer && !flag.extended->answer)
  106. name = wc2regex(name);
  107. if (regcomp(&regex, name,
  108. (flag.regex->answer ? 0 : REG_EXTENDED) | REG_NOSUB))
  109. G_fatal_error(
  110. _("Unable to compile regular expression %s"),
  111. name);
  112. if (!flag.regex->answer && !flag.extended->answer)
  113. G_free(name);
  114. G_set_ls_filter(ls_filter, &regex);
  115. files = G__ls(path, &num_files);
  116. regfree(&regex);
  117. for (j = 0; j < num_files; j++) {
  118. if (!flag.force->answer) {
  119. fprintf(stdout, "%s/%s@%s\n", list[n].alias, files[j],
  120. mapset);
  121. continue;
  122. }
  123. if (rast &&
  124. check_reclass(files[j], mapset, flag.basemap->answer))
  125. continue;
  126. if (do_remove(n, (char *)files[j]) == 1)
  127. result = EXIT_FAILURE;
  128. }
  129. }
  130. }
  131. }
  132. if (!flag.force->answer) {
  133. G_message(" ");
  134. G_message(_("You must use the force flag to actually remove them. Exiting."));
  135. }
  136. exit(result);
  137. }
  138. static int ls_filter(const char *filename, void *closure)
  139. {
  140. return filename[0] != '.' &&
  141. regexec((regex_t *) closure, filename, 0, NULL, 0) == 0;
  142. }