[GRASS-SVN] r40647 - in grass/trunk: display/d.title include lib/gis raster/r.out.mpeg raster/r.topmodel visualization/nviz/src visualization/xganim

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jan 25 07:20:14 EST 2010


Author: glynn
Date: 2010-01-25 07:20:13 -0500 (Mon, 25 Jan 2010)
New Revision: 40647

Removed:
   grass/trunk/lib/gis/clear_scrn.c
   grass/trunk/lib/gis/system.c
Modified:
   grass/trunk/display/d.title/main.c
   grass/trunk/include/gisdefs.h
   grass/trunk/include/spawn.h
   grass/trunk/lib/gis/parser_interface.c
   grass/trunk/lib/gis/spawn.c
   grass/trunk/raster/r.out.mpeg/main.c
   grass/trunk/raster/r.out.mpeg/write.c
   grass/trunk/raster/r.topmodel/global.h
   grass/trunk/raster/r.topmodel/misc.c
   grass/trunk/visualization/nviz/src/do_zoom.c
   grass/trunk/visualization/xganim/main.cc
Log:
Remove remaining usage of system() and G_system()


Modified: grass/trunk/display/d.title/main.c
===================================================================
--- grass/trunk/display/d.title/main.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/display/d.title/main.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -21,6 +21,7 @@
 #include <grass/display.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
+#include <grass/spawn.h>
 #include <grass/glocale.h>
 
 #include "options.h"
@@ -39,7 +40,6 @@
     struct Option *opt1, *opt2, *opt3;
     struct Flag *fancy_mode, *simple_mode, *draw;
     char *tmpfile;
-    char command[GPATH_MAX + 12];
     FILE *fp;
 
     /* Initialize the GIS calls */
@@ -126,10 +126,10 @@
 
 
     if (draw->answer) {
+	char inarg[GPATH_MAX];
 	fclose(fp);
-	sprintf(command, "d.text < \"%s\"", tmpfile);
-	G_debug(3, "cmd = [%s]", command);
-	G_system(command);
+	sprintf(inarg, "input=%s", tmpfile);
+	G_spawn("d.text", "d.text", inarg, NULL);
 	unlink(tmpfile);
 	/* note a tmp file will remain, created by d.text so it can survive d.redraw */
     }

Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/include/gisdefs.h	2010-01-25 12:20:13 UTC (rev 40647)
@@ -117,9 +117,6 @@
 /* bres_line.c */
 void G_bresenham_line(int, int, int, int, int (*)(int, int));
 
-/* clear_scrn.c */
-void G_clear_screen(void);
-
 /* clicker.c */
 void G_clicker(void);
 
@@ -577,9 +574,6 @@
 char *G_squeeze(char *);
 char *G_strdup(const char *);
 
-/* system.c */
-int G_system(const char *);
-
 /* tempfile.c */
 void G_init_tempfile(void);
 char *G_tempfile(void);

Modified: grass/trunk/include/spawn.h
===================================================================
--- grass/trunk/include/spawn.h	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/include/spawn.h	2010-01-25 12:20:13 UTC (rev 40647)
@@ -4,6 +4,7 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <unistd.h>
 #include <fcntl.h>
 
 #define SF_MODE_IN	((const char *) (O_RDONLY))

Deleted: grass/trunk/lib/gis/clear_scrn.c
===================================================================
--- grass/trunk/lib/gis/clear_scrn.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/lib/gis/clear_scrn.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -1,19 +0,0 @@
-
-/**********************************************************************
- *
- *   G_clear_screen()
- *
- *   clears the terminal screen
- *
- **********************************************************************/
-#include <stdlib.h>
-#include <grass/gis.h>
-
-void G_clear_screen(void)
-{
-#ifdef __MINGW32__
-    system("cls");
-#else
-    system("clear");
-#endif
-}

Modified: grass/trunk/lib/gis/parser_interface.c
===================================================================
--- grass/trunk/lib/gis/parser_interface.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/lib/gis/parser_interface.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -12,16 +12,66 @@
  * \author Soeren Gebbert added Dec. 2009 WPS process_description document
  */
 
+#include <grass/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <sys/types.h>
+
+#if defined(HAVE_LANGINFO_H)
+#include <langinfo.h>
+#endif
+#if defined(__MINGW32__) && defined(USE_NLS)
+#include <localcharset.h>
+#endif
+#ifdef HAVE_ICONV_H
+#include <iconv.h>
+#endif
+
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include <grass/spawn.h>
+
 #include "parser_local_proto.h"
 
+#ifdef HAVE_ICONV_H
+static const char *src_enc;
+#endif
+
 /*!
  * \brief Formats text for XML.
  *
  * \param[in,out] fp file to write to
  * \param str string to write
  */
