quotearg: fix right quote escaping when it's in quote_these_too
authorJoel E. Denny <jdenny@clemson.edu>
Sun, 23 Aug 2009 00:26:46 +0000 (20:26 -0400)
committerJoel E. Denny <jdenny@clemson.edu>
Sun, 23 Aug 2009 22:30:34 +0000 (18:30 -0400)
* lib/quotearg.c (quotearg_buffer_restyled): Upon seeing a right
quote, be sure to prepend only one backslash.
* tests/test-quotearg.c (use_quote_double_quotes): New function.
(main): Test it.

ChangeLog
lib/quotearg.c
tests/test-quotearg.c

index 469836711be31843d153af08871195ec547556df..57f5f32e2d94b9e849330ebf68c32b4227cbf73b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-08-22  Joel E. Denny  <jdenny@clemson.edu>
+
+       quotearg: fix right quote escaping when it's in quote_these_too
+       * lib/quotearg.c (quotearg_buffer_restyled): Upon seeing a right
+       quote, be sure to prepend only one backslash.
+       * tests/test-quotearg.c (use_quote_double_quotes): New function.
+       (main): Test it.
+
 2009-08-22  Joel E. Denny  <jdenny@clemson.edu>
 
        quotearg-tests: test escaping of embedded locale quotes
index 339bf34942854b0c59192058f24ec09576087275..3f9e628cf03581065b9a58a28dabbfdbf31d95f1 100644 (file)
@@ -280,6 +280,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
     {
       unsigned char c;
       unsigned char esc;
+      bool is_right_quote = false;
 
       if (backslash_escapes
          && quote_string_len
@@ -288,7 +289,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
        {
          if (elide_outer_quotes)
            goto force_outer_quoting_style;
-         STORE ('\\');
+         is_right_quote = true;
        }
 
       c = arg[i];
@@ -521,6 +522,11 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
                        STORE ('0' + ((c >> 3) & 7));
                        c = '0' + (c & 7);
                      }
+                   else if (is_right_quote)
+                     {
+                       STORE ('\\');
+                       is_right_quote = false;
+                     }
                    if (ilim <= i + 1)
                      break;
                    STORE (c);
@@ -534,7 +540,8 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
 
       if (! ((backslash_escapes || elide_outer_quotes)
             && quote_these_too
-            && quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS))))
+            && quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS)))
+         && !is_right_quote)
        goto store_c;
 
     store_escape:
index ef91c80127e63830814ffdcb388cba95621eb215..2e2c56b0d42832a969e82c76d3ec13235ccc0e44 100644 (file)
@@ -263,6 +263,15 @@ use_quotearg (const char *str, size_t *len)
   return p;
 }
 
+static char *
+use_quote_double_quotes (const char *str, size_t *len)
+{
+  char *p = *len == SIZE_MAX ? quotearg_char (str, '"')
+                               : quotearg_char_mem (str, *len, '"');
+  *len = strlen (p);
+  return p;
+}
+
 static char *
 use_quotearg_colon (const char *str, size_t *len)
 {
@@ -287,6 +296,8 @@ main (int argc, char *argv[])
       set_quoting_style (NULL, i);
       compare_strings (use_quotearg_buffer, &results_g[i].group1);
       compare_strings (use_quotearg, &results_g[i].group2);
+      if (i == c_quoting_style)
+        compare_strings (use_quote_double_quotes, &results_g[i].group2);
       compare_strings (use_quotearg_colon, &results_g[i].group3);
     }
 
@@ -301,12 +312,14 @@ main (int argc, char *argv[])
          == QA_ELIDE_NULL_BYTES);
   compare_strings (use_quotearg_buffer, &flag_results[1].group1);
   compare_strings (use_quotearg, &flag_results[1].group2);
+  compare_strings (use_quote_double_quotes, &flag_results[1].group2);
   compare_strings (use_quotearg_colon, &flag_results[1].group3);
 
   ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
          == QA_ELIDE_OUTER_QUOTES);
   compare_strings (use_quotearg_buffer, &flag_results[2].group1);
   compare_strings (use_quotearg, &flag_results[2].group2);
+  compare_strings (use_quote_double_quotes, &flag_results[2].group2);
   compare_strings (use_quotearg_colon, &flag_results[2].group3);
 
   ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);