d8307d
commit c74a91deaa5de416237c02bbb3e41bda76ca4c7b
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Tue Nov 27 21:35:56 2018 +0100
d8307d
d8307d
    support: Implement support_quote_string
d8307d
    
d8307d
    Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
d8307d
d8307d
diff --git a/support/Makefile b/support/Makefile
d8307d
index 2b663fbbfa334ea2..a2536980d1d5a89b 100644
d8307d
--- a/support/Makefile
d8307d
+++ b/support/Makefile
d8307d
@@ -58,6 +58,7 @@ libsupport-routines = \
d8307d
   support_openpty \
d8307d
   support_paths \
d8307d
   support_quote_blob \
d8307d
+  support_quote_string \
d8307d
   support_record_failure \
d8307d
   support_run_diff \
d8307d
   support_shared_allocate \
d8307d
@@ -196,6 +197,7 @@ tests = \
d8307d
   tst-support_capture_subprocess \
d8307d
   tst-support_format_dns_packet \
d8307d
   tst-support_quote_blob \
d8307d
+  tst-support_quote_string \
d8307d
   tst-support_record_failure \
d8307d
   tst-test_compare \
d8307d
   tst-test_compare_blob \
d8307d
diff --git a/support/support.h b/support/support.h
d8307d
index 9418cd11ef6e684d..835e7173eb566355 100644
d8307d
--- a/support/support.h
d8307d
+++ b/support/support.h
d8307d
@@ -69,6 +69,11 @@ void support_write_file_string (const char *path, const char *contents);
d8307d
    the result).  */
d8307d
 char *support_quote_blob (const void *blob, size_t length);
d8307d
 
d8307d
+/* Quote the contents of the at STR, in such a way that the result
d8307d
+   string can be included in a C literal (in single/double quotes,
d8307d
+   without putting the quotes into the result).  */
d8307d
+char *support_quote_string (const char *str);
d8307d
+
d8307d
 /* Returns non-zero if the file descriptor is a regular file on a file
d8307d
    system which supports holes (that is, seeking and writing does not
d8307d
    allocate storage for the range of zeros).  FD must refer to a
d8307d
diff --git a/support/support_quote_string.c b/support/support_quote_string.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..d324371b133a4d66
d8307d
--- /dev/null
d8307d
+++ b/support/support_quote_string.c
d8307d
@@ -0,0 +1,26 @@
d8307d
+/* Quote a string so that it can be used in C literals.
d8307d
+   Copyright (C) 2018 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+#include <string.h>
d8307d
+#include <support/support.h>
d8307d
+
d8307d
+char *
d8307d
+support_quote_string (const char *str)
d8307d
+{
d8307d
+  return support_quote_blob (str, strlen (str));
d8307d
+}
d8307d
diff --git a/support/tst-support_quote_string.c b/support/tst-support_quote_string.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..3c004759b76e21d7
d8307d
--- /dev/null
d8307d
+++ b/support/tst-support_quote_string.c
d8307d
@@ -0,0 +1,60 @@
d8307d
+/* Test the support_quote_string function.
d8307d
+   Copyright (C) 2018 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+#include <support/check.h>
d8307d
+#include <support/support.h>
d8307d
+#include <string.h>
d8307d
+#include <stdlib.h>
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  char *p = support_quote_string ("");
d8307d
+  TEST_COMPARE (strlen (p), 0);
d8307d
+  free (p);
d8307d
+  p = support_quote_string ("X");
d8307d
+  TEST_COMPARE (strlen (p), 1);
d8307d
+  TEST_COMPARE (p[0], 'X');
d8307d
+  free (p);
d8307d
+
d8307d
+  /* Check escaping of backslash-escaped characters, and lack of
d8307d
+     escaping for other shell meta-characters.  */
d8307d
+  p = support_quote_string ("$()*?`@[]{}~\'\"X");
d8307d
+  TEST_COMPARE (strcmp (p, "$()*?`@[]{}~\\'\\\"X"), 0);
d8307d
+  free (p);
d8307d
+
d8307d
+  /* Check lack of escaping for letters and digits.  */
d8307d
+#define LETTERS_AND_DIGTS                       \
d8307d
+  "abcdefghijklmnopqrstuvwxyz"                  \
d8307d
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                  \
d8307d
+  "0123456789"
d8307d
+  p = support_quote_string (LETTERS_AND_DIGTS "@");
d8307d
+  TEST_COMPARE (strcmp (p, LETTERS_AND_DIGTS "@"), 0);
d8307d
+  free (p);
d8307d
+
d8307d
+  /* Check escaping of control characters and other non-printable
d8307d
+     characters.  */
d8307d
+  p = support_quote_string ("\r\n\t\a\b\f\v\1\177\200\377@");
d8307d
+  TEST_COMPARE (strcmp (p, "\\r\\n\\t\\a\\b\\f\\v\\001"
d8307d
+                        "\\177\\200\\377@"), 0);
d8307d
+  free (p);
d8307d
+
d8307d
+  return 0;
d8307d
+}
d8307d
+
d8307d
+#include <support/test-driver.c>