1 /* quotearg.c - quote arguments for output
3 Copyright (C) 1998-2002, 2004-2012 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert <eggert@twinsun.com> */
20 /* Without this pragma, gcc 4.7.0 20111124 mistakenly suggests that
21 the quoting_options_from_style function might be candidate for
23 #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
24 # pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
32 #include "c-strcaseeq.h"
33 #include "localcharset.h"
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
49 # define SIZE_MAX ((size_t) -1)
52 #define INT_BITS (sizeof (int) * CHAR_BIT)
54 struct quoting_options
56 /* Basic quoting style. */
57 enum quoting_style style;
59 /* Additional flags. Bitwise combination of enum quoting_flags. */
62 /* Quote the characters indicated by this bit vector even if the
63 quoting style would not normally require them to be quoted. */
64 unsigned int quote_these_too[(UCHAR_MAX / INT_BITS) + 1];
66 /* The left quote for custom_quoting_style. */
67 char const *left_quote;
69 /* The right quote for custom_quoting_style. */
70 char const *right_quote;
73 /* Names of quoting styles. */
74 char const *const quoting_style_args[] =
87 /* Correspondences to quoting style names. */
88 enum quoting_style const quoting_style_vals[] =
90 literal_quoting_style,
92 shell_always_quoting_style,
94 c_maybe_quoting_style,
100 /* The default quoting options. */
101 static struct quoting_options default_quoting_options;
103 /* Allocate a new set of quoting options, with contents initially identical
104 to O if O is not null, or to the default if O is null.
105 It is the caller's responsibility to free the result. */
106 struct quoting_options *
107 clone_quoting_options (struct quoting_options *o)
110 struct quoting_options *p = xmemdup (o ? o : &default_quoting_options,
116 /* Get the value of O's quoting style. If O is null, use the default. */
118 get_quoting_style (struct quoting_options *o)
120 return (o ? o : &default_quoting_options)->style;
123 /* In O (or in the default if O is null),
124 set the value of the quoting style to S. */
126 set_quoting_style (struct quoting_options *o, enum quoting_style s)
128 (o ? o : &default_quoting_options)->style = s;
131 /* In O (or in the default if O is null),
132 set the value of the quoting options for character C to I.
133 Return the old value. Currently, the only values defined for I are
134 0 (the default) and 1 (which means to quote the character even if
135 it would not otherwise be quoted). */
137 set_char_quoting (struct quoting_options *o, char c, int i)
139 unsigned char uc = c;
141 (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
142 int shift = uc % INT_BITS;
143 int r = (*p >> shift) & 1;
144 *p ^= ((i & 1) ^ r) << shift;
148 /* In O (or in the default if O is null),
149 set the value of the quoting options flag to I, which can be a
150 bitwise combination of enum quoting_flags, or 0 for default
151 behavior. Return the old value. */
153 set_quoting_flags (struct quoting_options *o, int i)
157 o = &default_quoting_options;
164 set_custom_quoting (struct quoting_options *o,
165 char const *left_quote, char const *right_quote)
168 o = &default_quoting_options;
169 o->style = custom_quoting_style;
170 if (!left_quote || !right_quote)
172 o->left_quote = left_quote;
173 o->right_quote = right_quote;
176 /* Return quoting options for STYLE, with no extra quoting. */
177 static struct quoting_options /* NOT PURE!! */
178 quoting_options_from_style (enum quoting_style style)
180 struct quoting_options o = { 0 };
181 if (style == custom_quoting_style)
187 /* MSGID approximates a quotation mark. Return its translation if it
188 has one; otherwise, return either it or "\"", depending on S.
190 S is either clocale_quoting_style or locale_quoting_style. */
192 gettext_quote (char const *msgid, enum quoting_style s)
194 char const *translation = _(msgid);
195 char const *locale_code;
197 if (translation != msgid)
200 /* For UTF-8 and GB-18030, use single quotes U+2018 and U+2019.
201 Here is a list of other locales that include U+2018 and U+2019:
203 ISO-8859-7 0xA1 KOI8-T 0x91
204 CP869 0x8B CP874 0x91
205 CP932 0x81 0x65 CP936 0xA1 0xAE
206 CP949 0xA1 0xAE CP950 0xA1 0xA5
207 CP1250 0x91 CP1251 0x91
208 CP1252 0x91 CP1253 0x91
209 CP1254 0x91 CP1255 0x91
210 CP1256 0x91 CP1257 0x91
211 EUC-JP 0xA1 0xC6 EUC-KR 0xA1 0xAE
212 EUC-TW 0xA1 0xE4 BIG5 0xA1 0xA5
213 BIG5-HKSCS 0xA1 0xA5 EUC-CN 0xA1 0xAE
214 GBK 0xA1 0xAE Georgian-PS 0x91
217 None of these is still in wide use; using iconv is overkill. */
218 locale_code = locale_charset ();
219 if (STRCASEEQ (locale_code, "UTF-8", 'U','T','F','-','8',0,0,0,0))
220 return msgid[0] == '`' ? "\xe2\x80\x98": "\xe2\x80\x99";
221 if (STRCASEEQ (locale_code, "GB18030", 'G','B','1','8','0','3','0',0,0))
222 return msgid[0] == '`' ? "\xa1\ae": "\xa1\xaf";
224 return (s == clocale_quoting_style ? "\"" : "'");
227 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
228 argument ARG (of size ARGSIZE), using QUOTING_STYLE, FLAGS, and
229 QUOTE_THESE_TOO to control quoting.
230 Terminate the output with a null character, and return the written
231 size of the output, not counting the terminating null.
232 If BUFFERSIZE is too small to store the output string, return the
233 value that would have been returned had BUFFERSIZE been large enough.
234 If ARGSIZE is SIZE_MAX, use the string length of the argument for ARGSIZE.
236 This function acts like quotearg_buffer (BUFFER, BUFFERSIZE, ARG,
237 ARGSIZE, O), except it breaks O into its component pieces and is
238 not careful about errno. */
241 quotearg_buffer_restyled (char *buffer, size_t buffersize,
242 char const *arg, size_t argsize,
243 enum quoting_style quoting_style, int flags,
244 unsigned int const *quote_these_too,
245 char const *left_quote,
246 char const *right_quote)
250 char const *quote_string = 0;
251 size_t quote_string_len = 0;
252 bool backslash_escapes = false;
253 bool unibyte_locale = MB_CUR_MAX == 1;
254 bool elide_outer_quotes = (flags & QA_ELIDE_OUTER_QUOTES) != 0;
259 if (len < buffersize) \
265 switch (quoting_style)
267 case c_maybe_quoting_style:
268 quoting_style = c_quoting_style;
269 elide_outer_quotes = true;
271 case c_quoting_style:
272 if (!elide_outer_quotes)
274 backslash_escapes = true;
276 quote_string_len = 1;
279 case escape_quoting_style:
280 backslash_escapes = true;
281 elide_outer_quotes = false;
284 case locale_quoting_style:
285 case clocale_quoting_style:
286 case custom_quoting_style:
288 if (quoting_style != custom_quoting_style)
291 Get translations for open and closing quotation marks.
292 The message catalog should translate "`" to a left
293 quotation mark suitable for the locale, and similarly for
294 "'". For example, a French Unicode local should translate
295 these to U+00AB (LEFT-POINTING DOUBLE ANGLE
296 QUOTATION MARK), and U+00BB (RIGHT-POINTING DOUBLE ANGLE
297 QUOTATION MARK), respectively.
299 If the catalog has no translation, we will try to
300 use Unicode U+2018 (LEFT SINGLE QUOTATION MARK) and
301 Unicode U+2019 (RIGHT SINGLE QUOTATION MARK). If the
302 current locale is not Unicode, locale_quoting_style
303 will quote 'like this', and clocale_quoting_style will
304 quote "like this". You should always include translations
305 for "`" and "'" even if U+2018 and U+2019 are appropriate
308 If you don't know what to put here, please see
309 <http://en.wikipedia.org/wiki/Quotation_marks_in_other_languages>
310 and use glyphs suitable for your language. */
311 left_quote = gettext_quote (N_("`"), quoting_style);
312 right_quote = gettext_quote (N_("'"), quoting_style);
314 if (!elide_outer_quotes)
315 for (quote_string = left_quote; *quote_string; quote_string++)
316 STORE (*quote_string);
317 backslash_escapes = true;
318 quote_string = right_quote;
319 quote_string_len = strlen (quote_string);
323 case shell_quoting_style:
324 quoting_style = shell_always_quoting_style;
325 elide_outer_quotes = true;
327 case shell_always_quoting_style:
328 if (!elide_outer_quotes)
331 quote_string_len = 1;
334 case literal_quoting_style:
335 elide_outer_quotes = false;
342 for (i = 0; ! (argsize == SIZE_MAX ? arg[i] == '\0' : i == argsize); i++)
346 bool is_right_quote = false;
348 if (backslash_escapes
350 && i + quote_string_len <= argsize
351 && memcmp (arg + i, quote_string, quote_string_len) == 0)
353 if (elide_outer_quotes)
354 goto force_outer_quoting_style;
355 is_right_quote = true;
362 if (backslash_escapes)
364 if (elide_outer_quotes)
365 goto force_outer_quoting_style;
367 /* If quote_string were to begin with digits, we'd need to
368 test for the end of the arg as well. However, it's
369 hard to imagine any locale that would use digits in
370 quotes, and set_custom_quoting is documented not to
372 if (i + 1 < argsize && '0' <= arg[i + 1] && arg[i + 1] <= '9')
378 /* We don't have to worry that this last '0' will be
379 backslash-escaped because, again, quote_string should
380 not start with it and because quote_these_too is
381 documented as not accepting it. */
383 else if (flags & QA_ELIDE_NULL_BYTES)
388 switch (quoting_style)
390 case shell_always_quoting_style:
391 if (elide_outer_quotes)
392 goto force_outer_quoting_style;
395 case c_quoting_style:
396 if ((flags & QA_SPLIT_TRIGRAPHS)
397 && i + 2 < argsize && arg[i + 1] == '?')
401 case '(': case ')': case '-': case '/':
402 case '<': case '=': case '>':
403 /* Escape the second '?' in what would otherwise be
405 if (elide_outer_quotes)
406 goto force_outer_quoting_style;
425 case '\a': esc = 'a'; goto c_escape;
426 case '\b': esc = 'b'; goto c_escape;
427 case '\f': esc = 'f'; goto c_escape;
428 case '\n': esc = 'n'; goto c_and_shell_escape;
429 case '\r': esc = 'r'; goto c_and_shell_escape;
430 case '\t': esc = 't'; goto c_and_shell_escape;
431 case '\v': esc = 'v'; goto c_escape;
433 /* No need to escape the escape if we are trying to elide
434 outer quotes and nothing else is problematic. */
435 if (backslash_escapes && elide_outer_quotes && quote_string_len)
439 if (quoting_style == shell_always_quoting_style
440 && elide_outer_quotes)
441 goto force_outer_quoting_style;
444 if (backslash_escapes)
451 case '{': case '}': /* sometimes special if isolated */
452 if (! (argsize == SIZE_MAX ? arg[1] == '\0' : argsize == 1))
460 case '!': /* special in bash */
461 case '"': case '$': case '&':
462 case '(': case ')': case '*': case ';':
464 case '=': /* sometimes special in 0th or (with "set -k") later args */
466 case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
468 /* A shell special character. In theory, '$' and '`' could
469 be the first bytes of multibyte characters, which means
470 we should check them with mbrtowc, but in practice this
471 doesn't happen so it's not worth worrying about. */
472 if (quoting_style == shell_always_quoting_style
473 && elide_outer_quotes)
474 goto force_outer_quoting_style;
478 if (quoting_style == shell_always_quoting_style)
480 if (elide_outer_quotes)
481 goto force_outer_quoting_style;
488 case '%': case '+': case ',': case '-': case '.': case '/':
489 case '0': case '1': case '2': case '3': case '4': case '5':
490 case '6': case '7': case '8': case '9': case ':':
491 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
492 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
493 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
494 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
495 case 'Y': case 'Z': case ']': case '_': case 'a': case 'b':
496 case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
497 case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
498 case 'o': case 'p': case 'q': case 'r': case 's': case 't':
499 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
500 /* These characters don't cause problems, no matter what the
501 quoting style is. They cannot start multibyte sequences.
502 A digit or a special letter would cause trouble if it
503 appeared at the beginning of quote_string because we'd then
504 escape by prepending a backslash. However, it's hard to
505 imagine any locale that would use digits or letters as
506 quotes, and set_custom_quoting is documented not to accept
507 them. Also, a digit or a special letter would cause
508 trouble if it appeared in quote_these_too, but that's also
509 documented as not accepting them. */
513 /* If we have a multibyte sequence, copy it until we reach
514 its end, find an error, or come back to the initial shift
515 state. For C-like styles, if the sequence has
516 unprintable characters, escape the whole sequence, since
517 we can't easily escape single characters within it. */
519 /* Length of multibyte sequence found so far. */
527 printable = isprint (c) != 0;
532 memset (&mbstate, 0, sizeof mbstate);
536 if (argsize == SIZE_MAX)
537 argsize = strlen (arg);
542 size_t bytes = mbrtowc (&w, &arg[i + m],
543 argsize - (i + m), &mbstate);
546 else if (bytes == (size_t) -1)
551 else if (bytes == (size_t) -2)
554 while (i + m < argsize && arg[i + m])
560 /* Work around a bug with older shells that "see" a '\'
561 that is really the 2nd byte of a multibyte character.
562 In practice the problem is limited to ASCII
563 chars >= '@' that are shell special chars. */
564 if ('[' == 0x5b && elide_outer_quotes
565 && quoting_style == shell_always_quoting_style)
568 for (j = 1; j < bytes; j++)
569 switch (arg[i + m + j])
571 case '[': case '\\': case '^':
573 goto force_outer_quoting_style;
585 while (! mbsinit (&mbstate));
588 if (1 < m || (backslash_escapes && ! printable))
590 /* Output a multibyte sequence, or an escaped
591 unprintable unibyte character. */
596 if (backslash_escapes && ! printable)
598 if (elide_outer_quotes)
599 goto force_outer_quoting_style;
601 STORE ('0' + (c >> 6));
602 STORE ('0' + ((c >> 3) & 7));
605 else if (is_right_quote)
608 is_right_quote = false;
621 if (! ((backslash_escapes || elide_outer_quotes)
623 && quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS)))
628 if (elide_outer_quotes)
629 goto force_outer_quoting_style;
636 if (len == 0 && quoting_style == shell_always_quoting_style
637 && elide_outer_quotes)
638 goto force_outer_quoting_style;
640 if (quote_string && !elide_outer_quotes)
641 for (; *quote_string; quote_string++)
642 STORE (*quote_string);
644 if (len < buffersize)
648 force_outer_quoting_style:
649 /* Don't reuse quote_these_too, since the addition of outer quotes
650 sufficiently quotes the specified characters. */
651 return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
653 flags & ~QA_ELIDE_OUTER_QUOTES, NULL,
654 left_quote, right_quote);
657 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
658 argument ARG (of size ARGSIZE), using O to control quoting.
659 If O is null, use the default.
660 Terminate the output with a null character, and return the written
661 size of the output, not counting the terminating null.
662 If BUFFERSIZE is too small to store the output string, return the
663 value that would have been returned had BUFFERSIZE been large enough.
664 If ARGSIZE is SIZE_MAX, use the string length of the argument for
667 quotearg_buffer (char *buffer, size_t buffersize,
668 char const *arg, size_t argsize,
669 struct quoting_options const *o)
671 struct quoting_options const *p = o ? o : &default_quoting_options;
673 size_t r = quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
674 p->style, p->flags, p->quote_these_too,
675 p->left_quote, p->right_quote);
680 /* Equivalent to quotearg_alloc (ARG, ARGSIZE, NULL, O). */
682 quotearg_alloc (char const *arg, size_t argsize,
683 struct quoting_options const *o)
685 return quotearg_alloc_mem (arg, argsize, NULL, o);
688 /* Like quotearg_buffer (..., ARG, ARGSIZE, O), except return newly
689 allocated storage containing the quoted string, and store the
690 resulting size into *SIZE, if non-NULL. The result can contain
691 embedded null bytes only if ARGSIZE is not SIZE_MAX, SIZE is not
692 NULL, and set_quoting_flags has not set the null byte elision
695 quotearg_alloc_mem (char const *arg, size_t argsize, size_t *size,
696 struct quoting_options const *o)
698 struct quoting_options const *p = o ? o : &default_quoting_options;
700 /* Elide embedded null bytes if we can't return a size. */
701 int flags = p->flags | (size ? 0 : QA_ELIDE_NULL_BYTES);
702 size_t bufsize = quotearg_buffer_restyled (0, 0, arg, argsize, p->style,
703 flags, p->quote_these_too,
706 char *buf = xcharalloc (bufsize);
707 quotearg_buffer_restyled (buf, bufsize, arg, argsize, p->style, flags,
709 p->left_quote, p->right_quote);
716 /* A storage slot with size and pointer to a value. */
723 /* Preallocate a slot 0 buffer, so that the caller can always quote
724 one small component of a "memory exhausted" message in slot 0. */
725 static char slot0[256];
726 static unsigned int nslots = 1;
727 static struct slotvec slotvec0 = {sizeof slot0, slot0};
728 static struct slotvec *slotvec = &slotvec0;
733 struct slotvec *sv = slotvec;
735 for (i = 1; i < nslots; i++)
737 if (sv[0].val != slot0)
740 slotvec0.size = sizeof slot0;
741 slotvec0.val = slot0;
751 /* Use storage slot N to return a quoted version of argument ARG.
752 ARG is of size ARGSIZE, but if that is SIZE_MAX, ARG is a
753 null-terminated string.
754 OPTIONS specifies the quoting options.
755 The returned value points to static storage that can be
756 reused by the next call to this function with the same value of N.
757 N must be nonnegative. N is deliberately declared with type "int"
758 to allow for future extensions (using negative values). */
760 quotearg_n_options (int n, char const *arg, size_t argsize,
761 struct quoting_options const *options)
766 struct slotvec *sv = slotvec;
773 /* FIXME: technically, the type of n1 should be 'unsigned int',
774 but that evokes an unsuppressible warning from gcc-4.0.1 and
775 older. If gcc ever provides an option to suppress that warning,
776 revert to the original type, so that the test in xalloc_oversized
777 is once again performed only at compile time. */
779 bool preallocated = (sv == &slotvec0);
781 if (xalloc_oversized (n1, sizeof *sv))
784 slotvec = sv = xrealloc (preallocated ? NULL : sv, n1 * sizeof *sv);
787 memset (sv + nslots, 0, (n1 - nslots) * sizeof *sv);
792 size_t size = sv[n].size;
793 char *val = sv[n].val;
794 /* Elide embedded null bytes since we don't return a size. */
795 int flags = options->flags | QA_ELIDE_NULL_BYTES;
796 size_t qsize = quotearg_buffer_restyled (val, size, arg, argsize,
797 options->style, flags,
798 options->quote_these_too,
800 options->right_quote);
804 sv[n].size = size = qsize + 1;
807 sv[n].val = val = xcharalloc (size);
808 quotearg_buffer_restyled (val, size, arg, argsize, options->style,
809 flags, options->quote_these_too,
811 options->right_quote);
820 quotearg_n (int n, char const *arg)
822 return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options);
826 quotearg_n_mem (int n, char const *arg, size_t argsize)
828 return quotearg_n_options (n, arg, argsize, &default_quoting_options);
832 quotearg (char const *arg)
834 return quotearg_n (0, arg);
838 quotearg_mem (char const *arg, size_t argsize)
840 return quotearg_n_mem (0, arg, argsize);
844 quotearg_n_style (int n, enum quoting_style s, char const *arg)
846 struct quoting_options const o = quoting_options_from_style (s);
847 return quotearg_n_options (n, arg, SIZE_MAX, &o);
851 quotearg_n_style_mem (int n, enum quoting_style s,
852 char const *arg, size_t argsize)
854 struct quoting_options const o = quoting_options_from_style (s);
855 return quotearg_n_options (n, arg, argsize, &o);
859 quotearg_style (enum quoting_style s, char const *arg)
861 return quotearg_n_style (0, s, arg);
865 quotearg_style_mem (enum quoting_style s, char const *arg, size_t argsize)
867 return quotearg_n_style_mem (0, s, arg, argsize);
871 quotearg_char_mem (char const *arg, size_t argsize, char ch)
873 struct quoting_options options;
874 options = default_quoting_options;
875 set_char_quoting (&options, ch, 1);
876 return quotearg_n_options (0, arg, argsize, &options);
880 quotearg_char (char const *arg, char ch)
882 return quotearg_char_mem (arg, SIZE_MAX, ch);
886 quotearg_colon (char const *arg)
888 return quotearg_char (arg, ':');
892 quotearg_colon_mem (char const *arg, size_t argsize)
894 return quotearg_char_mem (arg, argsize, ':');
898 quotearg_n_custom (int n, char const *left_quote,
899 char const *right_quote, char const *arg)
901 return quotearg_n_custom_mem (n, left_quote, right_quote, arg,
906 quotearg_n_custom_mem (int n, char const *left_quote,
907 char const *right_quote,
908 char const *arg, size_t argsize)
910 struct quoting_options o = default_quoting_options;
911 set_custom_quoting (&o, left_quote, right_quote);
912 return quotearg_n_options (n, arg, argsize, &o);
916 quotearg_custom (char const *left_quote, char const *right_quote,
919 return quotearg_n_custom (0, left_quote, right_quote, arg);
923 quotearg_custom_mem (char const *left_quote, char const *right_quote,
924 char const *arg, size_t argsize)
926 return quotearg_n_custom_mem (0, left_quote, right_quote, arg,