[GRASS-SVN] r51471 - grass/trunk/general/g.mapsets

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Apr 18 11:44:55 EDT 2012


Author: martinl
Date: 2012-04-18 08:44:54 -0700 (Wed, 18 Apr 2012)
New Revision: 51471

Modified:
   grass/trunk/general/g.mapsets/g.mapsets.html
   grass/trunk/general/g.mapsets/g.mapsets_picker.py
   grass/trunk/general/g.mapsets/get_maps.c
   grass/trunk/general/g.mapsets/list.c
   grass/trunk/general/g.mapsets/local_proto.h
   grass/trunk/general/g.mapsets/main.c
Log:
g.mapsets: change syntax - addmapsets,removemapsets replaced by operation
           avoid global variables
           update manual page accordingly
           untabify


Modified: grass/trunk/general/g.mapsets/g.mapsets.html
===================================================================
--- grass/trunk/general/g.mapsets/g.mapsets.html	2012-04-18 11:01:05 UTC (rev 51470)
+++ grass/trunk/general/g.mapsets/g.mapsets.html	2012-04-18 15:44:54 UTC (rev 51471)
@@ -1,112 +1,141 @@
 <h2>DESCRIPTION</h2>
 
-For basic information about Grass <em>mapset</em>, <em>location</em>
-and <em>data base</em> refer to <a href="helptext.html">GRASS Quickstart</a>.
+For basic information about GRASS <em>mapset</em>, <em>location</em>
+and <em>data base</em> refer to <a href="helptext.html">GRASS
+Quickstart</a>.
 
-<p>A <em>mapset</em> holds a distinct set of data layers,
-each relevant to the same (or a subset of the same) geographic region,
-and each drawn in the same map coordinate system.
-At the outset of every GRASS session, the user identifies a
-GRASS data base, location, and mapset that are to be the
-user's <em>current data base</em>, <em>current location</em>,
-and <em>current mapset</em> for the duration of the session;
-any maps created by the user during the session
-will be stored under the <em>current mapset</em> set at the session's outset.
-(see <em><a href="g.mapset.html">g.mapset</a></em> [without an "s"] and
- <em><a href="g.gisenv.html">g.gisenv</a></em> for changing the mapset
- with a session)
+<p>
+A <em>mapset</em> holds a distinct set of data layers, each relevant
+to the same (or a subset of the same) geographic region, and each
+drawn in the same map coordinate system.  At the outset of every GRASS
+session, the user identifies a GRASS data base, location, and mapset
+that are to be the user's <em>current data base</em>, <em>current
+location</em>, and <em>current mapset</em> for the duration of the
+session; any maps created by the user during the session will be
+stored under the <em>current mapset</em> set at the session's outset
+(see <em><a href="g.mapset.html">g.mapset</a></em> [without an "s"]
+and <em><a href="g.gisenv.html">g.gisenv</a></em> for changing the
+mapset with a session).
 
-<p>The user can add, modify, and delete data layers that exist
-under his <em>current mapset</em>.  Although the user can
+<p>
+The user can add, modify, and delete data layers that exist under
+his <em>current mapset</em>. Although the user can
 also <em>access</em> (i.e., use) data that are stored under
 <em>other</em> mapsets in the same GRASS location using the
 <tt>mapname at mapsetname</tt> notation or mapset search path, the user
 can only make permanent changes (create or modify data)
 located in the <em>current mapset</em>.  The user's
-<em>mapset search path</em> lists the order in which other
-mapsets in the same GRASS location can be searched and
-their data accessed by the user.  The user can modify the
-listing and order in which these mapsets are accessed by
-modifying the mapset search path; this can be done using
-the <em>g.mapsets</em> command. This program allows the
-user to use other's relevant map data without altering the
-original data layer, and without taking up disk space with
-a copy of the original map. The <tt>mapname at mapsetname</tt>
-notation may be used irrespective of the mapset search path,
-i.e., any map found in another mapset with sufficient
-<em><a href="g.access.html">g.access</a></em> privileges may
-be called in such a manner.
+<em>mapset search path</em> lists the order in which other mapsets in
+the same GRASS location can be searched and their data accessed by the
+user. The user can modify the listing and order in which these mapsets
+are accessed by modifying the mapset search path; this can be done
+using the <em>g.mapsets</em> command. This program allows the user to
+use other's relevant map data without altering the original data
+layer, and without taking up disk space with a copy of the original
+map. The <tt>mapname at mapsetname</tt> notation may be used irrespective
+of the mapset search path, i.e., any map found in another mapset with
+sufficient <em><a href="g.access.html">g.access</a></em> privileges
+may be called in such a manner.
 
