Include quotearg.h immediately after config.h.
[gnulib.git] / lib / quotearg.c
index 47152bc..bffa14b 100644 (file)
@@ -1,5 +1,5 @@
 /* quotearg.c - quote arguments for output
-   Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 # include <config.h>
 #endif
 
-#include <sys/types.h>
-#include <quotearg.h>
-#include <xalloc.h>
+#include "quotearg.h"
 
-#include <ctype.h>
+#include "xalloc.h"
 
-#if ENABLE_NLS
-# include <libintl.h>
-# define _(text) gettext (text)
-#else
-# define _(text) text
-#endif
-#define N_(text) text
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
 
-#if HAVE_LIMITS_H
-# include <limits.h>
-#endif
-#ifndef CHAR_BIT
-# define CHAR_BIT 8
-#endif
-#ifndef UCHAR_MAX
-# define UCHAR_MAX ((unsigned char) -1)
-#endif
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+#define N_(msgid) msgid
 
-#if HAVE_C_BACKSLASH_A
-# define ALERT_CHAR '\a'
-#else
-# define ALERT_CHAR '\7'
-#endif
+#if HAVE_WCHAR_H
 
-#if HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
+/* BSD/OS 4.1 wchar.h requires FILE and struct tm to be declared.  */
+# include <stdio.h>
+# include <time.h>
 
-#if HAVE_STRING_H
-# include <string.h>
+# include <wchar.h>
 #endif
 
-#if HAVE_MBRTOWC && 1 < MB_LEN_MAX
-size_t mbrtowc ();
-# if HAVE_WCHAR_H
-#  include <wchar.h>
-# endif
-# ifdef mbstate_t
-#  define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
-#  define mbsinit(ps) 1
-# endif
-#else
+#if !HAVE_MBRTOWC
 /* Disable multibyte processing entirely.  Since MB_CUR_MAX is 1, the
    other macros are defined only for documentation and to satisfy C
    syntax.  */
 # undef MB_CUR_MAX
 # define MB_CUR_MAX 1
 # define mbrtowc(pwc, s, n, ps) ((*(pwc) = *(s)) != 0)
+# define iswprint(wc) isprint ((unsigned char) (wc))
+# undef HAVE_MBSINIT
+#endif
+
+#if !defined mbsinit && !HAVE_MBSINIT
 # define mbsinit(ps) 1
-# define iswprint(wc) ISPRINT ((unsigned char) (wc))
 #endif
 
 #ifndef iswprint
@@ -88,18 +68,11 @@ size_t mbrtowc ();
 # endif
 #endif
 
-#define INT_BITS (sizeof (int) * CHAR_BIT)
-
-#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
-/* Undefine to protect against the definition in wctype.h of solaris2.6.   */
-# undef ISASCII
-# define ISASCII(c) 1
-#else
-# define ISASCII(c) isascii (c)
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
 #endif
-/* Undefine to protect against the definition in wctype.h of solaris2.6.   */
-#undef ISPRINT
-#define ISPRINT(c) (ISASCII (c) && isprint (c))
+
+#define INT_BITS (sizeof (int) * CHAR_BIT)
 
 struct quoting_options
 {
@@ -145,9 +118,10 @@ static struct quoting_options default_quoting_options;
 struct quoting_options *
 clone_quoting_options (struct quoting_options *o)
 {
-  struct quoting_options *p
-    = (struct quoting_options *) xmalloc (sizeof (struct quoting_options));
+  int e = errno;
+  struct quoting_options *p = xmalloc (sizeof *p);
   *p = *(o ? o : &default_quoting_options);
+  errno = e;
   return p;
 }
 
@@ -279,7 +253,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
       break;
     }
 
