[GRASS-SVN] r40146 - in grass/trunk: display/d.histogram include raster/r.kappa raster/r.report raster/r.statistics raster/r.transect raster/r.watershed/front

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Dec 26 03:41:23 EST 2009


Author: glynn
Date: 2009-12-26 03:41:21 -0500 (Sat, 26 Dec 2009)
New Revision: 40146

Added:
   grass/trunk/raster/r.statistics/run_cmd.c
Modified:
   grass/trunk/display/d.histogram/get_stats.c
   grass/trunk/include/spawn.h
   grass/trunk/raster/r.kappa/stats.c
   grass/trunk/raster/r.report/stats.c
   grass/trunk/raster/r.statistics/method.h
   grass/trunk/raster/r.statistics/o_average.c
   grass/trunk/raster/r.statistics/o_distrib.c
   grass/trunk/raster/r.statistics/o_sum.c
   grass/trunk/raster/r.transect/main.c
   grass/trunk/raster/r.watershed/front/main.c
Log:
Replace system() with G_vspawn_ex()


Modified: grass/trunk/display/d.histogram/get_stats.c
===================================================================
--- grass/trunk/display/d.histogram/get_stats.c	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/display/d.histogram/get_stats.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -1,38 +1,46 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdarg.h>
 #include <unistd.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
+#include <grass/spawn.h>
 #include "options.h"
 #include "dhist.h"
 