+<p>
+<em>g.mapsets</em> shows the user available mapsets under the current
+GRASS location, lists mapsets to which the user currently has access,
+and lists the order in which accessible mapsets will be accessed by
+GRASS programs searching for data files.  The user is then given the
+opportunity to add or delete mapset names from his search path, or
+modify the order in which mapsets will be accessed.
 
-<p><em>g.mapsets</em> shows the user available mapsets under
-the current GRASS location, lists mapsets to which the user
-currently has access, and lists the order in which
-accessible mapsets will be accessed by GRASS programs
-searching for data files.  The user is then given the
-opportunity to add or delete mapset names from his search
-path, or modify the order in which mapsets will be
-accessed.
+<p>
+When the user specifies the name of a data base element file (e.g., a
+particular vector map, raster map, <a href="i.group.html">imagery</a>
+group file, etc.) to a GRASS program, the program searches for the
+named file under each of the mapsets listed in the user's mapset
+search path in the order listed there until the program finds a file
+of the given name. Users can also specify a file by its mapset, to
+make explicit the mapset from which the file is to be drawn; e.g., the
+command:
 
-<p>When the user specifies the name of a data base element file
-(e.g., a particular vector map, raster map, 
-<a href="i.group.html">imagery</a> group file, etc.)
-to a GRASS program, the program searches for the named file
-under each of the mapsets listed in the user's mapset search path
-in the order listed there until the program finds a file
-of the given name.  (Users can also specify a file by
-its mapset, to make explicit the mapset from which the file is to be
-drawn;  e.g., the command:
+<div class="code"><pre>
+g.copy rast=soils.file at PERMANENT,my.soils
+</pre></div>
 
+ensures that a new file named <tt>my.soils</tt> is to be a copy of
+the file <tt>soils.file</tt> from the mapset PERMANENT.
 
+<p>
+It is common for a user to have the special mapset
+<b>PERMANENT</b> included in his mapset search path, as this mapset
+typically contains finished base maps relevant to many
+applications. Often, other mapsets which contain sets of interpreted
+maps will be likewise included in the user's mapset search path.
+Suppose, for example, that the mapset <em>Soil_Maps</em> contains
+interpreted soils map layers to which the user wants access. The
+mapset <em>Soil_Maps</em> should then be included in the user's
+<em>search path</em> variable.
+
+<p>
+The <em>mapset search path</em> is saved as part of the current
+mapset. When the user works with that mapset in subsequent GRASS
+sessions, the previously saved mapset search path will be used (and
+will continue to be used until it is modified by the user
+with <em>g.mapsets</em>).
+
+<h2>NOTES</h2>
+
+By default <em>g.mapsets</em> adds to the current <em>mapset search
+path</em> mapsets named by <b>mapset</b> option. Alternatively mapsets
+can be removed (<b>operation=remove</b>) from the search path or
+defined by <b>operation=set</b>.
+
+<p>
+Users can restrict others' access to their mapset files through use
+of <em><a href="g.access.html">g.access</a></em>. Mapsets to which
+access is restricted can still be listed in another's mapset search
+path; however, access to these mapsets will remain restricted.
+
+<h2>EXAMPLES</h2>
+
+All available mapsets in the current location can be printed out by
+
 <div class="code"><pre>
-<b><a href="g.copy.html">g.copy</a> rast=</b><em>'soils.file at PERMANENT',my.soils</em>
+g.mapsets -l
+
+Available mapsets:
+PERMANENT user1 user2
 </pre></div>
 
+Add mapset 'user2' to the current mapset search path
 
-ensures that a new file named <em>my.soils</em> is to be a copy of
-the file <em>soils.file</em> from the mapset PERMANENT.)
+<div class="code"><pre>
+g.mapsets mapset=user2 operation=add
+</pre></div>
 
-<p>It is common for a user to have the special mapset
-<b>PERMANENT</b> included in his mapset search path, as
-this mapset typically contains finished base maps relevant
-to many applications.  Often, other mapsets which contain
-sets of interpreted map layers will be likewise included in
-the user's mapset search path.  Suppose, for example, that
-the mapset <em>Soil_Maps</em> contains interpreted soils
-map layers to which the user wants access.  The mapset
+The current mapset search path is changed accordingly
 
-<em>Soil_Maps</em> should then be included in the user's
-<em>search path</em> variable.
+<div class="code"><pre>
+g.mapsets -p
 
-<p>The <em>mapset search path</em> is saved as part of the
-current mapset.  When the user works with that mapset in
-subsequent GRASS sessions, the previously saved mapset
-search path will be used (and will continue to be used
-until it is modified by the user with <em>g.mapsets</em>).
+Accessible mapsets:
+user1 user2
+</pre></div>
 
