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