2
/****************************************************************************
6
* AUTHOR(S): Huidae Cho <grass4u gmail.com>
8
* Based on general/manage/cmd/remove.c by
9
* CERL (original contributor),
10
* Radim Blazek <radim.blazek gmail.com>,
11
* Cedric Shock <cedricgrass shockfamily.net>,
12
* Huidae Cho <grass4u gmail.com>,
13
* Glynn Clements <glynn gclements.plus.com>,
14
* Jachym Cepicky <jachym les-ejk.cz>,
15
* Markus Neteler <neteler itc.it>,
16
* Martin Landa <landa.martin gmail.com>
18
* PURPOSE: lets users remove GRASS database files
20
* COPYRIGHT: (C) 1999-2008 by the GRASS Development Team
22
* This program is free software under the GNU General Public
23
* License (>=v2). Read the file COPYING that comes with GRASS
26
*****************************************************************************/
36
static int ls_filter(const char *, void *);
38
int main(int argc, char *argv[])
40
struct GModule *module;
41
struct Option **opt, *o;
45
struct Flag *extended;
49
char *name, *mapset, *location_path, path[GPATH_MAX], **files;
51
int num_files, rast, result = EXIT_SUCCESS;
57
module = G_define_module();
58
module->keywords = _("general, map management");
60
_("Removes data base element files from "
61
"the user's current mapset.");
63
flag.regex = G_define_flag();
64
flag.regex->key = 'r';
65
flag.regex->description =
66
_("Use basic regular expressions instead of wildcards");
68
flag.extended = G_define_flag();
69
flag.extended->key = 'e';
70
flag.extended->description =
71
_("Use extended regular expressions instead of wildcards");
73
flag.force = G_define_flag();
74
flag.force->key = 'f';
75
flag.force->description =
76
_("Force removal (required for actual deletion of files)");
78
flag.basemap = G_define_flag();
79
flag.basemap->key = 'b';
80
flag.basemap->description = _("Remove base maps");
84
opt = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
86
for (n = 0; n < nlist; n++) {
87
o = opt[n] = G_define_option();
88
o->key = list[n].alias;
89
o->type = TYPE_STRING;
93
sprintf(buf, "old,%s,%s", list[n].mainelem, list[n].maindesc);
96
sprintf(buf2, _("%s file(s) to be removed"), list[n].alias);
97
o->description = buf2;
100
if (G_parser(argc, argv))
103
if (flag.regex->answer && flag.extended->answer)
104
G_fatal_error(_("-r and -e are mutually exclusive"));
106
if (!flag.force->answer)
107
G_message(_("The following files would be deleted:"));
109
for (n = 0; n < nlist; n++) {
111
G_free((char *)o->gisprompt);
112
G_free((char *)o->description);
115
location_path = G_location_path();
118
for (n = 0; n < nlist; n++) {
119
if (opt[n]->answers) {
120
G__file_name(path, list[n].element[0], "", mapset);
121
if (access(path, 0) != 0)
123
rast = !G_strcasecmp(list[n].alias, "rast");
124
for (i = 0; (name = opt[n]->answers[i]); i++) {
125
if (!flag.regex->answer && !flag.extended->answer)
126
name = wc2regex(name);
127
if (regcomp(®ex, name,
128
(flag.regex->answer ? 0 : REG_EXTENDED) | REG_NOSUB))
130
_("Unable to compile regular expression %s"),
132
if (!flag.regex->answer && !flag.extended->answer)
135
G_set_ls_filter(ls_filter, ®ex);
136
files = G__ls(path, &num_files);
139
for (j = 0; j < num_files; j++) {
140
if (!flag.force->answer) {
141
fprintf(stdout, "%s/%s@%s\n", list[n].alias, files[j],
146
check_reclass(files[j], mapset, flag.basemap->answer))
149
if (do_remove(n, (char *)files[j]) == 1)
150
result = EXIT_FAILURE;
156
if (!flag.force->answer) {
158
G_message(_("You must use the force flag to actually remove them. Exiting."));
164
static int ls_filter(const char *filename, void *closure)
166
return filename[0] != '.' &&
167
regexec((regex_t *) closure, filename, 0, NULL, 0) == 0;