-static void print_escaped_for_xml(FILE * fp, const char *str)
+static void print_escaped_for_xml(FILE *fp, const char *str)
 {
+#ifdef HAVE_ICONV_H
+    iconv_t conv = iconv_open("UTF-8", src_enc);
+    char *enc = NULL;
+
+    if (conv != (iconv_t) -1)
+    {
+	char *src = (char *) str;
+	size_t srclen = strlen(src);
+	size_t dstlen = srclen * 4 + 1;
+	char *dst = G__alloca(dstlen);
+	size_t ret;
+
+	enc = dst;
+
+	ret = iconv(conv, (char **)&src, &srclen, &dst, &dstlen);
+	if (ret != (size_t) -1 && srclen == 0) {
+	    str = enc;
+	    *dst = '\0';
+	}
+    }
+#endif
+
     for (; *str; str++) {
 	switch (*str) {
 	case '&':
@@ -37,6 +87,14 @@
 	    fputc(*str, fp);
 	}
     }
+
+#ifdef HAVE_ICONV_H
+    if (enc)
+	G__freea(enc);
+
+    if (conv != (iconv_t) -1)
+	iconv_close(conv);
+#endif
 }
 
 /*!
@@ -49,7 +107,7 @@
     char *type;
     char *s, *top;
     int i;
-    char *encoding;
+    const char *encoding;
     int new_prompt = 0;
 
     new_prompt = G__uses_new_gisprompt();
@@ -58,15 +116,15 @@
 
 #if defined(HAVE_LANGINFO_H)
     encoding = nl_langinfo(CODESET);
-    if (!encoding || strlen(encoding) == 0) {
-	encoding = "UTF-8";
-    }
 #elif defined(__MINGW32__) && defined(USE_NLS)
     encoding = locale_charset();
-    if (!encoding || strlen(encoding) == 0) {
+#endif
+
+    if (!encoding || strlen(encoding) == 0)
 	encoding = "UTF-8";
-    }
-#else
+
+#ifdef HAVE_ICONV_H
+    src_enc = encoding;
     encoding = "UTF-8";
 #endif
 
@@ -265,7 +323,7 @@
 	fprintf(stdout, "\t<flag name=\"%s\">\n", "overwrite");
 	fprintf(stdout, "\t\t<description>\n\t\t\t");
 	print_escaped_for_xml(stdout,
-			      "Allow output files to overwrite existing files");
+			      _("Allow output files to overwrite existing files"));
 	fprintf(stdout, "\n\t\t</description>\n");
 	fprintf(stdout, "\t</flag>\n");
     }
@@ -273,14 +331,14 @@
     /* verbose */
     fprintf(stdout, "\t<flag name=\"%s\">\n", "verbose");
     fprintf(stdout, "\t\t<description>\n\t\t\t");
-    print_escaped_for_xml(stdout, "Verbose module output");
+    print_escaped_for_xml(stdout, _("Verbose module output"));
     fprintf(stdout, "\n\t\t</description>\n");
     fprintf(stdout, "\t</flag>\n");
 
     /* quiet */
     fprintf(stdout, "\t<flag name=\"%s\">\n", "quiet");
     fprintf(stdout, "\t\t<description>\n\t\t\t");
-    print_escaped_for_xml(stdout, "Quiet module output");
+    print_escaped_for_xml(stdout, _("Quiet module output"));
     fprintf(stdout, "\n\t\t</description>\n");
     fprintf(stdout, "\t</flag>\n");
 

Modified: grass/trunk/lib/gis/spawn.c
===================================================================
--- grass/trunk/lib/gis/spawn.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/lib/gis/spawn.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -685,6 +685,14 @@
 
 	if (n != pid)
 	    status = -1;
+	else {
+	    if (WIFEXITED(status))
+		status = WEXITSTATUS(status);
+	    else if (WIFSIGNALED(status))
+		status = WTERMSIG(status);
+	    else
+		status = -0x100;
+	}
     }
 
     undo_signals(sp->signals, sp->num_signals, SST_POST);