+Overwrite current search path
 
-<h2>NOTES</h2>
+<div class="code"><pre>
+g.mapsets mapset=user1,PERMANENT operation=set
+</pre></div>
 
-<em>g.mapsets</em> sets the current <em>mapset search
-path</em> to the <em>mapsets</em> named on the command
-line. If <em>g.mapsets</em> is typed but no <em>mapset</em>
-names are specified by the user on the command line, the
-program will print the user's current mapset search path,
-list available mapsets, and prompt the user for a new
-mapset search path listing.
+<i>Note:</i> The current mapset will be always included in the search
+path on the first position even if you change its position or omit the
+current mapset from the <b>mapset</b> option.
 
-<p>The <em>addmapset</em> parameter allows for extending an existing
-<em>mapset search path</em>.
+<div class="code"><pre>
+g.mapsets -p
 
-<p>Users can restrict others' access to their mapset files
-through use of the GRASS program
+Accessible mapsets:
+user1 PERMANENT
+</pre></div>
 
-<em><a href="g.access.html">g.access</a></em>.  
-
-Mapsets to which access is restricted can still be listed
-in another's mapset search path; however, access to these
-mapsets will remain restricted.
-
 <h2>SEE ALSO</h2>
 
 <em>
@@ -119,10 +148,9 @@
 
 <h2>AUTHOR</h2>
 
-Michael Shapiro,
-U.S.Army Construction Engineering 
-Research Laboratory
-<p>Greg Koerper, 
-ManTech Environmental Technology, Inc.
+Michael Shapiro, U.S.Army Construction Engineering Research Laboratory<br>
+Greg Koerper, ManTech Environmental Technology, Inc.<br>
+Updated to GRASS 7 by Martin Landa, Czech Technical University in Prague, Czech Republic
 
-<p><i>Last changed: $Date$</i>
+<p>
+<i>Last changed: $Date$</i>

Modified: grass/trunk/general/g.mapsets/g.mapsets_picker.py
===================================================================
--- grass/trunk/general/g.mapsets/g.mapsets_picker.py	2012-04-18 11:01:05 UTC (rev 51470)
+++ grass/trunk/general/g.mapsets/g.mapsets_picker.py	2012-04-18 15:44:54 UTC (rev 51471)
@@ -20,18 +20,18 @@
         sizer = wx.BoxSizer(wx.VERTICAL)
         
         # list of mapsets
-        self.mapsetlb = CheckListMapset(parent=self)
+        self.mapsetlb = CheckListMapset(parent = self)
         self.mapsetlb.LoadData()
         
-        sizer.Add(item=self.mapsetlb, proportion=1,
-                  flag=wx.ALL | wx.EXPAND, border=5)
+        sizer.Add(item = self.mapsetlb, proportion = 1,
+                  flag = wx.ALL | wx.EXPAND, border = 5)
 
         
         # dialog buttons
-        line = wx.StaticLine(parent=self, id=wx.ID_ANY,
-                             style=wx.LI_HORIZONTAL)
-        sizer.Add(item=line, proportion=0,
-                  flag=wx.EXPAND | wx.ALIGN_CENTRE | wx.ALL, border=5)
+        line = wx.StaticLine(parent = self, id = wx.ID_ANY,
+                             style = wx.LI_HORIZONTAL)
+        sizer.Add(item = line, proportion = 0,
+                  flag = wx.EXPAND | wx.ALIGN_CENTRE | wx.ALL, border = 5)
         
         btnsizer = wx.StdDialogButtonSizer()
         btnOK = wx.Button(self, wx.ID_OK)
@@ -46,8 +46,8 @@
         btnsizer.AddButton(btnCancel)
         btnsizer.Realize()
         
-        sizer.Add(item=btnsizer, proportion=0,
-                  flag=wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border=5)
+        sizer.Add(item = btnsizer, proportion = 0,
+                  flag = wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border = 5)
         
         # do layout
         self.Layout()
@@ -75,18 +75,19 @@
 
         grass.run_command('g.mapsets',
                           quiet = True,
-                          mapset = mapsets)
+                          mapset = mapsets,
+                          operation = 'set')
         
         self.Close()
         
 class CheckListMapset(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.CheckListCtrlMixin):
     """List of mapset/owner/group"""
-    def __init__(self, parent, pos=wx.DefaultPosition,
-                 log=None):
+    def __init__(self, parent, pos = wx.DefaultPosition,
+                 log = None):
         self.parent = parent
         
         wx.ListCtrl.__init__(self, parent, wx.ID_ANY,
-                             style=wx.LC_REPORT)
+                             style = wx.LC_REPORT)
         listmix.CheckListCtrlMixin.__init__(self)
         self.log = log
         
