1 /* quotearg.h - quote arguments for output
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006, 2008 Free
4 Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert <eggert@twinsun.com> */
22 # define QUOTEARG_H_ 1
26 /* Basic quoting styles. For each style, an example is given on the
27 input strings "simple", "\0 \t\n'\"\033?""?/\\", and "a:b", using
28 quotearg_buffer, quotearg_mem, and quotearg_colon_mem with that
29 style and the default flags and quoted characters. Note that the
30 examples are shown here as valid C strings rather than what
31 displays on a terminal (with "??/" as a trigraph for "\\"). */
34 /* Output names as-is (ls --quoting-style=literal). Can result in
35 embedded null bytes if QA_ELIDE_NULL_BYTES is not in
39 "simple", "\0 \t\n'\"\033??/\\", "a:b"
41 "simple", " \t\n'\"\033??/\\", "a:b"
43 "simple", " \t\n'\"\033??/\\", "a:b"
45 literal_quoting_style,
47 /* Quote names for the shell if they contain shell metacharacters
48 or would cause ambiguous output (ls --quoting-style=shell).
49 Can result in embedded null bytes if QA_ELIDE_NULL_BYTES is not
53 "simple", "'\0 \t\n'\\''\"\033??/\\'", "a:b"
55 "simple", "' \t\n'\\''\"\033??/\\'", "a:b"
57 "simple", "' \t\n'\\''\"\033??/\\'", "'a:b'"
61 /* Quote names for the shell, even if they would normally not
62 require quoting (ls --quoting-style=shell-always). Can result
63 in embedded null bytes if QA_ELIDE_NULL_BYTES is not in effect.
64 Behaves like shell_quoting_style if QA_ELIDE_OUTER_QUOTES is in
68 "'simple'", "'\0 \t\n'\\''\"\033??/\\'", "'a:b'"
70 "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
72 "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
74 shell_always_quoting_style,
76 /* Quote names as for a C language string (ls --quoting-style=c).
77 Behaves like c_maybe_quoting_style if QA_ELIDE_OUTER_QUOTES is
81 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\""
83 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\""
85 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\""
89 /* Like c_quoting_style except omit the surrounding double-quote
90 characters if no quoted characters are encountered.
93 "simple", "\"\\0 \\t\\n'\\\"\\033?\"\"?/\\\\\"", "a:b"
95 "simple", "\"\\0 \\t\\n'\\\"\\033?\"\"?/\\\\\"", "a:b"
97 "simple", "\"\\0 \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\""
99 c_maybe_quoting_style,
101 /* Like c_quoting_style except always omit the surrounding
102 double-quote characters and don't worry about trigraphs (ls
103 --quoting-style=escape).
106 "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
108 "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
110 "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a\\:b"
112 escape_quoting_style,
114 /* Like clocale_quoting_style, but quote `like this' instead of
115 "like this" in the default C locale (ls --quoting-style=locale).
119 "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
121 "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
123 "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a\\:b'"
125 LC_MESSAGES=pt_PT.utf8
127 "\302\253simple\302\273",
128 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
130 "\302\253simple\302\273",
131 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
133 "\302\253simple\302\273",
134 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
136 locale_quoting_style,
138 /* Like c_quoting_style except use quotation marks appropriate for
139 the locale and don't worry about trigraphs (ls
140 --quoting-style=clocale).
144 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
146 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
148 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
150 LC_MESSAGES=pt_PT.utf8
152 "\302\253simple\302\273",
153 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
155 "\302\253simple\302\273",
156 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
158 "\302\253simple\302\273",
159 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
161 clocale_quoting_style
164 /* Flags for use in set_quoting_flags. */
167 /* Always elide null bytes from styles that do not quote them,
168 even when the length of the result is available to the
170 QA_ELIDE_NULL_BYTES = 0x01,
172 /* Omit the surrounding quote characters if no escaped characters
174 QA_ELIDE_OUTER_QUOTES = 0x02
177 /* For now, --quoting-style=literal is the default, but this may change. */
178 # ifndef DEFAULT_QUOTING_STYLE
179 # define DEFAULT_QUOTING_STYLE literal_quoting_style
182 /* Names of quoting styles and their corresponding values. */
183 extern char const *const quoting_style_args[];
184 extern enum quoting_style const quoting_style_vals[];
186 struct quoting_options;
188 /* The functions listed below set and use a hidden variable
189 that contains the default quoting style options. */
191 /* Allocate a new set of quoting options, with contents initially identical
192 to O if O is not null, or to the default if O is null.
193 It is the caller's responsibility to free the result. */
194 struct quoting_options *clone_quoting_options (struct quoting_options *o);
196 /* Get the value of O's quoting style. If O is null, use the default. */
197 enum quoting_style get_quoting_style (struct quoting_options *o);
199 /* In O (or in the default if O is null),
200 set the value of the quoting style to S. */
201 void set_quoting_style (struct quoting_options *o, enum quoting_style s);
203 /* In O (or in the default if O is null),
204 set the value of the quoting options for character C to I.
205 Return the old value. Currently, the only values defined for I are
206 0 (the default) and 1 (which means to quote the character even if
207 it would not otherwise be quoted). */
208 int set_char_quoting (struct quoting_options *o, char c, int i);
210 /* In O (or in the default if O is null),
211 set the value of the quoting options flag to I, which can be a
212 bitwise combination of enum quoting_flags, or 0 for default
213 behavior. Return the old value. */
214 int set_quoting_flags (struct quoting_options *o, int i);
216 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
217 argument ARG (of size ARGSIZE), using O to control quoting.
218 If O is null, use the default.
219 Terminate the output with a null character, and return the written
220 size of the output, not counting the terminating null.
221 If BUFFERSIZE is too small to store the output string, return the
222 value that would have been returned had BUFFERSIZE been large enough.
223 If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
224 On output, BUFFER might contain embedded null bytes if ARGSIZE was
225 not -1, the style of O does not use backslash escapes, and the
226 flags of O do not request elision of null bytes.*/
227 size_t quotearg_buffer (char *buffer, size_t buffersize,
228 char const *arg, size_t argsize,
229 struct quoting_options const *o);
231 /* Like quotearg_buffer, except return the result in a newly allocated
232 buffer. It is the caller's responsibility to free the result. The
233 result will not contain embedded null bytes. */
234 char *quotearg_alloc (char const *arg, size_t argsize,
235 struct quoting_options const *o);
237 /* Like quotearg_alloc, except that the length of the result,
238 excluding the terminating null byte, is stored into SIZE if it is
239 non-NULL. The result might contain embedded null bytes if ARGSIZE
240 was not -1, SIZE was not NULL, the style of O does not use
241 backslash escapes, and the flags of O do not request elision of
243 char *quotearg_alloc_mem (char const *arg, size_t argsize,
244 size_t *size, struct quoting_options const *o);
246 /* Use storage slot N to return a quoted version of the string ARG.
247 Use the default quoting options.
248 The returned value points to static storage that can be
249 reused by the next call to this function with the same value of N.
250 N must be nonnegative. The output of all functions in the
251 quotearg_n family are guaranteed to not contain embedded null
253 char *quotearg_n (int n, char const *arg);
255 /* Equivalent to quotearg_n (0, ARG). */
256 char *quotearg (char const *arg);
258 /* Use storage slot N to return a quoted version of the argument ARG
259 of size ARGSIZE. This is like quotearg_n (N, ARG), except it can
261 char *quotearg_n_mem (int n, char const *arg, size_t argsize);
263 /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE). */
264 char *quotearg_mem (char const *arg, size_t argsize);
266 /* Use style S and storage slot N to return a quoted version of the string ARG.
267 This is like quotearg_n (N, ARG), except that it uses S with no other
268 options to specify the quoting method. */
269 char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
271 /* Use style S and storage slot N to return a quoted version of the
272 argument ARG of size ARGSIZE. This is like quotearg_n_style
273 (N, S, ARG), except it can quote null bytes. */
274 char *quotearg_n_style_mem (int n, enum quoting_style s,
275 char const *arg, size_t argsize);
277 /* Equivalent to quotearg_n_style (0, S, ARG). */
278 char *quotearg_style (enum quoting_style s, char const *arg);
280 /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE). */
281 char *quotearg_style_mem (enum quoting_style s,
282 char const *arg, size_t argsize);
284 /* Like quotearg (ARG), except also quote any instances of CH. */
285 char *quotearg_char (char const *arg, char ch);
287 /* Like quotearg_char (ARG, CH), except it can quote null bytes. */
288 char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
290 /* Equivalent to quotearg_char (ARG, ':'). */
291 char *quotearg_colon (char const *arg);
293 /* Like quotearg_colon (ARG), except it can quote null bytes. */
294 char *quotearg_colon_mem (char const *arg, size_t argsize);
296 /* Free any dynamically allocated memory. */
297 void quotearg_free (void);
299 #endif /* !QUOTEARG_H_ */