Avoid doubling \ in common case of "c-maybe" quoting style.
[pspp] / tests / test-quotearg.c
1 /* Test of quotearg family of functions.
2    Copyright (C) 2008 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Written by Eric Blake <ebb9@byu.net>, 2008.  */
19
20 #include <config.h>
21
22 #include "quotearg.h"
23
24 #include <ctype.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #if ENABLE_NLS
32 /* On Linux, gettext is optionally defined as a forwarding macro,
33    which would cause syntax errors in our definition below.  But on
34    platforms that require -lintl, we cannot #undef gettext, since we
35    want to provide the entry point libintl_gettext.  So we disable
36    optimizations to avoid the Linux macros.  */
37 # undef __OPTIMIZE__
38 # include <libintl.h>
39
40 /* These quotes are borrowed from a pt_PT.utf8 translation.  */
41 # define LQ "\302\253"
42 # define RQ "\302\273"
43 #endif
44
45 #define ASSERT(expr) \
46   do                                                                         \
47     {                                                                        \
48       if (!(expr))                                                           \
49         {                                                                    \
50           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
51           abort ();                                                          \
52         }                                                                    \
53     }                                                                        \
54   while (0)
55
56 struct result_strings {
57   char const *str1; /* Translation of "".  */
58   char const *str2; /* Translation of "\0""1\0".  */
59   size_t len2; /* Length of str2.  */
60   char const *str3; /* Translation of "simple".  */
61   char const *str4; /* Translation of " \t\n'\"\033?""?/\\".  */
62   char const *str5; /* Translation of "a:b".  */
63   char const *str6; /* Translation of "a\\b".  */
64 };
65
66 struct result_groups {
67   struct result_strings group1; /* Via quotearg_buffer.  */
68   struct result_strings group2; /* Via quotearg{,_mem}.  */
69   struct result_strings group3; /* Via quotearg_colon{,_mem}.  */
70 };
71
72 static struct result_strings inputs = {
73   "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b"
74 };
75
76 static struct result_groups results_g[] = {
77   /* literal_quoting_style */
78   { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
79     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
80     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
81
82   /* shell_quoting_style */
83   { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
84       "'a\\b'" },
85     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
86       "'a\\b'" },
87     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
88       "'a\\b'" } },
89
90   /* shell_always_quoting_style */
91   { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
92       "'a\\b'" },
93     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
94       "'a\\b'" },
95     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
96       "'a\\b'" } },
97
98   /* c_quoting_style */
99   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
100       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
101     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
102       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
103     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
104       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } },
105
106   /* c_maybe_quoting_style */
107   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
108       "a:b", "a\\b" },
109     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
110       "a:b", "a\\b" },
111     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
112       "\"a:b\"", "a\\b" } },
113
114   /* escape_quoting_style */
115   { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
116       "a\\\\b" },
117     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
118       "a\\\\b" },
119     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b",
120       "a\\\\b" } },
121
122   /* locale_quoting_style */
123   { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
124       "`a:b'", "`a\\\\b'" },
125     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
126       "`a:b'", "`a\\\\b'" },
127     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
128       "`a\\:b'", "`a\\\\b'" } },
129
130   /* clocale_quoting_style */
131   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
132       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
133     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
134       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
135     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
136       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
137 };
138
139 static struct result_groups flag_results[] = {
140   /* literal_quoting_style and QA_ELIDE_NULL_BYTES */
141   { { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
142     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
143     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
144
145   /* c_quoting_style and QA_ELIDE_OUTER_QUOTES */
146   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
147       "a:b", "a\\b" },
148     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
149       "a:b", "a\\b" },
150     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
151       "\"a:b\"", "a\\b" } },
152
153   /* c_quoting_style and QA_SPLIT_TRIGRAPHS */
154   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
155       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
156     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
157       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
158     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
159       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
160 };
161
162 #if ENABLE_NLS
163 static struct result_groups locale_results[] = {
164   /* locale_quoting_style */
165   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
166       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
167     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
168       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
169     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
170       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } },
171
172   /* clocale_quoting_style */
173   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
174       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
175     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
176       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
177     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
178       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } }
179 };
180 #endif /* ENABLE_NLS */
181
182 static void
183 compare (char const *a, size_t la, char const *b, size_t lb)
184 {
185   ASSERT (la == lb);
186   ASSERT (memcmp (a, b, la) == 0);
187   ASSERT (b[lb] == '\0');
188 }
189
190 static void
191 compare_strings (char *(func) (char const *, size_t *),
192                  struct result_strings *results)
193 {
194   size_t len;
195   char *p;
196
197   len = 0;
198   p = func (inputs.str1, &len);
199   compare (results->str1, strlen (results->str1), p, len);
200
201   len = inputs.len2;
202   p = func (inputs.str2, &len);
203   compare (results->str2, results->len2, p, len);
204
205   len = SIZE_MAX;
206   p = func (inputs.str3, &len);
207   compare (results->str3, strlen (results->str3), p, len);
208
209   len = strlen (inputs.str4);
210   p = func (inputs.str4, &len);
211   compare (results->str4, strlen (results->str4), p, len);
212
213   len = SIZE_MAX;
214   p = func (inputs.str5, &len);
215   compare (results->str5, strlen (results->str5), p, len);
216
217   len = strlen (inputs.str6);
218   p = func (inputs.str6, &len);
219   compare (results->str6, strlen (results->str6), p, len);
220 }
221
222 static char *
223 use_quotearg_buffer (const char *str, size_t *len)
224 {
225   static char buf[100];
226   size_t size;
227   memset (buf, 0xa5, 100);
228   size = quotearg_buffer (buf, 100, str, *len, NULL);
229   *len = size;
230   ASSERT ((unsigned char) buf[size + 1] == 0xa5);
231   return buf;
232 }
233
234 static char *
235 use_quotearg (const char *str, size_t *len)
236 {
237   char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
238   *len = strlen (p);
239   return p;
240 }
241
242 static char *
243 use_quotearg_colon (const char *str, size_t *len)
244 {
245   char *p = (*len == SIZE_MAX ? quotearg_colon (str)
246              : quotearg_colon_mem (str, *len));
247   *len = strlen (p);
248   return p;
249 }
250
251 #if ENABLE_NLS
252 /* True if the locale should be faked.  */
253 static bool fake_locale;
254
255 /* A replacement gettext that allows testing of locale quotes without
256    requiring a locale.  */
257 char *
258 gettext (char const *str)
259 {
260   if (fake_locale)
261     {
262       static char lq[] = LQ;
263       static char rq[] = RQ;
264       if (strcmp (str, "`") == 0)
265         return lq;
266       if (strcmp (str, "'") == 0)
267         return rq;
268     }
269   return (char *) str;
270 }
271
272 char *
273 dgettext (char const *d, char const *str)
274 {
275   return gettext (str);
276 }
277
278 char *
279 dcgettext (char const *d, char const *str, int c)
280 {
281   return gettext (str);
282 }
283 #endif /* ENABLE_NLS */
284
285 int
286 main ()
287 {
288   int i;
289
290   /* This program is hard-wired to the C locale since it does not call
291      setlocale.  */
292   ASSERT (!isprint ('\033'));
293   for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
294     {
295       set_quoting_style (NULL, i);
296       compare_strings (use_quotearg_buffer, &results_g[i].group1);
297       compare_strings (use_quotearg, &results_g[i].group2);
298       compare_strings (use_quotearg_colon, &results_g[i].group3);
299     }
300
301   set_quoting_style (NULL, literal_quoting_style);
302   ASSERT (set_quoting_flags (NULL, QA_ELIDE_NULL_BYTES) == 0);
303   compare_strings (use_quotearg_buffer, &flag_results[0].group1);
304   compare_strings (use_quotearg, &flag_results[0].group2);
305   compare_strings (use_quotearg_colon, &flag_results[0].group3);
306
307   set_quoting_style (NULL, c_quoting_style);
308   ASSERT (set_quoting_flags (NULL, QA_ELIDE_OUTER_QUOTES)
309           == QA_ELIDE_NULL_BYTES);
310   compare_strings (use_quotearg_buffer, &flag_results[1].group1);
311   compare_strings (use_quotearg, &flag_results[1].group2);
312   compare_strings (use_quotearg_colon, &flag_results[1].group3);
313
314   ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
315           == QA_ELIDE_OUTER_QUOTES);
316   compare_strings (use_quotearg_buffer, &flag_results[2].group1);
317   compare_strings (use_quotearg, &flag_results[2].group2);
318   compare_strings (use_quotearg_colon, &flag_results[2].group3);
319
320   ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
321
322 #if ENABLE_NLS
323   /* Rather than change locales, and require a .gmo file with
324      translations for "`" and "'" that match our expectations, we
325      merely override the gettext function to satisfy the link
326      dependencies of quotearg.c.  */
327   fake_locale = true;
328
329   set_quoting_style (NULL, locale_quoting_style);
330   compare_strings (use_quotearg_buffer, &locale_results[0].group1);
331   compare_strings (use_quotearg, &locale_results[0].group2);
332   compare_strings (use_quotearg_colon, &locale_results[0].group3);
333
334   set_quoting_style (NULL, clocale_quoting_style);
335   compare_strings (use_quotearg_buffer, &locale_results[1].group1);
336   compare_strings (use_quotearg, &locale_results[1].group2);
337   compare_strings (use_quotearg_colon, &locale_results[1].group3);
338 #endif /* ENABLE_NLS */
339
340   quotearg_free ();
341   return 0;
342 }