Deleted: grass/trunk/lib/gis/system.c
===================================================================
--- grass/trunk/lib/gis/system.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/lib/gis/system.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -1,100 +0,0 @@
-/*!
- * \file gis/system.c
- *
- * \brief GIS Library - Command execution functions.
- *
- * (C) 2001-2009 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.
- *
- * \author Original author CERL
- */
-
-#include <grass/config.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <signal.h>
-#include <stdio.h>
-#include <sys/types.h>
-#ifndef __MINGW32__
-#include <sys/wait.h>
-#endif
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-
-/*!
- * \brief Run a shell level command.
- *
- * This is essentially the UNIX <i>system()</i> call, except for the 
- * signal handling. During the call, user generated signals (intr, quit)
- * for the parent are ignored, but allowed for the child. Parent
- * signals are reset upon completion.<br>
- *
- * This routine is useful for menu type programs that need to run
- * external commands and allow these commands to be interrupted by
- * the user without killing the menu itself.<br>
- *
- * <b>Note:</b> if you want the signal settings to be the same for the
- * parent and the command being run, set them yourself and use
- * the UNIX <i>system()</i> call instead.
- *
- * \param command
- *
- * \return -1 on error
- * \return status on success
- */
-int G_system(const char *command)
-{
-    int status;
-
-#ifndef __MINGW32__
-    int pid, w;
-#endif
-    RETSIGTYPE(*sigint) ();
-#ifdef SIGQUIT
-    RETSIGTYPE(*sigquit) ();
-#endif
-
-    sigint = signal(SIGINT, SIG_IGN);
-#ifdef SIGQUIT
-    sigquit = signal(SIGQUIT, SIG_IGN);
-#endif
-
-    fflush(stdout);
-    fflush(stderr);
-
-#ifdef __MINGW32__
-    signal(SIGINT, SIG_DFL);
-    _spawnlp(P_WAIT, "cmd.exe", "cmd.exe", "/c", command, NULL);
-    status = 0;
-#else
-    if ((pid = fork()) == 0) {
-	signal(SIGINT, SIG_DFL);
-	signal(SIGQUIT, SIG_DFL);
-
-	execl("/bin/sh", "sh", "-c", command, NULL);
-	_exit(127);
-    }
-
-    if (pid < 0) {
-	G_warning(_("Unable to create a new process!"));
-	status = -1;
-    }
-    else {
-	while ((w = wait(&status)) != pid && w != -1) ;
-
-	if (w == -1)
-	    status = -1;
-    }
-
-#endif
-
-    signal(SIGINT, sigint);
-#ifdef SIGQUIT
-    signal(SIGQUIT, sigquit);
-#endif
-
-    return (status);
-}

Modified: grass/trunk/raster/r.out.mpeg/main.c
===================================================================
--- grass/trunk/raster/r.out.mpeg/main.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/raster/r.out.mpeg/main.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -41,6 +41,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
+#include <grass/spawn.h>
 #include <grass/glocale.h>
 
 #include "rom_proto.h"
@@ -66,12 +67,27 @@
 /* function prototypes */
 static int load_files(void);
 static int use_r_out(void);
-static char **gee_wildfiles(char *wildarg, char *element, int *num);
+static char **gee_wildfiles(const char *wildarg, const char *element, int *num);
 static void parse_command(struct Option **viewopts,
 			  char *vfiles[MAXVIEWS][MAXIMAGES],
 			  int *numviews, int *numframes);
 
+static int check_encoder(const char *encoder)
+{
+    int status, prev;
 
+    prev = G_suppress_warnings(1);
+
+    status = G_spawn_ex(
+	encoder, encoder,
+	SF_REDIRECT_FILE, SF_STDERR, SF_MODE_OUT, "/dev/null",
+	NULL);
+
+    G_suppress_warnings(prev);
+
+    return status >= 0 && status != 127;
+}
+
 int main(int argc, char **argv)
 {
     struct GModule *module;
@@ -150,9 +166,9 @@
 	strcpy(outfile, "gmovie.mpg");
 
     /* find a working encoder */
-    if (256 == G_system("ppmtompeg 2> /dev/null"))
+    if (check_encoder("ppmtompeg"))
 	encoder = "ppmtompeg";
-    else if (256 == G_system("mpeg_encode 2> /dev/null"))
+    else if (check_encoder("mpeg_encode"))
 	encoder = "mpeg_encode";
     else
 	G_fatal_error(_("Either mpeg_encode or ppmtompeg must be installed"));
@@ -233,8 +249,9 @@
     char *pr, *pg, *pb;
     unsigned char *tr, *tg, *tb, *tset;
     char *mpfilename, *name;
-    char cmd[1000], *yfiles[MAXIMAGES];
+    char *yfiles[MAXIMAGES];
     struct Colors colors;
+    int ret;
 
     size = nrows * ncols;
 
@@ -324,15 +341,17 @@
     }
 
     mpfilename = G_tempfile();
-    write_params(mpfilename, yfiles, outfile, cnt, quality, y_rows, y_cols,
-		 0);
+    write_params(mpfilename, yfiles, outfile, cnt, quality, y_rows, y_cols, 0);
 
     if (quiet)
-	sprintf(cmd, "%s %s 2> /dev/null > /dev/null", encoder, mpfilename);
+	ret = G_spawn(encoder, encoder, mpfilename,
+		      SF_REDIRECT_FILE, SF_STDOUT, SF_MODE_OUT, "/dev/null",
+		      SF_REDIRECT_FILE, SF_STDERR, SF_MODE_OUT, "/dev/null",
+		      NULL);
     else
