~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to general/g.mremove/main.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/****************************************************************************
3
 
 *
4
 
 * MODULE:       g.xremove
5
 
 *
6
 
 * AUTHOR(S):    Huidae Cho <grass4u gmail.com>
7
 
 *
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>
17
 
 *
18
 
 * PURPOSE:      lets users remove GRASS database files
19
 
 *
20
 
 * COPYRIGHT:    (C) 1999-2008 by the GRASS Development Team
21
 
 *
22
 
 *               This program is free software under the GNU General Public
23
 
 *               License (>=v2). Read the file COPYING that comes with GRASS
24
 
 *               for details.
25
 
 *
26
 
 *****************************************************************************/
27
 
 
28
 
#include <stdlib.h>
29
 
#include <unistd.h>
30
 
#include <regex.h>
31
 
#include "global.h"
32
 
 
33
 
int nlist;
34
 
struct list *list;
35
 
 
36
 
static int ls_filter(const char *, void *);
37
 
 
38
 
int main(int argc, char *argv[])
39
 
{
40
 
    struct GModule *module;
41
 
    struct Option **opt, *o;
42
 
    struct
43
 
    {
44
 
        struct Flag *regex;
45
 
        struct Flag *extended;
46
 
        struct Flag *force;
47
 
        struct Flag *basemap;
48
 
    } flag;
49
 
    char *name, *mapset, *location_path, path[GPATH_MAX], **files;
50
 
    char *buf, *buf2;
51
 
    int num_files, rast, result = EXIT_SUCCESS;
52
 
    int i, j, n;
53
 
    regex_t regex;
54
 
 
55
 
    G_gisinit(argv[0]);
56
 
 
57
 
    module = G_define_module();
58
 
    module->keywords = _("general, map management");
59
 
    module->description =
60
 
        _("Removes data base element files from "
61
 
          "the user's current mapset.");
62
 
 
63
 
    flag.regex = G_define_flag();
64
 
    flag.regex->key = 'r';
65
 
    flag.regex->description =
66
 
        _("Use basic regular expressions instead of wildcards");
67
 
 
68
 
    flag.extended = G_define_flag();
69
 
    flag.extended->key = 'e';
70
 
    flag.extended->description =
71
 
        _("Use extended regular expressions instead of wildcards");
72
 
 
73
 
    flag.force = G_define_flag();
74
 
    flag.force->key = 'f';
75
 
    flag.force->description =
76
 
        _("Force removal (required for actual deletion of files)");
77
 
 
78
 
    flag.basemap = G_define_flag();
79
 
    flag.basemap->key = 'b';
80
 
    flag.basemap->description = _("Remove base maps");
81
 
 
82
 
    read_list(0);
83
 
 
84
 
    opt = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
85
 
 
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;
90
 
        o->required = NO;
91
 
        o->multiple = YES;
92
 
        buf = G_malloc(64);
93
 
        sprintf(buf, "old,%s,%s", list[n].mainelem, list[n].maindesc);
94
 
        o->gisprompt = buf;
95
 
        buf2 = G_malloc(64);
96
 
        sprintf(buf2, _("%s file(s) to be removed"), list[n].alias);
97
 
        o->description = buf2;
98
 
    }
99
 
 
100
 
    if (G_parser(argc, argv))
101
 
        exit(EXIT_FAILURE);
102
 
 
103
 
    if (flag.regex->answer && flag.extended->answer)
104
 
        G_fatal_error(_("-r and -e are mutually exclusive"));
105
 
 
106
 
    if (!flag.force->answer)
107
 
        G_message(_("The following files would be deleted:"));
108
 
 
109
 
    for (n = 0; n < nlist; n++) {
110
 
        o = opt[n];
111
 
        G_free((char *)o->gisprompt);
112
 
        G_free((char *)o->description);
113
 
    }
114
 
 
115
 
    location_path = G_location_path();
116
 
    mapset = G_mapset();
117
 
 
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)
122
 
                continue;
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(&regex, name,
128
 
                            (flag.regex->answer ? 0 : REG_EXTENDED) | REG_NOSUB))
129
 
                    G_fatal_error(
130
 
                                  _("Unable to compile regular expression %s"),
131
 
                                  name);
132
 
                if (!flag.regex->answer && !flag.extended->answer)
133
 
                    G_free(name);
134
 
 
135
 
                G_set_ls_filter(ls_filter, &regex);
136
 
                files = G__ls(path, &num_files);
137
 
                regfree(&regex);
138
 
 
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],
142
 
                                mapset);
143
 
                        continue;
144
 
                    }
145
 
                    if (rast &&
146
 
                        check_reclass(files[j], mapset, flag.basemap->answer))
147
 
                        continue;
148
 
 
149
 
                    if (do_remove(n, (char *)files[j]) == 1)
150
 
                        result = EXIT_FAILURE;
151
 
                }
152
 
            }
153
 
        }
154
 
    }
155
 
 
156
 
    if (!flag.force->answer) {
157
 
        G_message(" ");
158
 
        G_message(_("You must use the force flag to actually remove them. Exiting."));
159
 
    }
160
 
 
161
 
    exit(result);
162
 
}
163
 
 
164
 
static int ls_filter(const char *filename, void *closure)
165
 
{
166
 
    return filename[0] != '.' &&
167
 
        regexec((regex_t *) closure, filename, 0, NULL, 0) == 0;
168
 
}