[GRASS-SVN] r60872 - grass/trunk/general/g.mlist

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 19 09:04:47 PDT 2014


Author: hcho
Date: 2014-06-19 09:04:47 -0700 (Thu, 19 Jun 2014)
New Revision: 60872

Modified:
   grass/trunk/general/g.mlist/main.c
   grass/trunk/general/g.mlist/test_g_mlist.py
Log:
g.mlist:
1. Support multiple mapsets
2. Allow -p/-f with all mapsets (mapset=*)
3. Outputs from different types lumped into one pager output (-p/-f)
   Two advantages:
   a. One pager call per type does not allow the user to go back to previous
      types.
   b. Some pagers (eg., more) do not prompt the user to hit a key before
      quitting, which causes a problem where the user cannot see maps already
      listed from a previous type because they are simply scrolled up/cleared
      and another set of maps from a new type are displayed.


Modified: grass/trunk/general/g.mlist/main.c
===================================================================
--- grass/trunk/general/g.mlist/main.c	2014-06-19 14:35:40 UTC (rev 60871)
+++ grass/trunk/general/g.mlist/main.c	2014-06-19 16:04:47 UTC (rev 60872)
@@ -27,8 +27,8 @@
 
 static int any = 0;
 
-static void make_list(FILE *, const struct list *, const char *, const char *,
-		      int, int, int);
+static void make_list(FILE *, const struct list *, const char *,
+		      const char *, int, int);
 
 int main(int argc, char *argv[])
 {
@@ -51,8 +51,9 @@
 	struct Flag *pretty;
 	struct Flag *full;
     } flag;
-    int i, n, all, num_types, nlist;
+    int i, j, n, all, num_types, nlist;
     void *filter, *exclude;
+    struct Popen pager;
     FILE *fp;
     const char *mapset;
     char *separator;
@@ -64,8 +65,8 @@
     G_add_keyword(_("map management"));
     G_add_keyword(_("list"));
     module->description =
-	_("Lists available GRASS data base files "
-	  "of the user-specified data type optionally using the search pattern.");
+	_("Lists available GRASS data base files of "
+	  "the user-specified data type optionally using the search pattern.");
 
     M_read_list(FALSE, &nlist);
 
@@ -91,6 +92,7 @@
     opt.exclude->guisection = _("Pattern");
 
     opt.mapset = G_define_standard_option(G_OPT_M_MAPSET);
+    opt.mapset->multiple = YES;
     opt.mapset->label =
 	_("Name of mapset to list (default: current search path)");
     opt.mapset->description = _("'.' for current mapset; '*' for all mapsets in location");
@@ -141,9 +143,11 @@
         G_fatal_error(_("-%c/-%c and %s= are mutually exclusive"),
 		      flag.pretty->key, flag.full->key, opt.output->key);
 