-  for (i = 0;  ! (argsize == (size_t) -1 ? arg[i] == '\0' : i == argsize);  i++)
+  for (i = 0;  ! (argsize == SIZE_MAX ? arg[i] == '\0' : i == argsize);  i++)
     {
       unsigned char c;
       unsigned char esc;
@@ -293,6 +267,16 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
       c = arg[i];
       switch (c)
        {
+       case '\0':
+         if (backslash_escapes)
+           {
+             STORE ('\\');
+             STORE ('0');
+             STORE ('0');
+             c = '0';
+           }
+         break;
+
        case '?':
          switch (quoting_style)
            {
@@ -308,8 +292,8 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
                  case '<': case '=': case '>':
                    /* Escape the second '?' in what would otherwise be
                       a trigraph.  */
-                   i += 2;
                    c = arg[i + 2];
+                   i += 2;
                    STORE ('?');
                    STORE ('\\');
                    STORE ('?');
@@ -322,7 +306,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
            }
          break;
 
-       case ALERT_CHAR: esc = 'a'; goto c_escape;
+       case '\a': esc = 'a'; goto c_escape;
        case '\b': esc = 'b'; goto c_escape;
        case '\f': esc = 'f'; goto c_escape;
        case '\n': esc = 'n'; goto c_and_shell_escape;
@@ -410,7 +394,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
            if (unibyte_locale)
              {
                m = 1;
-               printable = ISPRINT (c);
+               printable = isprint (c);
              }
            else
              {
@@ -419,7 +403,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
 
                m = 0;
                printable = 1;
-               if (argsize == (size_t) -1)
+               if (argsize == SIZE_MAX)
                  argsize = strlen (arg);
 
                do
@@ -515,58 +499,82 @@ quotearg_buffer (char *buffer, size_t buffersize,
                 struct quoting_options const *o)
 {
   struct quoting_options const *p = o ? o : &default_quoting_options;
-  return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
-                                  p->style, p);
+  int e = errno;
+  size_t r = quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
+                                      p->style, p);
+  errno = e;
+  return r;
 }
 
-/* Use storage slot N to return a quoted version of the string ARG.
+/* Use storage slot N to return a quoted version of argument ARG.
+   ARG is of size ARGSIZE, but if that is -1, ARG is a null-terminated string.
    OPTIONS specifies the quoting options.
    The returned value points to static storage that can be
    reused by the next call to this function with the same value of N.
    N must be nonnegative.  N is deliberately declared with type "int"
    to allow for future extensions (using negative values).  */
 static char *
-quotearg_n_options (int n, char const *arg,
+quotearg_n_options (int n, char const *arg, size_t argsize,
                    struct quoting_options const *options)
 {
-  static unsigned int nslots;
-  static struct slotvec
+  int e = errno;
+
+  /* Preallocate a slot 0 buffer, so that the caller can always quote
+     one small component of a "memory exhausted" message in slot 0.  */
+  static char slot0[256];
+  static unsigned int nslots = 1;
+  unsigned int n0 = n;
+  struct slotvec
     {
       size_t size;
       char *val;
-    } *slotvec;
+    };
+  static struct slotvec slotvec0 = {sizeof slot0, slot0};
+  static struct slotvec *slotvec = &slotvec0;
+
+  if (n < 0)
+    abort ();
 
-  if (nslots <= n)
+  if (nslots <= n0)
     {
-      int n1 = n + 1;
-      size_t s = n1 * sizeof (struct slotvec);
-      if (! (0 < n1 && n1 == s / sizeof (struct slotvec)))
-       abort ();
-      slotvec = (struct slotvec *) xrealloc (slotvec, s);
-      memset (slotvec + nslots, 0, (n1 - nslots) * sizeof (struct slotvec));
-      nslots = n;
+      unsigned int n1 = n0 + 1;
+      size_t s = n1 * sizeof *slotvec;
+
+      if (SIZE_MAX / UINT_MAX <= sizeof *slotvec
+         && n1 != s / sizeof *slotvec)
+       xalloc_die ();
+
+      if (slotvec == &slotvec0)
+       {
+         slotvec = xmalloc (sizeof *slotvec);
+         *slotvec = slotvec0;
+       }
+      slotvec = xrealloc (slotvec, s);
+      memset (slotvec + nslots, 0, (n1 - nslots) * sizeof *slotvec);
+      nslots = n1;
     }
 
   {
     size_t size = slotvec[n].size;
     char *val = slotvec[n].val;
-    size_t qsize = quotearg_buffer (val, size, arg, (size_t) -1, options);
+    size_t qsize = quotearg_buffer (val, size, arg, argsize, options);
 
     if (size <= qsize)
       {
        slotvec[n].size = size = qsize + 1;
-       slotvec[n].val = val = xrealloc (val, size);
-       quotearg_buffer (val, size, arg, (size_t) -1, options);
+       slotvec[n].val = val = xrealloc (val == slot0 ? 0 : val, size);
+       quotearg_buffer (val, size, arg, argsize, options);
       }
 
+    errno = e;
     return val;
   }
 }
 
 char *
-quotearg_n (unsigned int n, char const *arg)
+quotearg_n (int n, char const *arg)
 {
-  return quotearg_n_options (n, arg, &default_quoting_options);
+  return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options);
 }
 
 char *
@@ -575,13 +583,29 @@ quotearg (char const *arg)
   return quotearg_n (0, arg);
 }
 
-char *
-quotearg_n_style (unsigned int n, enum quoting_style s, char const *arg)
+/* Return quoting options for STYLE, with no extra quoting.  */
+static struct quoting_options
+quoting_options_from_style (enum quoting_style style)
 {
   struct quoting_options o;
-  o.style = s;
+  o.style = style;
   memset (o.quote_these_too, 0, sizeof o.quote_these_too);
-  return quotearg_n_options (n, arg, &o);
+  return o;
+}
+
+char *
+quotearg_n_style (int n, enum quoting_style s, char const *arg)
+{
+  struct quoting_options const o = quoting_options_from_style (s);
+  return quotearg_n_options (n, arg, SIZE_MAX, &o);
+}
+
+char *
+quotearg_n_style_mem (int n, enum quoting_style s,
+                     char const *arg, size_t argsize)
+{
+  struct quoting_options const o = quoting_options_from_style (s);
+  return quotearg_n_options (n, arg, argsize, &o);
 }
 
 char *
@@ -596,7 +620,7 @@ quotearg_char (char const *arg, char ch)
   struct quoting_options options;
   options = default_quoting_options;
   set_char_quoting (&options, ch, 1);
-  return quotearg_n_options (0, arg, &options);
+  return quotearg_n_options (0, arg, SIZE_MAX, &options);
 }
 
 char *