@@ -125,11 +126,11 @@
             mapsetPath = os.path.join(locationPath,
                                       mapset)
             stat_info = os.stat(mapsetPath)
-	    if os.name in ('posix', 'mac'):
+            if os.name in ('posix', 'mac'):
                 self.SetStringItem(index, 1, "%s" % pwd.getpwuid(stat_info.st_uid)[0])
                 # FIXME: get group name
                 ### self.SetStringItem(index, 2, "%-8s" % stat_info.st_gid) 
-	    else:
+            else:
                 # FIXME: no pwd under MS Windows (owner: 0, group: 0)
                 self.SetStringItem(index, 1, "%-8s" % stat_info.st_uid)
                 ### self.SetStringItem(index, 2, "%-8s" % stat_info.st_gid)
@@ -137,8 +138,8 @@
             if mapset in mapsets_access:
                 self.CheckItem(self.mapsets.index(mapset), True)
             
-        self.SetColumnWidth(col=0, width=wx.LIST_AUTOSIZE)
-        self.SetColumnWidth(col=1, width=wx.LIST_AUTOSIZE)
+        self.SetColumnWidth(col = 0, width = wx.LIST_AUTOSIZE)
+        self.SetColumnWidth(col = 1, width = wx.LIST_AUTOSIZE)
 
     def OnCheckItem(self, index, flag):
         """Mapset checked/unchecked"""

Modified: grass/trunk/general/g.mapsets/get_maps.c
===================================================================
--- grass/trunk/general/g.mapsets/get_maps.c	2012-04-18 11:01:05 UTC (rev 51470)
+++ grass/trunk/general/g.mapsets/get_maps.c	2012-04-18 15:44:54 UTC (rev 51471)
@@ -6,25 +6,27 @@
 
 static int cmp(const void *, const void *);
 
-int get_available_mapsets(void)
+char** get_available_mapsets(int *nmapsets)
 {
-    char **ms;
-    int i;
+    char **ms, **mapset_name;
+    int i, n;
 
     ms = G_available_mapsets();
-    for (nmapsets = 0; ms[nmapsets]; nmapsets++);
+    for (n = 0; ms[n]; n++);
 
-    mapset_name = (char **)G_malloc(nmapsets*sizeof(char *));
-    for(i = 0; i < nmapsets; i++)
-	mapset_name[i] = G_store(ms[i]);
+    mapset_name = (char **)G_malloc(n * sizeof(char *));
+    for(i = 0; i < n; i++)
+        mapset_name[i] = G_store(ms[i]);
 
     /* sort mapsets */
-    qsort(mapset_name, nmapsets, sizeof(char *), cmp);
+    qsort(mapset_name, n, sizeof(char *), cmp);
 
-    return 0;
+    *nmapsets = n;
+    
+    return mapset_name;
 }
 
-static int cmp(const void *a, const void *b) 
+int cmp(const void *a, const void *b) 
 {
     return (strcmp(*(char **)a, *(char **)b));
 }

Modified: grass/trunk/general/g.mapsets/list.c
===================================================================
--- grass/trunk/general/g.mapsets/list.c	2012-04-18 11:01:05 UTC (rev 51470)
+++ grass/trunk/general/g.mapsets/list.c	2012-04-18 15:44:54 UTC (rev 51471)
@@ -4,25 +4,25 @@
 #include <grass/glocale.h>
 #include "local_proto.h"
 
-void list_available_mapsets(const char* fs)
+void list_available_mapsets(const char **mapset_name, int nmapsets, const char* fs)
 {
     int  n;
     G_message(_("Available mapsets:"));
     
     for (n = 0; n < nmapsets; n++) {
-	fprintf(stdout, "%s", mapset_name[n]);
-	if (n < nmapsets-1) {
-	    if (strcmp(fs, "newline") == 0)
-		fprintf(stdout, "\n");
-	    else if (strcmp(fs, "space") == 0)
-		fprintf(stdout, " ");
-	    else if (strcmp(fs, "comma") == 0)
-		fprintf(stdout, ",");
-	    else if (strcmp(fs, "tab") == 0)
-		fprintf(stdout, "\t");
-	    else
-		fprintf(stdout, "%s", fs);
-	}
+        fprintf(stdout, "%s", mapset_name[n]);
+        if (n < nmapsets-1) {
+            if (strcmp(fs, "newline") == 0)
+                fprintf(stdout, "\n");
+            else if (strcmp(fs, "space") == 0)
+                fprintf(stdout, " ");
+            else if (strcmp(fs, "comma") == 0)
+                fprintf(stdout, ",");
+            else if (strcmp(fs, "tab") == 0)
+                fprintf(stdout, "\t");
+            else
+                fprintf(stdout, "%s", fs);
+        }
     }
     fprintf(stdout, "\n");
 }
