Merge from coreutils.
[gnulib.git] / lib / quotearg.c
1 /* quotearg.c - quote arguments for output
2
3    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by Paul Eggert <eggert@twinsun.com> */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "quotearg.h"
27
28 #include "xalloc.h"
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
39
40 #if HAVE_WCHAR_H
41
42 /* BSD/OS 4.1 wchar.h requires FILE and struct tm to be declared.  */
43 # include <stdio.h>
44 # include <time.h>
45
46 # include <wchar.h>
47 #endif
48
49 #if !HAVE_MBRTOWC
50 /* Disable multibyte processing entirely.  Since MB_CUR_MAX is 1, the
51    other macros are defined only for documentation and to satisfy C
52    syntax.  */
53 # undef MB_CUR_MAX
54 # define MB_CUR_MAX 1
55 # define mbrtowc(pwc, s, n, ps) ((*(pwc) = *(s)) != 0)
56 # define iswprint(wc) isprint ((unsigned char) (wc))
57 # undef HAVE_MBSINIT
58 #endif
59
60 #if !defined mbsinit && !HAVE_MBSINIT
61 # define mbsinit(ps) 1
62 #endif
63
64 #ifndef iswprint
65 # if HAVE_WCTYPE_H
66 #  include <wctype.h>
67 # endif
68 # if !defined iswprint && !HAVE_ISWPRINT
69 #  define iswprint(wc) 1
70 # endif
71 #endif
72
73 #ifndef SIZE_MAX
74 # define SIZE_MAX ((size_t) -1)
75 #endif
76
77 #define INT_BITS (sizeof (int) * CHAR_BIT)
78
79 struct quoting_options
80 {
81   /* Basic quoting style.  */
82   enum quoting_style style;
83
84   /* Quote the characters indicated by this bit vector even if the
85      quoting style would not normally require them to be quoted.  */
86   int quote_these_too[(UCHAR_MAX / INT_BITS) + 1];
87 };
88
89 /* Names of quoting styles.  */
90 char const *const quoting_style_args[] =
91 {
92   "literal",
93   "shell",
94   "shell-always",
95   "c",
96   "escape",
97   "locale",
98   "clocale",
99   0
100 };
101
102 /* Correspondences to quoting style names.  */
103 enum quoting_style const quoting_style_vals[] =
104 {
105   literal_quoting_style,
106   shell_quoting_style,
107   shell_always_quoting_style,
108   c_quoting_style,
109   escape_quoting_style,
110   locale_quoting_style,
111   clocale_quoting_style
112 };
113
114 /* The default quoting options.  */
115 static struct quoting_options default_quoting_options;
116
117 /* Allocate a new set of quoting options, with contents initially identical
118    to O if O is not null, or to the default if O is null.
119    It is the caller's responsibility to free the result.  */
120 struct quoting_options *
121 clone_quoting_options (struct quoting_options *o)
122 {
123   int e = errno;
124   struct quoting_options *p = xmalloc (sizeof *p);
125   *p = *(o ? o : &default_quoting_options);
126   errno = e;
127   return p;
128 }
129
130 /* Get the value of O's quoting style.  If O is null, use the default.  */
131 enum quoting_style
132 get_quoting_style (struct quoting_options *o)
133 {
134   return (o ? o : &default_quoting_options)->style;
135 }
136
137 /* In O (or in the default if O is null),
138    set the value of the quoting style to S.  */
139 void
140 set_quoting_style (struct quoting_options *o, enum quoting_style s)
141 {
142   (o ? o : &default_quoting_options)->style = s;
143 }
144
145 /* In O (or in the default if O is null),
146    set the value of the quoting options for character C to I.
147    Return the old value.  Currently, the only values defined for I are
148    0 (the default) and 1 (which means to quote the character even if
149    it would not otherwise be quoted).  */
150 int
151 set_char_quoting (struct quoting_options *o, char c, int i)
152 {
153   unsigned char uc = c;
154   int *p = (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
155   int shift = uc % INT_BITS;
156   int r = (*p >> shift) & 1;
157   *p ^= ((i & 1) ^ r) << shift;
158   return r;
159 }
160
161 /* MSGID approximates a quotation mark.  Return its translation if it
162    has one; otherwise, return either it or "\"", depending on S.  */
163 static char const *
164 gettext_quote (char const *msgid, enum quoting_style s)
165 {
166   char const *translation = _(msgid);
167   if (translation == msgid && s == clocale_quoting_style)
168     translation = "\"";
169   return translation;
170 }
171
172 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
173    argument ARG (of size ARGSIZE), using QUOTING_STYLE and the
174    non-quoting-style part of O to control quoting.
175    Terminate the output with a null character, and return the written
176    size of the output, not counting the terminating null.
177    If BUFFERSIZE is too small to store the output string, return the
178    value that would have been returned had BUFFERSIZE been large enough.
179    If ARGSIZE is SIZE_MAX, use the string length of the argument for ARGSIZE.
180
181    This function acts like quotearg_buffer (BUFFER, BUFFERSIZE, ARG,
182    ARGSIZE, O), except it uses QUOTING_STYLE instead of the quoting
183    style specified by O, and O may not be null.  */
184
185 static size_t
186 quotearg_buffer_restyled (char *buffer, size_t buffersize,
187                           char const *arg, size_t argsize,
188                           enum quoting_style quoting_style,
189                           struct quoting_options const *o)
190 {
191   size_t i;
192   size_t len = 0;
193   char const *quote_string = 0;
194   size_t quote_string_len = 0;
195   int backslash_escapes = 0;
196   int unibyte_locale = MB_CUR_MAX == 1;
197
198 #define STORE(c) \
199     do \
200       { \
201         if (len < buffersize) \
202           buffer[len] = (c); \
203         len++; \
204       } \
205     while (0)
206
207   switch (quoting_style)
208     {
209     case c_quoting_style:
210       STORE ('"');
211       backslash_escapes = 1;
212       quote_string = "\"";
213       quote_string_len = 1;
214       break;
215
216     case escape_quoting_style:
217       backslash_escapes = 1;
218       break;
219
220     case locale_quoting_style:
221     case clocale_quoting_style:
222       {
223         /* Get translations for open and closing quotation marks.
224
225            The message catalog should translate "`" to a left
226            quotation mark suitable for the locale, and similarly for
227            "'".  If the catalog has no translation,
228            locale_quoting_style quotes `like this', and
229            clocale_quoting_style quotes "like this".
230
231            For example, an American English Unicode locale should
232            translate "`" to U+201C (LEFT DOUBLE QUOTATION MARK), and
233            should translate "'" to U+201D (RIGHT DOUBLE QUOTATION
234            MARK).  A British English Unicode locale should instead
235            translate these to U+2018 (LEFT SINGLE QUOTATION MARK) and
236            U+2019 (RIGHT SINGLE QUOTATION MARK), respectively.  */
237
238         char const *left = gettext_quote (N_("`"), quoting_style);
239         char const *right = gettext_quote (N_("'"), quoting_style);
240         for (quote_string = left; *quote_string; quote_string++)
241           STORE (*quote_string);
242         backslash_escapes = 1;
243         quote_string = right;
244         quote_string_len = strlen (quote_string);
245       }
246       break;
247
248     case shell_always_quoting_style:
249       STORE ('\'');
250       quote_string = "'";
251       quote_string_len = 1;
252       break;
253
254     default:
255       break;
256     }
257
258   for (i = 0;  ! (argsize == SIZE_MAX ? arg[i] == '\0' : i == argsize);  i++)
259     {
260       unsigned char c;
261       unsigned char esc;
262
263       if (backslash_escapes
264           && quote_string_len
265           && i + quote_string_len <= argsize
266           && memcmp (arg + i, quote_string, quote_string_len) == 0)
267         STORE ('\\');
268
269       c = arg[i];
270       switch (c)
271         {
272         case '\0':
273           if (backslash_escapes)
274             {
275               STORE ('\\');
276               STORE ('0');
277               STORE ('0');
278               c = '0';
279             }
280           break;
281
282         case '?':
283           switch (quoting_style)
284             {
285             case shell_quoting_style:
286               goto use_shell_always_quoting_style;
287
288             case c_quoting_style:
289               if (i + 2 < argsize && arg[i + 1] == '?')
290                 switch (arg[i + 2])
291                   {
292                   case '!': case '\'':
293                   case '(': case ')': case '-': case '/':
294                   case '<': case '=': case '>':
295                     /* Escape the second '?' in what would otherwise be
296                        a trigraph.  */
297                     c = arg[i + 2];
298                     i += 2;
299                     STORE ('?');
300                     STORE ('\\');
301                     STORE ('?');
302                     break;
303                   }
304               break;
305
306             default:
307               break;
308             }
309           break;
310
311         case '\a': esc = 'a'; goto c_escape;
312         case '\b': esc = 'b'; goto c_escape;
313         case '\f': esc = 'f'; goto c_escape;
314         case '\n': esc = 'n'; goto c_and_shell_escape;
315         case '\r': esc = 'r'; goto c_and_shell_escape;
316         case '\t': esc = 't'; goto c_and_shell_escape;
317         case '\v': esc = 'v'; goto c_escape;
318         case '\\': esc = c; goto c_and_shell_escape;
319
320         c_and_shell_escape:
321           if (quoting_style == shell_quoting_style)
322             goto use_shell_always_quoting_style;
323         c_escape:
324           if (backslash_escapes)
325             {
326               c = esc;
327               goto store_escape;
328             }
329           break;
330
331         case '{': case '}': /* sometimes special if isolated */
332           if (! (argsize == SIZE_MAX ? arg[1] == '\0' : argsize == 1))
333             break;
334           /* Fall through.  */
335         case '#': case '~':
336           if (i != 0)
337             break;
338           /* Fall through.  */
339         case ' ':
340         case '!': /* special in bash */
341         case '"': case '$': case '&':
342         case '(': case ')': case '*': case ';':
343         case '<':
344         case '=': /* sometimes special in 0th or (with "set -k") later args */
345         case '>': case '[':
346         case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
347         case '`': case '|':
348           /* A shell special character.  In theory, '$' and '`' could
349              be the first bytes of multibyte characters, which means
350              we should check them with mbrtowc, but in practice this
351              doesn't happen so it's not worth worrying about.  */
352           if (quoting_style == shell_quoting_style)
353             goto use_shell_always_quoting_style;
354           break;
355
356         case '\'':
357           switch (quoting_style)
358             {
359             case shell_quoting_style:
360               goto use_shell_always_quoting_style;
361
362             case shell_always_quoting_style:
363               STORE ('\'');
364               STORE ('\\');
365               STORE ('\'');
366               break;
367
368             default:
369               break;
370             }
371           break;
372
373         case '%': case '+': case ',': case '-': case '.': case '/':
374         case '0': case '1': case '2': case '3': case '4': case '5':
375         case '6': case '7': case '8': case '9': case ':':
376         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
377         case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
378         case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
379         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
380         case 'Y': case 'Z': case ']': case '_': case 'a': case 'b':
381         case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
382         case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
383         case 'o': case 'p': case 'q': case 'r': case 's': case 't':
384         case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
385           /* These characters don't cause problems, no matter what the
386              quoting style is.  They cannot start multibyte sequences.  */
387           break;
388
389         default:
390           /* If we have a multibyte sequence, copy it until we reach
391              its end, find an error, or come back to the initial shift
392              state.  For C-like styles, if the sequence has
393              unprintable characters, escape the whole sequence, since
394              we can't easily escape single characters within it.  */
395           {
396             /* Length of multibyte sequence found so far.  */
397             size_t m;
398
399             int printable;
400
401             if (unibyte_locale)
402               {
403                 m = 1;
404                 printable = isprint (c);
405               }
406             else
407               {
408                 mbstate_t mbstate;
409                 memset (&mbstate, 0, sizeof mbstate);
410
411                 m = 0;
412                 printable = 1;
413                 if (argsize == SIZE_MAX)
414                   argsize = strlen (arg);
415
416                 do
417                   {
418                     wchar_t w;
419                     size_t bytes = mbrtowc (&w, &arg[i + m],
420                                             argsize - (i + m), &mbstate);
421                     if (bytes == 0)
422                       break;
423                     else if (bytes == (size_t) -1)
424                       {
425                         printable = 0;
426                         break;
427                       }
428                     else if (bytes == (size_t) -2)
429                       {
430                         printable = 0;
431                         while (i + m < argsize && arg[i + m])
432                           m++;
433                         break;
434                       }
435                     else
436                       {
437                         /* Work around a bug with older shells that "see" a '\'
438                            that is really the 2nd byte of a multibyte character.
439                            In practice the problem is limited to ASCII
440                            chars >= '@' that are shell special chars.  */
441                         if ('[' == 0x5b && quoting_style == shell_quoting_style)
442                           {
443                             size_t j;
444                             for (j = 1; j < bytes; j++)
445                               switch (arg[i + m + j])
446                                 {
447                                 case '[': case '\\': case '^':
448                                 case '`': case '|':
449                                   goto use_shell_always_quoting_style;
450                                 }
451                           }
452                             
453                         if (! iswprint (w))
454                           printable = 0;
455                         m += bytes;
456                       }
457                   }
458                 while (! mbsinit (&mbstate));
459               }
460
461             if (1 < m || (backslash_escapes && ! printable))
462               {
463                 /* Output a multibyte sequence, or an escaped
464                    unprintable unibyte character.  */
465                 size_t ilim = i + m;
466
467                 for (;;)
468                   {
469                     if (backslash_escapes && ! printable)
470                       {
471                         STORE ('\\');
472                         STORE ('0' + (c >> 6));
473                         STORE ('0' + ((c >> 3) & 7));
474                         c = '0' + (c & 7);
475                       }
476                     if (ilim <= i + 1)
477                       break;
478                     STORE (c);
479                     c = arg[++i];
480                   }
481
482                 goto store_c;
483               }
484           }
485         }
486
487       if (! (backslash_escapes
488              && o->quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS))))
489         goto store_c;
490
491     store_escape:
492       STORE ('\\');
493
494     store_c:
495       STORE (c);
496     }
497
498   if (i == 0 && quoting_style == shell_quoting_style)
499     goto use_shell_always_quoting_style;
500
501   if (quote_string)
502     for (; *quote_string; quote_string++)
503       STORE (*quote_string);
504
505   if (len < buffersize)
506     buffer[len] = '\0';
507   return len;
508
509  use_shell_always_quoting_style:
510   return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
511                                    shell_always_quoting_style, o);
512 }
513
514 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
515    argument ARG (of size ARGSIZE), using O to control quoting.
516    If O is null, use the default.
517    Terminate the output with a null character, and return the written
518    size of the output, not counting the terminating null.
519    If BUFFERSIZE is too small to store the output string, return the
520    value that would have been returned had BUFFERSIZE been large enough.
521    If ARGSIZE is SIZE_MAX, use the string length of the argument for
522    ARGSIZE.  */
523 size_t
524 quotearg_buffer (char *buffer, size_t buffersize,
525                  char const *arg, size_t argsize,
526                  struct quoting_options const *o)
527 {
528   struct quoting_options const *p = o ? o : &default_quoting_options;
529   int e = errno;
530   size_t r = quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
531                                        p->style, p);
532   errno = e;
533   return r;
534 }
535
536 /* Like quotearg_buffer (..., ARG, ARGSIZE, O), except return newly
537    allocated storage containing the quoted string.  */
538 char *
539 quotearg_alloc (char const *arg, size_t argsize,
540                 struct quoting_options const *o)
541 {
542   int e = errno;
543   size_t bufsize = quotearg_buffer (0, 0, arg, argsize, o) + 1;
544   char *buf = xmalloc (bufsize);
545   quotearg_buffer (buf, bufsize, arg, argsize, o);
546   errno = e;
547   return buf;
548 }
549
550 /* Use storage slot N to return a quoted version of argument ARG.
551    ARG is of size ARGSIZE, but if that is SIZE_MAX, ARG is a
552    null-terminated string.
553    OPTIONS specifies the quoting options.
554    The returned value points to static storage that can be
555    reused by the next call to this function with the same value of N.
556    N must be nonnegative.  N is deliberately declared with type "int"
557    to allow for future extensions (using negative values).  */
558 static char *
559 quotearg_n_options (int n, char const *arg, size_t argsize,
560                     struct quoting_options const *options)
561 {
562   int e = errno;
563
564   /* Preallocate a slot 0 buffer, so that the caller can always quote
565      one small component of a "memory exhausted" message in slot 0.  */
566   static char slot0[256];
567   static unsigned int nslots = 1;
568   unsigned int n0 = n;
569   struct slotvec
570     {
571       size_t size;
572       char *val;
573     };
574   static struct slotvec slotvec0 = {sizeof slot0, slot0};
575   static struct slotvec *slotvec = &slotvec0;
576
577   if (n < 0)
578     abort ();
579
580   if (nslots <= n0)
581     {
582       unsigned int n1 = n0 + 1;
583
584       if (xalloc_oversized (n1, sizeof *slotvec))
585         xalloc_die ();
586
587       if (slotvec == &slotvec0)
588         {
589           slotvec = xmalloc (sizeof *slotvec);
590           *slotvec = slotvec0;
591         }
592       slotvec = xrealloc (slotvec, n1 * sizeof *slotvec);
593       memset (slotvec + nslots, 0, (n1 - nslots) * sizeof *slotvec);
594       nslots = n1;
595     }
596
597   {
598     size_t size = slotvec[n].size;
599     char *val = slotvec[n].val;
600     size_t qsize = quotearg_buffer (val, size, arg, argsize, options);
601
602     if (size <= qsize)
603       {
604         slotvec[n].size = size = qsize + 1;
605         if (val != slot0)
606           free (val);
607         slotvec[n].val = val = xmalloc (size);
608         quotearg_buffer (val, size, arg, argsize, options);
609       }
610
611     errno = e;
612     return val;
613   }
614 }
615
616 char *
617 quotearg_n (int n, char const *arg)
618 {
619   return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options);
620 }
621
622 char *
623 quotearg (char const *arg)
624 {
625   return quotearg_n (0, arg);
626 }
627
628 /* Return quoting options for STYLE, with no extra quoting.  */
629 static struct quoting_options
630 quoting_options_from_style (enum quoting_style style)
631 {
632   struct quoting_options o;
633   o.style = style;
634   memset (o.quote_these_too, 0, sizeof o.quote_these_too);
635   return o;
636 }
637
638 char *
639 quotearg_n_style (int n, enum quoting_style s, char const *arg)
640 {
641   struct quoting_options const o = quoting_options_from_style (s);
642   return quotearg_n_options (n, arg, SIZE_MAX, &o);
643 }
644
645 char *
646 quotearg_n_style_mem (int n, enum quoting_style s,
647                       char const *arg, size_t argsize)
648 {
649   struct quoting_options const o = quoting_options_from_style (s);
650   return quotearg_n_options (n, arg, argsize, &o);
651 }
652
653 char *
654 quotearg_style (enum quoting_style s, char const *arg)
655 {
656   return quotearg_n_style (0, s, arg);
657 }
658
659 char *
660 quotearg_char (char const *arg, char ch)
661 {
662   struct quoting_options options;
663   options = default_quoting_options;
664   set_char_quoting (&options, ch, 1);
665   return quotearg_n_options (0, arg, SIZE_MAX, &options);
666 }
667
668 char *
669 quotearg_colon (char const *arg)
670 {
671   return quotearg_char (arg, ':');
672 }