[GRASS-SVN] r58339 - in grass/trunk: general/g.parser lib/python/script

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Nov 30 05:41:58 PST 2013


Author: glynn
Date: 2013-11-30 05:41:58 -0800 (Sat, 30 Nov 2013)
New Revision: 58339

Modified:
   grass/trunk/general/g.parser/g.parser.html
   grass/trunk/general/g.parser/main.c
   grass/trunk/general/g.parser/parse.c
   grass/trunk/general/g.parser/proto.h
   grass/trunk/lib/python/script/core.py
Log:
Deal with newlines in option values (issue #2139)


Modified: grass/trunk/general/g.parser/g.parser.html
===================================================================
--- grass/trunk/general/g.parser/g.parser.html	2013-11-30 12:11:46 UTC (rev 58338)
+++ grass/trunk/general/g.parser/g.parser.html	2013-11-30 13:41:58 UTC (rev 58339)
@@ -22,6 +22,8 @@
 <dd>Print strings for translation</dd>
 <dt><b>-s</b></dt>
 <dd>Write option values to stdout instead of reinvoking script</dd>
+<dt><b>-n</b></dt>
+<dd>Write option values to stdout separated by nulls</dd>
 </dl>
 
 <h2>DESCRIPTION</h2>
@@ -34,7 +36,7 @@
 
 <h2>OPTIONS</h2>
 
-Unless the <b>-s</b> switch is used, the arguments are stored in
+Unless the <b>-s</b> or <b>-n</b> switch is used, the arguments are stored in
 environment variables for use in your scripts. These variables are
 named "GIS_FLAG_<NAME>" for flags and "GIS_OPT_<NAME>" for
 options. The names of variables are converted to upper case. For
@@ -47,7 +49,7 @@
 For flags, the value will be "1" if the flag was given, and "0" otherwise.
 
 <p>
-If the <b>-s</b> switch is used, the options and flags are written to
+If the <b>-s</b> or <b>-n</b> switch is used, the options and flags are written to
 stdout in the form <em>opt_<name>=<value></em> and
 <em>flag_<name>=<value></em>, preceded by the string
 <b>@ARGS_PARSED@</b>. If this string doesn't appear as the first line
@@ -56,6 +58,10 @@
 <em>g.parser</em> to stdout should be copied to the script's stdout
 verbatim.
 
+If the <b>-s</b> switch is used, the options and flags are separated
+by newlines. If the <b>-n</b> switch is used, the options and flags
+are separated by null characters.
+
 <p>
 Typical header definitions are as follows:
 

Modified: grass/trunk/general/g.parser/main.c
===================================================================
--- grass/trunk/general/g.parser/main.c	2013-11-30 12:11:46 UTC (rev 58338)
+++ grass/trunk/general/g.parser/main.c	2013-11-30 13:41:58 UTC (rev 58339)
@@ -32,6 +32,7 @@
     struct context ctx;
     const char *filename;
     int standard_output;
+    int separator_nul;
     
     ctx.module = NULL;
     ctx.option = NULL;
@@ -40,8 +41,8 @@
     ctx.first_flag = NULL;
     ctx.state = S_TOPLEVEL;
 
-    standard_output = translate_output = FALSE;
-    
+    standard_output = translate_output = separator_nul = FALSE;
+
     /* Detect request to get strings to translate from a file */
     /* It comes BEFORE the filename to completely avoid confusion with parser.c behaviours */
     if (argc >= 2 && (strcmp(argv[1], "-t") == 0)) {
@@ -56,6 +57,13 @@
 	argv++, argc--;
     }
 
+    if (argc >= 2 && (strcmp(argv[1], "-n") == 0)) {
+	/* write to stdout with NUL as separator */
+	standard_output = TRUE;
+	separator_nul = TRUE;
+	argv++, argc--;
+    }
+
     if ((argc < 2) || ((strcmp(argv[1], "help") == 0) ||
 		       (strcmp(argv[1], "-help") == 0) ||
 		       (strcmp(argv[1], "--help") == 0))) {
@@ -135,6 +143,6 @@
 	exit(EXIT_FAILURE);
 
     return standard_output
-	? print_options(&ctx)
+	? print_options(&ctx, separator_nul ? '\0' : '\n')
 	: reinvoke_script(&ctx, filename);
 }

Modified: grass/trunk/general/g.parser/parse.c
===================================================================
--- grass/trunk/general/g.parser/parse.c	2013-11-30 12:11:46 UTC (rev 58338)
+++ grass/trunk/general/g.parser/parse.c	2013-11-30 13:41:58 UTC (rev 58339)
@@ -228,27 +228,29 @@
 	    cmd, ctx->line);
 }
 