-	sprintf(cmd, "%s %s", encoder, mpfilename);
+	ret = G_spawn(encoder, encoder, mpfilename, NULL);
 
-    if (0 != G_system(cmd))
+    if (ret != 0)
 	G_warning(_("mpeg_encode ERROR"));
 
     clean_files(mpfilename, yfiles, cnt);
@@ -349,20 +368,23 @@
     return (cnt);
 }
 
-
 static int use_r_out(void)
 {
-    char *mpfilename, cmd[1000];
+    char *mpfilename;
+    int ret;
 
     mpfilename = G_tempfile();
     write_params(mpfilename, vfiles[0], outfile, frames, quality, 0, 0, 1);
 
     if (quiet)
-	sprintf(cmd, "%s %s 2> /dev/null > /dev/null", encoder, mpfilename);
+	ret = G_spawn(encoder, encoder, mpfilename,
+		      SF_REDIRECT_FILE, SF_STDOUT, SF_MODE_OUT, "/dev/null",
+		      SF_REDIRECT_FILE, SF_STDERR, SF_MODE_OUT, "/dev/null",
+		      NULL);
     else
-	sprintf(cmd, "%s %s", encoder, mpfilename);
+	ret = G_spawn(encoder, encoder, mpfilename, NULL);
 
-    if (0 != G_system(cmd))
+    if (ret != 0)
 	G_warning(_("mpeg_encode ERROR"));
 
     clean_files(mpfilename, NULL, 0);
@@ -370,60 +392,84 @@
     return (1);
 }
 
+/* ###################################################### */
 
