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