summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2008-02-07 06:39:57 -0700
committerEric Blake <ebb9@byu.net>2008-02-12 18:42:52 -0700
commit2aa21ba313a4b48ed239d5e2311c66d861351eb1 (patch)
tree54b845fde50817b4d31f4b119655c97a806d588f /lib
parente905aadb6e996c3040e311988e3dfbbf8b748edf (diff)
downloadgnulib-2aa21ba313a4b48ed239d5e2311c66d861351eb1.tar.gz
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>
Diffstat (limited to 'lib')
-rw-r--r--lib/quotearg.c55
-rw-r--r--lib/quotearg.h31
2 files changed, 78 insertions, 8 deletions
diff --git a/lib/quotearg.c b/lib/quotearg.c
index 00d397c418..34a9e770bc 100644
--- 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 @@ quotearg_buffer (char *buffer, size_t buffersize,
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 @@ quotearg_n (int n, char const *arg)
}
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 @@ quotearg_style (enum quoting_style s, char const *arg)
}
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 @@ quotearg_colon (char const *arg)
{
return quotearg_char (arg, ':');
}
+
+char *
+quotearg_colon_mem (char const *arg, size_t argsize)
+{
+ return quotearg_char_mem (arg, argsize, ':');
+}
diff --git a/lib/quotearg.h b/lib/quotearg.h
index d11d059fb3..d59ccd6efb 100644
--- 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 @@ size_t quotearg_buffer (char *buffer, size_t buffersize,
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 @@ char *quotearg_n (int n, char const *arg);
/* 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 @@ char *quotearg_n_style_mem (int n, enum quoting_style s,
/* 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);