-int print_options(const struct context *ctx)
+int print_options(const struct context *ctx, int sep)
 {
     struct Option *option;
     struct Flag *flag;
     const char *overwrite = getenv("GRASS_OVERWRITE");
     const char *verbose = getenv("GRASS_VERBOSE");
 
-    printf("@ARGS_PARSED@\n");
+    printf("@ARGS_PARSED@%c", sep);
 
     if (overwrite)
-	printf("GRASS_OVERWRITE=%s\n", overwrite);
+	printf("GRASS_OVERWRITE=%s%c", overwrite, sep);
 
     if (verbose)
-	printf("GRASS_VERBOSE=%s\n", verbose);
+	printf("GRASS_VERBOSE=%s%c", verbose, sep);
 
     for (flag = ctx->first_flag; flag; flag = flag->next_flag)
-	printf("flag_%c=%d\n", flag->key, flag->answer ? 1 : 0);
+	printf("flag_%c=%d%c", flag->key, flag->answer ? 1 : 0, sep);
 
     for (option = ctx->first_option; option; option = option->next_opt)
-	printf("opt_%s=%s\n", option->key,
-	       option->answer ? option->answer : "");
+	printf("opt_%s=%s%c", option->key,
+	       option->answer ? option->answer : "", sep);
 
+    fflush(stdout);
+
     return EXIT_SUCCESS;
 }

Modified: grass/trunk/general/g.parser/proto.h
===================================================================
--- grass/trunk/general/g.parser/proto.h	2013-11-30 12:11:46 UTC (rev 58338)
+++ grass/trunk/general/g.parser/proto.h	2013-11-30 13:41:58 UTC (rev 58339)
@@ -7,7 +7,7 @@
 void parse_flag(struct context *, const char *, const char *);
 int parse_type(struct context *, const char *);
 void parse_option(struct context *, const char *, const char *);
-int print_options(const struct context *);
+int print_options(const struct context *, int);
 
 /* revoke.c */
 int reinvoke_script(const struct context *, const char *);

Modified: grass/trunk/lib/python/script/core.py
===================================================================
--- grass/trunk/lib/python/script/core.py	2013-11-30 12:11:46 UTC (rev 58338)
+++ grass/trunk/lib/python/script/core.py	2013-11-30 13:41:58 UTC (rev 58339)
@@ -595,7 +595,6 @@
     options = {}
     flags = {}
     for line in lines:
-        line = line.rstrip('\r\n')
         if not line:
             break
         try:
@@ -645,11 +644,11 @@
         else:
             argv[0] = os.path.join(sys.path[0], name)
 
-    p = Popen(['g.parser', '-s'] + argv, stdout=PIPE)
+    p = Popen(['g.parser', '-n'] + argv, stdout=PIPE)
     s = p.communicate()[0]
-    lines = s.splitlines()
+    lines = s.split('\0')
 
-    if not lines or lines[0].rstrip('\r\n') != "@ARGS_PARSED@":
+    if not lines or lines[0] != "@ARGS_PARSED@":
         sys.stdout.write(s)
         sys.exit(p.returncode)
 



More information about the grass-commit mailing list