Debian Codegraph @

lib/quotearg.c

raw
1 /* quotearg.c - quote arguments for output
2
3 Copyright (C) 1998-2002, 2004-2025 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18 /* Written by Paul Eggert <[email protected]> */
19
20 #include <config.h>
21
22 /* Without this pragma, gcc 4.7.0 20111124 mistakenly suggests that
23 the quoting_options_from_style function might be candidate for
24 attribute 'pure' */
25 #if _GL_GNUC_PREREQ (4, 6)
26 # pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
27 #endif
28
29 #include "quotearg.h"
30 #include "quote.h"
31
32 #include "attribute.h"
33 #include "minmax.h"
34 #include "xalloc.h"
35 #include "c-strcaseeq.h"
36 #include "localcharset.h"
37
38 #include <ctype.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <uchar.h>
45 #include <wchar.h>
46
47 #include "gettext.h"
48 #define _(msgid) dgettext ("gnulib", msgid)
49 #define N_(msgid) msgid
50
51 #ifndef SIZE_MAX
52 # define SIZE_MAX ((size_t) -1)
53 #endif
54
55 #define INT_BITS (sizeof (int) * CHAR_BIT)
56
57 struct quoting_options
58 {
59 /* Basic quoting style. */
60 enum quoting_style style;
61
62 /* Additional flags. Bitwise combination of enum quoting_flags. */
63 int flags;
64
65 /* Quote the characters indicated by this bit vector even if the
66 quoting style would not normally require them to be quoted. */
67 unsigned int quote_these_too[(UCHAR_MAX / INT_BITS) + 1];
68
69 /* The left quote for custom_quoting_style. */
70 char const *left_quote;
71
72 /* The right quote for custom_quoting_style. */
73 char const *right_quote;
74 };
75
76 /* Names of quoting styles. */
77 char const *const quoting_style_args[] =
78 {
79 "literal",
80 "shell",
81 "shell-always",
82 "shell-escape",
83 "shell-escape-always",
84 "c",
85 "c-maybe",
86 "escape",
87 "locale",
88 "clocale",
89 NULL
90 };
91
92 /* Correspondences to quoting style names. */
93 enum quoting_style const quoting_style_vals[] =
94 {
95 literal_quoting_style,
96 shell_quoting_style,
97 shell_always_quoting_style,
98 shell_escape_quoting_style,
99 shell_escape_always_quoting_style,
100 c_quoting_style,
101 c_maybe_quoting_style,
102 escape_quoting_style,
103 locale_quoting_style,
104 clocale_quoting_style
105 };
106
107 /* The default quoting options. */
108 static struct quoting_options default_quoting_options;
109
110 /* Allocate a new set of quoting options, with contents initially identical
111 to O if O is not null, or to the default if O is null.
112 It is the caller's responsibility to free the result. */
113 struct quoting_options *
114 clone_quoting_options (struct quoting_options *o)
115 {
116 int e = errno;
117 struct quoting_options *p = xmemdup (o ? o : &default_quoting_options,
118 sizeof *o);
119 errno = e;
120 return p;
121 }
122
123 /* Get the value of O's quoting style. If O is null, use the default. */
124 enum quoting_style
125 get_quoting_style (struct quoting_options const *o)
126 {
127 return (o ? o : &default_quoting_options)->style;
128 }
129
130 /* In O (or in the default if O is null),
131 set the value of the quoting style to S. */
132 void
133 set_quoting_style (struct quoting_options *o, enum quoting_style s)
134 {
135 (o ? o : &default_quoting_options)->style = s;
136 }
137
138 /* In O (or in the default if O is null),
139 set the value of the quoting options for character C to I.
140 Return the old value. Currently, the only values defined for I are
141 0 (the default) and 1 (which means to quote the character even if
142 it would not otherwise be quoted). */
143 int
144 set_char_quoting (struct quoting_options *o, char c, int i)
145 {
146 unsigned char uc = c;
147 unsigned int *p =
148 (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
149 int shift = uc % INT_BITS;
150 unsigned int r = (*p >> shift) & 1;
151 *p ^= ((i & 1U) ^ r) << shift;
152 return r;
153 }
154
155 /* In O (or in the default if O is null),
156 set the value of the quoting options flag to I, which can be a
157 bitwise combination of enum quoting_flags, or 0 for default
158 behavior. Return the old value. */
159 int
160 set_quoting_flags (struct quoting_options *o, int i)
161 {
162 int r;
163 if (!o)
164 o = &default_quoting_options;
165 r = o->flags;
166 o->flags = i;
167 return r;
168 }
169
170 void
171 set_custom_quoting (struct quoting_options *o,
172 char const *left_quote, char const *right_quote)
173 {
174 if (!o)
175 o = &default_quoting_options;
176 o->style = custom_quoting_style;
177 if (!left_quote || !right_quote)
178 abort ();
179 o->left_quote = left_quote;
180 o->right_quote = right_quote;
181 }
182
183 /* Return quoting options for STYLE, with no extra quoting. */
184 static struct quoting_options /* NOT PURE!! */
185 quoting_options_from_style (enum quoting_style style)
186 {
187 struct quoting_options o = { literal_quoting_style, 0, { 0 }, NULL, NULL };
188 if (style == custom_quoting_style)
189 abort ();
190 o.style = style;
191 return o;
192 }
193
194 /* MSGID approximates a quotation mark. Return its translation if it
195 has one; otherwise, return either it or "\"", depending on S.
196
197 S is either clocale_quoting_style or locale_quoting_style. */
198 static char const *
199 gettext_quote (char const *msgid, enum quoting_style s)
200 {
201 char const *translation = _(msgid);
202 char const *locale_code;
203
204 if (translation != msgid)
205 return translation;
206
207 /* For UTF-8 and GB-18030, use single quotes U+2018 and U+2019.
208 Here is a list of other locales that include U+2018 and U+2019:
209
210 ISO-8859-7 0xA1 KOI8-T 0x91
211 CP869 0x8B CP874 0x91
212 CP932 0x81 0x65 CP936 0xA1 0xAE
213 CP949 0xA1 0xAE CP950 0xA1 0xA5
214 CP1250 0x91 CP1251 0x91
215 CP1252 0x91 CP1253 0x91
216 CP1254 0x91 CP1255 0x91
217 CP1256 0x91 CP1257 0x91
218 EUC-JP 0xA1 0xC6 EUC-KR 0xA1 0xAE
219 EUC-TW 0xA1 0xE4 BIG5 0xA1 0xA5
220 BIG5-HKSCS 0xA1 0xA5 EUC-CN 0xA1 0xAE
221 GBK 0xA1 0xAE Georgian-PS 0x91
222 PT154 0x91
223
224 None of these is still in wide use; using iconv is overkill. */
225 locale_code = locale_charset ();
226 if (STRCASEEQ (locale_code, "UTF-8", 'U','T','F','-','8',0,0,0,0))
227 return msgid[0] == '`' ? "\xe2\x80\x98": "\xe2\x80\x99";
228 if (STRCASEEQ (locale_code, "GB18030", 'G','B','1','8','0','3','0',0,0))
229 return msgid[0] == '`' ? "\xa1\ae": "\xa1\xaf";
230
231 return (s == clocale_quoting_style ? "\"" : "'");
232 }
233
234 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
235 argument ARG (of size ARGSIZE), using QUOTING_STYLE, FLAGS, and
236 QUOTE_THESE_TOO to control quoting.
237 Terminate the output with a null character, and return the written
238 size of the output, not counting the terminating null.
239 If BUFFERSIZE is too small to store the output string, return the
240 value that would have been returned had BUFFERSIZE been large enough.
241 If ARGSIZE is SIZE_MAX, use the string length of the argument for ARGSIZE.
242
243 This function acts like quotearg_buffer (BUFFER, BUFFERSIZE, ARG,
244 ARGSIZE, O), except it breaks O into its component pieces and is
245 not careful about errno. */
246
247 static size_t
248 quotearg_buffer_restyled (char *buffer, size_t buffersize,
249 char const *arg, size_t argsize,
250 enum quoting_style quoting_style, int flags,
251 unsigned int const *quote_these_too,
252 char const *left_quote,
253 char const *right_quote)
254 {
255 size_t i;
256 size_t len = 0;
257 size_t orig_buffersize = 0;
258 char const *quote_string = NULL;
259 size_t quote_string_len = 0;
260 bool backslash_escapes = false;
261 bool unibyte_locale = MB_CUR_MAX == 1;
262 bool elide_outer_quotes = (flags & QA_ELIDE_OUTER_QUOTES) != 0;
263 bool encountered_single_quote = false;
264 bool all_c_and_shell_quote_compat = true;
265
266 #define STORE(c) \
267 do \
268 { \
269 if (len < buffersize) \
270 buffer[len] = (c); \
271 len++; \
272 } \
273 while (0)
274
275 #define START_ESC() \
276 do \
277 { \
278 if (elide_outer_quotes) \
279 goto force_outer_quoting_style; \
280 escaping = true; \
281 if (quoting_style == shell_always_quoting_style \
282 && ! pending_shell_escape_end) \
283 { \
284 STORE ('\''); \
285 STORE ('$'); \
286 STORE ('\''); \
287 pending_shell_escape_end = true; \
288 } \
289 STORE ('\\'); \
290 } \
291 while (0)
292
293 #define END_ESC() \
294 do \
295 { \
296 if (pending_shell_escape_end && ! escaping) \
297 { \
298 STORE ('\''); \
299 STORE ('\''); \
300 pending_shell_escape_end = false; \
301 } \
302 } \
303 while (0)
304
305 process_input: ;
306 bool pending_shell_escape_end = false;
307
308 switch (quoting_style)
309 {
310 case c_maybe_quoting_style:
311 quoting_style = c_quoting_style;
312 elide_outer_quotes = true;
313 FALLTHROUGH;
314 case c_quoting_style:
315 if (!elide_outer_quotes)
316 STORE ('"');
317 backslash_escapes = true;
318 quote_string = "\"";
319 quote_string_len = 1;
320 break;
321
322 case escape_quoting_style:
323 backslash_escapes = true;
324 elide_outer_quotes = false;
325 break;
326
327 case locale_quoting_style:
328 case clocale_quoting_style:
329 case custom_quoting_style:
330 {
331 if (quoting_style != custom_quoting_style)
332 {
333 /* TRANSLATORS:
334 Get translations for open and closing quotation marks.
335 The message catalog should translate "`" to a left
336 quotation mark suitable for the locale, and similarly for
337 "'". For example, a French Unicode local should translate
338 these to U+00AB (LEFT-POINTING DOUBLE ANGLE
339 QUOTATION MARK), and U+00BB (RIGHT-POINTING DOUBLE ANGLE
340 QUOTATION MARK), respectively.
341
342 If the catalog has no translation, we will try to
343 use Unicode U+2018 (LEFT SINGLE QUOTATION MARK) and
344 Unicode U+2019 (RIGHT SINGLE QUOTATION MARK). If the
345 current locale is not Unicode, locale_quoting_style
346 will quote 'like this', and clocale_quoting_style will
347 quote "like this". You should always include translations
348 for "`" and "'" even if U+2018 and U+2019 are appropriate
349 for your locale.
350
351 If you don't know what to put here, please see
352 <https://en.wikipedia.org/wiki/Quotation_marks_in_other_languages>
353 and use glyphs suitable for your language. */
354 left_quote = gettext_quote (N_("`"), quoting_style);
355 right_quote = gettext_quote (N_("'"), quoting_style);
356 }
357 if (!elide_outer_quotes)
358 for (quote_string = left_quote; *quote_string; quote_string++)
359 STORE (*quote_string);
360 backslash_escapes = true;
361 quote_string = right_quote;
362 quote_string_len = strlen (quote_string);
363 }
364 break;
365
366 case shell_escape_quoting_style:
367 backslash_escapes = true;
368 FALLTHROUGH;
369 case shell_quoting_style:
370 elide_outer_quotes = true;
371 FALLTHROUGH;
372 case shell_escape_always_quoting_style:
373 if (!elide_outer_quotes)
374 backslash_escapes = true;
375 FALLTHROUGH;
376 case shell_always_quoting_style:
377 quoting_style = shell_always_quoting_style;
378 if (!elide_outer_quotes)
379 STORE ('\'');
380 quote_string = "'";
381 quote_string_len = 1;
382 break;
383
384 case literal_quoting_style:
385 elide_outer_quotes = false;
386 break;
387
388 default:
389 abort ();
390 }
391
392 for (i = 0; ! (argsize == SIZE_MAX ? arg[i] == '\0' : i == argsize); i++)
393 {
394 unsigned char c;
395 unsigned char esc;
396 bool is_right_quote = false;
397 bool escaping = false;
398 bool c_and_shell_quote_compat = false;
399
400 if (backslash_escapes
401 && quoting_style != shell_always_quoting_style
402 && quote_string_len
403 && (i + quote_string_len
404 <= (argsize == SIZE_MAX && 1 < quote_string_len
405 /* Use strlen only if we must: when argsize is SIZE_MAX,
406 and when the quote string is more than 1 byte long.
407 If we do call strlen, save the result. */
408 ? (argsize = strlen (arg)) : argsize))
409 && memcmp (arg + i, quote_string, quote_string_len) == 0)
410 {
411 if (elide_outer_quotes)
412 goto force_outer_quoting_style;
413 is_right_quote = true;
414 }
415
416 c = arg[i];
417 switch (c)
418 {
419 case '\0':
420 if (backslash_escapes)
421 {
422 START_ESC ();
423 /* If quote_string were to begin with digits, we'd need to
424 test for the end of the arg as well. However, it's
425 hard to imagine any locale that would use digits in
426 quotes, and set_custom_quoting is documented not to
427 accept them. Use only a single \0 with shell-escape
428 as currently digits are not printed within $'...' */
429 if (quoting_style != shell_always_quoting_style
430 && i + 1 < argsize && '0' <= arg[i + 1] && arg[i + 1] <= '9')
431 {
432 STORE ('0');
433 STORE ('0');
434 }
435 c = '0';
436 /* We don't have to worry that this last '0' will be
437 backslash-escaped because, again, quote_string should
438 not start with it and because quote_these_too is
439 documented as not accepting it. */
440 }
441 else if (flags & QA_ELIDE_NULL_BYTES)
442 continue;
443 break;
444
445 case '?':
446 switch (+quoting_style)
447 {
448 case shell_always_quoting_style:
449 if (elide_outer_quotes)
450 goto force_outer_quoting_style;
451 break;
452
453 case c_quoting_style:
454 if ((flags & QA_SPLIT_TRIGRAPHS)
455 && i + 2 < argsize && arg[i + 1] == '?')
456 switch (arg[i + 2])
457 {
458 case '!': case '\'':
459 case '(': case ')': case '-': case '/':
460 case '<': case '=': case '>':
461 /* Escape the second '?' in what would otherwise be
462 a trigraph. */
463 if (elide_outer_quotes)
464 goto force_outer_quoting_style;
465 c = arg[i + 2];
466 i += 2;
467 STORE ('?');
468 STORE ('"');
469 STORE ('"');
470 STORE ('?');
471 break;
472 }
473 break;
474 }
475 break;
476
477 case '\a': esc = 'a'; goto c_escape;
478 case '\b': esc = 'b'; goto c_escape;
479 case '\f': esc = 'f'; goto c_escape;
480 case '\n': esc = 'n'; goto c_and_shell_escape;
481 case '\r': esc = 'r'; goto c_and_shell_escape;
482 case '\t': esc = 't'; goto c_and_shell_escape;
483 case '\v': esc = 'v'; goto c_escape;
484 case '\\': esc = c;
485 /* Never need to escape '\' in shell case. */
486 if (quoting_style == shell_always_quoting_style)
487 {
488 if (elide_outer_quotes)
489 goto force_outer_quoting_style;
490 goto store_c;
491 }
492
493 /* No need to escape the escape if we are trying to elide
494 outer quotes and nothing else is problematic. */
495 if (backslash_escapes && elide_outer_quotes && quote_string_len)
496 goto store_c;
497
498 c_and_shell_escape:
499 if (quoting_style == shell_always_quoting_style
500 && elide_outer_quotes)
501 goto force_outer_quoting_style;
502 /* fall through */
503 c_escape:
504 if (backslash_escapes)
505 {
506 c = esc;
507 goto store_escape;
508 }
509 break;
510
511 case '{': case '}': /* sometimes special if isolated */
512 if (! (argsize == SIZE_MAX ? arg[1] == '\0' : argsize == 1))
513 break;
514 FALLTHROUGH;
515 case '#': case '~':
516 if (i != 0)
517 break;
518 FALLTHROUGH;
519 case ' ':
520 c_and_shell_quote_compat = true;
521 FALLTHROUGH;
522 case '!': /* special in bash */
523 case '"': case '$': case '&':
524 case '(': case ')': case '*': case ';':
525 case '<':
526 case '=': /* sometimes special in 0th or (with "set -k") later args */
527 case '>': case '[':
528 case '^': /* special in old /bin/sh, e.g., Solaris 10 */
529 case '`': case '|':
530 /* A shell special character. */
531 if (quoting_style == shell_always_quoting_style
532 && elide_outer_quotes)
533 goto force_outer_quoting_style;
534 break;
535
536 case '\'':
537 encountered_single_quote = true;
538 c_and_shell_quote_compat = true;
539 if (quoting_style == shell_always_quoting_style)
540 {
541 if (elide_outer_quotes)
542 goto force_outer_quoting_style;
543
544 if (buffersize && ! orig_buffersize)
545 {
546 /* Just scan string to see if supports a more concise
547 representation, rather than writing a longer string
548 but returning the length of the more concise form. */
549 orig_buffersize = buffersize;
550 buffersize = 0;
551 }
552
553 STORE ('\'');
554 STORE ('\\');
555 STORE ('\'');
556 pending_shell_escape_end = false;
557 }
558 break;
559
560 case '%': case '+': case ',': case '-': case '.': case '/':
561 case '0': case '1': case '2': case '3': case '4': case '5':
562 case '6': case '7': case '8': case '9': case ':':
563 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
564 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
565 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
566 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
567 case 'Y': case 'Z': case ']': case '_': case 'a': case 'b':
568 case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
569 case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
570 case 'o': case 'p': case 'q': case 'r': case 's': case 't':
571 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
572 /* These characters don't cause problems, no matter what the
573 quoting style is. They cannot start multibyte sequences.
574 A digit or a special letter would cause trouble if it
575 appeared at the beginning of quote_string because we'd then
576 escape by prepending a backslash. However, it's hard to
577 imagine any locale that would use digits or letters as
578 quotes, and set_custom_quoting is documented not to accept
579 them. Also, a digit or a special letter would cause
580 trouble if it appeared in quote_these_too, but that's also
581 documented as not accepting them. */
582 c_and_shell_quote_compat = true;
583 break;
584
585 default:
586 /* If we have a multibyte sequence, copy it until we reach
587 its end, find an error, or come back to the initial shift
588 state. For C-like styles, if the sequence has
589 unprintable characters, escape the whole sequence, since
590 we can't easily escape single characters within it. */
591 {
592 /* Length of multibyte sequence found so far. */
593 size_t m;
594
595 bool printable;
596
597 if (unibyte_locale)
598 {
599 m = 1;
600 printable = isprint (c) != 0;
601 }
602 else
603 {
604 mbstate_t mbstate;
605 mbszero (&mbstate);
606
607 m = 0;
608 printable = true;
609 if (argsize == SIZE_MAX)
610 argsize = strlen (arg);
611
612 for (;;)
613 {
614 char32_t w;
615 size_t bytes = mbrtoc32 (&w, &arg[i + m],
616 argsize - (i + m), &mbstate);
617 if (bytes == 0)
618 break;
619 else if (bytes == (size_t) -1)
620 {
621 printable = false;
622 break;
623 }
624 else if (bytes == (size_t) -2)
625 {
626 printable = false;
627 while (i + m < argsize && arg[i + m])
628 m++;
629 break;
630 }
631 else
632 {
633 #if !GNULIB_MBRTOC32_REGULAR
634 if (bytes == (size_t) -3)
635 bytes = 0;
636 #endif
637 /* Work around a bug with older shells that "see" a '\'
638 that is really the 2nd byte of a multibyte character.
639 In practice the problem is limited to ASCII
640 chars >= '@' that are shell special chars. */
641 if ('[' == 0x5b && elide_outer_quotes
642 && quoting_style == shell_always_quoting_style)
643 {
644 size_t j;
645 for (j = 1; j < bytes; j++)
646 switch (arg[i + m + j])
647 {
648 case '[': case '\\': case '^':
649 case '`': case '|':
650 goto force_outer_quoting_style;
651 }
652 }
653
654 if (! c32isprint (w))
655 printable = false;
656 m += bytes;
657 }
658 #if !GNULIB_MBRTOC32_REGULAR
659 if (mbsinit (&mbstate))
660 #endif
661 break;
662 }
663 }
664
665 c_and_shell_quote_compat = printable;
666
667 if (1 < m || (backslash_escapes && ! printable))
668 {
669 /* Output a multibyte sequence, or an escaped
670 unprintable unibyte character. */
671 size_t ilim = i + m;
672
673 for (;;)
674 {
675 if (backslash_escapes && ! printable)
676 {
677 START_ESC ();
678 STORE ('0' + (c >> 6));
679 STORE ('0' + ((c >> 3) & 7));
680 c = '0' + (c & 7);
681 }
682 else if (is_right_quote)
683 {
684 STORE ('\\');
685 is_right_quote = false;
686 }
687 if (ilim <= i + 1)
688 break;
689 END_ESC ();
690 STORE (c);
691 c = arg[++i];
692 }
693
694 goto store_c;
695 }
696 }
697 }
698
699 if (! (((backslash_escapes && quoting_style != shell_always_quoting_style)
700 || elide_outer_quotes)
701 && quote_these_too
702 && quote_these_too[c / INT_BITS] >> (c % INT_BITS) & 1)
703 && !is_right_quote)
704 goto store_c;
705
706 store_escape:
707 START_ESC ();
708
709 store_c:
710 END_ESC ();
711 STORE (c);
712
713 if (! c_and_shell_quote_compat)
714 all_c_and_shell_quote_compat = false;
715 }
716
717 if (len == 0 && quoting_style == shell_always_quoting_style
718 && elide_outer_quotes)
719 goto force_outer_quoting_style;
720
721 /* Single shell quotes (') are commonly enough used as an apostrophe,
722 that we attempt to minimize the quoting in this case. Note itʼs
723 better to use the apostrophe modifier "\u02BC" if possible, as that
724 renders better and works with the word match regex \W+ etc. */
725 if (quoting_style == shell_always_quoting_style && ! elide_outer_quotes
726 && encountered_single_quote)
727 {
728 if (all_c_and_shell_quote_compat)
729 return quotearg_buffer_restyled (buffer, orig_buffersize, arg, argsize,
730 c_quoting_style,
731 flags, quote_these_too,
732 left_quote, right_quote);
733 else if (! buffersize && orig_buffersize)
734 {
735 /* Disable read-only scan, and reprocess to write quoted string. */
736 buffersize = orig_buffersize;
737 len = 0;
738 goto process_input;
739 }
740 }
741
742 if (quote_string && !elide_outer_quotes)
743 for (; *quote_string; quote_string++)
744 STORE (*quote_string);
745
746 if (len < buffersize)
747 buffer[len] = '\0';
748 return len;
749
750 force_outer_quoting_style:
751 /* Don't reuse quote_these_too, since the addition of outer quotes
752 sufficiently quotes the specified characters. */
753 if (quoting_style == shell_always_quoting_style && backslash_escapes)
754 quoting_style = shell_escape_always_quoting_style;
755 return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
756 quoting_style,
757 flags & ~QA_ELIDE_OUTER_QUOTES, NULL,
758 left_quote, right_quote);
759 }
760
761 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
762 argument ARG (of size ARGSIZE), using O to control quoting.
763 If O is null, use the default.
764 Terminate the output with a null character, and return the written
765 size of the output, not counting the terminating null.
766 If BUFFERSIZE is too small to store the output string, return the
767 value that would have been returned had BUFFERSIZE been large enough.
768 If ARGSIZE is SIZE_MAX, use the string length of the argument for
769 ARGSIZE. */
770 size_t
771 quotearg_buffer (char *buffer, size_t buffersize,
772 char const *arg, size_t argsize,
773 struct quoting_options const *o)
774 {
775 struct quoting_options const *p = o ? o : &default_quoting_options;
776 int e = errno;
777 size_t r = quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
778 p->style, p->flags, p->quote_these_too,
779 p->left_quote, p->right_quote);
780 errno = e;
781 return r;
782 }
783
784 char *
785 quotearg_alloc (char const *arg, size_t argsize,
786 struct quoting_options const *o)
787 {
788 return quotearg_alloc_mem (arg, argsize, NULL, o);
789 }
790
791 /* Like quotearg_buffer (..., ARG, ARGSIZE, O), except return newly
792 allocated storage containing the quoted string, and store the
793 resulting size into *SIZE, if non-NULL. The result can contain
794 embedded null bytes only if ARGSIZE is not SIZE_MAX, SIZE is not
795 NULL, and set_quoting_flags has not set the null byte elision
796 flag. */
797 char *
798 quotearg_alloc_mem (char const *arg, size_t argsize, size_t *size,
799 struct quoting_options const *o)
800 {
801 struct quoting_options const *p = o ? o : &default_quoting_options;
802 int e = errno;
803 /* Elide embedded null bytes if we can't return a size. */
804 int flags = p->flags | (size ? 0 : QA_ELIDE_NULL_BYTES);
805 size_t bufsize = quotearg_buffer_restyled (NULL, 0, arg, argsize, p->style,
806 flags, p->quote_these_too,
807 p->left_quote,
808 p->right_quote) + 1;
809 char *buf = xcharalloc (bufsize);
810 quotearg_buffer_restyled (buf, bufsize, arg, argsize, p->style, flags,
811 p->quote_these_too,
812 p->left_quote, p->right_quote);
813 errno = e;
814 if (size)
815 *size = bufsize - 1;
816 return buf;
817 }
818
819 /* A storage slot with size and pointer to a value. */
820 struct slotvec
821 {
822 size_t size;
823 char *val;
824 };
825
826 /* Preallocate a slot 0 buffer, so that the caller can always quote
827 one small component of a "memory exhausted" message in slot 0. */
828 static char slot0[256];
829 static int nslots = 1;
830 static struct slotvec slotvec0 = {sizeof slot0, slot0};
831 static struct slotvec *slotvec = &slotvec0;
832
833 void
834 quotearg_free (void)
835 {
836 struct slotvec *sv = slotvec;
837 int i;
838 for (i = 1; i < nslots; i++)
839 free (sv[i].val);
840 if (sv[0].val != slot0)
841 {
842 free (sv[0].val);
843 slotvec0.size = sizeof slot0;
844 slotvec0.val = slot0;
845 }
846 if (sv != &slotvec0)
847 {
848 free (sv);
849 slotvec = &slotvec0;
850 }
851 nslots = 1;
852 }
853
854 /* Use storage slot N to return a quoted version of argument ARG.
855 ARG is of size ARGSIZE, but if that is SIZE_MAX, ARG is a
856 null-terminated string.
857 OPTIONS specifies the quoting options.
858 The returned value points to static storage that can be
859 reused by the next call to this function with the same value of N.
860 N must be nonnegative; it is typically small, and must be
861 less than MIN (INT_MAX, IDX_MAX). The type of N is signed
862 to allow for future extensions (using negative values). */
863 static char *
864 quotearg_n_options (int n, char const *arg, size_t argsize,
865 struct quoting_options const *options)
866 {
867 int e = errno;
868
869 struct slotvec *sv = slotvec;
870
871 int nslots_max = MIN (INT_MAX, IDX_MAX);
872 if (! (0 <= n && n < nslots_max))
873 abort ();
874
875 if (nslots <= n)
876 {
877 bool preallocated = (sv == &slotvec0);
878 idx_t new_nslots = nslots;
879
880 slotvec = sv = xpalloc (preallocated ? NULL : sv, &new_nslots,
881 n - nslots + 1, nslots_max, sizeof *sv);
882 if (preallocated)
883 *sv = slotvec0;
884 memset (sv + nslots, 0, (new_nslots - nslots) * sizeof *sv);
885 nslots = new_nslots;
886 }
887
888 {
889 size_t size = sv[n].size;
890 char *val = sv[n].val;
891 /* Elide embedded null bytes since we don't return a size. */
892 int flags = options->flags | QA_ELIDE_NULL_BYTES;
893 size_t qsize = quotearg_buffer_restyled (val, size, arg, argsize,
894 options->style, flags,
895 options->quote_these_too,
896 options->left_quote,
897 options->right_quote);
898
899 if (size <= qsize)
900 {
901 sv[n].size = size = qsize + 1;
902 if (val != slot0)
903 free (val);
904 sv[n].val = val = xcharalloc (size);
905 quotearg_buffer_restyled (val, size, arg, argsize, options->style,
906 flags, options->quote_these_too,
907 options->left_quote,
908 options->right_quote);
909 }
910
911 errno = e;
912 return val;
913 }
914 }
915
916 char *
917 quotearg_n (int n, char const *arg)
918 {
919 return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options);
920 }
921
922 char *
923 quotearg_n_mem (int n, char const *arg, size_t argsize)
924 {
925 return quotearg_n_options (n, arg, argsize, &default_quoting_options);
926 }
927
928 char *
929 quotearg (char const *arg)
930 {
931 return quotearg_n (0, arg);
932 }
933
934 char *
935 quotearg_mem (char const *arg, size_t argsize)
936 {
937 return quotearg_n_mem (0, arg, argsize);
938 }
939
940 char *
941 quotearg_n_style (int n, enum quoting_style s, char const *arg)
942 {
943 struct quoting_options const o = quoting_options_from_style (s);
944 return quotearg_n_options (n, arg, SIZE_MAX, &o);
945 }
946
947 char *
948 quotearg_n_style_mem (int n, enum quoting_style s,
949 char const *arg, size_t argsize)
950 {
951 struct quoting_options const o = quoting_options_from_style (s);
952 return quotearg_n_options (n, arg, argsize, &o);
953 }
954
955 char *
956 quotearg_style (enum quoting_style s, char const *arg)
957 {
958 return quotearg_n_style (0, s, arg);
959 }
960
961 char *
962 quotearg_style_mem (enum quoting_style s, char const *arg, size_t argsize)
963 {
964 return quotearg_n_style_mem (0, s, arg, argsize);
965 }
966
967 char *
968 quotearg_char_mem (char const *arg, size_t argsize, char ch)
969 {
970 struct quoting_options options;
971 options = default_quoting_options;
972 set_char_quoting (&options, ch, 1);
973 return quotearg_n_options (0, arg, argsize, &options);
974 }
975
976 char *
977 quotearg_char (char const *arg, char ch)
978 {
979 return quotearg_char_mem (arg, SIZE_MAX, ch);
980 }
981
982 char *
983 quotearg_colon (char const *arg)
984 {
985 return quotearg_char (arg, ':');
986 }
987
988 char *
989 quotearg_colon_mem (char const *arg, size_t argsize)
990 {
991 return quotearg_char_mem (arg, argsize, ':');
992 }
993
994 char *
995 quotearg_n_style_colon (int n, enum quoting_style s, char const *arg)
996 {
997 struct quoting_options options;
998 options = quoting_options_from_style (s);
999 set_char_quoting (&options, ':', 1);
1000 return quotearg_n_options (n, arg, SIZE_MAX, &options);
1001 }
1002
1003 char *
1004 quotearg_n_custom (int n, char const *left_quote,
1005 char const *right_quote, char const *arg)
1006 {
1007 return quotearg_n_custom_mem (n, left_quote, right_quote, arg,
1008 SIZE_MAX);
1009 }
1010
1011 char *
1012 quotearg_n_custom_mem (int n, char const *left_quote,
1013 char const *right_quote,
1014 char const *arg, size_t argsize)
1015 {
1016 struct quoting_options o = default_quoting_options;
1017 set_custom_quoting (&o, left_quote, right_quote);
1018 return quotearg_n_options (n, arg, argsize, &o);
1019 }
1020
1021 char *
1022 quotearg_custom (char const *left_quote, char const *right_quote,
1023 char const *arg)
1024 {
1025 return quotearg_n_custom (0, left_quote, right_quote, arg);
1026 }
1027
1028 char *
1029 quotearg_custom_mem (char const *left_quote, char const *right_quote,
1030 char const *arg, size_t argsize)
1031 {
1032 return quotearg_n_custom_mem (0, left_quote, right_quote, arg,
1033 argsize);
1034 }
1035
1036
1037 /* The quoting option used by the functions of quote.h. */
1038 struct quoting_options quote_quoting_options =
1039 {
1040 locale_quoting_style,
1041 0,
1042 { 0 },
1043 NULL, NULL
1044 };
1045
1046 char const *
1047 quote_n_mem (int n, char const *arg, size_t argsize)
1048 {
1049 return quotearg_n_options (n, arg, argsize, &quote_quoting_options);
1050 }
1051
1052 char const *
1053 quote_mem (char const *arg, size_t argsize)
1054 {
1055 return quote_n_mem (0, arg, argsize);
1056 }
1057
1058 char const *
1059 quote_n (int n, char const *arg)
1060 {
1061 return quote_n_mem (n, arg, SIZE_MAX);
1062 }
1063
1064 char const *
1065 quote (char const *arg)
1066 {
1067 return quote_n (0, arg);
1068 }
1069
1070 /*
1071 * Hey Emacs!
1072 * Local Variables:
1073 * coding: utf-8
1074 * End:
1075 */