1 /* Test of quotearg family of functions.
2 Copyright (C) 2008 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Eric Blake <ebb9@byu.net>, 2008. */
34 /* These quotes are borrowed from a pt_PT.utf8 translation. */
35 # define LQ "\302\253"
36 # define RQ "\302\273"
39 #define ASSERT(expr) \
44 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
50 struct result_strings {
51 char const *str1; /* Translation of "". */
52 char const *str2; /* Translation of "\0""1\0". */
53 size_t len2; /* Length of str2. */
54 char const *str3; /* Translation of "simple". */
55 char const *str4; /* Translation of " \t\n'\"\033?""?/\\". */
56 char const *str5; /* Translation of "a:b". */
59 struct result_groups {
60 struct result_strings group1; /* Via quotearg_buffer. */
61 struct result_strings group2; /* Via quotearg{,_mem}. */
62 struct result_strings group3; /* Via quotearg_colon{,_mem}. */
65 static struct result_strings inputs = {
66 "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b"
69 static struct result_groups results[] = {
70 /* literal_quoting_style */
71 { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b" },
72 { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" },
73 { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" } },
75 /* shell_quoting_style */
76 { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b" },
77 { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b" },
78 { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" } },
80 /* shell_always_quoting_style */
81 { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" },
82 { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" },
83 { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" } },
86 { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
87 "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"" },
88 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
89 "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"" },
90 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
91 "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"" } },
93 /* c_maybe_quoting_style */
94 { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
96 { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
98 { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
101 /* escape_quoting_style */
102 { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b" },
103 { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b" },
104 { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b" } },
106 /* locale_quoting_style */
107 { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
109 { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
111 { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
114 /* clocale_quoting_style */
115 { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
116 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
117 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
118 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
119 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
120 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"" } }
124 static struct result_groups locale_results[] = {
125 /* locale_quoting_style */
126 { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
127 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
128 { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
129 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
130 { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
131 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ } },
133 /* clocale_quoting_style */
134 { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
135 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
136 { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
137 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
138 { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
139 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ } }
141 #endif /* ENABLE_NLS */
144 compare (char const *a, size_t la, char const *b, size_t lb)
147 ASSERT (memcmp (a, b, la) == 0);
148 ASSERT (b[lb] == '\0');
152 compare_strings (char *(func) (char const *, size_t *),
153 struct result_strings *results)
159 p = func (inputs.str1, &len);
160 compare (results->str1, strlen (results->str1), p, len);
163 p = func (inputs.str2, &len);
164 compare (results->str2, results->len2, p, len);
167 p = func (inputs.str3, &len);
168 compare (results->str3, strlen (results->str3), p, len);
170 len = strlen (inputs.str4);
171 p = func (inputs.str4, &len);
172 compare (results->str4, strlen (results->str4), p, len);
175 p = func (inputs.str5, &len);
176 compare (results->str5, strlen (results->str5), p, len);
180 use_quotearg_buffer (const char *str, size_t *len)
182 static char buf[100];
184 memset (buf, 0xa5, 100);
185 size = quotearg_buffer (buf, 100, str, *len, NULL);
187 ASSERT ((unsigned char) buf[size + 1] == 0xa5);
192 use_quotearg (const char *str, size_t *len)
194 char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
200 use_quotearg_colon (const char *str, size_t *len)
202 char *p = (*len == SIZE_MAX ? quotearg_colon (str)
203 : quotearg_colon_mem (str, *len));
209 /* True if the locale should be faked. */
210 static bool fake_locale;
212 /* A replacement gettext that allows testing of locale quotes without
213 requiring a locale. */
215 gettext (char const *str)
219 static char lq[] = LQ;
220 static char rq[] = RQ;
221 if (strcmp (str, "`") == 0)
223 if (strcmp (str, "'") == 0)
230 dgettext (char const *d, char const *str)
232 return gettext (str);
234 #endif /* ENABLE_NLS */
237 main (int argc, char **argv)
241 /* This program is hard-wired to the C locale since it does not call
243 ASSERT (!isprint ('\033'));
244 for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
246 set_quoting_style (NULL, i);
247 compare_strings (use_quotearg_buffer, &results[i].group1);
248 compare_strings (use_quotearg, &results[i].group2);
249 compare_strings (use_quotearg_colon, &results[i].group3);
253 /* Rather than change locales, and require a .gmo file with
254 translations for "`" and "'" that match our expectations, we
255 merely override the gettext function to satisfy the link
256 dependencies of quotearg.c. */
259 set_quoting_style (NULL, locale_quoting_style);
260 compare_strings (use_quotearg_buffer, &locale_results[0].group1);
261 compare_strings (use_quotearg, &locale_results[0].group2);
262 compare_strings (use_quotearg_colon, &locale_results[0].group3);
264 set_quoting_style (NULL, clocale_quoting_style);
265 compare_strings (use_quotearg_buffer, &locale_results[1].group1);
266 compare_strings (use_quotearg, &locale_results[1].group2);
267 compare_strings (use_quotearg_colon, &locale_results[1].group3);
268 #endif /* ENABLE_NLS */