>From d51f785bbc797651edc5b696f09a53317d4f7de7 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 31 Oct 2020 20:00:41 +0100 Subject: [PATCH 2/3] quotearg: Export quotearg_slot_n_mem. * lib/quotearg.h (quotearg_buffer, quotearg_alloc_mem): Write SIZE_MAX instead of -1. (quotearg_slot_n_mem): New declaration. * lib/quotearg.c (quotearg_slot_n_mem): Renamed from quotearg_n_options. Use default_quoting_options if the options argument is NULL. --- ChangeLog | 9 +++++++++ lib/quotearg.c | 33 ++++++++++++++------------------- lib/quotearg.h | 21 ++++++++++++++++----- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9cc1a43..e7ecf39 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2020-10-31 Bruno Haible + quotearg: Export quotearg_slot_n_mem. + * lib/quotearg.h (quotearg_buffer, quotearg_alloc_mem): Write SIZE_MAX + instead of -1. + (quotearg_slot_n_mem): New declaration. + * lib/quotearg.c (quotearg_slot_n_mem): Renamed from quotearg_n_options. + Use default_quoting_options if the options argument is NULL. + +2020-10-31 Bruno Haible + quotearg: Allow static init of 'struct quoting_options' variables. * lib/quotearg.h: Include . (struct quoting_options): Move to here from lib/quotearg.c. Use diff --git a/lib/quotearg.c b/lib/quotearg.c index c742632..2954b12 100644 --- a/lib/quotearg.c +++ b/lib/quotearg.c @@ -838,17 +838,9 @@ quotearg_free (void) nslots = 1; } -/* Use storage slot N to return a quoted version of argument ARG. - ARG is of size ARGSIZE, but if that is SIZE_MAX, 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, size_t argsize, - struct quoting_options const *options) +char * +quotearg_slot_n_mem (int n, char const *arg, size_t argsize, + struct quoting_options const *options) { int e = errno; @@ -872,6 +864,9 @@ quotearg_n_options (int n, char const *arg, size_t argsize, nslots = n + 1; } + if (options == NULL) + options = &default_quoting_options; + { size_t size = sv[n].size; char *val = sv[n].val; @@ -903,13 +898,13 @@ quotearg_n_options (int n, char const *arg, size_t argsize, char * quotearg_n (int n, char const *arg) { - return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options); + return quotearg_slot_n_mem (n, arg, SIZE_MAX, &default_quoting_options); } char * quotearg_n_mem (int n, char const *arg, size_t argsize) { - return quotearg_n_options (n, arg, argsize, &default_quoting_options); + return quotearg_slot_n_mem (n, arg, argsize, &default_quoting_options); } char * @@ -928,7 +923,7 @@ 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); + return quotearg_slot_n_mem (n, arg, SIZE_MAX, &o); } char * @@ -936,7 +931,7 @@ 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); + return quotearg_slot_n_mem (n, arg, argsize, &o); } char * @@ -957,7 +952,7 @@ 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, argsize, &options); + return quotearg_slot_n_mem (0, arg, argsize, &options); } char * @@ -984,7 +979,7 @@ quotearg_n_style_colon (int n, enum quoting_style s, char const *arg) struct quoting_options options; options = quoting_options_from_style (s); set_char_quoting (&options, ':', 1); - return quotearg_n_options (n, arg, SIZE_MAX, &options); + return quotearg_slot_n_mem (n, arg, SIZE_MAX, &options); } char * @@ -1002,7 +997,7 @@ quotearg_n_custom_mem (int n, char const *left_quote, { struct quoting_options o = default_quoting_options; set_custom_quoting (&o, left_quote, right_quote); - return quotearg_n_options (n, arg, argsize, &o); + return quotearg_slot_n_mem (n, arg, argsize, &o); } char * @@ -1028,7 +1023,7 @@ struct quoting_options quote_quoting_options = char const * quote_n_mem (int n, char const *arg, size_t argsize) { - return quotearg_n_options (n, arg, argsize, "e_quoting_options); + return quotearg_slot_n_mem (n, arg, argsize, "e_quoting_options); } char const * diff --git a/lib/quotearg.h b/lib/quotearg.h index 55fa464..d601b80 100644 --- a/lib/quotearg.h +++ b/lib/quotearg.h @@ -349,10 +349,10 @@ void set_custom_quoting (struct quoting_options *o, size of the output, not counting the terminating null. If BUFFERSIZE is too small to store the output string, return the value that would have been returned had BUFFERSIZE been large enough. - If ARGSIZE is -1, use the string length of the argument for ARGSIZE. - On output, BUFFER might contain embedded null bytes if ARGSIZE was - not -1, the style of O does not use backslash escapes, and the - flags of O do not request elision of null bytes.*/ + If ARGSIZE is SIZE_MAX, use the string length of the argument for ARGSIZE. + On output, BUFFER might contain embedded null bytes if ARGSIZE was not + SIZE_MAX, the style of O does not use backslash escapes, and the flags + of O do not request elision of null bytes.*/ size_t quotearg_buffer (char *restrict buffer, size_t buffersize, char const *arg, size_t argsize, struct quoting_options const *o); @@ -366,12 +366,23 @@ char *quotearg_alloc (char const *arg, size_t argsize, /* Like quotearg_alloc, except that the length of the result, excluding the terminating null byte, is stored into SIZE if it is non-NULL. The result might contain embedded null bytes if ARGSIZE - was not -1, SIZE was not NULL, the style of O does not use + was not SIZE_MAX, SIZE was not NULL, the style of O does not use backslash escapes, and the flags of O do not request elision of 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 argument ARG. + ARG is of size ARGSIZE, but if that is SIZE_MAX, ARG is a + null-terminated string. + OPTIONS specifies the quoting options. If null, use the default. + 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). */ +char *quotearg_slot_n_mem (int n, char const *arg, size_t argsize, + struct quoting_options const *options); + /* 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 -- 2.7.4