1 /* quotearg.c - quote arguments for output
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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 2, or (at your option)
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Eggert <eggert@twinsun.com> */
25 # include <stddef.h> /* For the definition of size_t on windows w/MSVC. */
27 #include <sys/types.h>
35 # define _(text) gettext (text)
48 # define SIZE_MAX ((size_t) -1)
51 # define UCHAR_MAX ((unsigned char) -1)
54 # define UINT_MAX ((unsigned int) -1)
57 #if HAVE_C_BACKSLASH_A
58 # define ALERT_CHAR '\a'
60 # define ALERT_CHAR '\7'
73 /* BSD/OS 4.1 wchar.h requires FILE and struct tm to be declared. */
81 /* Disable multibyte processing entirely. Since MB_CUR_MAX is 1, the
82 other macros are defined only for documentation and to satisfy C
86 # define mbrtowc(pwc, s, n, ps) ((*(pwc) = *(s)) != 0)
87 # define mbsinit(ps) 1
88 # define iswprint(wc) ISPRINT ((unsigned char) (wc))
95 # if !defined iswprint && !HAVE_ISWPRINT
96 # define iswprint(wc) 1
100 #define INT_BITS (sizeof (int) * CHAR_BIT)
102 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
103 # define IN_CTYPE_DOMAIN(c) 1
105 # define IN_CTYPE_DOMAIN(c) isascii(c)
108 /* Undefine to protect against the definition in wctype.h of solaris2.6. */
110 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
112 struct quoting_options
114 /* Basic quoting style. */
115 enum quoting_style style;
117 /* Quote the characters indicated by this bit vector even if the
118 quoting style would not normally require them to be quoted. */
119 int quote_these_too[(UCHAR_MAX / INT_BITS) + 1];
122 /* Names of quoting styles. */
123 char const *const quoting_style_args[] =
135 /* Correspondences to quoting style names. */
136 enum quoting_style const quoting_style_vals[] =
138 literal_quoting_style,
140 shell_always_quoting_style,
142 escape_quoting_style,
143 locale_quoting_style,
144 clocale_quoting_style
147 /* The default quoting options. */
148 static struct quoting_options default_quoting_options;
150 /* Allocate a new set of quoting options, with contents initially identical
151 to O if O is not null, or to the default if O is null.
152 It is the caller's responsibility to free the result. */
153 struct quoting_options *
154 clone_quoting_options (struct quoting_options *o)
156 struct quoting_options *p
157 = (struct quoting_options *) xmalloc (sizeof (struct quoting_options));
158 *p = *(o ? o : &default_quoting_options);
162 /* Get the value of O's quoting style. If O is null, use the default. */
164 get_quoting_style (struct quoting_options *o)
166 return (o ? o : &default_quoting_options)->style;
169 /* In O (or in the default if O is null),
170 set the value of the quoting style to S. */
172 set_quoting_style (struct quoting_options *o, enum quoting_style s)
174 (o ? o : &default_quoting_options)->style = s;
177 /* In O (or in the default if O is null),
178 set the value of the quoting options for character C to I.
179 Return the old value. Currently, the only values defined for I are
180 0 (the default) and 1 (which means to quote the character even if
181 it would not otherwise be quoted). */
183 set_char_quoting (struct quoting_options *o, char c, int i)
185 unsigned char uc = c;
186 int *p = (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
187 int shift = uc % INT_BITS;
188 int r = (*p >> shift) & 1;
189 *p ^= ((i & 1) ^ r) << shift;
193 /* MSGID approximates a quotation mark. Return its translation if it
194 has one; otherwise, return either it or "\"", depending on S. */
196 gettext_quote (char const *msgid, enum quoting_style s)
198 char const *translation = _(msgid);
199 if (translation == msgid && s == clocale_quoting_style)
204 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
205 argument ARG (of size ARGSIZE), using QUOTING_STYLE and the
206 non-quoting-style part of O to control quoting.
207 Terminate the output with a null character, and return the written
208 size of the output, not counting the terminating null.
209 If BUFFERSIZE is too small to store the output string, return the
210 value that would have been returned had BUFFERSIZE been large enough.
211 If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
213 This function acts like quotearg_buffer (BUFFER, BUFFERSIZE, ARG,
214 ARGSIZE, O), except it uses QUOTING_STYLE instead of the quoting
215 style specified by O, and O may not be null. */
218 quotearg_buffer_restyled (char *buffer, size_t buffersize,
219 char const *arg, size_t argsize,
220 enum quoting_style quoting_style,
221 struct quoting_options const *o)
225 char const *quote_string = 0;
226 size_t quote_string_len = 0;
227 int backslash_escapes = 0;
228 int unibyte_locale = MB_CUR_MAX == 1;
233 if (len < buffersize) \
239 switch (quoting_style)
241 case c_quoting_style:
243 backslash_escapes = 1;
245 quote_string_len = 1;
248 case escape_quoting_style:
249 backslash_escapes = 1;
252 case locale_quoting_style:
253 case clocale_quoting_style:
255 /* Get translations for open and closing quotation marks.
257 The message catalog should translate "`" to a left
258 quotation mark suitable for the locale, and similarly for
259 "'". If the catalog has no translation,
260 locale_quoting_style quotes `like this', and
261 clocale_quoting_style quotes "like this".
263 For example, an American English Unicode locale should
264 translate "`" to U+201C (LEFT DOUBLE QUOTATION MARK), and
265 should translate "'" to U+201D (RIGHT DOUBLE QUOTATION
266 MARK). A British English Unicode locale should instead
267 translate these to U+2018 (LEFT SINGLE QUOTATION MARK) and
268 U+2019 (RIGHT SINGLE QUOTATION MARK), respectively. */
270 char const *left = gettext_quote (N_("`"), quoting_style);
271 char const *right = gettext_quote (N_("'"), quoting_style);
272 for (quote_string = left; *quote_string; quote_string++)
273 STORE (*quote_string);
274 backslash_escapes = 1;
275 quote_string = right;
276 quote_string_len = strlen (quote_string);
280 case shell_always_quoting_style:
283 quote_string_len = 1;
290 for (i = 0; ! (argsize == (size_t) -1 ? arg[i] == '\0' : i == argsize); i++)
295 if (backslash_escapes
297 && i + quote_string_len <= argsize
298 && memcmp (arg + i, quote_string, quote_string_len) == 0)
305 switch (quoting_style)
307 case shell_quoting_style:
308 goto use_shell_always_quoting_style;
310 case c_quoting_style:
311 if (i + 2 < argsize && arg[i + 1] == '?')
315 case '(': case ')': case '-': case '/':
316 case '<': case '=': case '>':
317 /* Escape the second '?' in what would otherwise be
333 case ALERT_CHAR: esc = 'a'; goto c_escape;
334 case '\b': esc = 'b'; goto c_escape;
335 case '\f': esc = 'f'; goto c_escape;
336 case '\n': esc = 'n'; goto c_and_shell_escape;
337 case '\r': esc = 'r'; goto c_and_shell_escape;
338 case '\t': esc = 't'; goto c_and_shell_escape;
339 case '\v': esc = 'v'; goto c_escape;
340 case '\\': esc = c; goto c_and_shell_escape;
343 if (quoting_style == shell_quoting_style)
344 goto use_shell_always_quoting_style;
346 if (backslash_escapes)
358 case '!': /* special in bash */
359 case '"': case '$': case '&':
360 case '(': case ')': case '*': case ';':
361 case '<': case '>': case '[':
362 case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
364 /* A shell special character. In theory, '$' and '`' could
365 be the first bytes of multibyte characters, which means
366 we should check them with mbrtowc, but in practice this
367 doesn't happen so it's not worth worrying about. */
368 if (quoting_style == shell_quoting_style)
369 goto use_shell_always_quoting_style;
373 switch (quoting_style)
375 case shell_quoting_style:
376 goto use_shell_always_quoting_style;
378 case shell_always_quoting_style:
389 case '%': case '+': case ',': case '-': case '.': case '/':
390 case '0': case '1': case '2': case '3': case '4': case '5':
391 case '6': case '7': case '8': case '9': case ':': case '=':
392 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
393 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
394 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
395 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
396 case 'Y': case 'Z': case ']': case '_': case 'a': case 'b':
397 case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
398 case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
399 case 'o': case 'p': case 'q': case 'r': case 's': case 't':
400 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
402 /* These characters don't cause problems, no matter what the
403 quoting style is. They cannot start multibyte sequences. */
407 /* If we have a multibyte sequence, copy it until we reach
408 its end, find an error, or come back to the initial shift
409 state. For C-like styles, if the sequence has
410 unprintable characters, escape the whole sequence, since
411 we can't easily escape single characters within it. */
413 /* Length of multibyte sequence found so far. */
421 printable = ISPRINT (c);
426 memset (&mbstate, 0, sizeof mbstate);
430 if (argsize == (size_t) -1)
431 argsize = strlen (arg);
436 size_t bytes = mbrtowc (&w, &arg[i + m],
437 argsize - (i + m), &mbstate);
440 else if (bytes == (size_t) -1)
445 else if (bytes == (size_t) -2)
448 while (i + m < argsize && arg[i + m])
459 while (! mbsinit (&mbstate));
462 if (1 < m || (backslash_escapes && ! printable))
464 /* Output a multibyte sequence, or an escaped
465 unprintable unibyte character. */
470 if (backslash_escapes && ! printable)
473 STORE ('0' + (c >> 6));
474 STORE ('0' + ((c >> 3) & 7));
488 if (! (backslash_escapes
489 && o->quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS))))
500 for (; *quote_string; quote_string++)
501 STORE (*quote_string);
503 if (len < buffersize)
507 use_shell_always_quoting_style:
508 return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
509 shell_always_quoting_style, o);
512 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
513 argument ARG (of size ARGSIZE), using O to control quoting.
514 If O is null, use the default.
515 Terminate the output with a null character, and return the written
516 size of the output, not counting the terminating null.
517 If BUFFERSIZE is too small to store the output string, return the
518 value that would have been returned had BUFFERSIZE been large enough.
519 If ARGSIZE is -1, use the string length of the argument for ARGSIZE. */
521 quotearg_buffer (char *buffer, size_t buffersize,
522 char const *arg, size_t argsize,
523 struct quoting_options const *o)
525 struct quoting_options const *p = o ? o : &default_quoting_options;
526 return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
530 /* Use storage slot N to return a quoted version of the string ARG.
531 OPTIONS specifies the quoting options.
532 The returned value points to static storage that can be
533 reused by the next call to this function with the same value of N.
534 N must be nonnegative. N is deliberately declared with type "int"
535 to allow for future extensions (using negative values). */
537 quotearg_n_options (int n, char const *arg,
538 struct quoting_options const *options)
540 /* Preallocate a slot 0 buffer, so that the caller can always quote
541 one small component of a "memory exhausted" message in slot 0. */
542 static char slot0[256];
543 static unsigned int nslots = 1;
550 static struct slotvec slotvec0 = {sizeof slot0, slot0};
551 static struct slotvec *slotvec = &slotvec0;
558 unsigned int n1 = n0 + 1;
559 size_t s = n1 * sizeof *slotvec;
561 if (SIZE_MAX / UINT_MAX <= sizeof *slotvec
562 && n1 != s / sizeof *slotvec)
565 if (slotvec == &slotvec0)
567 slotvec = (struct slotvec *) xmalloc (sizeof *slotvec);
570 slotvec = (struct slotvec *) xrealloc (slotvec, s);
571 memset (slotvec + nslots, 0, (n1 - nslots) * sizeof *slotvec);
576 size_t size = slotvec[n].size;
577 char *val = slotvec[n].val;
578 size_t qsize = quotearg_buffer (val, size, arg, (size_t) -1, options);
582 slotvec[n].size = size = qsize + 1;
583 slotvec[n].val = val = xrealloc (val == slot0 ? 0 : val, size);
584 quotearg_buffer (val, size, arg, (size_t) -1, options);
592 quotearg_n (int n, char const *arg)
594 return quotearg_n_options (n, arg, &default_quoting_options);
598 quotearg (char const *arg)
600 return quotearg_n (0, arg);
604 quotearg_n_style (int n, enum quoting_style s, char const *arg)
606 struct quoting_options o;
608 memset (o.quote_these_too, 0, sizeof o.quote_these_too);
609 return quotearg_n_options (n, arg, &o);
613 quotearg_style (enum quoting_style s, char const *arg)
615 return quotearg_n_style (0, s, arg);
619 quotearg_char (char const *arg, char ch)
621 struct quoting_options options;
622 options = default_quoting_options;
623 set_char_quoting (&options, ch, 1);
624 return quotearg_n_options (0, arg, &options);
628 quotearg_colon (char const *arg)
630 return quotearg_char (arg, ':');