changeset 9697:6dd30e9cb2a1

Quotearg part 1: more wrappers, restore quotearg_char state. * lib/quotearg.h (quotearg_alloc_mem, quotearg_n_mem) (quotearg_mem, quotearg_style_mem, quotearg_char_mem) (quotearg_colon_mem): New wrappers. * lib/quotearg.c (quotearg_alloc, quotearg_char): Rewrite... (quotearg_alloc_mem, quotearg_char_mem): ...in terms of these new functions. (quotearg_n_mem, quotearg_mem, quotearg_style_mem) (quotearg_colon_mem): New functions. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Thu, 07 Feb 2008 06:39:57 -0700
parents eddb5ac75889
children 2351f4d6d9f7
files ChangeLog lib/quotearg.c lib/quotearg.h
diffstat 3 files changed, 90 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2008-02-07  Eric Blake  <ebb9@byu.net>
+
+	Quotearg part 1: more wrappers, restore quotearg_char state.
+	* lib/quotearg.h (quotearg_alloc_mem, quotearg_n_mem)
+	(quotearg_mem, quotearg_style_mem, quotearg_char_mem)
+	(quotearg_colon_mem): New wrappers.
+	* lib/quotearg.c (quotearg_alloc, quotearg_char): Rewrite...
+	(quotearg_alloc_mem, quotearg_char_mem): ...in terms of these new
+	functions.
+	(quotearg_n_mem, quotearg_mem, quotearg_style_mem)
+	(quotearg_colon_mem): New functions.
+
 2008-02-11  Bruno Haible  <bruno@clisp.org>
 
 	* modules/git-merge-changelog (Makefile.am): Don't use -L and -l for a
--- a/lib/quotearg.c
+++ b/lib/quotearg.c
@@ -1,7 +1,7 @@
 /* quotearg.c - quote arguments for output
 
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007 Free
-   Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007,
+   2008 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
@@ -529,17 +529,30 @@
   return r;
 }
 
-/* Like quotearg_buffer (..., ARG, ARGSIZE, O), except return newly
-   allocated storage containing the quoted string.  */
+/* Equivalent to quotearg_alloc (ARG, ARGSIZE, NULL, O).  */
 char *
 quotearg_alloc (char const *arg, size_t argsize,
 		struct quoting_options const *o)
 {
+  return quotearg_alloc_mem (arg, argsize, NULL, o);
+}
+
+/* Like quotearg_buffer (..., ARG, ARGSIZE, O), except return newly
+   allocated storage containing the quoted string, and store the
+   resulting size into *SIZE, if non-NULL.  If SIZE is NULL, then
+   either ARGSIZE should be -1, or O should escape or elide any
+   embedded null bytes.  */
+char *
+quotearg_alloc_mem (char const *arg, size_t argsize, size_t *size,
+		    struct quoting_options const *o)
+{
   int e = errno;
   size_t bufsize = quotearg_buffer (0, 0, arg, argsize, o) + 1;
   char *buf = xcharalloc (bufsize);
   quotearg_buffer (buf, bufsize, arg, argsize, o);
   errno = e;
+  if (size)
+    *size = bufsize - 1;
   return buf;
 }
 
@@ -644,11 +657,23 @@
 }
 
 char *
+quotearg_n_mem (int n, char const *arg, size_t argsize)
+{
+  return quotearg_n_options (n, arg, argsize, &default_quoting_options);
+}
+
+char *
 quotearg (char const *arg)
 {
   return quotearg_n (0, arg);
 }
 
+char *
+quotearg_mem (char const *arg, size_t argsize)
+{
+  return quotearg_n_mem (0, arg, argsize);
+}
+
 /* Return quoting options for STYLE, with no extra quoting.  */
 static struct quoting_options
 quoting_options_from_style (enum quoting_style style)
@@ -681,12 +706,24 @@
 }
 
 char *
-quotearg_char (char const *arg, char ch)
+quotearg_style_mem (enum quoting_style s, char const *arg, size_t argsize)
+{
+  return quotearg_n_style_mem (0, s, arg, argsize);
+}
+
+char *
+quotearg_char_mem (char const *arg, size_t argsize, char ch)
 {
   struct quoting_options options;
   options = default_quoting_options;
   set_char_quoting (&options, ch, 1);
-  return quotearg_n_options (0, arg, SIZE_MAX, &options);
+  return quotearg_n_options (0, arg, argsize, &options);
+}
+
+char *
+quotearg_char (char const *arg, char ch)
+{
+  return quotearg_char_mem (arg, SIZE_MAX, ch);
 }
 
 char *
@@ -694,3 +731,9 @@
 {
   return quotearg_char (arg, ':');
 }
+
+char *
+quotearg_colon_mem (char const *arg, size_t argsize)
+{
+  return quotearg_char_mem (arg, argsize, ':');
+}
--- a/lib/quotearg.h
+++ b/lib/quotearg.h
@@ -1,6 +1,6 @@
 /* quotearg.h - quote arguments for output
 
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006 Free
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006, 2008 Free
    Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
@@ -99,10 +99,19 @@
 			struct quoting_options const *o);
 
 /* Like quotearg_buffer, except return the result in a newly allocated
-   buffer.  It is the caller's responsibility to free the result.  */
+   buffer.  It is the caller's responsibility to free the result.
+   Either ARGSIZE should be -1, or O should not permit embedded null
+   bytes in the output.  */
 char *quotearg_alloc (char const *arg, size_t argsize,
 		      struct quoting_options const *o);
 
+/* Like quotearg_alloc, except that the length of the result,
+   excluding the terminating null byte, is stored into SIZE if it is
+   non-NULL.  Thus, this can be safe to use even when O specifies
+   embedded null bytes.  */
+char *quotearg_alloc_mem (char const *arg, size_t argsize,
+			  size_t *size, struct quoting_options const *o);
+
 /* Use storage slot N to return a quoted version of the string ARG.
    Use the default quoting options.
    The returned value points to static storage that can be
@@ -113,6 +122,14 @@
 /* Equivalent to quotearg_n (0, ARG).  */
 char *quotearg (char const *arg);
 
+/* Use storage slot N to return a quoted version of the argument ARG
+   of size ARGSIZE.  This is like quotearg_n (N, ARG), except it can
+   quote null bytes.  */
+char *quotearg_n_mem (int n, char const *arg, size_t argsize);
+
+/* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE).  */
+char *quotearg_mem (char const *arg, size_t argsize);
+
 /* Use style S and storage slot N to return a quoted version of the string ARG.
    This is like quotearg_n (N, ARG), except that it uses S with no other
    options to specify the quoting method.  */
@@ -127,12 +144,22 @@
 /* Equivalent to quotearg_n_style (0, S, ARG).  */
 char *quotearg_style (enum quoting_style s, char const *arg);
 
+/* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE).  */
+char *quotearg_style_mem (enum quoting_style s,
+			  char const *arg, size_t argsize);
+
 /* Like quotearg (ARG), except also quote any instances of CH.  */
 char *quotearg_char (char const *arg, char ch);
 
+/* Like quotearg_char (ARG, CH), except it can quote null bytes.  */
+char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
+
 /* Equivalent to quotearg_char (ARG, ':').  */
 char *quotearg_colon (char const *arg);
 
+/* Like quotearg_colon (ARG), except it can quote null bytes.  */
+char *quotearg_colon_mem (char const *arg, size_t argsize);
+
 /* Free any dynamically allocated memory.  */
 void quotearg_free (void);