annotate lib/quotearg.c @ 39211:28a8d93056e6

maint: Add encoding marker for Emacs to non-ASCII sources. Similar to commit f406941a8a2ec5fbf3eacc386b9be09d6593d53b from Paul Eggert <eggert@cs.ucla.edu> 2015-09-24. To determine the file list that need the marker, I used the command: for f in `find . -type f | grep -v '^\./\.git/' | grep -v '^\./tests/'`; do if iconv -f ASCII -t ASCII < $f > /dev/null 2>&1 ; then : ; else if iconv -f UTF-8 -t UTF-8 < $f > /dev/null 2>&1 ; then if grep 'The GNU C Library is' $f > /dev/null; then :; else if grep 'coding: utf-8' $f > /dev/null; then :; else echo $f fi fi fi fi done | LC_ALL=C sort
author Bruno Haible <bruno@clisp.org>
date Fri, 05 Jan 2018 22:08:14 +0100
parents 24e347e0e326
children b06060465f09
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
1 /* quotearg.c - quote arguments for output
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
2
39199
24e347e0e326 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 38901
diff changeset
3 Copyright (C) 1998-2002, 2004-2018 Free Software Foundation, Inc.
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
4
29000
49d1cc4454bf Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 28124
diff changeset
5 This program is free software: you can redistribute it and/or modify
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
29000
49d1cc4454bf Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 28124
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
49d1cc4454bf Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 28124
diff changeset
8 (at your option) any later version.
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
9
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13 GNU General Public License for more details.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
38844
a8586d5e30d2 all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 38623
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
17
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18 /* Written by Paul Eggert <eggert@twinsun.com> */
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19
35819
54babea19a87 mark functions with const and pure attributes
Jim Meyering <meyering@redhat.com>
parents: 34343
diff changeset
20 /* Without this pragma, gcc 4.7.0 20111124 mistakenly suggests that
54babea19a87 mark functions with const and pure attributes
Jim Meyering <meyering@redhat.com>
parents: 34343
diff changeset
21 the quoting_options_from_style function might be candidate for
54babea19a87 mark functions with const and pure attributes
Jim Meyering <meyering@redhat.com>
parents: 34343
diff changeset
22 attribute 'pure' */
35822
4f58744e982a quotearg, propername: correct pragma guard expression
Jim Meyering <meyering@redhat.com>
parents: 35819
diff changeset
23 #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
35819
54babea19a87 mark functions with const and pure attributes
Jim Meyering <meyering@redhat.com>
parents: 34343
diff changeset
24 # pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
54babea19a87 mark functions with const and pure attributes
Jim Meyering <meyering@redhat.com>
parents: 34343
diff changeset
25 #endif
54babea19a87 mark functions with const and pure attributes
Jim Meyering <meyering@redhat.com>
parents: 34343
diff changeset
26
26993
8aa31e5d3ec4 * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 26273
diff changeset
27 #include <config.h>
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
28
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
29 #include "quotearg.h"
36285
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
30 #include "quote.h"
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
31
38241
909a440ad711 quotearg: pacify GCC better
Paul Eggert <eggert@cs.ucla.edu>
parents: 38143
diff changeset
32 #include "minmax.h"
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
33 #include "xalloc.h"
35936
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
34 #include "c-strcaseeq.h"
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
35 #include "localcharset.h"
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
36
Jim Meyering <jim@meyering.net>
parents:
diff changeset
37 #include <ctype.h>
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
38 #include <errno.h>
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
39 #include <limits.h>
24850
406719fe9dad Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24634
diff changeset
40 #include <stdbool.h>
38241
909a440ad711 quotearg: pacify GCC better
Paul Eggert <eggert@cs.ucla.edu>
parents: 38143
diff changeset
41 #include <stdint.h>
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
42 #include <stdlib.h>
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
43 #include <string.h>
27579
08f4e5266298 * modules/fnmatch (Depends-on): Depend on wchar.
Eric Blake <ebb9@byu.net>
parents: 27424
diff changeset
44 #include <wchar.h>
28124
9d662dab2f58 * lib/quotearg.c: Include <wctype.h> early, before the definition of
Paul Eggert <eggert@cs.ucla.edu>
parents: 27579
diff changeset
45 #include <wctype.h>
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
46
23657
3a611cdd9601 Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents: 23400
diff changeset
47 #include "gettext.h"
3a611cdd9601 Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents: 23400
diff changeset
48 #define _(msgid) gettext (msgid)
3a611cdd9601 Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents: 23400
diff changeset
49 #define N_(msgid) msgid
21611
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
50
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
51 #ifndef SIZE_MAX
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
52 # define SIZE_MAX ((size_t) -1)
21925
b3271f3304e7 (ISASCII): Add #undef and move definition to follow
Jim Meyering <jim@meyering.net>
parents: 21874
diff changeset
53 #endif
22547
99fd34c97bd6 Rename ISASCII to IN_CTYPE_DOMAIN.
Jim Meyering <jim@meyering.net>
parents: 22490
diff changeset
54
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
55 #define INT_BITS (sizeof (int) * CHAR_BIT)
21925
b3271f3304e7 (ISASCII): Add #undef and move definition to follow
Jim Meyering <jim@meyering.net>
parents: 21874
diff changeset
56
38609
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
57 #ifndef FALLTHROUGH
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
58 # if __GNUC__ < 7
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
59 # define FALLTHROUGH ((void) 0)
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
60 # else
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
61 # define FALLTHROUGH __attribute__ ((__fallthrough__))
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
62 # endif
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
63 #endif
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
64
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65 struct quoting_options
Jim Meyering <jim@meyering.net>
parents:
diff changeset
66 {
Jim Meyering <jim@meyering.net>
parents:
diff changeset
67 /* Basic quoting style. */
Jim Meyering <jim@meyering.net>
parents:
diff changeset
68 enum quoting_style style;
Jim Meyering <jim@meyering.net>
parents:
diff changeset
69
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
70 /* Additional flags. Bitwise combination of enum quoting_flags. */
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
71 int flags;
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
72
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
73 /* Quote the characters indicated by this bit vector even if the
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
74 quoting style would not normally require them to be quoted. */
25106
9c80e9bc340c (struct quoting_options): Use unsigned int for quote_these_too, so
Paul Eggert <eggert@cs.ucla.edu>
parents: 24850
diff changeset
75 unsigned int quote_these_too[(UCHAR_MAX / INT_BITS) + 1];
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
76
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
77 /* The left quote for custom_quoting_style. */
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
78 char const *left_quote;
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
79
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
80 /* The right quote for custom_quoting_style. */
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
81 char const *right_quote;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
82 };
Jim Meyering <jim@meyering.net>
parents:
diff changeset
83
Jim Meyering <jim@meyering.net>
parents:
diff changeset
84 /* Names of quoting styles. */
Jim Meyering <jim@meyering.net>
parents:
diff changeset
85 char const *const quoting_style_args[] =
Jim Meyering <jim@meyering.net>
parents:
diff changeset
86 {
21288
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
87 "literal",
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
88 "shell",
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
89 "shell-always",
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
90 "shell-escape",
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
91 "shell-escape-always",
21288
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
92 "c",
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
93 "c-maybe",
21288
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
94 "escape",
21611
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
95 "locale",
22427
8a2bb2a30e56 (quoting_style_args, quoting_style_vals,
Jim Meyering <jim@meyering.net>
parents: 22413
diff changeset
96 "clocale",
21288
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
97 0
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
98 };
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
99
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
100 /* Correspondences to quoting style names. */
21288
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
101 enum quoting_style const quoting_style_vals[] =
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
102 {
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
103 literal_quoting_style,
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
104 shell_quoting_style,
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
105 shell_always_quoting_style,
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
106 shell_escape_quoting_style,
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
107 shell_escape_always_quoting_style,
21288
1243584a8143 (quotearg_buffer): Cast -1 to size_t before comparing.
Jim Meyering <jim@meyering.net>
parents: 21256
diff changeset
108 c_quoting_style,
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
109 c_maybe_quoting_style,
21611
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
110 escape_quoting_style,
22427
8a2bb2a30e56 (quoting_style_args, quoting_style_vals,
Jim Meyering <jim@meyering.net>
parents: 22413
diff changeset
111 locale_quoting_style,
8a2bb2a30e56 (quoting_style_args, quoting_style_vals,
Jim Meyering <jim@meyering.net>
parents: 22413
diff changeset
112 clocale_quoting_style
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
113 };
Jim Meyering <jim@meyering.net>
parents:
diff changeset
114
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
115 /* The default quoting options. */
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
116 static struct quoting_options default_quoting_options;
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
117
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
118 /* Allocate a new set of quoting options, with contents initially identical
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
119 to O if O is not null, or to the default if O is null.
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
120 It is the caller's responsibility to free the result. */
Jim Meyering <jim@meyering.net>
parents:
diff changeset
121 struct quoting_options *
Jim Meyering <jim@meyering.net>
parents:
diff changeset
122 clone_quoting_options (struct quoting_options *o)
Jim Meyering <jim@meyering.net>
parents:
diff changeset
123 {
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
124 int e = errno;
27278
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
125 struct quoting_options *p = xmemdup (o ? o : &default_quoting_options,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
126 sizeof *o);
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
127 errno = e;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
128 return p;
Jim Meyering <jim@meyering.net>
parents:
diff changeset
129 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
130
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
131 /* Get the value of O's quoting style. If O is null, use the default. */
34303
0d7f62d12356 Revert "use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE"
Jim Meyering <meyering@redhat.com>
parents: 34301
diff changeset
132 enum quoting_style
37834
732f7aa378d3 quotearg: constify get_quoting_style parameters
Pádraig Brady <P@draigBrady.com>
parents: 37833
diff changeset
133 get_quoting_style (struct quoting_options const *o)
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
134 {
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
135 return (o ? o : &default_quoting_options)->style;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
136 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
137
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
138 /* In O (or in the default if O is null),
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
139 set the value of the quoting style to S. */
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
140 void
Jim Meyering <jim@meyering.net>
parents:
diff changeset
141 set_quoting_style (struct quoting_options *o, enum quoting_style s)
Jim Meyering <jim@meyering.net>
parents:
diff changeset
142 {
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
143 (o ? o : &default_quoting_options)->style = s;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
144 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
145
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
146 /* In O (or in the default if O is null),
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
147 set the value of the quoting options for character C to I.
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
148 Return the old value. Currently, the only values defined for I are
Jim Meyering <jim@meyering.net>
parents:
diff changeset
149 0 (the default) and 1 (which means to quote the character even if
Jim Meyering <jim@meyering.net>
parents:
diff changeset
150 it would not otherwise be quoted). */
Jim Meyering <jim@meyering.net>
parents:
diff changeset
151 int
Jim Meyering <jim@meyering.net>
parents:
diff changeset
152 set_char_quoting (struct quoting_options *o, char c, int i)
Jim Meyering <jim@meyering.net>
parents:
diff changeset
153 {
Jim Meyering <jim@meyering.net>
parents:
diff changeset
154 unsigned char uc = c;
25106
9c80e9bc340c (struct quoting_options): Use unsigned int for quote_these_too, so
Paul Eggert <eggert@cs.ucla.edu>
parents: 24850
diff changeset
155 unsigned int *p =
9c80e9bc340c (struct quoting_options): Use unsigned int for quote_these_too, so
Paul Eggert <eggert@cs.ucla.edu>
parents: 24850
diff changeset
156 (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
157 int shift = uc % INT_BITS;
Jim Meyering <jim@meyering.net>
parents:
diff changeset
158 int r = (*p >> shift) & 1;
Jim Meyering <jim@meyering.net>
parents:
diff changeset
159 *p ^= ((i & 1) ^ r) << shift;
Jim Meyering <jim@meyering.net>
parents:
diff changeset
160 return r;
Jim Meyering <jim@meyering.net>
parents:
diff changeset
161 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
162
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
163 /* In O (or in the default if O is null),
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
164 set the value of the quoting options flag to I, which can be a
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
165 bitwise combination of enum quoting_flags, or 0 for default
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
166 behavior. Return the old value. */
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
167 int
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
168 set_quoting_flags (struct quoting_options *o, int i)
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
169 {
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
170 int r;
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
171 if (!o)
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
172 o = &default_quoting_options;
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
173 r = o->flags;
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
174 o->flags = i;
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
175 return r;
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
176 }
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
177
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
178 void
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
179 set_custom_quoting (struct quoting_options *o,
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
180 char const *left_quote, char const *right_quote)
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
181 {
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
182 if (!o)
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
183 o = &default_quoting_options;
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
184 o->style = custom_quoting_style;
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
185 if (!left_quote || !right_quote)
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
186 abort ();
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
187 o->left_quote = left_quote;
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
188 o->right_quote = right_quote;
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
189 }
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
190
29391
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
191 /* Return quoting options for STYLE, with no extra quoting. */
35819
54babea19a87 mark functions with const and pure attributes
Jim Meyering <meyering@redhat.com>
parents: 34343
diff changeset
192 static struct quoting_options /* NOT PURE!! */
29391
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
193 quoting_options_from_style (enum quoting_style style)
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
194 {
37346
da6a1eed4c1b mbsstr, quotearg, xstrtol: pacify IRIX 6.5 cc
Paul Eggert <eggert@cs.ucla.edu>
parents: 37267
diff changeset
195 struct quoting_options o = { literal_quoting_style, 0, { 0 }, NULL, NULL };
34343
b67d6b0a8e70 quotearg: avoid uninitialized variable use
Eric Blake <eblake@redhat.com>
parents: 34303
diff changeset
196 if (style == custom_quoting_style)
b67d6b0a8e70 quotearg: avoid uninitialized variable use
Eric Blake <eblake@redhat.com>
parents: 34303
diff changeset
197 abort ();
29391
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
198 o.style = style;
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
199 return o;
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
200 }
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
201
22427
8a2bb2a30e56 (quoting_style_args, quoting_style_vals,
Jim Meyering <jim@meyering.net>
parents: 22413
diff changeset
202 /* MSGID approximates a quotation mark. Return its translation if it
35936
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
203 has one; otherwise, return either it or "\"", depending on S.
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
204
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
205 S is either clocale_quoting_style or locale_quoting_style. */
22413
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
206 static char const *
22427
8a2bb2a30e56 (quoting_style_args, quoting_style_vals,
Jim Meyering <jim@meyering.net>
parents: 22413
diff changeset
207 gettext_quote (char const *msgid, enum quoting_style s)
22413
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
208 {
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
209 char const *translation = _(msgid);
35936
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
210 char const *locale_code;
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
211
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
212 if (translation != msgid)
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
213 return translation;
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
214
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
215 /* For UTF-8 and GB-18030, use single quotes U+2018 and U+2019.
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
216 Here is a list of other locales that include U+2018 and U+2019:
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
217
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
218 ISO-8859-7 0xA1 KOI8-T 0x91
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
219 CP869 0x8B CP874 0x91
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
220 CP932 0x81 0x65 CP936 0xA1 0xAE
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
221 CP949 0xA1 0xAE CP950 0xA1 0xA5
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
222 CP1250 0x91 CP1251 0x91
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
223 CP1252 0x91 CP1253 0x91
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
224 CP1254 0x91 CP1255 0x91
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
225 CP1256 0x91 CP1257 0x91
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
226 EUC-JP 0xA1 0xC6 EUC-KR 0xA1 0xAE
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
227 EUC-TW 0xA1 0xE4 BIG5 0xA1 0xA5
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
228 BIG5-HKSCS 0xA1 0xA5 EUC-CN 0xA1 0xAE
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
229 GBK 0xA1 0xAE Georgian-PS 0x91
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
230 PT154 0x91
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
231
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
232 None of these is still in wide use; using iconv is overkill. */
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
233 locale_code = locale_charset ();
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
234 if (STRCASEEQ (locale_code, "UTF-8", 'U','T','F','-','8',0,0,0,0))
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
235 return msgid[0] == '`' ? "\xe2\x80\x98": "\xe2\x80\x99";
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
236 if (STRCASEEQ (locale_code, "GB18030", 'G','B','1','8','0','3','0',0,0))
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
237 return msgid[0] == '`' ? "\xa1\ae": "\xa1\xaf";
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
238
35937
423b8d5bd881 quotearg: do not use grave accent for left quote
Paolo Bonzini <bonzini@gnu.org>
parents: 35936
diff changeset
239 return (s == clocale_quoting_style ? "\"" : "'");
22413
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
240 }
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
241
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
242 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
29391
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
243 argument ARG (of size ARGSIZE), using QUOTING_STYLE, FLAGS, and
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
244 QUOTE_THESE_TOO to control quoting.
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
245 Terminate the output with a null character, and return the written
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
246 size of the output, not counting the terminating null.
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
247 If BUFFERSIZE is too small to store the output string, return the
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
248 value that would have been returned had BUFFERSIZE been large enough.
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
249 If ARGSIZE is SIZE_MAX, use the string length of the argument for ARGSIZE.
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
250
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
251 This function acts like quotearg_buffer (BUFFER, BUFFERSIZE, ARG,
29391
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
252 ARGSIZE, O), except it breaks O into its component pieces and is
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
253 not careful about errno. */
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
254
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
255 static size_t
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
256 quotearg_buffer_restyled (char *buffer, size_t buffersize,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
257 char const *arg, size_t argsize,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
258 enum quoting_style quoting_style, int flags,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
259 unsigned int const *quote_these_too,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
260 char const *left_quote,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
261 char const *right_quote)
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
262 {
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
263 size_t i;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
264 size_t len = 0;
38143
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
265 size_t orig_buffersize = 0;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
266 char const *quote_string = 0;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
267 size_t quote_string_len = 0;
24850
406719fe9dad Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24634
diff changeset
268 bool backslash_escapes = false;
406719fe9dad Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24634
diff changeset
269 bool unibyte_locale = MB_CUR_MAX == 1;
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
270 bool elide_outer_quotes = (flags & QA_ELIDE_OUTER_QUOTES) != 0;
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
271 bool pending_shell_escape_end = false;
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
272 bool encountered_single_quote = false;
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
273 bool all_c_and_shell_quote_compat = true;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
274
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
275 #define STORE(c) \
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
276 do \
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
277 { \
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
278 if (len < buffersize) \
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
279 buffer[len] = (c); \
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
280 len++; \
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
281 } \
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
282 while (0)
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
283
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
284 #define START_ESC() \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
285 do \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
286 { \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
287 if (elide_outer_quotes) \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
288 goto force_outer_quoting_style; \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
289 escaping = true; \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
290 if (quoting_style == shell_always_quoting_style \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
291 && ! pending_shell_escape_end) \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
292 { \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
293 STORE ('\''); \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
294 STORE ('$'); \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
295 STORE ('\''); \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
296 pending_shell_escape_end = true; \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
297 } \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
298 STORE ('\\'); \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
299 } \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
300 while (0)
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
301
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
302 #define END_ESC() \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
303 do \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
304 { \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
305 if (pending_shell_escape_end && ! escaping) \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
306 { \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
307 STORE ('\''); \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
308 STORE ('\''); \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
309 pending_shell_escape_end = false; \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
310 } \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
311 } \
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
312 while (0)
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
313
38143
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
314 process_input:
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
315
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
316 switch (quoting_style)
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
317 {
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
318 case c_maybe_quoting_style:
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
319 quoting_style = c_quoting_style;
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
320 elide_outer_quotes = true;
38609
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
321 FALLTHROUGH;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
322 case c_quoting_style:
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
323 if (!elide_outer_quotes)
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
324 STORE ('"');
24850
406719fe9dad Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24634
diff changeset
325 backslash_escapes = true;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
326 quote_string = "\"";
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
327 quote_string_len = 1;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
328 break;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
329
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
330 case escape_quoting_style:
24850
406719fe9dad Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24634
diff changeset
331 backslash_escapes = true;
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
332 elide_outer_quotes = false;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
333 break;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
334
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
335 case locale_quoting_style:
22427
8a2bb2a30e56 (quoting_style_args, quoting_style_vals,
Jim Meyering <jim@meyering.net>
parents: 22413
diff changeset
336 case clocale_quoting_style:
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
337 case custom_quoting_style:
22413
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
338 {
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
339 if (quoting_style != custom_quoting_style)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
340 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
341 /* TRANSLATORS:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
342 Get translations for open and closing quotation marks.
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
343 The message catalog should translate "`" to a left
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
344 quotation mark suitable for the locale, and similarly for
35936
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
345 "'". For example, a French Unicode local should translate
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
346 these to U+00AB (LEFT-POINTING DOUBLE ANGLE
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
347 QUOTATION MARK), and U+00BB (RIGHT-POINTING DOUBLE ANGLE
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
348 QUOTATION MARK), respectively.
22413
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
349
35936
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
350 If the catalog has no translation, we will try to
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
351 use Unicode U+2018 (LEFT SINGLE QUOTATION MARK) and
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
352 Unicode U+2019 (RIGHT SINGLE QUOTATION MARK). If the
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
353 current locale is not Unicode, locale_quoting_style
35937
423b8d5bd881 quotearg: do not use grave accent for left quote
Paolo Bonzini <bonzini@gnu.org>
parents: 35936
diff changeset
354 will quote 'like this', and clocale_quoting_style will
35936
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
355 quote "like this". You should always include translations
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
356 for "`" and "'" even if U+2018 and U+2019 are appropriate
51739ab2c8f6 quotearg: fall back to Unicode single quotes in UTF-8 and GB-18030 locales
Paolo Bonzini <bonzini@gnu.org>
parents: 35935
diff changeset
357 for your locale.
25681
190f81c2cd20 Add translator comment suggested by Bruno Haible, with a minor change.
Paul Eggert <eggert@cs.ucla.edu>
parents: 25539
diff changeset
358
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
359 If you don't know what to put here, please see
38846
aba5dc56f9f9 all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents: 38844
diff changeset
360 <https://en.wikipedia.org/wiki/Quotation_marks_in_other_languages>
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
361 and use glyphs suitable for your language. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
362 left_quote = gettext_quote (N_("`"), quoting_style);
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
363 right_quote = gettext_quote (N_("'"), quoting_style);
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
364 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
365 if (!elide_outer_quotes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
366 for (quote_string = left_quote; *quote_string; quote_string++)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
367 STORE (*quote_string);
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
368 backslash_escapes = true;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
369 quote_string = right_quote;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
370 quote_string_len = strlen (quote_string);
22413
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
371 }
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
372 break;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
373
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
374 case shell_escape_quoting_style:
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
375 backslash_escapes = true;
38609
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
376 FALLTHROUGH;
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
377 case shell_quoting_style:
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
378 elide_outer_quotes = true;
38609
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
379 FALLTHROUGH;
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
380 case shell_escape_always_quoting_style:
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
381 if (!elide_outer_quotes)
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
382 backslash_escapes = true;
38609
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
383 FALLTHROUGH;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
384 case shell_always_quoting_style:
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
385 quoting_style = shell_always_quoting_style;
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
386 if (!elide_outer_quotes)
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
387 STORE ('\'');
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
388 quote_string = "'";
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
389 quote_string_len = 1;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
390 break;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
391
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
392 case literal_quoting_style:
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
393 elide_outer_quotes = false;
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
394 break;
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
395
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
396 default:
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
397 abort ();
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
398 }
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
399
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
400 for (i = 0; ! (argsize == SIZE_MAX ? arg[i] == '\0' : i == argsize); i++)
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
401 {
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
402 unsigned char c;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
403 unsigned char esc;
31580
bb8f4214033e quotearg: fix right quote escaping when it's in quote_these_too
Joel E. Denny <jdenny@clemson.edu>
parents: 30645
diff changeset
404 bool is_right_quote = false;
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
405 bool escaping = false;
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
406 bool c_and_shell_quote_compat = false;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
407
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
408 if (backslash_escapes
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
409 && quoting_style != shell_always_quoting_style
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
410 && quote_string_len
37080
8991ae9223a6 quotearg: do not read beyond end of buffer
Jim Meyering <meyering@fb.com>
parents: 36940
diff changeset
411 && (i + quote_string_len
8991ae9223a6 quotearg: do not read beyond end of buffer
Jim Meyering <meyering@fb.com>
parents: 36940
diff changeset
412 <= (argsize == SIZE_MAX && 1 < quote_string_len
8991ae9223a6 quotearg: do not read beyond end of buffer
Jim Meyering <meyering@fb.com>
parents: 36940
diff changeset
413 /* Use strlen only if we must: when argsize is SIZE_MAX,
8991ae9223a6 quotearg: do not read beyond end of buffer
Jim Meyering <meyering@fb.com>
parents: 36940
diff changeset
414 and when the quote string is more than 1 byte long.
8991ae9223a6 quotearg: do not read beyond end of buffer
Jim Meyering <meyering@fb.com>
parents: 36940
diff changeset
415 If we do call strlen, save the result. */
8991ae9223a6 quotearg: do not read beyond end of buffer
Jim Meyering <meyering@fb.com>
parents: 36940
diff changeset
416 ? (argsize = strlen (arg)) : argsize))
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
417 && memcmp (arg + i, quote_string, quote_string_len) == 0)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
418 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
419 if (elide_outer_quotes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
420 goto force_outer_quoting_style;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
421 is_right_quote = true;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
422 }
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
423
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
424 c = arg[i];
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
425 switch (c)
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
426 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
427 case '\0':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
428 if (backslash_escapes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
429 {
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
430 START_ESC ();
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
431 /* If quote_string were to begin with digits, we'd need to
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
432 test for the end of the arg as well. However, it's
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
433 hard to imagine any locale that would use digits in
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
434 quotes, and set_custom_quoting is documented not to
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
435 accept them. Use only a single \0 with shell-escape
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
436 as currently digits are not printed within $'...' */
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
437 if (quoting_style != shell_always_quoting_style
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
438 && i + 1 < argsize && '0' <= arg[i + 1] && arg[i + 1] <= '9')
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
439 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
440 STORE ('0');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
441 STORE ('0');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
442 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
443 c = '0';
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
444 /* We don't have to worry that this last '0' will be
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
445 backslash-escaped because, again, quote_string should
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
446 not start with it and because quote_these_too is
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
447 documented as not accepting it. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
448 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
449 else if (flags & QA_ELIDE_NULL_BYTES)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
450 continue;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
451 break;
23400
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
452
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
453 case '?':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
454 switch (quoting_style)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
455 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
456 case shell_always_quoting_style:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
457 if (elide_outer_quotes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
458 goto force_outer_quoting_style;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
459 break;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
460
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
461 case c_quoting_style:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
462 if ((flags & QA_SPLIT_TRIGRAPHS)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
463 && i + 2 < argsize && arg[i + 1] == '?')
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
464 switch (arg[i + 2])
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
465 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
466 case '!': case '\'':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
467 case '(': case ')': case '-': case '/':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
468 case '<': case '=': case '>':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
469 /* Escape the second '?' in what would otherwise be
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
470 a trigraph. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
471 if (elide_outer_quotes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
472 goto force_outer_quoting_style;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
473 c = arg[i + 2];
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
474 i += 2;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
475 STORE ('?');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
476 STORE ('"');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
477 STORE ('"');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
478 STORE ('?');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
479 break;
26273
63410d97c583 * quotearg.c (quotearg_buffer_restyled): Add "default: break;"
Paul Eggert <eggert@cs.ucla.edu>
parents: 25966
diff changeset
480
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
481 default:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
482 break;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
483 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
484 break;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
485
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
486 default:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
487 break;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
488 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
489 break;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
490
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
491 case '\a': esc = 'a'; goto c_escape;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
492 case '\b': esc = 'b'; goto c_escape;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
493 case '\f': esc = 'f'; goto c_escape;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
494 case '\n': esc = 'n'; goto c_and_shell_escape;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
495 case '\r': esc = 'r'; goto c_and_shell_escape;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
496 case '\t': esc = 't'; goto c_and_shell_escape;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
497 case '\v': esc = 'v'; goto c_escape;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
498 case '\\': esc = c;
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
499 /* Never need to escape '\' in shell case. */
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
500 if (quoting_style == shell_always_quoting_style)
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
501 {
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
502 if (elide_outer_quotes)
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
503 goto force_outer_quoting_style;
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
504 goto store_c;
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
505 }
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
506
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
507 /* No need to escape the escape if we are trying to elide
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
508 outer quotes and nothing else is problematic. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
509 if (backslash_escapes && elide_outer_quotes && quote_string_len)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
510 goto store_c;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
511
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
512 c_and_shell_escape:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
513 if (quoting_style == shell_always_quoting_style
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
514 && elide_outer_quotes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
515 goto force_outer_quoting_style;
38623
92a1dd1d5642 quotearg: fix compilation failure due to FALLTHROUGH misuse
Jim Meyering <meyering@fb.com>
parents: 38617
diff changeset
516 /* fall through */
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
517 c_escape:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
518 if (backslash_escapes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
519 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
520 c = esc;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
521 goto store_escape;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
522 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
523 break;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
524
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
525 case '{': case '}': /* sometimes special if isolated */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
526 if (! (argsize == SIZE_MAX ? arg[1] == '\0' : argsize == 1))
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
527 break;
38609
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
528 FALLTHROUGH;
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
529 case '#': case '~':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
530 if (i != 0)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
531 break;
38609
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
532 FALLTHROUGH;
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
533 case ' ':
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
534 c_and_shell_quote_compat = true;
38609
57e1cf3aab2b fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents: 38305
diff changeset
535 FALLTHROUGH;
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
536 case '!': /* special in bash */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
537 case '"': case '$': case '&':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
538 case '(': case ')': case '*': case ';':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
539 case '<':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
540 case '=': /* sometimes special in 0th or (with "set -k") later args */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
541 case '>': case '[':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
542 case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
543 case '`': case '|':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
544 /* A shell special character. In theory, '$' and '`' could
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
545 be the first bytes of multibyte characters, which means
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
546 we should check them with mbrtowc, but in practice this
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
547 doesn't happen so it's not worth worrying about. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
548 if (quoting_style == shell_always_quoting_style
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
549 && elide_outer_quotes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
550 goto force_outer_quoting_style;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
551 break;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
552
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
553 case '\'':
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
554 encountered_single_quote = true;
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
555 c_and_shell_quote_compat = true;
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
556 if (quoting_style == shell_always_quoting_style)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
557 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
558 if (elide_outer_quotes)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
559 goto force_outer_quoting_style;
38143
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
560
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
561 if (buffersize && ! orig_buffersize)
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
562 {
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
563 /* Just scan string to see if supports a more concise
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
564 representation, rather than writing a longer string
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
565 but returning the length of the more concise form. */
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
566 orig_buffersize = buffersize;
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
567 buffersize = 0;
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
568 }
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
569
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
570 STORE ('\'');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
571 STORE ('\\');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
572 STORE ('\'');
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
573 pending_shell_escape_end = false;
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
574 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
575 break;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
576
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
577 case '%': case '+': case ',': case '-': case '.': case '/':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
578 case '0': case '1': case '2': case '3': case '4': case '5':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
579 case '6': case '7': case '8': case '9': case ':':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
580 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
581 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
582 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
583 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
584 case 'Y': case 'Z': case ']': case '_': case 'a': case 'b':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
585 case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
586 case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
587 case 'o': case 'p': case 'q': case 'r': case 's': case 't':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
588 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
589 /* These characters don't cause problems, no matter what the
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
590 quoting style is. They cannot start multibyte sequences.
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
591 A digit or a special letter would cause trouble if it
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
592 appeared at the beginning of quote_string because we'd then
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
593 escape by prepending a backslash. However, it's hard to
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
594 imagine any locale that would use digits or letters as
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
595 quotes, and set_custom_quoting is documented not to accept
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
596 them. Also, a digit or a special letter would cause
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
597 trouble if it appeared in quote_these_too, but that's also
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
598 documented as not accepting them. */
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
599 c_and_shell_quote_compat = true;
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
600 break;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
601
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
602 default:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
603 /* If we have a multibyte sequence, copy it until we reach
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
604 its end, find an error, or come back to the initial shift
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
605 state. For C-like styles, if the sequence has
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
606 unprintable characters, escape the whole sequence, since
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
607 we can't easily escape single characters within it. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
608 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
609 /* Length of multibyte sequence found so far. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
610 size_t m;
22459
18f9a82a2a87 Streamline by invoking multibyte code only if needed.
Jim Meyering <jim@meyering.net>
parents: 22452
diff changeset
611
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
612 bool printable;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
613
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
614 if (unibyte_locale)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
615 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
616 m = 1;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
617 printable = isprint (c) != 0;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
618 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
619 else
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
620 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
621 mbstate_t mbstate;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
622 memset (&mbstate, 0, sizeof mbstate);
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
623
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
624 m = 0;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
625 printable = true;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
626 if (argsize == SIZE_MAX)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
627 argsize = strlen (arg);
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
628
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
629 do
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
630 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
631 wchar_t w;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
632 size_t bytes = mbrtowc (&w, &arg[i + m],
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
633 argsize - (i + m), &mbstate);
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
634 if (bytes == 0)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
635 break;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
636 else if (bytes == (size_t) -1)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
637 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
638 printable = false;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
639 break;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
640 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
641 else if (bytes == (size_t) -2)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
642 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
643 printable = false;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
644 while (i + m < argsize && arg[i + m])
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
645 m++;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
646 break;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
647 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
648 else
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
649 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
650 /* Work around a bug with older shells that "see" a '\'
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
651 that is really the 2nd byte of a multibyte character.
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
652 In practice the problem is limited to ASCII
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
653 chars >= '@' that are shell special chars. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
654 if ('[' == 0x5b && elide_outer_quotes
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
655 && quoting_style == shell_always_quoting_style)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
656 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
657 size_t j;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
658 for (j = 1; j < bytes; j++)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
659 switch (arg[i + m + j])
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
660 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
661 case '[': case '\\': case '^':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
662 case '`': case '|':
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
663 goto force_outer_quoting_style;
26273
63410d97c583 * quotearg.c (quotearg_buffer_restyled): Add "default: break;"
Paul Eggert <eggert@cs.ucla.edu>
parents: 25966
diff changeset
664
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
665 default:
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
666 break;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
667 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
668 }
24850
406719fe9dad Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24634
diff changeset
669
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
670 if (! iswprint (w))
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
671 printable = false;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
672 m += bytes;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
673 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
674 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
675 while (! mbsinit (&mbstate));
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
676 }
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
677
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
678 c_and_shell_quote_compat = printable;
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
679
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
680 if (1 < m || (backslash_escapes && ! printable))
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
681 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
682 /* Output a multibyte sequence, or an escaped
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
683 unprintable unibyte character. */
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
684 size_t ilim = i + m;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
685
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
686 for (;;)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
687 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
688 if (backslash_escapes && ! printable)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
689 {
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
690 START_ESC ();
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
691 STORE ('0' + (c >> 6));
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
692 STORE ('0' + ((c >> 3) & 7));
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
693 c = '0' + (c & 7);
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
694 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
695 else if (is_right_quote)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
696 {
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
697 STORE ('\\');
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
698 is_right_quote = false;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
699 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
700 if (ilim <= i + 1)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
701 break;
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
702 END_ESC ();
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
703 STORE (c);
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
704 c = arg[++i];
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
705 }
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
706
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
707 goto store_c;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
708 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
709 }
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
710 }
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
711
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
712 if (! (((backslash_escapes && quoting_style != shell_always_quoting_style)
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
713 || elide_outer_quotes)
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
714 && quote_these_too
37235
2ea05e953064 quotearg: don't attempt to store 1 << 31 into an "int"
Jim Meyering <meyering@fb.com>
parents: 37080
diff changeset
715 && quote_these_too[c / INT_BITS] >> (c % INT_BITS) & 1)
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
716 && !is_right_quote)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
717 goto store_c;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
718
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
719 store_escape:
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
720 START_ESC ();
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
721
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
722 store_c:
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
723 END_ESC ();
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
724 STORE (c);
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
725
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
726 if (! c_and_shell_quote_compat)
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
727 all_c_and_shell_quote_compat = false;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
728 }
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
729
29391
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
730 if (len == 0 && quoting_style == shell_always_quoting_style
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
731 && elide_outer_quotes)
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
732 goto force_outer_quoting_style;
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
733
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
734 /* Single shell quotes (') are commonly enough used as an apostrophe,
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
735 that we attempt to minimize the quoting in this case. Note itʼs
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
736 better to use the apostrophe modifier "\u02BC" if possible, as that
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
737 renders better and works with the word match regex \W+ etc. */
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
738 if (quoting_style == shell_always_quoting_style && ! elide_outer_quotes
38143
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
739 && encountered_single_quote)
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
740 {
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
741 if (all_c_and_shell_quote_compat)
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
742 return quotearg_buffer_restyled (buffer, orig_buffersize, arg, argsize,
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
743 c_quoting_style,
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
744 flags, quote_these_too,
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
745 left_quote, right_quote);
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
746 else if (! buffersize && orig_buffersize)
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
747 {
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
748 /* Disable read-only scan, and reprocess to write quoted string. */
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
749 buffersize = orig_buffersize;
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
750 len = 0;
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
751 goto process_input;
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
752 }
4ede3d82793b quotearg: never write beyond the returned length
Pádraig Brady <pbrady@fb.com>
parents: 38119
diff changeset
753 }
38119
04ab53b19919 quotearg: minimize shell quoting using double quotes
Pádraig Brady <P@draigBrady.com>
parents: 37869
diff changeset
754
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
755 if (quote_string && !elide_outer_quotes)
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
756 for (; *quote_string; quote_string++)
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
757 STORE (*quote_string);
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
758
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
759 if (len < buffersize)
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
760 buffer[len] = '\0';
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
761 return len;
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
762
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
763 force_outer_quoting_style:
29391
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
764 /* Don't reuse quote_these_too, since the addition of outer quotes
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
765 sufficiently quotes the specified characters. */
37833
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
766 if (quoting_style == shell_always_quoting_style && backslash_escapes)
f165e94ee16b quotearg: add support for $'' shell escaping
Pádraig Brady <P@draigBrady.com>
parents: 37528
diff changeset
767 quoting_style = shell_escape_always_quoting_style;
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
768 return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
769 quoting_style,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
770 flags & ~QA_ELIDE_OUTER_QUOTES, NULL,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
771 left_quote, right_quote);
21815
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
772 }
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
773
e66ecacb810e Quote multibyte characters correctly.
Jim Meyering <jim@meyering.net>
parents: 21611
diff changeset
774 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
775 argument ARG (of size ARGSIZE), using O to control quoting.
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
776 If O is null, use the default.
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
777 Terminate the output with a null character, and return the written
Jim Meyering <jim@meyering.net>
parents:
diff changeset
778 size of the output, not counting the terminating null.
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
779 If BUFFERSIZE is too small to store the output string, return the
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
780 value that would have been returned had BUFFERSIZE been large enough.
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
781 If ARGSIZE is SIZE_MAX, use the string length of the argument for
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
782 ARGSIZE. */
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
783 size_t
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
784 quotearg_buffer (char *buffer, size_t buffersize,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
785 char const *arg, size_t argsize,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
786 struct quoting_options const *o)
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
787 {
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
788 struct quoting_options const *p = o ? o : &default_quoting_options;
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
789 int e = errno;
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
790 size_t r = quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
791 p->style, p->flags, p->quote_these_too,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
792 p->left_quote, p->right_quote);
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
793 errno = e;
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
794 return r;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
795 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
796
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
797 /* Equivalent to quotearg_alloc (ARG, ARGSIZE, NULL, O). */
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
798 char *
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
799 quotearg_alloc (char const *arg, size_t argsize,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
800 struct quoting_options const *o)
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
801 {
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
802 return quotearg_alloc_mem (arg, argsize, NULL, o);
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
803 }
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
804
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
805 /* Like quotearg_buffer (..., ARG, ARGSIZE, O), except return newly
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
806 allocated storage containing the quoted string, and store the
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
807 resulting size into *SIZE, if non-NULL. The result can contain
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
808 embedded null bytes only if ARGSIZE is not SIZE_MAX, SIZE is not
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
809 NULL, and set_quoting_flags has not set the null byte elision
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
810 flag. */
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
811 char *
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
812 quotearg_alloc_mem (char const *arg, size_t argsize, size_t *size,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
813 struct quoting_options const *o)
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
814 {
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
815 struct quoting_options const *p = o ? o : &default_quoting_options;
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
816 int e = errno;
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
817 /* Elide embedded null bytes if we can't return a size. */
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
818 int flags = p->flags | (size ? 0 : QA_ELIDE_NULL_BYTES);
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
819 size_t bufsize = quotearg_buffer_restyled (0, 0, arg, argsize, p->style,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
820 flags, p->quote_these_too,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
821 p->left_quote,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
822 p->right_quote) + 1;
27278
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
823 char *buf = xcharalloc (bufsize);
29391
c806149c588b Quotearg part 4: add tests, fix c-maybe colon quoting.
Eric Blake <ebb9@byu.net>
parents: 29390
diff changeset
824 quotearg_buffer_restyled (buf, bufsize, arg, argsize, p->style, flags,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
825 p->quote_these_too,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
826 p->left_quote, p->right_quote);
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
827 errno = e;
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
828 if (size)
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
829 *size = bufsize - 1;
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
830 return buf;
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
831 }
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
832
27278
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
833 /* A storage slot with size and pointer to a value. */
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
834 struct slotvec
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
835 {
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
836 size_t size;
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
837 char *val;
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
838 };
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
839
27318
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
840 /* Preallocate a slot 0 buffer, so that the caller can always quote
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
841 one small component of a "memory exhausted" message in slot 0. */
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
842 static char slot0[256];
38241
909a440ad711 quotearg: pacify GCC better
Paul Eggert <eggert@cs.ucla.edu>
parents: 38143
diff changeset
843 static int nslots = 1;
27318
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
844 static struct slotvec slotvec0 = {sizeof slot0, slot0};
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
845 static struct slotvec *slotvec = &slotvec0;
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
846
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
847 void
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
848 quotearg_free (void)
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
849 {
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
850 struct slotvec *sv = slotvec;
38241
909a440ad711 quotearg: pacify GCC better
Paul Eggert <eggert@cs.ucla.edu>
parents: 38143
diff changeset
851 int i;
27318
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
852 for (i = 1; i < nslots; i++)
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
853 free (sv[i].val);
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
854 if (sv[0].val != slot0)
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
855 {
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
856 free (sv[0].val);
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
857 slotvec0.size = sizeof slot0;
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
858 slotvec0.val = slot0;
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
859 }
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
860 if (sv != &slotvec0)
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
861 {
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
862 free (sv);
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
863 slotvec = &slotvec0;
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
864 }
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
865 nslots = 1;
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
866 }
b544246df8c8 * lib/quotearg.h (quotearg_free): New decl.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27278
diff changeset
867
23400
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
868 /* Use storage slot N to return a quoted version of argument ARG.
24634
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
869 ARG is of size ARGSIZE, but if that is SIZE_MAX, ARG is a
23b095cd7085 New function quotearg_alloc. Treat { } = as special sometimes.
Paul Eggert <eggert@cs.ucla.edu>
parents: 24521
diff changeset
870 null-terminated string.
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
871 OPTIONS specifies the quoting options.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
872 The returned value points to static storage that can be
Jim Meyering <jim@meyering.net>
parents:
diff changeset
873 reused by the next call to this function with the same value of N.
22413
a00ebdb9e8c7 The old behavior of quoting `like this' doesn't look good with
Jim Meyering <jim@meyering.net>
parents: 22402
diff changeset
874 N must be nonnegative. N is deliberately declared with type "int"
21354
5e627ff972e6 (quotearg_n_options): Revert type of parameter `n'
Jim Meyering <jim@meyering.net>
parents: 21341
diff changeset
875 to allow for future extensions (using negative values). */
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
876 static char *
23400
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
877 quotearg_n_options (int n, char const *arg, size_t argsize,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
878 struct quoting_options const *options)
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
879 {
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
880 int e = errno;
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
881
27278
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
882 struct slotvec *sv = slotvec;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
883
23348
e41b7e04f6cc (quotearg_n, quotearg_n_style): Likewise.
Jim Meyering <jim@meyering.net>
parents: 23101
diff changeset
884 if (n < 0)
e41b7e04f6cc (quotearg_n, quotearg_n_style): Likewise.
Jim Meyering <jim@meyering.net>
parents: 23101
diff changeset
885 abort ();
e41b7e04f6cc (quotearg_n, quotearg_n_style): Likewise.
Jim Meyering <jim@meyering.net>
parents: 23101
diff changeset
886
38241
909a440ad711 quotearg: pacify GCC better
Paul Eggert <eggert@cs.ucla.edu>
parents: 38143
diff changeset
887 if (nslots <= n)
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
888 {
27278
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
889 bool preallocated = (sv == &slotvec0);
38901
ead4e3bf6c56 quotearg: pacify compiler re unsigned
Paul Eggert <eggert@cs.ucla.edu>
parents: 38846
diff changeset
890 int nmax = MIN (INT_MAX, MIN (PTRDIFF_MAX, SIZE_MAX) / sizeof *sv) - 1;
23348
e41b7e04f6cc (quotearg_n, quotearg_n_style): Likewise.
Jim Meyering <jim@meyering.net>
parents: 23101
diff changeset
891
38901
ead4e3bf6c56 quotearg: pacify compiler re unsigned
Paul Eggert <eggert@cs.ucla.edu>
parents: 38846
diff changeset
892 if (nmax < n)
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
893 xalloc_die ();
23348
e41b7e04f6cc (quotearg_n, quotearg_n_style): Likewise.
Jim Meyering <jim@meyering.net>
parents: 23101
diff changeset
894
38241
909a440ad711 quotearg: pacify GCC better
Paul Eggert <eggert@cs.ucla.edu>
parents: 38143
diff changeset
895 slotvec = sv = xrealloc (preallocated ? NULL : sv, (n + 1) * sizeof *sv);
27278
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
896 if (preallocated)
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
897 *sv = slotvec0;
38241
909a440ad711 quotearg: pacify GCC better
Paul Eggert <eggert@cs.ucla.edu>
parents: 38143
diff changeset
898 memset (sv + nslots, 0, (n + 1 - nslots) * sizeof *sv);
909a440ad711 quotearg: pacify GCC better
Paul Eggert <eggert@cs.ucla.edu>
parents: 38143
diff changeset
899 nslots = n + 1;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
900 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
901
Jim Meyering <jim@meyering.net>
parents:
diff changeset
902 {
27278
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
903 size_t size = sv[n].size;
fb5a6443f8d3 Avoid some C++ diagnostics reported by Bruno Haible.
Paul Eggert <eggert@cs.ucla.edu>
parents: 27085
diff changeset
904 char *val = sv[n].val;
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
905 /* Elide embedded null bytes since we don't return a size. */
29390
af70d8bf0bd7 Quotearg part 3: add flag to control outer quote elision.
Eric Blake <ebb9@byu.net>
parents: 29389
diff changeset
906 int flags = options->flags | QA_ELIDE_NULL_BYTES;
29389
b94773b04d21 Quotearg part 2: add flag that can control NUL elision.
Eric Blake <ebb9@byu.net>
parents: 29388
diff changeset
907 size_t qsize = quotearg_buffer_restyled (val, size, arg, argsize,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
908 options->style, flags,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
909 options->quote_these_too,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
910 options->left_quote,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
911 options->right_quote);
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
912
Jim Meyering <jim@meyering.net>
parents:
diff changeset
913 if (size <= qsize)
Jim Meyering <jim@meyering.net>
parents:
diff changeset
914 {
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
915 sv[n].size = size = qsize + 1;
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
916 if (val != slot0)
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
917 free (val);
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
918 sv[n].val = val = xcharalloc (size);
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
919 quotearg_buffer_restyled (val, size, arg, argsize, options->style,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
920 flags, options->quote_these_too,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
921 options->left_quote,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
922 options->right_quote);
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
923 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
924
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
925 errno = e;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
926 return val;
Jim Meyering <jim@meyering.net>
parents:
diff changeset
927 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
928 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
929
Jim Meyering <jim@meyering.net>
parents:
diff changeset
930 char *
23348
e41b7e04f6cc (quotearg_n, quotearg_n_style): Likewise.
Jim Meyering <jim@meyering.net>
parents: 23101
diff changeset
931 quotearg_n (int n, char const *arg)
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
932 {
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
933 return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options);
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
934 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
935
Jim Meyering <jim@meyering.net>
parents:
diff changeset
936 char *
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
937 quotearg_n_mem (int n, char const *arg, size_t argsize)
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
938 {
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
939 return quotearg_n_options (n, arg, argsize, &default_quoting_options);
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
940 }
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
941
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
942 char *
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
943 quotearg (char const *arg)
Jim Meyering <jim@meyering.net>
parents:
diff changeset
944 {
Jim Meyering <jim@meyering.net>
parents:
diff changeset
945 return quotearg_n (0, arg);
Jim Meyering <jim@meyering.net>
parents:
diff changeset
946 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
947
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
948 char *
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
949 quotearg_mem (char const *arg, size_t argsize)
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
950 {
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
951 return quotearg_n_mem (0, arg, argsize);
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
952 }
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
953
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
954 char *
23348
e41b7e04f6cc (quotearg_n, quotearg_n_style): Likewise.
Jim Meyering <jim@meyering.net>
parents: 23101
diff changeset
955 quotearg_n_style (int n, enum quoting_style s, char const *arg)
21611
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
956 {
23400
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
957 struct quoting_options const o = quoting_options_from_style (s);
23695
b89931af0123 Include quotearg.h immediately after config.h.
Paul Eggert <eggert@cs.ucla.edu>
parents: 23657
diff changeset
958 return quotearg_n_options (n, arg, SIZE_MAX, &o);
23400
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
959 }
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
960
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
961 char *
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
962 quotearg_n_style_mem (int n, enum quoting_style s,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
963 char const *arg, size_t argsize)
23400
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
964 {
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
965 struct quoting_options const o = quoting_options_from_style (s);
389a4ba1f1a3 (quotearg_buffer_restyled):
Jim Meyering <jim@meyering.net>
parents: 23348
diff changeset
966 return quotearg_n_options (n, arg, argsize, &o);
21611
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
967 }
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
968
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
969 char *
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
970 quotearg_style (enum quoting_style s, char const *arg)
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
971 {
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
972 return quotearg_n_style (0, s, arg);
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
973 }
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
974
7aaff578690a Include <libintl.h> if ENABLE_NLS.
Jim Meyering <jim@meyering.net>
parents: 21582
diff changeset
975 char *
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
976 quotearg_style_mem (enum quoting_style s, char const *arg, size_t argsize)
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
977 {
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
978 return quotearg_n_style_mem (0, s, arg, argsize);
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
979 }
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
980
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
981 char *
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
982 quotearg_char_mem (char const *arg, size_t argsize, char ch)
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
983 {
Jim Meyering <jim@meyering.net>
parents:
diff changeset
984 struct quoting_options options;
20973
f8c86eba19f9 Update from patch-2.5.3.
Jim Meyering <jim@meyering.net>
parents: 20939
diff changeset
985 options = default_quoting_options;
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
986 set_char_quoting (&options, ch, 1);
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
987 return quotearg_n_options (0, arg, argsize, &options);
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
988 }
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
989
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
990 char *
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
991 quotearg_char (char const *arg, char ch)
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
992 {
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
993 return quotearg_char_mem (arg, SIZE_MAX, ch);
20939
Jim Meyering <jim@meyering.net>
parents:
diff changeset
994 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
995
Jim Meyering <jim@meyering.net>
parents:
diff changeset
996 char *
Jim Meyering <jim@meyering.net>
parents:
diff changeset
997 quotearg_colon (char const *arg)
Jim Meyering <jim@meyering.net>
parents:
diff changeset
998 {
Jim Meyering <jim@meyering.net>
parents:
diff changeset
999 return quotearg_char (arg, ':');
Jim Meyering <jim@meyering.net>
parents:
diff changeset
1000 }
29388
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
1001
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
1002 char *
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
1003 quotearg_colon_mem (char const *arg, size_t argsize)
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
1004 {
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
1005 return quotearg_char_mem (arg, argsize, ':');
0e7281b031d5 Quotearg part 1: more wrappers, restore quotearg_char state.
Eric Blake <ebb9@byu.net>
parents: 29000
diff changeset
1006 }
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1007
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1008 char *
37837
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1009 quotearg_n_style_colon (int n, enum quoting_style s, char const *arg)
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1010 {
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1011 struct quoting_options options;
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1012 options = quoting_options_from_style (s);
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1013 set_char_quoting (&options, ':', 1);
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1014 return quotearg_n_options (n, arg, SIZE_MAX, &options);
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1015 }
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1016
25fb2c91f66a quotearg: add quotearg_n_style_colon()
Pádraig Brady <P@draigBrady.com>
parents: 37834
diff changeset
1017 char *
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1018 quotearg_n_custom (int n, char const *left_quote,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
1019 char const *right_quote, char const *arg)
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1020 {
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1021 return quotearg_n_custom_mem (n, left_quote, right_quote, arg,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
1022 SIZE_MAX);
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1023 }
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1024
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1025 char *
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1026 quotearg_n_custom_mem (int n, char const *left_quote,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
1027 char const *right_quote,
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
1028 char const *arg, size_t argsize)
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1029 {
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1030 struct quoting_options o = default_quoting_options;
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1031 set_custom_quoting (&o, left_quote, right_quote);
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1032 return quotearg_n_options (n, arg, argsize, &o);
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1033 }
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1034
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1035 char *
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1036 quotearg_custom (char const *left_quote, char const *right_quote,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
1037 char const *arg)
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1038 {
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1039 return quotearg_n_custom (0, left_quote, right_quote, arg);
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1040 }
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1041
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1042 char *
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1043 quotearg_custom_mem (char const *left_quote, char const *right_quote,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
1044 char const *arg, size_t argsize)
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1045 {
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1046 return quotearg_n_custom_mem (0, left_quote, right_quote, arg,
32112
dc7644a1c024 Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 31582
diff changeset
1047 argsize);
31581
7aa82f7bc97d quotearg: implement custom_quoting_style
Joel E. Denny <jdenny@clemson.edu>
parents: 31580
diff changeset
1048 }
36285
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1049
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1050
36837
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1051 /* The quoting option used by the functions of quote.h. */
36285
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1052 struct quoting_options quote_quoting_options =
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1053 {
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1054 locale_quoting_style,
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1055 0,
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1056 { 0 },
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1057 NULL, NULL
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1058 };
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1059
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1060 char const *
36837
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1061 quote_n_mem (int n, char const *arg, size_t argsize)
36285
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1062 {
36837
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1063 return quotearg_n_options (n, arg, argsize, &quote_quoting_options);
36285
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1064 }
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1065
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1066 char const *
36837
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1067 quote_mem (char const *arg, size_t argsize)
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1068 {
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1069 return quote_n_mem (0, arg, argsize);
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1070 }
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1071
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1072 char const *
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1073 quote_n (int n, char const *arg)
36285
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1074 {
36837
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1075 return quote_n_mem (n, arg, SIZE_MAX);
36285
17be314ee7ea quote: fuse into quotearg
Akim Demaille <demaille@gostai.com>
parents: 35937
diff changeset
1076 }
36837
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1077
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1078 char const *
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1079 quote (char const *arg)
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1080 {
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1081 return quote_n (0, arg);
ddb822e83c39 quote: provide a means to escape strings with nul characters
Akim Demaille <akim@lrde.epita.fr>
parents: 36285
diff changeset
1082 }
39211
28a8d93056e6 maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents: 39199
diff changeset
1083
28a8d93056e6 maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents: 39199
diff changeset
1084 /*
28a8d93056e6 maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents: 39199
diff changeset
1085 * Hey Emacs!
28a8d93056e6 maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents: 39199
diff changeset
1086 * Local Variables:
28a8d93056e6 maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents: 39199
diff changeset
1087 * coding: utf-8
28a8d93056e6 maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents: 39199
diff changeset
1088 * End:
28a8d93056e6 maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents: 39199
diff changeset
1089 */