@@ -34,20 +34,20 @@
     
     G_message(_("Accessible mapsets:"));
     for (n = 0; (name = G__mapset_name(n)); n++) {
-	/* match each mapset to its numeric equivalent */
-	fprintf(stdout, "%s", name);
-	if (G__mapset_name(n+1)) {
-	    if (strcmp(fs, "newline") == 0)
-		fprintf(stdout, "\n");
-	    else if (strcmp(fs, "space") == 0)
-		fprintf(stdout, " ");
-	    else if (strcmp(fs, "comma") == 0)
-		fprintf(stdout, ",");
-	    else if (strcmp(fs, "tab") == 0)
-		fprintf(stdout, "\t");
-	    else
-		fprintf(stdout, "%s", fs);
-	}
+        /* match each mapset to its numeric equivalent */
+        fprintf(stdout, "%s", name);
+        if (G__mapset_name(n+1)) {
+            if (strcmp(fs, "newline") == 0)
+                fprintf(stdout, "\n");
+            else if (strcmp(fs, "space") == 0)
+                fprintf(stdout, " ");
+            else if (strcmp(fs, "comma") == 0)
+                fprintf(stdout, ",");
+            else if (strcmp(fs, "tab") == 0)
+                fprintf(stdout, "\t");
+            else
+                fprintf(stdout, "%s", fs);
+        }
     }
     fprintf(stdout, "\n");
 }

Modified: grass/trunk/general/g.mapsets/local_proto.h
===================================================================
--- grass/trunk/general/g.mapsets/local_proto.h	2012-04-18 11:01:05 UTC (rev 51470)
+++ grass/trunk/general/g.mapsets/local_proto.h	2012-04-18 15:44:54 UTC (rev 51471)
@@ -1,15 +1,6 @@
-#ifdef _MAIN_C_
-#define GLOBAL
-#else
-#define GLOBAL extern
-#endif
-
-GLOBAL char **mapset_name;
-GLOBAL int nmapsets;
-
 /* get_maps.c */
-int get_available_mapsets(void);
+char** get_available_mapsets(int *);
 
 /* list.c */
-void list_available_mapsets(const char *);
+void list_available_mapsets(const char **, int, const char *);
 void list_accessible_mapsets(const char *);

Modified: grass/trunk/general/g.mapsets/main.c
===================================================================
--- grass/trunk/general/g.mapsets/main.c	2012-04-18 11:01:05 UTC (rev 51470)
+++ grass/trunk/general/g.mapsets/main.c	2012-04-18 15:44:54 UTC (rev 51471)
@@ -2,8 +2,8 @@
 /****************************************************************************
  *
  * MODULE:       g.mapsets
- * AUTHOR(S):    Michael Shapiro (CERL), Greg Koerper (ManTech Environmental
- *                    Technology) (original contributors), 
+ * AUTHOR(S):    Michael Shapiro (CERL),
+ *               Greg Koerper (ManTech Environmental Technology) (original contributors), 
  *               Glynn Clements <glynn gclements.plus.com>
  *               Hamish Bowman <hamish_b yahoo.com>, 
  *               Markus Neteler <neteler itc.it>, 
@@ -11,15 +11,14 @@
  *               Martin Landa <landa.martin gmail.com>,
  *               Huidae Cho <grass4u gmail.com>
  * PURPOSE:      set current mapset path
- * COPYRIGHT:    (C) 1994-2009 by the GRASS Development Team
+ * COPYRIGHT:    (C) 1994-2009, 2012 by the GRASS Development Team
  *
- *               This program is free software under the GNU General Public
- *               License (>=v2). Read the file COPYING that comes with GRASS
- *               for details.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  *****************************************************************************/
 
-#define _MAIN_C_
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -28,29 +27,33 @@
 #include <grass/glocale.h>
 #include "local_proto.h"
 
