~ubuntu-branches/ubuntu/karmic/grass/karmic

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2009-07-13 13:34:03 UTC
  • mfrom: (3.1.15 karmic)
  • Revision ID: james.westby@ubuntu.com-20090713133403-0x81m7hb1q06zu13
Tags: 6.4.0~rc5-2
* Added a grass menu item (Hamish).
* Added a grass.menu.in template with related icon.
* Added a simple x-grass wrapper to allow running grass without a pre-running
  controlling terminal. At least it works within ordinary WMs launchers.
  (closes: #503371)
* Added newlocation.dpatch patch to manage correctly new locations.
  (closes: #533738)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/****************************************************************************
 
3
 *
 
4
 * MODULE:       g.mapsets
 
5
 * AUTHOR(S):    Michael Shapiro (CERL), Greg Koerper (ManTech Environmental
 
6
 *                    Technology) (original contributors), 
 
7
 *               Glynn Clements <glynn gclements.plus.com>
 
8
 *               Hamish Bowman <hamish_nospam yahoo.com>, 
 
9
 *               Markus Neteler <neteler itc.it>, 
 
10
 *               Moritz Lennert <mlennert club.worldonline.be>,
 
11
 *               Martin Landa <landa.martin gmail.com>,
 
12
 *               Huidae Cho <grass4u gmail.com>
 
13
 * PURPOSE:      set current mapset path
 
14
 * COPYRIGHT:    (C) 1994-2009 by the GRASS Development Team
 
15
 *
 
16
 *               This program is free software under the GNU General Public
 
17
 *               License (>=v2). Read the file COPYING that comes with GRASS
 
18
 *               for details.
 
19
 *
 
20
 *****************************************************************************/
 
21
 
 
22
#define _MAIN_C_
 
23
#include <string.h>
 
24
#include <stdlib.h>
 
25
#include <unistd.h>
 
26
#include <grass/gis.h>
 
27
#include <grass/spawn.h>
 
28
#include <grass/glocale.h>
 
29
#include "local_proto.h"
 
30
 
 
31
static void append_mapset(char **, const char *);
 
32
 
 
33
int main(int argc, char *argv[])
 
34
{
 
35
    int n;
 
36
    int i;
 
37
    int skip;
 
38
    const char *cur_mapset;
 
39
    char **ptr;
 
40
    char **tokens;
 
41
    int no_tokens;
 
42
    FILE *fp;
 
43
    char path[GPATH_MAX];
 
44
    char *Path;
 
45
    int nchoices;
 
46
 
 
47
    struct GModule *module;    
 
48
    struct _opt {
 
49
        struct Option *mapset, *add, *remove;
 
50
        struct Option *fs;
 
51
        struct Flag *print;
 
52
        struct Flag *list;
 
53
        struct Flag *dialog;
 
54
    } opt;
 
55
 
 
56
    G_gisinit(argv[0]);
 
57
 
 
58
    module = G_define_module();
 
59
    module->keywords = _("general, settings");
 
60
    module->description =
 
61
        _("Modifies the user's current mapset "
 
62
          "search path, affecting the user's access to data existing "
 
63
          "under the other GRASS mapsets in the current location.");
 
64
 
 
65
    opt.mapset = G_define_option();
 
66
    opt.mapset->key = "mapset";
 
67
    opt.mapset->type = TYPE_STRING;
 
68
    opt.mapset->required = NO;
 
69
    opt.mapset->multiple = YES;
 
70
    opt.mapset->description = _("Name(s) of existing mapset(s)");
 
71
 
 
72
    opt.add = G_define_option();
 
73
    opt.add->key = "addmapset";
 
74
    opt.add->type = TYPE_STRING;
 
75
    opt.add->required = NO;
 
76
    opt.add->multiple = YES;
 
77
    opt.add->description =
 
78
        _("Name(s) of existing mapset(s) to add to search list");
 
79
 
 
80
    opt.remove = G_define_option();
 
81
    opt.remove->key = "removemapset";
 
82
    opt.remove->type = TYPE_STRING;
 
83
    opt.remove->required = NO;
 
84
    opt.remove->multiple = YES;
 
85
    opt.remove->description =
 
86
        _("Name(s) of existing mapset(s) to remove from search list");
 
87
    
 
88
    opt.fs = G_define_standard_option(G_OPT_F_SEP);
 
89
    opt.fs->answer = " ";
 
90
    
 
91
    opt.list = G_define_flag();
 
92
    opt.list->key = 'l';
 
93
    opt.list->description = _("List all available mapsets");
 
94
 
 
95
    opt.print = G_define_flag();
 
96
    opt.print->key = 'p';
 
97
    opt.print->description = _("Print current mapset search path");
 
98
 
 
99
    opt.dialog = G_define_flag();
 
100
    opt.dialog->key = 's';
 
101
    opt.dialog->description = _("Show mapset selection dialog");
 
102
 
 
103
    Path = NULL;
 
104
    nmapsets = 0;
 
105
    nchoices = 0;
 
106
 
 
107
    if (G_parser(argc, argv))
 
108
        exit(EXIT_FAILURE);
 
109
 
 
110
    if (opt.list->answer) {
 
111
        get_available_mapsets();
 
112
        display_available_mapsets(opt.fs->answer);
 
113
    }
 
114
 
 
115
    if (opt.dialog->answer) {
 
116
        sprintf(path, "%s/etc/g.mapsets.tcl", G_gisbase());
 
117
        G_spawn(path, "g.mapsets.tcl", NULL);
 
118
    }
 
119
 
 
120
    if (opt.mapset->answer) {
 
121
        for (ptr = opt.mapset->answers; *ptr != NULL; ptr++) {
 
122
            const char *mapset;
 
123
 
 
124
            mapset = *ptr;
 
125
            if (G__mapset_permissions(mapset) < 0)
 
126
                G_fatal_error(_("Mapset <%s> not found"), mapset);
 
127
            nchoices++;
 
128
            append_mapset(&Path, mapset);
 
129
        }
 
130
    }
 
131
 
 
132
    /* add to existing search path */
 
133
    if (opt.add->answer) {
 
134
        const char *oldname;
 
135
 
 
136
        if (Path) {
 
137
            G_free(Path);
 
138
            Path = NULL;
 
139
        }
 
140
 
 
141
        /* read existing mapsets from SEARCH_PATH */
 
142
        for (n = 0; (oldname = G__mapset_name(n)); n++)
 
143
            append_mapset(&Path, oldname);
 
144
 
 
145
        /* fetch and add new mapsets from param list */
 
146
        for (ptr = opt.add->answers; *ptr != NULL; ptr++) {
 
147
            char *mapset;
 
148
 
 
149
            mapset = *ptr;
 
150
 
 
151
            if (G_is_mapset_in_search_path(mapset))
 
152
                continue;
 
153
            
 
154
            if (G__mapset_permissions(mapset) < 0)
 
155
                G_fatal_error(_("Mapset <%s> not found"), mapset);
 
156
            else
 
157
                G_verbose_message(_("Mapset <%s> added to search path"),
 
158
                                  mapset);
 
159
 
 
160
            nchoices++;
 
161
            append_mapset(&Path, mapset);
 
162
        }
 
163
    }
 
164
 
 
165
    /* remove from existing search path */
 
166
    if (opt.remove->answer) {
 
167
        const char *oldname;
 
168
 
 
169
        if (Path) {
 
170
            G_free(Path);
 
171
            Path = NULL;
 
172
        }
 
173
 
 
174
        /* read existing mapsets from SEARCH_PATH */
 
175
        for (n = 0; (oldname = G__mapset_name(n)); n++) {
 
176
            int found = 0;
 
177
 
 
178
            for (ptr = opt.remove->answers; *ptr; ptr++)
 
179
                if (strcmp(oldname, *ptr) == 0)
 
180
                    found = 1;
 
181
 
 
182
            if (found) {
 
183
                G_verbose_message(_("Mapset <%s> removed from search path"),
 
184
                                  oldname);
 
185
                continue;
 
186
            }
 
187
 
 
188
            nchoices++;
 
189
            append_mapset(&Path, oldname);
 
190
        }
 
191
    }
 
192
 
 
193
    /* stuffem sets nchoices */
 
194
 
 
195
    if (nchoices == 0) {
 
196
        if (opt.print->answer)
 
197
            display_mapset_path(opt.fs->answer);
 
198
 
 
199
        if (Path)
 
200
            G_free(Path);
 
201
 
 
202
        if (nmapsets) {
 
203
            for(nmapsets--; nmapsets >= 0; nmapsets--)
 
204
                G_free(mapset_name[nmapsets]);
 
205
            G_free(mapset_name);
 
206
        }
 
207
 
 
208
        exit(EXIT_SUCCESS);
 
209
    }
 
210
 
 
211
    /* note I'm assuming that mapsets cannot have ' 's in them */
 
212
    tokens = G_tokenize(Path, " ");
 
213
 
 
214
    fp = G_fopen_new("", "SEARCH_PATH");
 
215
    if (!fp)
 
216
        G_fatal_error(_("Cannot open SEARCH_PATH for write"));
 
217
 
 
218
    cur_mapset = G_mapset();
 
219
 
 
220
    /*
 
221
     * make sure current mapset is specified in the list
 
222
     * if not add it to the head of the list
 
223
     */
 
224
 
 
225
    skip = 0;
 
226
    for (n = 0; n < nchoices; n++)
 
227
        if (strcmp(cur_mapset, tokens[n]) == 0) {
 
228
            skip = 1;
 
229
            break;
 
230
        }
 
231
    if (!skip) {
 
232
        fprintf(fp, "%s\n", cur_mapset);
 
233
    }
 
234
 
 
235
    /*
 
236
     * output the list, removing duplicates
 
237
     */
 
238
 
 
239
    no_tokens = G_number_of_tokens(tokens);
 
240
 
 
241
    for (n = 0; n < no_tokens; n++) {
 
242
        skip = 0;
 
243
        for (i = n; i < no_tokens; i++) {
 
244
            if (i != n) {
 
245
                if (strcmp(tokens[i], tokens[n]) == 0)
 
246
                    skip = 1;
 
247
            }
 
248
        }
 
249
 
 
250
        if (!skip)
 
251
            fprintf(fp, "%s\n", tokens[n]);
 
252
    }
 
253
 
 
254
    fclose(fp);
 
255
    G_free_tokens(tokens);
 
256
 
 
257
    if (Path)
 
258
        G_free(Path);
 
259
 
 
260
    if (nmapsets) {
 
261
        for(nmapsets--; nmapsets >= 0; nmapsets--)
 
262
            G_free(mapset_name[nmapsets]);
 
263
        G_free(mapset_name);
 
264
    }
 
265
 
 
266
    exit(EXIT_SUCCESS);
 
267
}
 
268
 
 
269
static void append_mapset(char **path, const char *mapset)
 
270
{
 
271
    int len = (*path == NULL ? 0 : strlen(*path));
 
272
 
 
273
    *path = (char *)G_realloc(*path, len + strlen(mapset) + 2);
 
274
    if (!len)
 
275
        *path[0] = '\0';
 
276
    strcat(*path, mapset);
 
277
    strcat(*path, " ");
 
278
    return;
 
279
}