-    if ((flag.pretty->answer || flag.full->answer) && flag.type->answer)
-	G_fatal_error(_("-%c/-%c and -%c are mutually exclusive"),
-		      flag.pretty->key, flag.full->key, flag.type->key);
+    if ((flag.pretty->answer || flag.full->answer) &&
+	(flag.mapset->answer || flag.type->answer))
+	G_fatal_error(_("-%c/-%c and -%c/-%c are mutually exclusive"),
+		      flag.pretty->key, flag.full->key,
+		      flag.mapset->key, flag.type->key);
 
     if (flag.pretty->answer && flag.full->answer)
 	G_fatal_error(_("-%c and -%c are mutually exclusive"),
@@ -202,25 +206,7 @@
 	exclude = NULL;
 
     separator = G_option_to_separator(opt.separator);
-    fp = G_open_option_file(opt.output);
 
-    if ((mapset = opt.mapset->answer) == NULL)
-	mapset = "";
-    else if (strcmp(mapset, ".") == 0)
-	mapset = G_mapset();   /* current mapset */
-    else if (strcmp(mapset, "*") == 0) {
-        if (flag.pretty->answer || flag.full->answer)
-            G_fatal_error(_("-%c/-%c and %s=%s are mutually exclusive"),
-                          flag.pretty->key, flag.full->key, opt.mapset->key,
-			  mapset);
-        mapset = NULL;         /* all mapsets */
-    }
-    else if ((i = G__mapset_permissions(mapset)) == -1)
-	G_fatal_error(_("Mapset <%s> does not exist"), mapset);
-    else if (i == 0)
-	G_warning(_("Permission denied for mapset <%s>. "
-		    "Trying to list files..."), mapset);
-
     for (i = 0; opt.type->answers[i]; i++) {
 	if (strcmp(opt.type->answers[i], "all") == 0)
 	    break;
@@ -234,11 +220,37 @@
 	num_types = i;
     }
 
+    if (opt.mapset->answers && opt.mapset->answers[0]) {
+	G_create_alt_search_path();
+	for (i = 0; (mapset = opt.mapset->answers[i]); i++) {
+	    if (strcmp(mapset, "*") == 0) {
+		/* all mapsets from current location */
+		char **ms;
+
+		ms = G_get_available_mapsets();
+		for (j = 0; (mapset = ms[j]); j++)
+		    G_add_mapset_to_search_path(mapset);
+		continue;
+	    }
+	    else if (strcmp(mapset, ".") == 0)
+		mapset = G_mapset();
+	    else if (G__mapset_permissions(mapset) == -1)
+		G_fatal_error(_("Mapset <%s> does not exist"), mapset);
+	    G_add_mapset_to_search_path(mapset);
+	}
+    }
+
+    if (flag.pretty->answer || flag.full->answer) {
+	fp = G_open_pager(&pager);
+	dup2(fileno(fp), STDOUT_FILENO);
+    }
+    else
+	fp = G_open_option_file(opt.output);
+
     for (i = 0; i < num_types; i++) {
 	const struct list *elem;
 
 	n = all ? i : M_get_element(opt.type->answers[i]);
-
 	elem = M_get_list(n);
 
 	if (flag.full->answer) {
@@ -251,20 +263,27 @@
 	    if (access(lister, X_OK) == 0)	/* execute permission? */
 		G_spawn(lister, lister, mapset, NULL);
 	    else
-		M_do_list(n, mapset);
+		M_do_list(n, "");
 	}
 	else if (flag.pretty->answer)
-	    M_do_list(n, mapset);
-	else
-	    make_list(fp, elem, mapset, separator, flag.type->answer,
-		      flag.mapset->answer, mapset && *mapset);
+	    M_do_list(n, "");
+	else {
+	    for (j = 0; (mapset = G_get_mapset_name(j)); j++)
+		make_list(fp, elem, mapset, separator, flag.type->answer,
+			  flag.mapset->answer);
+	}
     }
 
-    if (any)
-	fprintf(fp, "\n");
+    if (flag.pretty->answer || flag.full->answer) {
+	fclose(stdout);
+	G_close_pager(&pager);
+    }
+    else {
+	if (any)
+	    fprintf(fp, "\n");
+	G_close_option_file(fp);
+    }
 
-    G_close_option_file(fp);
-
     if (filter)
 	G_free_ls_filter(filter);
 
@@ -275,9 +294,9 @@
 }
 
 static void make_list(FILE *fp, const struct list *elem, const char *mapset,
-		      const char *separator, int add_type, int add_mapset,
-		      int single_mapset)
+		      const char *separator, int add_type, int add_mapset)
 {
+    static int first_mapset = 1;
     char path[GPATH_MAX];
     const char *element = elem->element[0];
     const char *alias = elem->alias;
@@ -285,26 +304,6 @@
     int count;
     int i;
 
-    if (!mapset) {
-        /* all mapsets from current location */
-        int n;
-        char **ms;
-        ms = G_get_available_mapsets();
-        for (n = 0; ms[n]; n++) {
-            make_list(fp, elem, ms[n], separator, add_type, add_mapset,
-		      n == 0);
-        }
-        return;
-    }
-    else if (!*mapset) {
-        /* mapsets from search path only */
-	int n;
-	for (n = 0; mapset = G_get_mapset_name(n), mapset; n++)
-	    make_list(fp, elem, mapset, separator, add_type, add_mapset,
-		      n == 0);
-	return;
-    }
-
     G_file_name(path, element, "", mapset);
     if (access(path, 0) != 0)
 	return;
@@ -334,7 +333,7 @@
 
 	fprintf(fp, "%s", name);
 
-	if (!add_mapset && !single_mapset) {
+	if (!add_mapset && !first_mapset) {
 	    const char *mapset2 = G_find_file2(element, name, "");
             if (mapset2)
                 need_mapset = strcmp(mapset, mapset2) != 0;
@@ -349,7 +348,8 @@
 
     G_suppress_warnings(0);
     fflush(fp);
-    
+
     G_free(list);
+
+    first_mapset = 0;
 }
-

Modified: grass/trunk/general/g.mlist/test_g_mlist.py
===================================================================
--- grass/trunk/general/g.mlist/test_g_mlist.py	2014-06-19 14:35:40 UTC (rev 60871)
+++ grass/trunk/general/g.mlist/test_g_mlist.py	2014-06-19 16:04:47 UTC (rev 60872)
@@ -12,13 +12,13 @@
         self.maxDiff = None
         p = start_command('g.mlist', flags='pt', type='rast', stderr=subprocess.PIPE)
         stderr = p.communicate()[1]
-        self.assertEqual(stderr, "ERROR: -p/-f and -t are mutually exclusive\n")
+        self.assertEqual(stderr, "ERROR: -p/-f and -m/-t are mutually exclusive\n")
 
     def test_ft_flags(self):
         self.maxDiff = None
         p = start_command('g.mlist', flags='ft', type='rast', stderr=subprocess.PIPE)
         stderr = p.communicate()[1]
-        self.assertEqual(stderr, "ERROR: -p/-f and -t are mutually exclusive\n")
+        self.assertEqual(stderr, "ERROR: -p/-f and -m/-t are mutually exclusive\n")
 
     def test_pf_flags(self):
         self.maxDiff = None



More information about the grass-commit mailing list