1 /* quotearg.c - quote arguments for output
3 Copyright (C) 1998-2002, 2004-2020 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 <https://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 "attribute.h"
35 #include "c-strcaseeq.h"
36 #include "localcharset.h"
49 #define _(msgid) gettext (msgid)
50 #define N_(msgid) msgid
53 # define SIZE_MAX ((size_t) -1)
56 #define INT_BITS (sizeof (int) * CHAR_BIT)
58 struct quoting_options
60 /* Basic quoting style. */
61 enum quoting_style style
;
63 /* Additional flags. Bitwise combination of enum quoting_flags. */
66 /* Quote the characters indicated by this bit vector even if the
67 quoting style would not normally require them to be quoted. */
68 unsigned int quote_these_too
[(UCHAR_MAX
/ INT_BITS
) + 1];
70 /* The left quote for custom_quoting_style. */
71 char const *left_quote
;
73 /* The right quote for custom_quoting_style. */
74 char const *right_quote
;
77 /* Names of quoting styles. */
78 char const *const quoting_style_args
[] =
84 "shell-escape-always",
93 /* Correspondences to quoting style names. */
94 enum quoting_style
const quoting_style_vals
[] =
96 literal_quoting_style
,
98 shell_always_quoting_style
,
99 shell_escape_quoting_style
,
100 shell_escape_always_quoting_style
,
102 c_maybe_quoting_style
,
103 escape_quoting_style
,
104 locale_quoting_style
,
105 clocale_quoting_style
108 /* The default quoting options. */
109 static struct quoting_options default_quoting_options
;
111 /* Allocate a new set of quoting options, with contents initially identical
112 to O if O is not null, or to the default if O is null.
113 It is the caller's responsibility to free the result. */
114 struct quoting_options
*
115 clone_quoting_options (struct quoting_options
*o
)
118 struct quoting_options
*p
= xmemdup (o
? o
: &default_quoting_options
,
124 /* Get the value of O's quoting style. If O is null, use the default. */
126 get_quoting_style (struct quoting_options
const *o
)
128 return (o
? o
: &default_quoting_options
)->style
;
131 /* In O (or in the default if O is null),
132 set the value of the quoting style to S. */
134 set_quoting_style (struct quoting_options
*o
, enum quoting_style s
)
136 (o
? o
: &default_quoting_options
)->style
= s
;
139 /* In O (or in the default if O is null),
140 set the value of the quoting options for character C to I.
141 Return the old value. Currently, the only values defined for I are
142 0 (the default) and 1 (which means to quote the character even if
143 it would not otherwise be quoted). */
145 set_char_quoting (struct quoting_options
*o
, char c
, int i
)
147 unsigned char uc
= c
;
149 (o
? o
: &default_quoting_options
)->quote_these_too
+ uc
/ INT_BITS
;
150 int shift
= uc
% INT_BITS
;
151 int r
= (*p
>> shift
) & 1;
152 *p
^= ((i
& 1) ^ r
) << shift
;
156 /* In O (or in the default if O is null),
157 set the value of the quoting options flag to I, which can be a
158 bitwise combination of enum quoting_flags, or 0 for default
159 behavior. Return the old value. */
161 set_quoting_flags (struct quoting_options
*o
, int i
)
165 o
= &default_quoting_options
;
172 set_custom_quoting (struct quoting_options
*o
,
173 char const *left_quote
, char const *right_quote
)
176 o
= &default_quoting_options
;
177 o
->style
= custom_quoting_style
;
178 if (!left_quote
|| !right_quote
)
180 o
->left_quote
= left_quote
;
181 o
->right_quote
= right_quote
;
184 /* Return quoting options for STYLE, with no extra quoting. */
185 static struct quoting_options
/* NOT PURE!! */
186 quoting_options_from_style (enum quoting_style style
)
188 struct quoting_options o
= { literal_quoting_style
, 0, { 0 }, NULL
, NULL
};
189 if (style
== custom_quoting_style
)
195 /* MSGID approximates a quotation mark. Return its translation if it
196 has one; otherwise, return either it or "\"", depending on S.
198 S is either clocale_quoting_style or locale_quoting_style. */
200 gettext_quote (char const *msgid
, enum quoting_style s
)
202 char const *translation
= _(msgid
);
203 char const *locale_code
;
205 if (translation
!= msgid
)
208 /* For UTF-8 and GB-18030, use single quotes U+2018 and U+2019.
209 Here is a list of other locales that include U+2018 and U+2019:
211 ISO-8859-7 0xA1 KOI8-T 0x91
212 CP869 0x8B CP874 0x91
213 CP932 0x81 0x65 CP936 0xA1 0xAE
214 CP949 0xA1 0xAE CP950 0xA1 0xA5
215 CP1250 0x91 CP1251 0x91
216 CP1252 0x91 CP1253 0x91
217 CP1254 0x91 CP1255 0x91
218 CP1256 0x91 CP1257 0x91
219 EUC-JP 0xA1 0xC6 EUC-KR 0xA1 0xAE
220 EUC-TW 0xA1 0xE4 BIG5 0xA1 0xA5
221 BIG5-HKSCS 0xA1 0xA5 EUC-CN 0xA1 0xAE
222 GBK 0xA1 0xAE Georgian-PS 0x91
225 None of these is still in wide use; using iconv is overkill. */
226 locale_code
= locale_charset ();
227 if (STRCASEEQ (locale_code
, "UTF-8", 'U','T','F','-','8',0,0,0,0))
228 return msgid
[0] == '`' ? "\xe2\x80\x98": "\xe2\x80\x99";
229 if (STRCASEEQ (locale_code
, "GB18030", 'G','B','1','8','0','3','0',0,0))
230 return msgid
[0] == '`' ? "\xa1\ae": "\xa1\xaf";
232 return (s
== clocale_quoting_style
? "\"" : "'");
235 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
236 argument ARG (of size ARGSIZE), using QUOTING_STYLE, FLAGS, and
237 QUOTE_THESE_TOO to control quoting.
238 Terminate the output with a null character, and return the written
239 size of the output, not counting the terminating null.
240 If BUFFERSIZE is too small to store the output string, return the
241 value that would have been returned had BUFFERSIZE been large enough.
242 If ARGSIZE is SIZE_MAX, use the string length of the argument for ARGSIZE.
244 This function acts like quotearg_buffer (BUFFER, BUFFERSIZE, ARG,
245 ARGSIZE, O), except it breaks O into its component pieces and is
246 not careful about errno. */
249 quotearg_buffer_restyled (char *buffer
, size_t buffersize
,
250 char const *arg
, size_t argsize
,
251 enum quoting_style quoting_style
, int flags
,
252 unsigned int const *quote_these_too
,
253 char const *left_quote
,
254 char const *right_quote
)
258 size_t orig_buffersize
= 0;
259 char const *quote_string
= 0;
260 size_t quote_string_len
= 0;
261 bool backslash_escapes
= false;
262 bool unibyte_locale
= MB_CUR_MAX
== 1;
263 bool elide_outer_quotes
= (flags
& QA_ELIDE_OUTER_QUOTES
) != 0;
264 bool pending_shell_escape_end
= false;
265 bool encountered_single_quote
= false;
266 bool all_c_and_shell_quote_compat
= true;
271 if (len < buffersize) \
277 #define START_ESC() \
280 if (elide_outer_quotes) \
281 goto force_outer_quoting_style; \
283 if (quoting_style == shell_always_quoting_style \
284 && ! pending_shell_escape_end) \
289 pending_shell_escape_end = true; \
298 if (pending_shell_escape_end && ! escaping) \
302 pending_shell_escape_end = false; \
309 switch (quoting_style
)
311 case c_maybe_quoting_style
:
312 quoting_style
= c_quoting_style
;
313 elide_outer_quotes
= true;
315 case c_quoting_style
:
316 if (!elide_outer_quotes
)
318 backslash_escapes
= true;
320 quote_string_len
= 1;
323 case escape_quoting_style
:
324 backslash_escapes
= true;
325 elide_outer_quotes
= false;
328 case locale_quoting_style
:
329 case clocale_quoting_style
:
330 case custom_quoting_style
:
332 if (quoting_style
!= custom_quoting_style
)
335 Get translations for open and closing quotation marks.
336 The message catalog should translate "`" to a left
337 quotation mark suitable for the locale, and similarly for
338 "'". For example, a French Unicode local should translate
339 these to U+00AB (LEFT-POINTING DOUBLE ANGLE
340 QUOTATION MARK), and U+00BB (RIGHT-POINTING DOUBLE ANGLE
341 QUOTATION MARK), respectively.
343 If the catalog has no translation, we will try to
344 use Unicode U+2018 (LEFT SINGLE QUOTATION MARK) and
345 Unicode U+2019 (RIGHT SINGLE QUOTATION MARK). If the
346 current locale is not Unicode, locale_quoting_style
347 will quote 'like this', and clocale_quoting_style will
348 quote "like this". You should always include translations
349 for "`" and "'" even if U+2018 and U+2019 are appropriate
352 If you don't know what to put here, please see
353 <https://en.wikipedia.org/wiki/Quotation_marks_in_other_languages>
354 and use glyphs suitable for your language. */
355 left_quote
= gettext_quote (N_("`"), quoting_style
);
356 right_quote
= gettext_quote (N_("'"), quoting_style
);
358 if (!elide_outer_quotes
)
359 for (quote_string
= left_quote
; *quote_string
; quote_string
++)
360 STORE (*quote_string
);
361 backslash_escapes
= true;
362 quote_string
= right_quote
;
363 quote_string_len
= strlen (quote_string
);
367 case shell_escape_quoting_style
:
368 backslash_escapes
= true;
370 case shell_quoting_style
:
371 elide_outer_quotes
= true;
373 case shell_escape_always_quoting_style
:
374 if (!elide_outer_quotes
)
375 backslash_escapes
= true;
377 case shell_always_quoting_style
:
378 quoting_style
= shell_always_quoting_style
;
379 if (!elide_outer_quotes
)
382 quote_string_len
= 1;
385 case literal_quoting_style
:
386 elide_outer_quotes
= false;
393 for (i
= 0; ! (argsize
== SIZE_MAX
? arg
[i
] == '\0' : i
== argsize
); i
++)
397 bool is_right_quote
= false;
398 bool escaping
= false;
399 bool c_and_shell_quote_compat
= false;
401 if (backslash_escapes
402 && quoting_style
!= shell_always_quoting_style
404 && (i
+ quote_string_len
405 <= (argsize
== SIZE_MAX
&& 1 < quote_string_len
406 /* Use strlen only if we must: when argsize is SIZE_MAX,
407 and when the quote string is more than 1 byte long.
408 If we do call strlen, save the result. */
409 ? (argsize
= strlen (arg
)) : argsize
))
410 && memcmp (arg
+ i
, quote_string
, quote_string_len
) == 0)
412 if (elide_outer_quotes
)
413 goto force_outer_quoting_style
;
414 is_right_quote
= true;
421 if (backslash_escapes
)
424 /* If quote_string were to begin with digits, we'd need to
425 test for the end of the arg as well. However, it's
426 hard to imagine any locale that would use digits in
427 quotes, and set_custom_quoting is documented not to
428 accept them. Use only a single \0 with shell-escape
429 as currently digits are not printed within $'...' */
430 if (quoting_style
!= shell_always_quoting_style
431 && i
+ 1 < argsize
&& '0' <= arg
[i
+ 1] && arg
[i
+ 1] <= '9')
437 /* We don't have to worry that this last '0' will be
438 backslash-escaped because, again, quote_string should
439 not start with it and because quote_these_too is
440 documented as not accepting it. */
442 else if (flags
& QA_ELIDE_NULL_BYTES
)
447 switch (quoting_style
)
449 case shell_always_quoting_style
:
450 if (elide_outer_quotes
)
451 goto force_outer_quoting_style
;
454 case c_quoting_style
:
455 if ((flags
& QA_SPLIT_TRIGRAPHS
)
456 && i
+ 2 < argsize
&& arg
[i
+ 1] == '?')
460 case '(': case ')': case '-': case '/':
461 case '<': case '=': case '>':
462 /* Escape the second '?' in what would otherwise be
464 if (elide_outer_quotes
)
465 goto force_outer_quoting_style
;
484 case '\a': esc
= 'a'; goto c_escape
;
485 case '\b': esc
= 'b'; goto c_escape
;
486 case '\f': esc
= 'f'; goto c_escape
;
487 case '\n': esc
= 'n'; goto c_and_shell_escape
;
488 case '\r': esc
= 'r'; goto c_and_shell_escape
;
489 case '\t': esc
= 't'; goto c_and_shell_escape
;
490 case '\v': esc
= 'v'; goto c_escape
;
492 /* Never need to escape '\' in shell case. */
493 if (quoting_style
== shell_always_quoting_style
)
495 if (elide_outer_quotes
)
496 goto force_outer_quoting_style
;
500 /* No need to escape the escape if we are trying to elide
501 outer quotes and nothing else is problematic. */
502 if (backslash_escapes
&& elide_outer_quotes
&& quote_string_len
)
506 if (quoting_style
== shell_always_quoting_style
507 && elide_outer_quotes
)
508 goto force_outer_quoting_style
;
511 if (backslash_escapes
)
518 case '{': case '}': /* sometimes special if isolated */
519 if (! (argsize
== SIZE_MAX
? arg
[1] == '\0' : argsize
== 1))
527 c_and_shell_quote_compat
= true;
529 case '!': /* special in bash */
530 case '"': case '$': case '&':
531 case '(': case ')': case '*': case ';':
533 case '=': /* sometimes special in 0th or (with "set -k") later args */
535 case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
537 /* A shell special character. In theory, '$' and '`' could
538 be the first bytes of multibyte characters, which means
539 we should check them with mbrtowc, but in practice this
540 doesn't happen so it's not worth worrying about. */
541 if (quoting_style
== shell_always_quoting_style
542 && elide_outer_quotes
)
543 goto force_outer_quoting_style
;
547 encountered_single_quote
= true;
548 c_and_shell_quote_compat
= true;
549 if (quoting_style
== shell_always_quoting_style
)
551 if (elide_outer_quotes
)
552 goto force_outer_quoting_style
;
554 if (buffersize
&& ! orig_buffersize
)
556 /* Just scan string to see if supports a more concise
557 representation, rather than writing a longer string
558 but returning the length of the more concise form. */
559 orig_buffersize
= buffersize
;
566 pending_shell_escape_end
= false;
570 case '%': case '+': case ',': case '-': case '.': case '/':
571 case '0': case '1': case '2': case '3': case '4': case '5':
572 case '6': case '7': case '8': case '9': case ':':
573 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
574 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
575 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
576 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
577 case 'Y': case 'Z': case ']': case '_': case 'a': case 'b':
578 case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
579 case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
580 case 'o': case 'p': case 'q': case 'r': case 's': case 't':
581 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
582 /* These characters don't cause problems, no matter what the
583 quoting style is. They cannot start multibyte sequences.
584 A digit or a special letter would cause trouble if it
585 appeared at the beginning of quote_string because we'd then
586 escape by prepending a backslash. However, it's hard to
587 imagine any locale that would use digits or letters as
588 quotes, and set_custom_quoting is documented not to accept
589 them. Also, a digit or a special letter would cause
590 trouble if it appeared in quote_these_too, but that's also
591 documented as not accepting them. */
592 c_and_shell_quote_compat
= true;
596 /* If we have a multibyte sequence, copy it until we reach
597 its end, find an error, or come back to the initial shift
598 state. For C-like styles, if the sequence has
599 unprintable characters, escape the whole sequence, since
600 we can't easily escape single characters within it. */
602 /* Length of multibyte sequence found so far. */
610 printable
= isprint (c
) != 0;
615 memset (&mbstate
, 0, sizeof mbstate
);
619 if (argsize
== SIZE_MAX
)
620 argsize
= strlen (arg
);
625 size_t bytes
= mbrtowc (&w
, &arg
[i
+ m
],
626 argsize
- (i
+ m
), &mbstate
);
629 else if (bytes
== (size_t) -1)
634 else if (bytes
== (size_t) -2)
637 while (i
+ m
< argsize
&& arg
[i
+ m
])
643 /* Work around a bug with older shells that "see" a '\'
644 that is really the 2nd byte of a multibyte character.
645 In practice the problem is limited to ASCII
646 chars >= '@' that are shell special chars. */
647 if ('[' == 0x5b && elide_outer_quotes
648 && quoting_style
== shell_always_quoting_style
)
651 for (j
= 1; j
< bytes
; j
++)
652 switch (arg
[i
+ m
+ j
])
654 case '[': case '\\': case '^':
656 goto force_outer_quoting_style
;
668 while (! mbsinit (&mbstate
));
671 c_and_shell_quote_compat
= printable
;
673 if (1 < m
|| (backslash_escapes
&& ! printable
))
675 /* Output a multibyte sequence, or an escaped
676 unprintable unibyte character. */
681 if (backslash_escapes
&& ! printable
)
684 STORE ('0' + (c
>> 6));
685 STORE ('0' + ((c
>> 3) & 7));
688 else if (is_right_quote
)
691 is_right_quote
= false;
705 if (! (((backslash_escapes
&& quoting_style
!= shell_always_quoting_style
)
706 || elide_outer_quotes
)
708 && quote_these_too
[c
/ INT_BITS
] >> (c
% INT_BITS
) & 1)
719 if (! c_and_shell_quote_compat
)
720 all_c_and_shell_quote_compat
= false;
723 if (len
== 0 && quoting_style
== shell_always_quoting_style
724 && elide_outer_quotes
)
725 goto force_outer_quoting_style
;
727 /* Single shell quotes (') are commonly enough used as an apostrophe,
728 that we attempt to minimize the quoting in this case. Note itʼs
729 better to use the apostrophe modifier "\u02BC" if possible, as that
730 renders better and works with the word match regex \W+ etc. */
731 if (quoting_style
== shell_always_quoting_style
&& ! elide_outer_quotes
732 && encountered_single_quote
)
734 if (all_c_and_shell_quote_compat
)
735 return quotearg_buffer_restyled (buffer
, orig_buffersize
, arg
, argsize
,
737 flags
, quote_these_too
,
738 left_quote
, right_quote
);
739 else if (! buffersize
&& orig_buffersize
)
741 /* Disable read-only scan, and reprocess to write quoted string. */
742 buffersize
= orig_buffersize
;
748 if (quote_string
&& !elide_outer_quotes
)
749 for (; *quote_string
; quote_string
++)
750 STORE (*quote_string
);
752 if (len
< buffersize
)
756 force_outer_quoting_style
:
757 /* Don't reuse quote_these_too, since the addition of outer quotes
758 sufficiently quotes the specified characters. */
759 if (quoting_style
== shell_always_quoting_style
&& backslash_escapes
)
760 quoting_style
= shell_escape_always_quoting_style
;
761 return quotearg_buffer_restyled (buffer
, buffersize
, arg
, argsize
,
763 flags
& ~QA_ELIDE_OUTER_QUOTES
, NULL
,
764 left_quote
, right_quote
);
767 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
768 argument ARG (of size ARGSIZE), using O to control quoting.
769 If O is null, use the default.
770 Terminate the output with a null character, and return the written
771 size of the output, not counting the terminating null.
772 If BUFFERSIZE is too small to store the output string, return the
773 value that would have been returned had BUFFERSIZE been large enough.
774 If ARGSIZE is SIZE_MAX, use the string length of the argument for
777 quotearg_buffer (char *buffer
, size_t buffersize
,
778 char const *arg
, size_t argsize
,
779 struct quoting_options
const *o
)
781 struct quoting_options
const *p
= o
? o
: &default_quoting_options
;
783 size_t r
= quotearg_buffer_restyled (buffer
, buffersize
, arg
, argsize
,
784 p
->style
, p
->flags
, p
->quote_these_too
,
785 p
->left_quote
, p
->right_quote
);
790 /* Equivalent to quotearg_alloc (ARG, ARGSIZE, NULL, O). */
792 quotearg_alloc (char const *arg
, size_t argsize
,
793 struct quoting_options
const *o
)
795 return quotearg_alloc_mem (arg
, argsize
, NULL
, o
);
798 /* Like quotearg_buffer (..., ARG, ARGSIZE, O), except return newly
799 allocated storage containing the quoted string, and store the
800 resulting size into *SIZE, if non-NULL. The result can contain
801 embedded null bytes only if ARGSIZE is not SIZE_MAX, SIZE is not
802 NULL, and set_quoting_flags has not set the null byte elision
805 quotearg_alloc_mem (char const *arg
, size_t argsize
, size_t *size
,
806 struct quoting_options
const *o
)
808 struct quoting_options
const *p
= o
? o
: &default_quoting_options
;
810 /* Elide embedded null bytes if we can't return a size. */
811 int flags
= p
->flags
| (size
? 0 : QA_ELIDE_NULL_BYTES
);
812 size_t bufsize
= quotearg_buffer_restyled (0, 0, arg
, argsize
, p
->style
,
813 flags
, p
->quote_these_too
,
816 char *buf
= xcharalloc (bufsize
);
817 quotearg_buffer_restyled (buf
, bufsize
, arg
, argsize
, p
->style
, flags
,
819 p
->left_quote
, p
->right_quote
);
826 /* A storage slot with size and pointer to a value. */
833 /* Preallocate a slot 0 buffer, so that the caller can always quote
834 one small component of a "memory exhausted" message in slot 0. */
835 static char slot0
[256];
836 static int nslots
= 1;
837 static struct slotvec slotvec0
= {sizeof slot0
, slot0
};
838 static struct slotvec
*slotvec
= &slotvec0
;
843 struct slotvec
*sv
= slotvec
;
845 for (i
= 1; i
< nslots
; i
++)
847 if (sv
[0].val
!= slot0
)
850 slotvec0
.size
= sizeof slot0
;
851 slotvec0
.val
= slot0
;
861 /* Use storage slot N to return a quoted version of argument ARG.
862 ARG is of size ARGSIZE, but if that is SIZE_MAX, ARG is a
863 null-terminated string.
864 OPTIONS specifies the quoting options.
865 The returned value points to static storage that can be
866 reused by the next call to this function with the same value of N.
867 N must be nonnegative. N is deliberately declared with type "int"
868 to allow for future extensions (using negative values). */
870 quotearg_n_options (int n
, char const *arg
, size_t argsize
,
871 struct quoting_options
const *options
)
875 struct slotvec
*sv
= slotvec
;
882 bool preallocated
= (sv
== &slotvec0
);
883 int nmax
= MIN (INT_MAX
, MIN (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *sv
) - 1;
888 slotvec
= sv
= xrealloc (preallocated
? NULL
: sv
, (n
+ 1) * sizeof *sv
);
891 memset (sv
+ nslots
, 0, (n
+ 1 - nslots
) * sizeof *sv
);
896 size_t size
= sv
[n
].size
;
897 char *val
= sv
[n
].val
;
898 /* Elide embedded null bytes since we don't return a size. */
899 int flags
= options
->flags
| QA_ELIDE_NULL_BYTES
;
900 size_t qsize
= quotearg_buffer_restyled (val
, size
, arg
, argsize
,
901 options
->style
, flags
,
902 options
->quote_these_too
,
904 options
->right_quote
);
908 sv
[n
].size
= size
= qsize
+ 1;
911 sv
[n
].val
= val
= xcharalloc (size
);
912 quotearg_buffer_restyled (val
, size
, arg
, argsize
, options
->style
,
913 flags
, options
->quote_these_too
,
915 options
->right_quote
);
924 quotearg_n (int n
, char const *arg
)
926 return quotearg_n_options (n
, arg
, SIZE_MAX
, &default_quoting_options
);
930 quotearg_n_mem (int n
, char const *arg
, size_t argsize
)
932 return quotearg_n_options (n
, arg
, argsize
, &default_quoting_options
);
936 quotearg (char const *arg
)
938 return quotearg_n (0, arg
);
942 quotearg_mem (char const *arg
, size_t argsize
)
944 return quotearg_n_mem (0, arg
, argsize
);
948 quotearg_n_style (int n
, enum quoting_style s
, char const *arg
)
950 struct quoting_options
const o
= quoting_options_from_style (s
);
951 return quotearg_n_options (n
, arg
, SIZE_MAX
, &o
);
955 quotearg_n_style_mem (int n
, enum quoting_style s
,
956 char const *arg
, size_t argsize
)
958 struct quoting_options
const o
= quoting_options_from_style (s
);
959 return quotearg_n_options (n
, arg
, argsize
, &o
);
963 quotearg_style (enum quoting_style s
, char const *arg
)
965 return quotearg_n_style (0, s
, arg
);
969 quotearg_style_mem (enum quoting_style s
, char const *arg
, size_t argsize
)
971 return quotearg_n_style_mem (0, s
, arg
, argsize
);
975 quotearg_char_mem (char const *arg
, size_t argsize
, char ch
)
977 struct quoting_options options
;
978 options
= default_quoting_options
;
979 set_char_quoting (&options
, ch
, 1);
980 return quotearg_n_options (0, arg
, argsize
, &options
);
984 quotearg_char (char const *arg
, char ch
)
986 return quotearg_char_mem (arg
, SIZE_MAX
, ch
);
990 quotearg_colon (char const *arg
)
992 return quotearg_char (arg
, ':');
996 quotearg_colon_mem (char const *arg
, size_t argsize
)
998 return quotearg_char_mem (arg
, argsize
, ':');
1002 quotearg_n_style_colon (int n
, enum quoting_style s
, char const *arg
)
1004 struct quoting_options options
;
1005 options
= quoting_options_from_style (s
);
1006 set_char_quoting (&options
, ':', 1);
1007 return quotearg_n_options (n
, arg
, SIZE_MAX
, &options
);
1011 quotearg_n_custom (int n
, char const *left_quote
,
1012 char const *right_quote
, char const *arg
)
1014 return quotearg_n_custom_mem (n
, left_quote
, right_quote
, arg
,
1019 quotearg_n_custom_mem (int n
, char const *left_quote
,
1020 char const *right_quote
,
1021 char const *arg
, size_t argsize
)
1023 struct quoting_options o
= default_quoting_options
;
1024 set_custom_quoting (&o
, left_quote
, right_quote
);
1025 return quotearg_n_options (n
, arg
, argsize
, &o
);
1029 quotearg_custom (char const *left_quote
, char const *right_quote
,
1032 return quotearg_n_custom (0, left_quote
, right_quote
, arg
);
1036 quotearg_custom_mem (char const *left_quote
, char const *right_quote
,
1037 char const *arg
, size_t argsize
)
1039 return quotearg_n_custom_mem (0, left_quote
, right_quote
, arg
,
1044 /* The quoting option used by the functions of quote.h. */
1045 struct quoting_options quote_quoting_options
=
1047 locale_quoting_style
,
1054 quote_n_mem (int n
, char const *arg
, size_t argsize
)
1056 return quotearg_n_options (n
, arg
, argsize
, "e_quoting_options
);
1060 quote_mem (char const *arg
, size_t argsize
)
1062 return quote_n_mem (0, arg
, argsize
);
1066 quote_n (int n
, char const *arg
)
1068 return quote_n_mem (n
, arg
, SIZE_MAX
);
1072 quote (char const *arg
)
1074 return quote_n (0, arg
);