-static char *mk_command(const char *fmt, int nargs, ...)
+static void run_stats(const char *mapname, int quiet, const char *tempfile)
 {
-    /* asprintf() would solve this problem better */
-    size_t len = strlen(fmt) + 1;
-    char *cmd;
-    va_list ap;
+    char buf[32];
+    const char *argv[12];
+    int argc = 0;
 
-    va_start(ap, nargs);
+    argv[argc++] = "r.stats";
 
-    while (nargs--) {
-	cmd = va_arg(ap, char *);
+    argv[argc++] = "-r";
+    if (cat_ranges)
+	argv[argc++] = "-C";
+    if (quiet)
+	argv[argc++] = "-q";
+    argv[argc++] = type == COUNT
+	? "-c"
+	: "-a";
 
-	len += strlen(cmd);
+    argv[argc++] = mapname;
+
+    if (!cat_ranges) {
+	sprintf(buf, "nsteps=%d", nsteps);
+	argv[argc++] = buf;
     }
 
-    va_end(ap);
+    argv[argc++] = SF_REDIRECT_FILE;
+    argv[argc++] = SF_STDOUT;
+    argv[argc++] = SF_MODE_OUT;
+    argv[argc++] = tempfile;
 
-    cmd = G_malloc(len);
+    argv[argc++] = NULL;
 
-    va_start(ap, nargs);
-    vsprintf(cmd, fmt, ap);
-
-    va_end(ap);
-
-    return cmd;
+    if (G_vspawn_ex(argv[0], argv) != 0)
+	G_fatal_error("error running r.stats");
 }
 
 int get_stats(const char *mapname, struct stat_list *dist_stats,	/* linked list of stats */
@@ -41,7 +49,6 @@
     char buf[1024];		/* input buffer for reading stats */
     int done = 0;
     char *tempfile;		/* temp file name */
-    char *cmd;
     FILE *fd;			/* temp file pointer */
 
     long int cat;		/* a category value */
@@ -64,20 +71,8 @@
 	if (Rast_read_fp_range(map_name, "", &fp_range) <= 0)
 	    G_fatal_error("Can't read frange file");
     }
-    if (cat_ranges) {
-	cmd = mk_command("r.stats -Cr%s%s \"%s\" > \"%s\"\n", 4,
-			 type == COUNT ? "c" : "a", quiet ? "q" : "",
-			 mapname, tempfile);
-    }
-    else {
-	sprintf(buf, "%d", nsteps);
-	cmd = mk_command("r.stats -r%s%s \"%s\" nsteps=%s > \"%s\"\n", 5,
-			 type == COUNT ? "c" : "a", quiet ? "q" : "",
-			 mapname, buf, tempfile);
-    }
 
-    if (system(cmd))
-	G_fatal_error("%s: ERROR running r.stats", G_program_name());
+    run_stats(mapname, quiet, tempfile);
 
     /* open temp file and read the stats into a linked list */
     fd = fopen(tempfile, "r");

Modified: grass/trunk/include/spawn.h
===================================================================
--- grass/trunk/include/spawn.h	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/include/spawn.h	2009-12-26 08:41:21 UTC (rev 40146)
@@ -2,6 +2,18 @@
 #ifndef GRASS_SPAWN_H
 #define GRASS_SPAWN_H
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define SF_MODE_IN	((const char *) (O_RDONLY))
+#define SF_MODE_OUT	((const char *) (O_WRONLY|O_CREAT|O_TRUNC))
+#define SF_MODE_APPEND	((const char *) (O_WRONLY|O_CREAT|O_APPEND))
+
+#define SF_STDIN	((const char *) STDIN_FILENO)
+#define SF_STDOUT	((const char *) STDOUT_FILENO)
+#define SF_STDERR	((const char *) STDERR_FILENO)
+
 #define SF_REDIRECT_FILE		((const char *) 1)
 #define SF_REDIRECT_DESCRIPTOR		((const char *) 2)
 #define SF_CLOSE_DESCRIPTOR		((const char *) 3)

Modified: grass/trunk/raster/r.kappa/stats.c
===================================================================
--- grass/trunk/raster/r.kappa/stats.c	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/raster/r.kappa/stats.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <grass/gis.h>
+#include <grass/spawn.h>
 #include "kappa.h"
 #include <grass/glocale.h>
 #include "local_proto.h"
@@ -23,6 +24,8 @@
     size_t ns;
     FILE *fd;
     char **tokens;
+    const char *argv[9];
+    int argc = 0;
 
     strcpy(mname, maps[0]);
     mmapset = G_find_raster2(mname, "");
@@ -35,19 +38,28 @@
 	G_fatal_error(_("Raster map <%s> not found"), maps[1]);
 
     stats_file = G_tempfile();
-    strcpy(buf, "r.stats -cin");
-    strcat(buf, " fs=:");
-    strcat(buf, " input=");
-    strcat(buf, G_fully_qualified_name(maps[0], mmapset));
-    strcat(buf, ",");
-    strcat(buf, G_fully_qualified_name(maps[1], rmapset));
-    strcat(buf, " > \"");
-    strcat(buf, stats_file);
-    strcat(buf, "\"");
 
-    if (system(buf)) {
-	unlink(stats_file);
-	exit(1);
+    argv[argc++] = "r.stats";
+
+    argv[argc++] = "-cin";
+
+    argv[argc++] = "fs=:";
+
+    sprintf(buf, "input=%s,%s",
+	    G_fully_qualified_name(maps[0], mmapset),
+	    G_fully_qualified_name(maps[1], rmapset));
+    argv[argc++] = buf;
+
+    argv[argc++] = SF_REDIRECT_FILE;
+    argv[argc++] = SF_STDOUT;
+    argv[argc++] = SF_MODE_OUT;
+    argv[argc++] = stats_file;
+
+    argv[argc++] = NULL;
+
+    if (G_vspawn_ex(argv[0], argv) != 0) {
+	remove(stats_file);
+	G_fatal_error("error running r.stats");
     }
 
     fd = fopen(stats_file, "r");

Modified: grass/trunk/raster/r.report/stats.c
===================================================================
--- grass/trunk/raster/r.report/stats.c	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/raster/r.report/stats.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -1,7 +1,9 @@
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <grass/gis.h>
 #include <grass/glocale.h>
+#include <grass/spawn.h>
 #include "global.h"
 
 static int die(void);
@@ -9,7 +11,6 @@
 int get_stats(void)
 {
     char buf[1024];
-    char tmp[50];
     int i, nl, ns;
     FILE *fd;
     char **tokens;
@@ -18,43 +19,64 @@
 	stats_file = G_tempfile();
 
     if (stats_flag != REPORT_ONLY) {
-	strcpy(buf, "r.stats -acr");
-	/* if (!masking) strcat (buf, "m"); */
+	char tmp[50];
+	int n_argv = 50;
+	const char **argv = G_calloc(n_argv, sizeof(*argv));
+	int argc = 0;
+
+	argv[argc++] = "r.stats";
+	argv[argc++] = "-acr";
+
+	/* if (!masking) argv[argc++] = "-m"; */
 	if (!verbose)
-	    strcat(buf, "q");
+	    argv[argc++] = "--quiet";
+
 	if (no_nulls)
-	    strcat(buf, "n");
+	    argv[argc++] = "-n";
+
 	if (no_nulls_all)
-	    strcat(buf, "N");
+	    argv[argc++] = "-N";
+
 	if (as_int)
-	    strcat(buf, "i");
+	    argv[argc++] = "-i";
+
 	if (cat_ranges)
-	    strcat(buf, "C");
+	    argv[argc++] = "-C";
 	else if (nsteps != 255) {
-	    sprintf(tmp, " nsteps=%d", nsteps);
-	    strcat(buf, tmp);
+	    sprintf(tmp, "nsteps=%d", nsteps);
+	    argv[argc++] = tmp;
 	}
 
-	strcat(buf, " fs=: \"input=");
+	argv[argc++] = "fs=:";
 
+	argv[argc++] = SF_REDIRECT_FILE;
+	argv[argc++] = SF_STDOUT;
+	argv[argc++] = SF_MODE_OUT;
+	argv[argc++] = stats_file;
+
 	for (i = 0; i < nlayers; i++) {
-	    if (i)
-		strcat(buf, ",");
-	    strcat(buf,
-		   G_fully_qualified_name(layers[i].name, layers[i].mapset));
+	    char *name = G_fully_qualified_name(layers[i].name, layers[i].mapset);
+	    char *buf = G_malloc(6 + strlen(name) + 1);
+
+	    sprintf(buf, "input=%s", name);
+	    G_free(name);
+
+	    if (argc + 1 >= n_argv) {
+		n_argv += 50;
+		argv = G_realloc(argv, n_argv * sizeof(*argv));
+	    }
+
+	    argv[argc++] = buf;
 	}
-	strcat(buf, "\"");
 
-	strcat(buf, " > \"");
-	strcat(buf, stats_file);
-	strcat(buf, "\"");
-	/*      G_fatal_error(buf); */
-	if (system(buf)) {
-	    if (stats_flag == EVERYTHING)
-		unlink(stats_file);
-	    exit(1);
+	argv[argc++] = NULL;
+
+	if (G_vspawn_ex(argv[0], argv) != 0) {
+	    remove(stats_file);
+	    G_fatal_error("error running r.stats");
 	}
     }
+
     if (stats_flag == STATS_ONLY)
 	return 0;
 
@@ -65,6 +87,7 @@
 	    unlink(stats_file);
 	G_fatal_error(_("Unable to open result file <%s>"), stats_file);
     }
+
     while (G_getl(buf, sizeof buf, fd)) {
 	tokens = G_tokenize(buf, ":");
 	i = 0;

Modified: grass/trunk/raster/r.statistics/method.h
===================================================================
--- grass/trunk/raster/r.statistics/method.h	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/raster/r.statistics/method.h	2009-12-26 08:41:21 UTC (rev 40146)
@@ -32,6 +32,11 @@
 
 extern struct menu menu[];
 
+/* run_cmd.c */
+
+extern void run_stats(const char *, const char *, const char *, const char *);
+extern int run_reclass(const char *, const char *, const char *);
+
 /* o_adev.c */
 int o_adev(const char *, const char *, const char *, int, struct Categories *);
 

Modified: grass/trunk/raster/r.statistics/o_average.c
===================================================================
--- grass/trunk/raster/r.statistics/o_average.c	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/raster/r.statistics/o_average.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -5,19 +5,14 @@
 #include <grass/glocale.h>
 #include "method.h"
 
-#define STATS "r.stats"
-#define RECLASS "r.reclass"
-
 /* function prototypes */
 static int out(FILE *, long, double, double);
 
-
 int
 o_average(const char *basemap, const char *covermap, const char *outputmap, int usecats,
 	  struct Categories *cats)
 {
     char *me = "o_average";
-    char command[1024];
     long catb, basecat, covercat;
     double x, area, sum1, sum2;
     int stat;
@@ -27,12 +22,7 @@
     tempfile1 = G_tempfile();
     tempfile2 = G_tempfile();
 
-    sprintf(command, "%s -an input=\"%s,%s\" fs=space > %s",
-	    STATS, basemap, covermap, tempfile1);
-    if (stat = system(command)) {
-	unlink(tempfile1);
-	G_fatal_error(_("%s: running %s command"), me, STATS);
-    }
+    run_stats(basemap, covermap, "-a", tempfile1);
 
     fd1 = fopen(tempfile1, "r");
     fd2 = fopen(tempfile2, "w");
@@ -63,9 +53,7 @@
     out(fd2, basecat, sum1, sum2);
     fclose(fd1);
     fclose(fd2);
-    sprintf(command, "%s input=\"%s\" output=\"%s\" < %s",
-	    RECLASS, basemap, outputmap, tempfile2);
-    stat = system(command);
+    stat = run_reclass(basemap, outputmap, tempfile2);
     unlink(tempfile1);
     unlink(tempfile2);
 

Modified: grass/trunk/raster/r.statistics/o_distrib.c
===================================================================
--- grass/trunk/raster/r.statistics/o_distrib.c	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/raster/r.statistics/o_distrib.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -4,8 +4,6 @@
 #include <grass/glocale.h>
 #include "method.h"
 
-#define STATS "r.stats"
-
 /* function prototypes */
 static int o_out(FILE *, long, long);
 
@@ -13,7 +11,6 @@
 int o_distrib(const char *basemap, const char *covermap, const char *outputmap, int usecats)
 {
     char *me = "o_distrib";
-    char command[1024];
     long csum, area, catb, basecat, covercat;
     double sum, tot;
     long stat, cat, total_count;
@@ -23,14 +20,8 @@
     tempfile1 = G_tempfile();
     tempfile2 = G_tempfile();
 
-    sprintf(command, "%s -cn input=\"%s,%s\" fs=space > %s", STATS, basemap,
-	    covermap, tempfile1);
+    run_stats(basemap, covermap, "-c", tempfile1);
 
-    if (stat = system(command)) {
-	unlink(tempfile1);
-	G_fatal_error(_("%s: running %s command"), me, STATS);
-    }
-
     fd1 = fopen(tempfile1, "r");
     fd2 = fopen(tempfile2, "w");
     if (fd1 == NULL || fd2 == NULL) {

Modified: grass/trunk/raster/r.statistics/o_sum.c
===================================================================
--- grass/trunk/raster/r.statistics/o_sum.c	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/raster/r.statistics/o_sum.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -17,7 +17,6 @@
       struct Categories *cats)
 {
     char *me = "o_sum";
-    char command[1024];
 
     long catb, basecat, covercat;
     double x, area, sum1;
@@ -28,14 +27,8 @@
     tempfile1 = G_tempfile();
     tempfile2 = G_tempfile();
 
-    sprintf(command, "%s -cn input=\"%s,%s\" fs=space > %s", STATS, basemap,
-	    covermap, tempfile1);
+    run_stats(basemap, covermap, "-c", tempfile1);
 
-    if (stat = system(command)) {
-	unlink(tempfile1);
-	G_fatal_error(_("%s: running %s command"), me, STATS);
-    }
-
     fd1 = fopen(tempfile1, "r");
     fd2 = fopen(tempfile2, "w");
     if (fd1 == NULL || fd2 == NULL) {
@@ -66,9 +59,7 @@
     sum_out(fd2, basecat, sum1);
     fclose(fd1);
     fclose(fd2);
-    sprintf(command, "%s input=\"%s\" output=\"%s\" < %s",
-	    RECLASS, basemap, outputmap, tempfile2);
-    stat = system(command);
+    stat = run_reclass(basemap, outputmap, tempfile2);
     unlink(tempfile1);
     unlink(tempfile2);
 

Added: grass/trunk/raster/r.statistics/run_cmd.c
===================================================================
--- grass/trunk/raster/r.statistics/run_cmd.c	                        (rev 0)
+++ grass/trunk/raster/r.statistics/run_cmd.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <grass/gis.h>
+#include <grass/spawn.h>
+#include "method.h"
+
+void run_stats(const char *basemap, const char *covermap, const char *mode,
+	       const char *tempfile)
+{
+    char buf[6 + GNAME_MAX + 1 + GMAPSET_MAX + 1 + GNAME_MAX + 1 + GMAPSET_MAX + 1];
+    const char *argv[10];
+    int argc = 0;
+
+    argv[argc++] = "r.stats";
+
+    argv[argc++] = mode;
+
+    argv[argc++] = "-n";
+
+    sprintf(buf, "input=%s,%s", basemap, covermap);
+    argv[argc++] = buf;
+
+    argv[argc++] = "fs=space";
+
+    argv[argc++] = SF_REDIRECT_FILE;
+    argv[argc++] = SF_STDOUT;
+    argv[argc++] = SF_MODE_OUT;
+    argv[argc++] = tempfile;
+
+    argv[argc++] = NULL;
+
+    if (G_vspawn_ex(argv[0], argv) != 0) {
+	remove(tempfile);
+	G_fatal_error("error running r.stats");
+    }
+}
+
+int run_reclass(const char *basemap, const char *outputmap, const char *tempfile)
+{
+    char buf1[6 + GNAME_MAX + 1 + GMAPSET_MAX + 1];
+    char buf2[7 + GNAME_MAX + 1];
+    const char *argv[8];
+    int argc = 0;
+
+    argv[argc++] = "r.reclass";
+
+    sprintf(buf1, "input=%s", basemap);
+    argv[argc++] = buf1;
+
+    sprintf(buf2, "output=%s", outputmap);
+    argv[argc++] = buf2;
+
+    argv[argc++] = SF_REDIRECT_FILE;
+    argv[argc++] = SF_STDIN;
+    argv[argc++] = SF_MODE_IN;
+    argv[argc++] = tempfile;
+
+    argv[argc++] = NULL;
+
+    return G_vspawn_ex(argv[0], argv);
+}
+

Modified: grass/trunk/raster/r.transect/main.c
===================================================================
--- grass/trunk/raster/r.transect/main.c	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/raster/r.transect/main.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -12,7 +12,7 @@
  *               which lie along one or more user-defined transect lines.
  *               The transects are described by their starting coordinates,
  *               azimuth, and distance. 
- * COPYRIGHT:    (C) 1999-2006 by the GRASS Development Team
+ * COPYRIGHT:    (C) 1999-2006,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
@@ -24,16 +24,70 @@
 #include <grass/gis.h>
 #include "local_proto.h"
 #include <grass/glocale.h>
+#include <grass/spawn.h>
 
-int main(int argc, char *argv[])
+static int profile(int coords, const char *map, const char *nulls, char **line)
 {
     double e1, n1, e2, n2;
-    char buf[256];
-    char command[GPATH_MAX];
-
-    int n, err;
+    char buf[1024], profile[1024];
+    const char *argv[7];
+    int argc = 0;
+    int n;
     int projection;
 
+    projection = G_projection();
+
+    argv[argc++] = "r.profile";
+
+    if (coords)
+	argv[argc++] = "-g";
+
+    sprintf(buf, "input=%s", map);
+    argv[argc++] = G_store(buf);
+
+    argv[argc++] = "output=-";
+
+    sprintf(buf, "null=%s", nulls);
+    argv[argc++] = G_store(buf);
+
+    strcpy(profile, "profile=");
+    for (n = 0; line[n]; n += 4) {
+	int err = parse_line("line", &line[n], &e1, &n1, &e2, &n2, projection);
+
+	if (err) {
+	    G_usage();
+	    exit(EXIT_FAILURE);
+	}
+
+	if (n > 0)
+	    strcat(profile, ",");
+	G_format_easting(e1, buf, projection);
+	strcat(profile, buf);
+
+	G_format_northing(n1, buf, projection);
+	strcat(profile, ",");
+	strcat(profile, buf);
+
+	G_format_easting(e2, buf, projection);
+	strcat(profile, ",");
+	strcat(profile, buf);
+
+	G_format_northing(n2, buf, projection);
+	strcat(profile, ",");
+	strcat(profile, buf);
+    }
+
+    argv[argc++] = profile;
+
+    argv[argc++] = NULL;
+
+    G_verbose_message(_("End coordinate: %.15g, %.15g"), e2, n2);
+
+    return G_vspawn_ex(argv[0], argv);
+}
+
+int main(int argc, char *argv[])
+{
     struct GModule *module;
     struct
     {
@@ -42,7 +96,6 @@
 	struct Option *null_str;
     } parms;
     struct Flag *coord;
-    char coord_str[3];
 
     G_gisinit(argv[0]);
 
@@ -78,44 +131,8 @@
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-
-    projection = G_projection();
-
-    if (coord->answer)
-	strcpy(coord_str, "-g");
-    else
-	strcpy(coord_str, "");
-
-    sprintf(command,
-	    "r.profile %s input=\"%s\" output=\"-\" null=\"%s\" profile=",
-	    coord_str, parms.map->answer, parms.null_str->answer);
-
-    err = 0;
-    for (n = 0; parms.line->answers[n]; n += 4) {
-	err += parse_line(parms.line->key, parms.line->answers + n,
-			  &e1, &n1, &e2, &n2, projection);
-	if (!err) {
-	    if (n)
-		strcat(command, ",");
-	    G_format_easting(e1, buf, projection);
-	    strcat(command, buf);
-	    G_format_northing(n1, buf, projection);
-	    strcat(command, ",");
-	    strcat(command, buf);
-	    G_format_easting(e2, buf, projection);
-	    strcat(command, ",");
-	    strcat(command, buf);
-	    G_format_northing(n2, buf, projection);
-	    strcat(command, ",");
-	    strcat(command, buf);
-	}
-    }
-    if (err) {
-	G_usage();
-	exit(EXIT_FAILURE);
-    }
-
-    G_verbose_message(_("End coordinate: %.15g, %.15g"), e2, n2);
-
-    exit(system(command));
+    return profile(coord->answer,
+		   parms.map->answer,
+		   parms.null_str->answer,
+		   parms.line->answers) != 0;
 }

Modified: grass/trunk/raster/r.watershed/front/main.c
===================================================================
--- grass/trunk/raster/r.watershed/front/main.c	2009-12-25 21:56:14 UTC (rev 40145)
+++ grass/trunk/raster/r.watershed/front/main.c	2009-12-26 08:41:21 UTC (rev 40146)
@@ -20,13 +20,27 @@
 #include <grass/gis.h>
 #include <grass/raster.h>
 #include <grass/glocale.h>
+#include <grass/spawn.h>
 
 int write_hist(char *, char *, char *, int, int);
 
+static const char *new_argv[22];
+static int new_argc;
+
+static void do_opt(const struct Option *opt)
+{
+    char *buf;
+    if (!opt->answer)
+	return;
+    buf = G_malloc(strlen(opt->key) + 1 + strlen(opt->answer) + 1);
+    sprintf(buf, "%s=%s", opt->key, opt->answer);
+    new_argv[new_argc++] = buf;
+}
+
 int main(int argc, char *argv[])
 {
+    char command[GPATH_MAX];
     int err, ret;
-    char command[512];
     struct Option *opt1;
     struct Option *opt2;
     struct Option *opt3;
@@ -264,151 +278,42 @@
     }
 
     /* Build command line */
-#ifdef __MINGW32__
-    sprintf(command, "\"\"%s/etc/", G_gisbase());
-#else
-    sprintf(command, "%s/etc/", G_gisbase());
-#endif
+    sprintf(command, "%s/etc/r.watershed.%s",
+	    G_gisbase(),
+	    flag_seg->answer ? "seg" : "ram");
+    new_argv[new_argc++] = command;
 
-    if (flag_seg->answer)
-	strcat(command, "r.watershed.seg");
-    else
-	strcat(command, "r.watershed.ram");
+    if (flag_sfd->answer)
+	new_argv[new_argc++] = "-s";
 
-#ifdef __MINGW32__
-    strcat(command, "\"");
-#endif
-
-    if (flag_sfd->answer) {
-	strcat(command, " -s");
-    }
-
     if (flag_flow->answer)
-	strcat(command, " -4");
+	new_argv[new_argc++] = "-4";
 
     if (flag_abs->answer)
-	strcat(command, " -a");
+	new_argv[new_argc++] = "-a";
 
-    if (opt1->answer) {
-	strcat(command, " el=");
-	strcat(command, "\"");
-	strcat(command, opt1->answer);
-	strcat(command, "\"");
-    }
+    do_opt(opt1);
+    do_opt(opt2);
+    do_opt(opt3);
+    do_opt(opt4);
+    do_opt(opt5);
+    do_opt(opt6);
+    do_opt(opt7);
+    do_opt(opt8);
+    do_opt(opt9);
+    do_opt(opt10);
+    do_opt(opt11);
+    do_opt(opt12);
+/*  do_opt(opt13); */
+    do_opt(opt14);
+    do_opt(opt15);
+    do_opt(opt16);
+    do_opt(opt17);
+    new_argv[new_argc++] = NULL;
 
-    if (opt2->answer) {
-	strcat(command, " de=");
-	strcat(command, "\"");
-	strcat(command, opt2->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt3->answer) {
-	strcat(command, " ov=");
-	strcat(command, "\"");
-	strcat(command, opt3->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt4->answer) {
-	strcat(command, " r=");
-	strcat(command, "\"");
-	strcat(command, opt4->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt5->answer) {
-	strcat(command, " ob=");
-	strcat(command, "\"");
-	strcat(command, opt5->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt6->answer) {
-	strcat(command, " t=");
-	strcat(command, opt6->answer);
-    }
-
-    if (opt7->answer) {
-	strcat(command, " ms=");
-	strcat(command, opt7->answer);
-    }
-
-    if (opt8->answer) {
-	strcat(command, " ac=");
-	strcat(command, "\"");
-	strcat(command, opt8->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt9->answer) {
-	strcat(command, " dr=");
-	strcat(command, "\"");
-	strcat(command, opt9->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt10->answer) {
-	strcat(command, " ba=");
-	strcat(command, "\"");
-	strcat(command, opt10->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt11->answer) {
-	strcat(command, " se=");
-	strcat(command, "\"");
-	strcat(command, opt11->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt12->answer) {
-	strcat(command, " ha=");
-	strcat(command, "\"");
-	strcat(command, opt12->answer);
-	strcat(command, "\"");
-    }
-
-/*    if (opt13->answer) {
-	strcat(command, " di=");
-	strcat(command, "\"");
-	strcat(command, opt13->answer);
-	strcat(command, "\"");
-    }
-*/
-
-    if (opt14->answer) {
-	strcat(command, " LS=");
-	strcat(command, "\"");
-	strcat(command, opt14->answer);
-	strcat(command, "\"");
-    }
-
-    if (opt15->answer) {
-	strcat(command, " S=");
-	strcat(command, "\"");
-	strcat(command, opt15->answer);
-	strcat(command, "\"");
-    }
-
-    if (!flag_sfd->answer && opt16->answer) {
-	strcat(command, " conv=");
-	strcat(command, opt16->answer);
-    }
-
-    if (flag_seg->answer && opt17->answer) {
-	strcat(command, " mb=");
-	strcat(command, opt17->answer);
-    }
-
-#ifdef __MINGW32__
-    strcat(command, "\"");
-#endif
-
     G_debug(1, "Mode: %s", flag_seg->answer ? "Segmented" : "All in RAM");
-    G_debug(1, "Running: %s", command);
 
-    ret = system(command);
+    ret = G_vspawn_ex(new_argv[0], new_argv);
 
     if (ret != EXIT_SUCCESS)
 	G_warning(_("Subprocess failed with exit code %d"), ret);



More information about the grass-commit mailing list