+#define OP_UKN 0
+#define OP_SET 1
+#define OP_ADD 2
+#define OP_REM 3
+
 static void append_mapset(char **, const char *);
 
 int main(int argc, char *argv[])
 {
-    int n;
-    int i;
+    int n, i;
     int skip;
     const char *cur_mapset;
     char **ptr;
     char **tokens;
     int no_tokens;
     FILE *fp;
-    char path[GPATH_MAX];
-    char *Path;
-    int nchoices;
-
+    char path_buf[GPATH_MAX];
+    char *path;
+    int operation, nchoices;
+    
+    char **mapset_name;
+    int nmapsets;
+    
     struct GModule *module;    
     struct _opt {
-	struct Option *mapset, *add, *remove;
-	struct Option *fs;
-	struct Flag *print;
-	struct Flag *list;
-	struct Flag *dialog;
+        struct Option *mapset, *op, *fs;
+        struct Flag *print, *list, *dialog;
     } opt;
 
     G_gisinit(argv[0]);
@@ -59,183 +62,227 @@
     G_add_keyword(_("general"));
     G_add_keyword(_("settings"));
     G_add_keyword(_("search path"));
-    module->label = _("Modifies the user's current mapset search path.");
+    module->label = _("Modifies/prints the user's current mapset search path.");
     module->description = _("Affects the user's access to data existing "
-			    "under the other mapsets in the current location.");
+                            "under the other mapsets in the current location.");
 
     opt.mapset = G_define_option();
     opt.mapset->key = "mapset";
     opt.mapset->type = TYPE_STRING;
-    opt.mapset->required = NO;
+    opt.mapset->required = YES;
     opt.mapset->multiple = YES;
     opt.mapset->description = _("Name(s) of existing mapset(s)");
     opt.mapset->guisection = _("Search path");
     
-    opt.add = G_define_option();
-    opt.add->key = "addmapset";
-    opt.add->type = TYPE_STRING;
-    opt.add->required = NO;
-    opt.add->multiple = YES;
-    opt.add->description =
-	_("Name(s) of existing mapset(s) to add to search path");
-    opt.add->guisection = _("Search path");
-
-    opt.remove = G_define_option();
-    opt.remove->key = "removemapset";
-    opt.remove->type = TYPE_STRING;
-    opt.remove->required = NO;
-    opt.remove->multiple = YES;
-    opt.remove->description =
-	_("Name(s) of existing mapset(s) to remove from search path");
-    opt.remove->guisection = _("Search path");
-
+    opt.op = G_define_option();
+    opt.op->key = "operation";
+    opt.op->type = TYPE_STRING;
+    opt.op->required = YES;
+    opt.op->multiple = NO;
+    opt.op->options = "set,add,remove";
+    opt.op->description = _("Operation to perform");
+    opt.op->guisection = _("Search path");
+    opt.op->answer = "add";
+    
     opt.fs = G_define_standard_option(G_OPT_F_SEP);
+    opt.fs->label = _("Field separator for printing (-l and -p flags)");
     opt.fs->answer = "space";
+    opt.fs->guisection = _("Print");
     
     opt.list = G_define_flag();
     opt.list->key = 'l';
     opt.list->description = _("List all available mapsets in alphabetical order");
     opt.list->guisection = _("Print");
+    opt.list->suppress_required = YES;
 
     opt.print = G_define_flag();
     opt.print->key = 'p';
     opt.print->description = _("Print mapsets in current search path");
     opt.print->guisection = _("Print");
+    opt.print->suppress_required = YES;
 
     opt.dialog = G_define_flag();
     opt.dialog->key = 's';
-    opt.dialog->description = _("Show mapset selection dialog");
+    opt.dialog->description = _("Launch mapset selection GUI dialog");
+    opt.dialog->suppress_required = YES;
+    
+    path = NULL;
+    mapset_name = NULL;
+    nmapsets = nchoices = 0;
 
-    Path = NULL;
-    nmapsets = 0;
-    nchoices = 0;
-
     if (G_parser(argc, argv))
-	exit(EXIT_FAILURE);
+        exit(EXIT_FAILURE);
 
+    operation = OP_UKN;
+    if (opt.mapset->answer && opt.op->answer) {
+        switch(opt.op->answer[0]) {
+        case 's':
+            operation = OP_SET;
+            break;
+        case 'a':
+            operation = OP_ADD;
+            break;
+        case 'r':
+            operation = OP_REM;
+            break;
+        default:
+            G_fatal_error(_("Unknown operation '%s'"), opt.op->answer);
+            break;
+        }
+    }
+    
+    /* list available mapsets */
     if (opt.list->answer) {
-	get_available_mapsets();
-	list_available_mapsets(opt.fs->answer);
+        if (opt.print->answer)
+            G_warning(_("Flag -%c ignored"), opt.print->key);
+        if (opt.dialog->answer)
+            G_warning(_("Flag -%c ignored"), opt.dialog->key);
+        if (opt.mapset->answer)
+            G_warning(_("Option <%s> ignored"), opt.mapset->key);
+        mapset_name = get_available_mapsets(&nmapsets);
+        list_available_mapsets((const char **)mapset_name, nmapsets, opt.fs->answer);
+        exit(EXIT_SUCCESS);
     }
 
