Avoid shadowing warning and compile errors on Linux.
[gnulib.git] / 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 };
64
65 struct result_groups {
66   struct result_strings group1; /* Via quotearg_buffer.  */
67   struct result_strings group2; /* Via quotearg{,_mem}.  */
68   struct result_strings group3; /* Via quotearg_colon{,_mem}.  */
69 };
70
71 static struct result_strings inputs = {
72   "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b"
73 };
74
75 static struct result_groups results_g[] = {
76   /* literal_quoting_style */
77   { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b" },
78     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" },
79     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" } },
80
81   /* shell_quoting_style */
82   { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b" },
83     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b" },
84     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" } },
85
86   /* shell_always_quoting_style */
87   { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" },
88     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" },
89     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" } },
90
91   /* c_quoting_style */
92   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
93       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"" },
94     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
95       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"" },
96     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
97       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"" } },
98
99   /* c_maybe_quoting_style */
100   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
101       "a:b" },
102     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
103       "a:b" },
104     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
105       "\"a:b\"" } },
106
107   /* escape_quoting_style */
108   { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b" },
109     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b" },
110     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b" } },
111
112   /* locale_quoting_style */
113   { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
114       "`a:b'" },
115     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
116       "`a:b'" },
117     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
118       "`a\\:b'" } },
119
120   /* clocale_quoting_style */
121   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
122       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
123     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
124       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
125     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
126       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"" } }
127 };
128
129 #if ENABLE_NLS
130 static struct result_groups locale_results[] = {
131   /* locale_quoting_style */
132   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
133       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
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
139   /* clocale_quoting_style */
140   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
141       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
142     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
143       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
144     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
145       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ } }
146 };
147 #endif /* ENABLE_NLS */
148
149 static void
150 compare (char const *a, size_t la, char const *b, size_t lb)
151 {
152   ASSERT (la == lb);
153   ASSERT (memcmp (a, b, la) == 0);
154   ASSERT (b[lb] == '\0');
155 }
156
157 static void
158 compare_strings (char *(func) (char const *, size_t *),
159                  struct result_strings *results)
160 {
161   size_t len;
162   char *p;
163
164   len = 0;
165   p = func (inputs.str1, &len);
166   compare (results->str1, strlen (results->str1), p, len);
167
168   len = inputs.len2;
169   p = func (inputs.str2, &len);
170   compare (results->str2, results->len2, p, len);
171
172   len = SIZE_MAX;
173   p = func (inputs.str3, &len);
174   compare (results->str3, strlen (results->str3), p, len);
175
176   len = strlen (inputs.str4);
177   p = func (inputs.str4, &len);
178   compare (results->str4, strlen (results->str4), p, len);
179
180   len = SIZE_MAX;
181   p = func (inputs.str5, &len);
182   compare (results->str5, strlen (results->str5), p, len);
183 }
184
185 static char *
186 use_quotearg_buffer (const char *str, size_t *len)
187 {
188   static char buf[100];
189   size_t size;
190   memset (buf, 0xa5, 100);
191   size = quotearg_buffer (buf, 100, str, *len, NULL);
192   *len = size;
193   ASSERT ((unsigned char) buf[size + 1] == 0xa5);
194   return buf;
195 }
196
197 static char *
198 use_quotearg (const char *str, size_t *len)
199 {
200   char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
201   *len = strlen (p);
202   return p;
203 }
204
205 static char *
206 use_quotearg_colon (const char *str, size_t *len)
207 {
208   char *p = (*len == SIZE_MAX ? quotearg_colon (str)
209              : quotearg_colon_mem (str, *len));
210   *len = strlen (p);
211   return p;
212 }
213
214 #if ENABLE_NLS
215 /* True if the locale should be faked.  */
216 static bool fake_locale;
217
218 /* A replacement gettext that allows testing of locale quotes without
219    requiring a locale.  */
220 char *
221 gettext (char const *str)
222 {
223   if (fake_locale)
224     {
225       static char lq[] = LQ;
226       static char rq[] = RQ;
227       if (strcmp (str, "`") == 0)
228         return lq;
229       if (strcmp (str, "'") == 0)
230         return rq;
231     }
232   return (char *) str;
233 }
234
235 char *
236 dgettext (char const *d, char const *str)
237 {
238   return gettext (str);
239 }
240
241 char *
242 dcgettext (char const *d, char const *str, int c)
243 {
244   return gettext (str);
245 }
246 #endif /* ENABLE_NLS */
247
248 int
249 main ()
250 {
251   int i;
252
253   /* This program is hard-wired to the C locale since it does not call
254      setlocale.  */
255   ASSERT (!isprint ('\033'));
256   for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
257     {
258       set_quoting_style (NULL, i);
259       compare_strings (use_quotearg_buffer, &results_g[i].group1);
260       compare_strings (use_quotearg, &results_g[i].group2);
261       compare_strings (use_quotearg_colon, &results_g[i].group3);
262     }
263
264 #if ENABLE_NLS
265   /* Rather than change locales, and require a .gmo file with
266      translations for "`" and "'" that match our expectations, we
267      merely override the gettext function to satisfy the link
268      dependencies of quotearg.c.  */
269   fake_locale = true;
270
271   set_quoting_style (NULL, locale_quoting_style);
272   compare_strings (use_quotearg_buffer, &locale_results[0].group1);
273   compare_strings (use_quotearg, &locale_results[0].group2);
274   compare_strings (use_quotearg_colon, &locale_results[0].group3);
275
276   set_quoting_style (NULL, clocale_quoting_style);
277   compare_strings (use_quotearg_buffer, &locale_results[1].group1);
278   compare_strings (use_quotearg, &locale_results[1].group2);
279   compare_strings (use_quotearg_colon, &locale_results[1].group3);
280 #endif /* ENABLE_NLS */
281
282   quotearg_free ();
283   return 0;
284 }