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