+    if (opt.print->answer) {
+        if (opt.dialog->answer)
+            G_warning(_("Flag -%c ignored"), opt.dialog->key);
+        if (opt.mapset->answer)
+            G_warning(_("Option <%s> ignored"), opt.mapset->key);
+        list_accessible_mapsets(opt.fs->answer);
+        exit(EXIT_SUCCESS);
+    }
+    
+    /* show GUI dialog */
     if (opt.dialog->answer) {
-	sprintf(path, "%s/etc/gui/scripts/g.mapsets_picker.py", G_gisbase());
-	G_spawn(getenv("GRASS_PYTHON"), "g.mapsets_picker.py", path, NULL);
+        if (opt.mapset->answer)
+            G_warning(_("Option <%s> ignored"), opt.mapset->key);
+        sprintf(path_buf, "%s/etc/gui/scripts/g.mapsets_picker.py", G_gisbase());
+        G_spawn(getenv("GRASS_PYTHON"), "g.mapsets_picker.py", path_buf, NULL);
+        exit(EXIT_SUCCESS);
     }
 
-    if (opt.mapset->answer) {
-	for (ptr = opt.mapset->answers; *ptr != NULL; ptr++) {
-	    const char *mapset;
-
-	    mapset = *ptr;
-	    if (G__mapset_permissions(mapset) < 0)
-		G_fatal_error(_("Mapset <%s> not found"), mapset);
-	    nchoices++;
-	    append_mapset(&Path, mapset);
-	}
+    cur_mapset = G_mapset();
+    
+    /* modify search path */
+    if (operation == OP_SET) {
+        const char *mapset;
+        int cur_found;
+        
+        cur_found = FALSE;
+        for (ptr = opt.mapset->answers; *ptr != NULL; ptr++) {
+            mapset = *ptr;
+            if (G__mapset_permissions(mapset) < 0)
+                G_fatal_error(_("Mapset <%s> not found"), mapset);
+            if (strcmp(mapset, cur_mapset) == 0)
+                cur_found = TRUE;
+            nchoices++;
+            append_mapset(&path, mapset);
+        }
+        if (!cur_found)
+            G_warning(_("Current mapset (<%s>) must always included in the search path"),
+                      cur_mapset);
     }
+    else if (operation == OP_ADD) {
+        /* add to existing search path */
+        const char *oldname;
+        
+        if (path) {
+            G_free(path);
+            path = NULL;
+        }
 
-    /* add to existing search path */
-    if (opt.add->answer) {
-	const char *oldname;
+        /* read existing mapsets from SEARCH_PATH */
+        for (n = 0; (oldname = G__mapset_name(n)); n++)
+            append_mapset(&path, oldname);
 
-	if (Path) {
-	    G_free(Path);
-	    Path = NULL;
-	}
+        /* fetch and add new mapsets from param list */
+        for (ptr = opt.mapset->answers; *ptr != NULL; ptr++) {
+            char *mapset;
 
-	/* read existing mapsets from SEARCH_PATH */
-	for (n = 0; (oldname = G__mapset_name(n)); n++)
-	    append_mapset(&Path, oldname);
+            mapset = *ptr;
 
-	/* fetch and add new mapsets from param list */
-	for (ptr = opt.add->answers; *ptr != NULL; ptr++) {
-	    char *mapset;
+            if (G_is_mapset_in_search_path(mapset))
+                continue;
+            
+            if (G__mapset_permissions(mapset) < 0)
+                G_fatal_error(_("Mapset <%s> not found"), mapset);
+            else
+                G_verbose_message(_("Mapset <%s> added to search path"),
+                                  mapset);
 
-	    mapset = *ptr;
-
-	    if (G_is_mapset_in_search_path(mapset))
-		continue;
-	    
-	    if (G__mapset_permissions(mapset) < 0)
-		G_fatal_error(_("Mapset <%s> not found"), mapset);
-	    else
-		G_verbose_message(_("Mapset <%s> added to search path"),
-				  mapset);
-
-	    nchoices++;
-	    append_mapset(&Path, mapset);
-	}
+            nchoices++;
+            append_mapset(&path, mapset);
+        }
     }
-
-    /* remove from existing search path */
-    if (opt.remove->answer) {
-	const char *oldname;
-
-	if (Path) {
-	    G_free(Path);
-	    Path = NULL;
-	}
-
-	/* read existing mapsets from SEARCH_PATH */
-	for (n = 0; (oldname = G__mapset_name(n)); n++) {
-	    int found = 0;
-
-	    for (ptr = opt.remove->answers; *ptr; ptr++)
-		if (strcmp(oldname, *ptr) == 0)
-		    found = 1;
-
-	    if (found) {
-		G_verbose_message(_("Mapset <%s> removed from search path"),
-				  oldname);
-		continue;
-	    }
-
-	    nchoices++;
-	    append_mapset(&Path, oldname);
-	}
+    else if (operation == OP_REM) {
+        /* remove from existing search path */
+        const char *oldname;
+        int found;
+        
+        if (path) {
+            G_free(path);
+            path = NULL;
+        }
+        
+        /* read existing mapsets from SEARCH_PATH */
+        for (n = 0; (oldname = G__mapset_name(n)); n++) {
+            found = FALSE;
+            
+            for (ptr = opt.mapset->answers; *ptr && !found; ptr++)
+                if (strcmp(oldname, *ptr) == 0)
+                    found = TRUE;
+            
+            if (found) {
+                if (strcmp(oldname, cur_mapset) == 0)
+                    G_warning(_("Current mapset (<%s>) must always included in the search path"),
+                              cur_mapset);
+                else
+                    G_verbose_message(_("Mapset <%s> removed from search path"),
+                                      oldname);
+                continue;
+            }
+            
+            nchoices++;
+            append_mapset(&path, oldname);
+        }
     }
-
     /* stuffem sets nchoices */
 
     if (nchoices == 0) {
-	if (opt.print->answer)
-	    list_accessible_mapsets(opt.fs->answer);
-
-	if (Path)
-	    G_free(Path);
-
-	if (nmapsets) {
-	    for(nmapsets--; nmapsets >= 0; nmapsets--)
-		G_free(mapset_name[nmapsets]);
-	    G_free(mapset_name);
-	}
-
-	exit(EXIT_SUCCESS);
+        G_important_message(_("Search path not modified"));
+        if (path)
+            G_free(path);
+        
+        if (nmapsets) {
+            for(nmapsets--; nmapsets >= 0; nmapsets--)
+                G_free(mapset_name[nmapsets]);
+            G_free(mapset_name);
+        }
+        
+        exit(EXIT_SUCCESS);
     }
-
+    
     /* note I'm assuming that mapsets cannot have ' 's in them */
-    tokens = G_tokenize(Path, " ");
+    tokens = G_tokenize(path, " ");
 
     fp = G_fopen_new("", "SEARCH_PATH");
     if (!fp)
-	G_fatal_error(_("Cannot open SEARCH_PATH for write"));
+        G_fatal_error(_("Unable to open SEARCH_PATH for write"));
 
-    cur_mapset = G_mapset();
-
     /*
-     * make sure current mapset is specified in the list
-     * if not add it to the head of the list
+     * make sure current mapset is specified in the list if not add it
+     * to the head of the list
      */
-
+    
     skip = 0;
     for (n = 0; n < nchoices; n++)
-	if (strcmp(cur_mapset, tokens[n]) == 0) {
-	    skip = 1;
-	    break;
-	}
+        if (strcmp(cur_mapset, tokens[n]) == 0) {
+            skip = 1;
+            break;
+        }
     if (!skip) {
-	fprintf(fp, "%s\n", cur_mapset);
+        fprintf(fp, "%s\n", cur_mapset);
     }
 
     /*
@@ -245,34 +292,34 @@
     no_tokens = G_number_of_tokens(tokens);
 
     for (n = 0; n < no_tokens; n++) {
-	skip = 0;
-	for (i = n; i < no_tokens; i++) {
-	    if (i != n) {
-		if (strcmp(tokens[i], tokens[n]) == 0)
-		    skip = 1;
-	    }
-	}
+        skip = 0;
+        for (i = n; i < no_tokens; i++) {
+            if (i != n) {
+                if (strcmp(tokens[i], tokens[n]) == 0)
+                    skip = 1;
+            }
+        }
 
-	if (!skip)
-	    fprintf(fp, "%s\n", tokens[n]);
+        if (!skip)
+            fprintf(fp, "%s\n", tokens[n]);
     }
 
     fclose(fp);
     G_free_tokens(tokens);
 
-    if (Path)
-	G_free(Path);
+    if (path)
+        G_free(path);
 
     if (nmapsets) {
-	for(nmapsets--; nmapsets >= 0; nmapsets--)
-	    G_free(mapset_name[nmapsets]);
-	G_free(mapset_name);
+        for(nmapsets--; nmapsets >= 0; nmapsets--)
+            G_free(mapset_name[nmapsets]);
+        G_free(mapset_name);
     }
 
     exit(EXIT_SUCCESS);
 }
 
-static void append_mapset(char **path, const char *mapset)
+void append_mapset(char **path, const char *mapset)
 {
     int len = (*path == NULL ? 0 : strlen(*path));
 



More information about the grass-commit mailing list