>From 4d27e85e3db848a844e64a9bc0961ba293c9f09b Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 31 Oct 2020 19:13:23 +0100 Subject: [PATCH 1/3] quotearg: Allow static init of 'struct quoting_options' variables. * lib/quotearg.h: Include . (struct quoting_options): Move to here from lib/quotearg.c. Use INT_WIDTH instead of INT_BITS. (QUOTING_OPTIONS_INIT): New macro. * lib/quotearg.c (INT_BITS): Remove macro. Use INT_WIDTH instead. (default_quoting_options, quote_quoting_options): Use QUOTING_OPTIONS_INIT. * modules/quotearg (Depends-on): Add limits-h. --- ChangeLog | 12 ++++++++++++ lib/quotearg.c | 37 ++++++------------------------------- lib/quotearg.h | 32 +++++++++++++++++++++++++++++++- modules/quotearg | 1 + 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8cc39d3..9cc1a43 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +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 + INT_WIDTH instead of INT_BITS. + (QUOTING_OPTIONS_INIT): New macro. + * lib/quotearg.c (INT_BITS): Remove macro. Use INT_WIDTH instead. + (default_quoting_options, quote_quoting_options): Use + QUOTING_OPTIONS_INIT. + * modules/quotearg (Depends-on): Add limits-h. + 2020-10-30 Bernhard Voelker verify tests: avoid -Wmissing-declarations warnings diff --git a/lib/quotearg.c b/lib/quotearg.c index b13574d..c742632 100644 --- a/lib/quotearg.c +++ b/lib/quotearg.c @@ -53,27 +53,6 @@ # define SIZE_MAX ((size_t) -1) #endif -#define INT_BITS (sizeof (int) * CHAR_BIT) - -struct quoting_options -{ - /* Basic quoting style. */ - enum quoting_style style; - - /* Additional flags. Bitwise combination of enum quoting_flags. */ - int flags; - - /* Quote the characters indicated by this bit vector even if the - quoting style would not normally require them to be quoted. */ - unsigned int quote_these_too[(UCHAR_MAX / INT_BITS) + 1]; - - /* The left quote for custom_quoting_style. */ - char const *left_quote; - - /* The right quote for custom_quoting_style. */ - char const *right_quote; -}; - /* Names of quoting styles. */ char const *const quoting_style_args[] = { @@ -106,7 +85,8 @@ enum quoting_style const quoting_style_vals[] = }; /* The default quoting options. */ -static struct quoting_options default_quoting_options; +static struct quoting_options default_quoting_options = + QUOTING_OPTIONS_INIT (literal_quoting_style, 0); /* Allocate a new set of quoting options, with contents initially identical to O if O is not null, or to the default if O is null. @@ -146,8 +126,8 @@ set_char_quoting (struct quoting_options *o, char c, int i) { unsigned char uc = c; unsigned int *p = - (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS; - int shift = uc % INT_BITS; + (o ? o : &default_quoting_options)->quote_these_too + uc / INT_WIDTH; + int shift = uc % INT_WIDTH; int r = (*p >> shift) & 1; *p ^= ((i & 1) ^ r) << shift; return r; @@ -705,7 +685,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize, if (! (((backslash_escapes && quoting_style != shell_always_quoting_style) || elide_outer_quotes) && quote_these_too - && quote_these_too[c / INT_BITS] >> (c % INT_BITS) & 1) + && quote_these_too[c / INT_WIDTH] >> (c % INT_WIDTH) & 1) && !is_right_quote) goto store_c; @@ -1043,12 +1023,7 @@ quotearg_custom_mem (char const *left_quote, char const *right_quote, /* The quoting option used by the functions of quote.h. */ struct quoting_options quote_quoting_options = - { - locale_quoting_style, - 0, - { 0 }, - NULL, NULL - }; + QUOTING_OPTIONS_INIT (locale_quoting_style, 0); char const * quote_n_mem (int n, char const *arg, size_t argsize) diff --git a/lib/quotearg.h b/lib/quotearg.h index 3bf149b..55fa464 100644 --- a/lib/quotearg.h +++ b/lib/quotearg.h @@ -21,6 +21,7 @@ #ifndef QUOTEARG_H_ # define QUOTEARG_H_ 1 +# include # include /* Basic quoting styles. For each style, an example is given on the @@ -267,7 +268,36 @@ enum quoting_flags extern char const *const quoting_style_args[]; extern enum quoting_style const quoting_style_vals[]; -struct quoting_options; +/* Quoting options. + The fields of this struct are considered private. Instead of accessing + them directly, use the accessors + get_quoting_style, set_quoting_style, + set_quoting_flags, + set_char_quoting, + set_custom_quoting + declared below. */ +struct quoting_options +{ + /* Basic quoting style. */ + enum quoting_style style; + + /* Additional flags. Bitwise combination of enum quoting_flags. */ + int flags; + + /* Quote the characters indicated by this bit vector even if the + quoting style would not normally require them to be quoted. */ + unsigned int quote_these_too[(UCHAR_MAX / INT_WIDTH) + 1]; + + /* The left quote for custom_quoting_style. */ + char const *left_quote; + + /* The right quote for custom_quoting_style. */ + char const *right_quote; +}; + +/* Initializer for a variable of type 'struct quoting_options'. */ +#define QUOTING_OPTIONS_INIT(style, flags) \ + { style, flags, { 0 }, NULL, NULL } /* The functions listed below set and use a hidden variable that contains the default quoting style options. */ diff --git a/modules/quotearg b/modules/quotearg index 6f5356d..28b5353 100644 --- a/modules/quotearg +++ b/modules/quotearg @@ -14,6 +14,7 @@ attribute c-strcaseeq extensions gettext-h +limits-h mbrtowc mbsinit memcmp -- 2.7.4