1
/* quotearg.h - quote arguments for output
3
Copyright (C) 1998-2002, 2004, 2006, 2008-2012 Free Software Foundation,
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
78
in effect. Split into consecutive strings if
82
"\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
84
"\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
86
"\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
90
/* Like c_quoting_style except omit the surrounding double-quote
91
characters if no quoted characters are encountered.
94
"simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
96
"simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
98
"simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
100
c_maybe_quoting_style,
102
/* Like c_quoting_style except always omit the surrounding
103
double-quote characters and ignore QA_SPLIT_TRIGRAPHS
104
(ls --quoting-style=escape).
107
"simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
109
"simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
111
"simple", "\\0 \\t\\n'\"\\033??/\\\\", "a\\:b"
113
escape_quoting_style,
115
/* Like clocale_quoting_style, but use single quotes in the
116
default C locale or if the program does not use gettext
117
(ls --quoting-style=locale). For UTF-8 locales, quote
118
characters will use Unicode.
122
"`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
124
"`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
126
"`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a\\:b'"
128
LC_MESSAGES=pt_PT.utf8
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
"\302\253simple\302\273",
137
"\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
139
locale_quoting_style,
141
/* Like c_quoting_style except use quotation marks appropriate for
142
the locale and ignore QA_SPLIT_TRIGRAPHS
143
(ls --quoting-style=clocale).
147
"\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
149
"\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
151
"\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
153
LC_MESSAGES=pt_PT.utf8
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
"\302\253simple\302\273",
162
"\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
164
clocale_quoting_style,
166
/* Like clocale_quoting_style except use the custom quotation marks
167
set by set_custom_quoting. If custom quotation marks are not
168
set, the behavior is undefined.
170
left_quote = right_quote = "'"
172
"'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
174
"'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
176
"'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a\\:b'"
178
left_quote = "(" and right_quote = ")"
180
"(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
182
"(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
184
"(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a\\:b)"
186
left_quote = ":" and right_quote = " "
188
":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
190
":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
192
":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a\\:b "
194
left_quote = "\"'" and right_quote = "'\""
195
Notice that this is treated as a single level of quotes or two
196
levels where the outer quote need not be escaped within the inner
197
quotes. For two levels where the outer quote must be escaped
198
within the inner quotes, you must use separate quotearg
201
"\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
203
"\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
205
"\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a\\:b'\""
210
/* Flags for use in set_quoting_flags. */
213
/* Always elide null bytes from styles that do not quote them,
214
even when the length of the result is available to the
216
QA_ELIDE_NULL_BYTES = 0x01,
218
/* Omit the surrounding quote characters if no escaped characters
219
are encountered. Note that if no other character needs
220
escaping, then neither does the escape character. */
221
QA_ELIDE_OUTER_QUOTES = 0x02,
223
/* In the c_quoting_style and c_maybe_quoting_style, split ANSI
224
trigraph sequences into concatenated strings (for example,
225
"?""?/" rather than "??/", which could be confused with
227
QA_SPLIT_TRIGRAPHS = 0x04
230
/* For now, --quoting-style=literal is the default, but this may change. */
231
# ifndef DEFAULT_QUOTING_STYLE
232
# define DEFAULT_QUOTING_STYLE literal_quoting_style
235
/* Names of quoting styles and their corresponding values. */
236
extern char const *const quoting_style_args[];
237
extern enum quoting_style const quoting_style_vals[];
239
struct quoting_options;
241
/* The functions listed below set and use a hidden variable
242
that contains the default quoting style options. */
244
/* Allocate a new set of quoting options, with contents initially identical
245
to O if O is not null, or to the default if O is null.
246
It is the caller's responsibility to free the result. */
247
struct quoting_options *clone_quoting_options (struct quoting_options *o);
249
/* Get the value of O's quoting style. If O is null, use the default. */
250
enum quoting_style get_quoting_style (struct quoting_options *o);
252
/* In O (or in the default if O is null),
253
set the value of the quoting style to S. */
254
void set_quoting_style (struct quoting_options *o, enum quoting_style s);
256
/* In O (or in the default if O is null),
257
set the value of the quoting options for character C to I.
258
Return the old value. Currently, the only values defined for I are
259
0 (the default) and 1 (which means to quote the character even if
260
it would not otherwise be quoted). C must never be a digit or a
261
letter that has special meaning after a backslash (for example, "\t"
263
int set_char_quoting (struct quoting_options *o, char c, int i);
265
/* In O (or in the default if O is null),
266
set the value of the quoting options flag to I, which can be a
267
bitwise combination of enum quoting_flags, or 0 for default
268
behavior. Return the old value. */
269
int set_quoting_flags (struct quoting_options *o, int i);
271
/* In O (or in the default if O is null),
272
set the value of the quoting style to custom_quoting_style,
273
set the left quote to LEFT_QUOTE, and set the right quote to
274
RIGHT_QUOTE. Each of LEFT_QUOTE and RIGHT_QUOTE must be
275
null-terminated and can be the empty string. Because backslashes are
276
used for escaping, it does not make sense for RIGHT_QUOTE to contain
277
a backslash. RIGHT_QUOTE must not begin with a digit or a letter
278
that has special meaning after a backslash (for example, "\t" for
280
void set_custom_quoting (struct quoting_options *o,
281
char const *left_quote,
282
char const *right_quote);
284
/* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
285
argument ARG (of size ARGSIZE), using O to control quoting.
286
If O is null, use the default.
287
Terminate the output with a null character, and return the written
288
size of the output, not counting the terminating null.
289
If BUFFERSIZE is too small to store the output string, return the
290
value that would have been returned had BUFFERSIZE been large enough.
291
If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
292
On output, BUFFER might contain embedded null bytes if ARGSIZE was
293
not -1, the style of O does not use backslash escapes, and the
294
flags of O do not request elision of null bytes.*/
295
size_t quotearg_buffer (char *buffer, size_t buffersize,
296
char const *arg, size_t argsize,
297
struct quoting_options const *o);
299
/* Like quotearg_buffer, except return the result in a newly allocated
300
buffer. It is the caller's responsibility to free the result. The
301
result will not contain embedded null bytes. */
302
char *quotearg_alloc (char const *arg, size_t argsize,
303
struct quoting_options const *o);
305
/* Like quotearg_alloc, except that the length of the result,
306
excluding the terminating null byte, is stored into SIZE if it is
307
non-NULL. The result might contain embedded null bytes if ARGSIZE
308
was not -1, SIZE was not NULL, the style of O does not use
309
backslash escapes, and the flags of O do not request elision of
311
char *quotearg_alloc_mem (char const *arg, size_t argsize,
312
size_t *size, struct quoting_options const *o);
314
/* Use storage slot N to return a quoted version of the string ARG.
315
Use the default quoting options.
316
The returned value points to static storage that can be
317
reused by the next call to this function with the same value of N.
318
N must be nonnegative. The output of all functions in the
319
quotearg_n family are guaranteed to not contain embedded null
321
char *quotearg_n (int n, char const *arg);
323
/* Equivalent to quotearg_n (0, ARG). */
324
char *quotearg (char const *arg);
326
/* Use storage slot N to return a quoted version of the argument ARG
327
of size ARGSIZE. This is like quotearg_n (N, ARG), except it can
329
char *quotearg_n_mem (int n, char const *arg, size_t argsize);
331
/* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE). */
332
char *quotearg_mem (char const *arg, size_t argsize);
334
/* Use style S and storage slot N to return a quoted version of the string ARG.
335
This is like quotearg_n (N, ARG), except that it uses S with no other
336
options to specify the quoting method. */
337
char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
339
/* Use style S and storage slot N to return a quoted version of the
340
argument ARG of size ARGSIZE. This is like quotearg_n_style
341
(N, S, ARG), except it can quote null bytes. */
342
char *quotearg_n_style_mem (int n, enum quoting_style s,
343
char const *arg, size_t argsize);
345
/* Equivalent to quotearg_n_style (0, S, ARG). */
346
char *quotearg_style (enum quoting_style s, char const *arg);
348
/* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE). */
349
char *quotearg_style_mem (enum quoting_style s,
350
char const *arg, size_t argsize);
352
/* Like quotearg (ARG), except also quote any instances of CH.
353
See set_char_quoting for a description of acceptable CH values. */
354
char *quotearg_char (char const *arg, char ch);
356
/* Like quotearg_char (ARG, CH), except it can quote null bytes. */
357
char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
359
/* Equivalent to quotearg_char (ARG, ':'). */
360
char *quotearg_colon (char const *arg);
362
/* Like quotearg_colon (ARG), except it can quote null bytes. */
363
char *quotearg_colon_mem (char const *arg, size_t argsize);
365
/* Like quotearg_n_style (N, S, ARG) but with S as custom_quoting_style
366
with left quote as LEFT_QUOTE and right quote as RIGHT_QUOTE. See
367
set_custom_quoting for a description of acceptable LEFT_QUOTE and
368
RIGHT_QUOTE values. */
369
char *quotearg_n_custom (int n, char const *left_quote,
370
char const *right_quote, char const *arg);
372
/* Like quotearg_n_custom (N, LEFT_QUOTE, RIGHT_QUOTE, ARG) except it
373
can quote null bytes. */
374
char *quotearg_n_custom_mem (int n, char const *left_quote,
375
char const *right_quote,
376
char const *arg, size_t argsize);
378
/* Equivalent to quotearg_n_custom (0, LEFT_QUOTE, RIGHT_QUOTE, ARG). */
379
char *quotearg_custom (char const *left_quote, char const *right_quote,
382
/* Equivalent to quotearg_n_custom_mem (0, LEFT_QUOTE, RIGHT_QUOTE, ARG,
384
char *quotearg_custom_mem (char const *left_quote,
385
char const *right_quote,
386
char const *arg, size_t argsize);
388
/* Free any dynamically allocated memory. */
389
void quotearg_free (void);
391
#endif /* !QUOTEARG_H_ */