2
/****************************************************************************
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
16
* This program is free software under the GNU General Public
17
* License (>=v2). Read the file COPYING that comes with GRASS
20
*****************************************************************************/
26
#include <grass/gis.h>
27
#include <grass/spawn.h>
28
#include <grass/glocale.h>
29
#include "local_proto.h"
31
static void append_mapset(char **, const char *);
33
int main(int argc, char *argv[])
38
const char *cur_mapset;
47
struct GModule *module;
49
struct Option *mapset, *add, *remove;
58
module = G_define_module();
59
module->keywords = _("general, settings");
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.");
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)");
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");
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");
88
opt.fs = G_define_standard_option(G_OPT_F_SEP);
91
opt.list = G_define_flag();
93
opt.list->description = _("List all available mapsets");
95
opt.print = G_define_flag();
97
opt.print->description = _("Print current mapset search path");
99
opt.dialog = G_define_flag();
100
opt.dialog->key = 's';
101
opt.dialog->description = _("Show mapset selection dialog");
107
if (G_parser(argc, argv))
110
if (opt.list->answer) {
111
get_available_mapsets();
112
display_available_mapsets(opt.fs->answer);
115
if (opt.dialog->answer) {
116
sprintf(path, "%s/etc/g.mapsets.tcl", G_gisbase());
117
G_spawn(path, "g.mapsets.tcl", NULL);
120
if (opt.mapset->answer) {
121
for (ptr = opt.mapset->answers; *ptr != NULL; ptr++) {
125
if (G__mapset_permissions(mapset) < 0)
126
G_fatal_error(_("Mapset <%s> not found"), mapset);
128
append_mapset(&Path, mapset);
132
/* add to existing search path */
133
if (opt.add->answer) {
141
/* read existing mapsets from SEARCH_PATH */
142
for (n = 0; (oldname = G__mapset_name(n)); n++)
143
append_mapset(&Path, oldname);
145
/* fetch and add new mapsets from param list */
146
for (ptr = opt.add->answers; *ptr != NULL; ptr++) {
151
if (G_is_mapset_in_search_path(mapset))
154
if (G__mapset_permissions(mapset) < 0)
155
G_fatal_error(_("Mapset <%s> not found"), mapset);
157
G_verbose_message(_("Mapset <%s> added to search path"),
161
append_mapset(&Path, mapset);
165
/* remove from existing search path */
166
if (opt.remove->answer) {
174
/* read existing mapsets from SEARCH_PATH */
175
for (n = 0; (oldname = G__mapset_name(n)); n++) {
178
for (ptr = opt.remove->answers; *ptr; ptr++)
179
if (strcmp(oldname, *ptr) == 0)
183
G_verbose_message(_("Mapset <%s> removed from search path"),
189
append_mapset(&Path, oldname);
193
/* stuffem sets nchoices */
196
if (opt.print->answer)
197
display_mapset_path(opt.fs->answer);
203
for(nmapsets--; nmapsets >= 0; nmapsets--)
204
G_free(mapset_name[nmapsets]);
211
/* note I'm assuming that mapsets cannot have ' 's in them */
212
tokens = G_tokenize(Path, " ");
214
fp = G_fopen_new("", "SEARCH_PATH");
216
G_fatal_error(_("Cannot open SEARCH_PATH for write"));
218
cur_mapset = G_mapset();
221
* make sure current mapset is specified in the list
222
* if not add it to the head of the list
226
for (n = 0; n < nchoices; n++)
227
if (strcmp(cur_mapset, tokens[n]) == 0) {
232
fprintf(fp, "%s\n", cur_mapset);
236
* output the list, removing duplicates
239
no_tokens = G_number_of_tokens(tokens);
241
for (n = 0; n < no_tokens; n++) {
243
for (i = n; i < no_tokens; i++) {
245
if (strcmp(tokens[i], tokens[n]) == 0)
251
fprintf(fp, "%s\n", tokens[n]);
255
G_free_tokens(tokens);
261
for(nmapsets--; nmapsets >= 0; nmapsets--)
262
G_free(mapset_name[nmapsets]);
269
static void append_mapset(char **path, const char *mapset)
271
int len = (*path == NULL ? 0 : strlen(*path));
273
*path = (char *)G_realloc(*path, len + strlen(mapset) + 2);
276
strcat(*path, mapset);