-/* ###################################################### */
-static char **gee_wildfiles(char *wildarg, char *element, int *num)
+static void mlist(const char *element, const char *wildarg, const char *outfile)
 {
-    int n, cnt = 0;
-    char path[GPATH_MAX], cmd[GPATH_MAX], buf[512];
+    int n;
     const char *mapset;
-    char *p, *tfile;
-    static char *newfiles[MAXIMAGES];
-    FILE *tf;
 
-    *num = 0;
-    tfile = G_tempfile();
+    for (n = 0; (mapset = G__mapset_name(n)); n++) {
+	char type_arg[GNAME_MAX];
+	char pattern_arg[GNAME_MAX];
+	char mapset_arg[GMAPSET_MAX];
 
-    /* build list of filenames */
-    for (n = 0; (mapset = G__mapset_name(n)); n++) {
 	if (strcmp(mapset, ".") == 0)
 	    mapset = G_mapset();
 
-	G__file_name(path, element, "", mapset);
-	if (access(path, 0) == 0) {
-	    sprintf(cmd, "cd %s; \\ls %s >> %s 2> /dev/null",
-		    path, wildarg, tfile);
-	    G_system(cmd);
-	}
+	sprintf(type_arg, "type=%s", element);
+	sprintf(pattern_arg, "pattern=%s", wildarg);
+	sprintf(mapset_arg, "mapset=%s", mapset);
+
+	G_spawn_ex("g.mlist", "g.mlist",
+		   type_arg, pattern_arg, mapset_arg,
+		   SF_REDIRECT_FILE, SF_STDOUT, SF_MODE_APPEND, outfile,
+		   NULL);
     }
+}
 
-    if (NULL == (tf = fopen(tfile, "r")))
-	G_warning(_("Error reading wildcard"));
-    else {
-	while (NULL != fgets(buf, 512, tf)) {
-	    /* replace newline with null */
-	    if ((p = strchr(buf, '\n')))
-		*p = '\0';
-	    /* replace first space with null */
-	    else if ((p = strchr(buf, ' ')))
-		*p = '\0';
+static char **parse(const char *filename, int *num)
+{
+    char buf[GNAME_MAX];
+    char **files = NULL;
+    int max_files = 0;
+    int num_files = 0;
+    FILE *fp;
 
-	    if (strlen(buf) > 1)
-		newfiles[cnt++] = G_store(buf);
+    fp = fopen(filename, "r");
+    if (!fp)
+	G_fatal_error(_("Error reading wildcard"));
+
+    while (fgets(buf, sizeof(buf), fp)) {
+	char *p = strchr(buf, '\n');
+	if (p)
+	    *p = '\0';
+
+	if (!*buf)
+	    continue;
+
+	if (num_files >= max_files) {
+	    max_files += 50;
+	    files = (char **) G_realloc((void *) files,
+					max_files * sizeof(char *));
 	}
 
-	fclose(tf);
+	files[num_files++] = G_store(buf);
     }
 
-    *num = cnt;
-    sprintf(cmd, "\\rm %s", tfile);
-    G_system(cmd);
+    fclose(fp);
+
+    *num = num_files;
+
+    return files;
+}
+
+static char **gee_wildfiles(const char *wildarg, const char *element, int *num)
+{
+    char *tfile;
+    char **files;
+
+    tfile = G_tempfile();
+
+    mlist(element, wildarg, tfile);
+    files = parse(tfile, num);
+
+    remove(tfile);
     G_free(tfile);
 
-    return (newfiles);
+    return files;
 }
 
-
 /********************************************************************/
 static void parse_command(struct Option **viewopts,
 			  char *vfiles[MAXVIEWS][MAXIMAGES],
@@ -444,7 +490,7 @@
 		    (NULL != strchr(viewopts[i]->answers[j], '?')) ||
 		    (NULL != strchr(viewopts[i]->answers[j], '['))) {
 		    char **wildfiles = gee_wildfiles(viewopts[i]->answers[j],
-						     "cell", &wildnum);
+						     "rast", &wildnum);
 
 		    for (k = 0; k < wildnum; k++)
 			vfiles[i][numi++] = wildfiles[k];

Modified: grass/trunk/raster/r.out.mpeg/write.c
===================================================================
--- grass/trunk/raster/r.out.mpeg/write.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/raster/r.out.mpeg/write.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -326,16 +326,11 @@
 /*******************************************************/
 void clean_files(char *file, char *files[], int num)
 {
-    char cmd[1000];
     int i;
 
-    sprintf(cmd, "\\rm %s", file);
-    G_system(cmd);
-
-    for (i = 0; i < num; i++) {
-	sprintf(cmd, "\\rm %s", files[i]);
-	G_system(cmd);
-    }
+    remove(file);
+    for (i = 0; i < num; i++)
+	remove(files[i]);
 }
 
 /*******************************************************/

Modified: grass/trunk/raster/r.topmodel/global.h
===================================================================
--- grass/trunk/raster/r.topmodel/global.h	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/raster/r.topmodel/global.h	2010-01-25 12:20:13 UTC (rev 40647)
@@ -28,7 +28,6 @@
 int check_io(void);
 
 /* misc.c */
-int run(char *buf);
 void gregion(void);
 void depressionless(void);
 void basin_elevation(void);

Modified: grass/trunk/raster/r.topmodel/misc.c
===================================================================
--- grass/trunk/raster/r.topmodel/misc.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/raster/r.topmodel/misc.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -1,27 +1,8 @@
 #include "global.h"
+#include <grass/spawn.h>
 
-
-int run(char *cmd)
-{
-    int retval;
-
-
-    if (G_system(cmd)) {
-	G_warning("Subprocess failed");
-	retval = 1;
-    }
-    else {
-	G_verbose_message("Subprocess complete.");
-	retval = 0;
-    }
-
-    return retval;
-}
-
-
 void gregion(void)
 {
-    char buf[GPATH_MAX];
     char *hdmap;
 
     hdmap = NULL;
@@ -36,29 +17,34 @@
     }
 
     if (hdmap) {
-	sprintf(buf, "g.region rast=%s --quiet", hdmap);
-	G_verbose_message("%s ...", buf);
+	char buf[GPATH_MAX];
 
-	if (run(buf))
-	    exit(1);
+	sprintf(buf, "rast=%s", hdmap);
+
+	G_verbose_message("g.region %s ...", buf);
+
+	if (G_spawn("g.region", "g.region", "--quiet", buf, NULL) != 0)
+	    G_fatal_error("g.region failed");
     }
 }
 
 
 void depressionless(void)
 {
-    char buf[GPATH_MAX];
+    char input[GPATH_MAX];
+    char elev[GPATH_MAX];
+    char dir[GPATH_MAX];
 
-    sprintf(buf, "r.fill.dir input=%s elev=%s dir=%s type=grass --quiet",
-	    map.elev, map.fill, map.dir);
-    G_verbose_message("%s ...", buf);
+    sprintf(input, "input=%s", map.elev);
+    sprintf(elev, "elev=%s", map.fill);
+    sprintf(dir, "dir=%s", map.dir);
 
-    if (run(buf))
-	exit(1);
+    G_verbose_message("r.fill.dir %s %s %s", input, elev, dir);
 
-    map.elev = map.fill;
+    if (G_spawn("r.fill.dir", "r.fill.dir", "--quiet", input, elev, dir, NULL) != 0)
+	G_fatal_error("r.fill.dir failed");
 
-    return;
+    map.elev = map.fill;
 }
 
 
@@ -66,37 +52,40 @@
 {
     char buf[GPATH_MAX];
 
-    sprintf(buf, "r.mapcalc expression=\"%s = if(%s == 0 || isnull(%s), null(), %s)\" --quiet",
+    sprintf(buf, "expression=%s = if(%s == 0 || isnull(%s), null(), %s)",
 	    map.belev, map.basin, map.basin, map.elev);
-    G_verbose_message("%s ...", buf);
+    G_verbose_message("r.mapcalc \"%s\" ...", buf);
 
-    if (run(buf))
-	exit(1);
-
-    return;
+    if (G_spawn("r.mapcalc", "r.mapcalc", "--quiet", buf, NULL) != 0)
+	G_fatal_error("r.mapcalc failed");
 }
 
 
 void top_index(void)
 {
-    char buf[GPATH_MAX];
+    char input[GPATH_MAX];
+    char output[GPATH_MAX];
+    char nsteps[32];
+
     if (map.belev) {
-	sprintf(buf, "r.topidx input=%s output=%s --quiet",
-		map.belev, map.topidx);
-	G_verbose_message("%s ...", buf);
+	sprintf(input, "input=%s", map.belev);
+	sprintf(output, "output=%s", map.topidx);
 
-	if (run(buf))
-	    exit(1);
+	G_verbose_message("r.topidx %s %s ...", input, output);
+
+	if (G_spawn("r.topidx", "r.topidx", "--quiet", input, output, NULL) != 0)
+	    G_fatal_error("r.topidx failed");
     }
 
     if (map.topidx) {
-	sprintf(buf, "r.stats -Anc input=%s nsteps=%d output=\"%s\"",
-		map.topidx, misc.nidxclass, file.idxstats);
-	G_verbose_message("%s ...", buf);
+	sprintf(input, "input=%s", map.topidx);
+	sprintf(input, "nsteps=%d", misc.nidxclass);
+	sprintf(output, "output=%s", file.idxstats);
 
-	if (run(buf))
-	    exit(1);
-    }
+	G_verbose_message("r.stats -Anc %s %s %s ...", input, nsteps, output);
 
-    return;
+	if (G_spawn("r.stats", "r.stats", "-Anc", input, nsteps, output, NULL) != 0)
+	    G_fatal_error("r.stats failed");
+    }
 }
+

Modified: grass/trunk/visualization/nviz/src/do_zoom.c
===================================================================
--- grass/trunk/visualization/nviz/src/do_zoom.c	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/visualization/nviz/src/do_zoom.c	2010-01-25 12:20:13 UTC (rev 40647)
@@ -14,6 +14,8 @@
 /* Nvision includes */
 #include "interface.h"
 
+#include <grass/spawn.h>
+
 /* Standard includes */
 #include <stdio.h>
 #include <stdlib.h>
@@ -42,6 +44,89 @@
 
 static int init_ctx(void);
 
+static void pnmcat(const char *pref, int var_i)
+{
+    char filename[GPATH_MAX];
+    const char **args2 = G__alloca((var_i + 7) * sizeof(char *));
+    int n_args2 = 0;
+    const char **args1 = G__alloca((var_i + 7) * sizeof(char *));
+    int n_args1;
+    int i, j, k, m;
+
+    args2[n_args2++] = G_store("pmncat");
+    args2[n_args2++] = G_store("-tb");
+    k = var_i;
+    for (i = 1; i <= var_i; i++) {
+	n_args1 = 0;
+	args1[n_args1++] = G_store("pmncat");
+	args1[n_args1++] = G_store("-lr");
+
+	for (j = 1; j <= var_i; j++) {
+	    sprintf(filename, "%s_%d_%d.ppm", pref, i, j);
+	    args1[n_args1++] = G_store(filename);
+	}
+
+	sprintf(filename, "%stmp%d.ppm", pref, i);
+
+	args1[n_args1++] = SF_REDIRECT_FILE;
+	args1[n_args1++] = SF_STDOUT;
+	args1[n_args1++] = SF_MODE_OUT;
+	args1[n_args1++] = G_store(filename);
+
+	args1[n_args1++] = NULL;
+
+	sprintf(filename, "%stmp%d.ppm", pref, k);
+	args2[n_args2++] = G_store(filename);
+
+	if (G_vspawn_ex(args1[0], args1) != 0) {
+	    fprintf(stderr, "pnmcat failed to create assembled image\n");
+	    fprintf(stderr, "Check that pnmcat is installed and path is set\n");
+	}
+	else {
+	    for (m = 1; m <= var_i; m++) {
+		sprintf(filename, "%s_%d_%d.ppm", pref, i, m);
+		remove(filename);
+	    }
+	}
+	k--;
+
+	for (j = 0; args1[j]; j++)
+	    if (args1[j] != SF_REDIRECT_FILE &&
+		args1[j] != SF_STDOUT &&
+		args1[j] != SF_MODE_OUT)
+		G_free((char *) args1[j]);
+    }
+
+    sprintf(filename, "%s.ppm", pref);
+
+    args2[n_args2++] = SF_REDIRECT_FILE;
+    args2[n_args2++] = SF_STDOUT;
+    args2[n_args2++] = SF_MODE_OUT;
+    args2[n_args2++] = G_store(filename);
+
+    args2[n_args2++] = NULL;
+
+    if (G_vspawn_ex(args2[0], args2) != 0) {
+	fprintf(stderr, "pnmcat failed to create assembled images\n");
+	fprintf(stderr, "Check that pnmcat is installed and path is set\n");
+    }
+    else {
+	for (m = 1; m <= var_i; m++) {
+	    sprintf(filename, "%stmp%d.ppm", pref, m);
+	    remove(filename);
+	}
+    }
+
+    for (i = 0; args1[i]; i++)
+	if (args1[i] != SF_REDIRECT_FILE &&
+	    args1[i] != SF_STDOUT &&
+	    args1[i] != SF_MODE_OUT)
+	    G_free((char *) args1[i]);
+
+    G__freea(args1);
+    G__freea(args2);
+}
+
 /**********************************************/
 int Nstart_zoom_cmd(Nv_data * data,	/* Local data */
 		    Tcl_Interp * interp,	/* Current interpreter */
@@ -53,11 +138,11 @@
     int a_orig, b_orig, c_orig, d_orig;
     int maxx, maxy;
     int img_width, img_height;
-    int row, col, i, j, k, m;
+    int row, col;
     int XX, YY, var_i;
     int cnt = 1;
     double aspect;
-    char pref[64], filename[1024], cmd[1024], cmd2[1024];
+    char pref[64], filename[1024];
     char inform_text[128];
 
 #if defined(OPENGL_X11) && (defined(HAVE_PBUFFERS) || defined(HAVE_PIXMAPS))
@@ -156,49 +241,13 @@
 
     /* Done writing ppm tiles */
 
-
     /* Cat ppm tiles together */
     sprintf(inform_text, "inform \"Assembling Tiles\"");
     Tcl_Eval(interp, inform_text);
     fprintf(stderr, "Assembling Tiles\n");
-    strcpy(cmd2, "pnmcat -tb ");
-    k = var_i;
-    for (i = 1; i <= var_i; i++) {
-	strcpy(cmd, "pnmcat -lr ");
-	for (j = 1; j <= var_i; j++) {
-	    sprintf(filename, "%s_%d_%d.ppm ", pref, i, j);
-	    strcat(cmd, filename);
-	}
-	sprintf(filename, "> %stmp%d.ppm", pref, i);
-	strcat(cmd, filename);
-	sprintf(filename, "%stmp%d.ppm ", pref, k);
-	strcat(cmd2, filename);
-	if (system(cmd) != 0) {
-	    fprintf(stderr, "pnmcat failed to create assembled image\n");
-	    fprintf(stderr,
-		    "Check that pnmcat is installed and path is set\n");
-	}
-	else {
-	    for (m = 1; m <= var_i; m++) {
-		sprintf(filename, "%s_%d_%d.ppm", pref, i, m);
-		remove(filename);
-	    }
-	}
-	k--;
-    }
-    sprintf(filename, "> %s.ppm", pref);
-    strcat(cmd2, filename);
-    if (system(cmd2) != 0) {
-	fprintf(stderr, "pnmcat failed to create assembled images\n");
-	fprintf(stderr, "Check that pnmcat is installed and path is set\n");
-    }
-    else {
-	for (m = 1; m <= var_i; m++) {
-	    sprintf(filename, "%stmp%d.ppm", pref, m);
-	    remove(filename);
-	}
-    }
 
+    pnmcat(pref, var_i);
+
     GS_set_viewport(a_orig, c_orig, b_orig, d_orig);
 
 #if defined(OPENGL_X11) && (defined(HAVE_PBUFFERS) || defined(HAVE_PIXMAPS))

Modified: grass/trunk/visualization/xganim/main.cc
===================================================================
--- grass/trunk/visualization/xganim/main.cc	2010-01-25 10:21:35 UTC (rev 40646)
+++ grass/trunk/visualization/xganim/main.cc	2010-01-25 12:20:13 UTC (rev 40647)
@@ -31,6 +31,7 @@
 extern "C" {
 #include <grass/gis.h>
 #include <grass/raster.h>
+#include <grass/spawn.h>
 #include <grass/glocale.h>
 }
 
@@ -40,9 +41,10 @@
 #define DEF_MIN 600
 #define BORDER_W    2
 
-static char **gee_wildfiles(char *wildarg, char *element, int *num);
+static char **gee_wildfiles(const char *wildarg, const char *element, int *num);
 static void parse_command(
-    struct Option **viewopts, char *vfiles[MAXVIEWS][MAXIMAGES],
+    struct Option **viewopts,
+    char *vfiles[MAXVIEWS][MAXIMAGES],
     int *numviews, int *numframes);
 
 struct Option *viewopts[MAXVIEWS];
@@ -354,60 +356,85 @@
     }
 }
 
-
 /* ###################################################### */
 
-char **gee_wildfiles(char *wildarg, char *element, int *num)
+static void mlist(const char *element, const char *wildarg, const char *outfile)
 {
-    int n, cnt = 0;
-    char path[GPATH_MAX], cmd[GPATH_MAX], buf[512];
+    int n;
     const char *mapset;
-    char *p, *tfile;
-    static char *newfiles[MAXIMAGES];
-    FILE *tf;
 
-    *num = 0;
-    tfile = G_tempfile();
+    for (n = 0; (mapset = G__mapset_name(n)); n++) {
+	char type_arg[GNAME_MAX];
+	char pattern_arg[GNAME_MAX];
+	char mapset_arg[GMAPSET_MAX];
 
-    /* build list of filenames */
-    for (n = 0; (mapset = G__mapset_name(n)); n++) {
 	if (strcmp(mapset, ".") == 0)
 	    mapset = G_mapset();
 
-	G__file_name(path, element, "", mapset);
-	if (access(path, 0) == 0) {
-	    sprintf(cmd, "cd %s; \\ls %s >> %s 2> /dev/null", path, wildarg,
-		    tfile);
-	    system(cmd);
-	}
+	sprintf(type_arg, "type=%s", element);
+	sprintf(pattern_arg, "pattern=%s", wildarg);
+	sprintf(mapset_arg, "mapset=%s", mapset);
+
+	G_spawn_ex("g.mlist", "g.mlist",
+		   type_arg, pattern_arg, mapset_arg,
+		   SF_REDIRECT_FILE, SF_STDOUT, SF_MODE_APPEND, outfile,
+		   NULL);
     }
+}
 
-    if (NULL == (tf = fopen(tfile, "r")))
-	G_warning(_("Error reading wildcard"));
-    else {
-	while (NULL != fgets(buf, 512, tf)) {
-	    /* replace newline with null */
-	    if ((p = strchr(buf, '\n')))
-		*p = '\0';
-	    /* replace first space with null */
-	    else if ((p = strchr(buf, ' ')))
-		*p = '\0';
+static char **parse(const char *filename, int *num)
+{
+    char buf[GNAME_MAX];
+    char **files = NULL;
+    int max_files = 0;
+    int num_files = 0;
+    FILE *fp;
 
-	    if (strlen(buf) > 1) {
-		newfiles[cnt++] = G_store(buf);
-	    }
+    fp = fopen(filename, "r");
+    if (!fp)
+	G_fatal_error(_("Error reading wildcard"));
+
+    while (fgets(buf, sizeof(buf), fp)) {
+	char *p = strchr(buf, '\n');
+	if (p)
+	    *p = '\0';
+
+	if (!*buf)
+	    continue;
+
+	if (num_files >= max_files) {
+	    max_files += 50;
+	    files = (char **) G_realloc((void *) files,
+					max_files * sizeof(char *));
 	}
-	fclose(tf);
+
+	files[num_files++] = G_store(buf);
     }
-    *num = cnt;
+
+    fclose(fp);
+
+    *num = num_files;
+
+    return files;
+}
+
+static char **gee_wildfiles(const char *wildarg, const char *element, int *num)
+{
+    char *tfile;
+    char **files;
+
+    tfile = G_tempfile();
+
+    mlist(element, wildarg, tfile);
+    files = parse(tfile, num);
+
+    remove(tfile);
     G_free(tfile);
 
-    return (newfiles);
+    return files;
 }
 
-
-/********************************************************************/
-void parse_command(struct Option **viewopts,
+static void parse_command(struct Option **viewopts,
 			  char *vfiles[MAXVIEWS][MAXIMAGES],
 			  int *numviews, int *numframes)
 {
@@ -426,7 +453,7 @@
 		    (NULL != strchr(viewopts[i]->answers[j], '?')) ||
 		    (NULL != strchr(viewopts[i]->answers[j], '['))) {
 		    char **wildfiles = gee_wildfiles(viewopts[i]->answers[j],
-						     "cell", &wildnum);
+						     "rast", &wildnum);
 
 		    for (k = 0; k < wildnum; k++)
 			vfiles[i][numi++] = wildfiles[k];
@@ -441,5 +468,7 @@
     }
 }
 
+/********************************************************************/
+
 IMPLEMENT_APP_NO_MAIN(MyApp)
 



More information about the grass-commit mailing list