xref: /dragonfly/contrib/grep/lib/quotearg.h (revision 6e278935)
1 /* quotearg.h - quote arguments for output
2 
3    Copyright (C) 1998-2002, 2004, 2006, 2008-2011 Free Software Foundation,
4    Inc.
5 
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.
10 
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.
15 
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/>.  */
18 
19 /* Written by Paul Eggert <eggert@twinsun.com> */
20 
21 #ifndef QUOTEARG_H_
22 # define QUOTEARG_H_ 1
23 
24 # include <stddef.h>
25 
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 "\\").  */
32 enum quoting_style
33   {
34     /* Output names as-is (ls --quoting-style=literal).  Can result in
35        embedded null bytes if QA_ELIDE_NULL_BYTES is not in
36        effect.
37 
38        quotearg_buffer:
39        "simple", "\0 \t\n'\"\033??/\\", "a:b"
40        quotearg:
41        "simple", " \t\n'\"\033??/\\", "a:b"
42        quotearg_colon:
43        "simple", " \t\n'\"\033??/\\", "a:b"
44     */
45     literal_quoting_style,
46 
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
50        in effect.
51 
52        quotearg_buffer:
53        "simple", "'\0 \t\n'\\''\"\033??/\\'", "a:b"
54        quotearg:
55        "simple", "' \t\n'\\''\"\033??/\\'", "a:b"
56        quotearg_colon:
57        "simple", "' \t\n'\\''\"\033??/\\'", "'a:b'"
58     */
59     shell_quoting_style,
60 
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
65        effect.
66 
67        quotearg_buffer:
68        "'simple'", "'\0 \t\n'\\''\"\033??/\\'", "'a:b'"
69        quotearg:
70        "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
71        quotearg_colon:
72        "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
73     */
74     shell_always_quoting_style,
75 
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
79        QA_SPLIT_TRIGRAPHS.
80 
81        quotearg_buffer:
82        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
83        quotearg:
84        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
85        quotearg_colon:
86        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
87     */
88     c_quoting_style,
89 
90     /* Like c_quoting_style except omit the surrounding double-quote
91        characters if no quoted characters are encountered.
92 
93        quotearg_buffer:
94        "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
95        quotearg:
96        "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
97        quotearg_colon:
98        "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
99     */
100     c_maybe_quoting_style,
101 
102     /* Like c_quoting_style except always omit the surrounding
103        double-quote characters and ignore QA_SPLIT_TRIGRAPHS
104        (ls --quoting-style=escape).
105 
106        quotearg_buffer:
107        "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
108        quotearg:
109        "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
110        quotearg_colon:
111        "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a\\:b"
112     */
113     escape_quoting_style,
114 
115     /* Like clocale_quoting_style, but quote `like this' instead of
116        "like this" in the default C locale (ls --quoting-style=locale).
117 
118        LC_MESSAGES=C
119        quotearg_buffer:
120        "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
121        quotearg:
122        "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
123        quotearg_colon:
124        "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a\\:b'"
125 
126        LC_MESSAGES=pt_PT.utf8
127        quotearg_buffer:
128        "\302\253simple\302\273",
129        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
130        quotearg:
131        "\302\253simple\302\273",
132        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
133        quotearg_colon:
134        "\302\253simple\302\273",
135        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
136     */
137     locale_quoting_style,
138 
139     /* Like c_quoting_style except use quotation marks appropriate for
140        the locale and ignore QA_SPLIT_TRIGRAPHS
141        (ls --quoting-style=clocale).
142 
143        LC_MESSAGES=C
144        quotearg_buffer:
145        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
146        quotearg:
147        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
148        quotearg_colon:
149        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
150 
151        LC_MESSAGES=pt_PT.utf8
152        quotearg_buffer:
153        "\302\253simple\302\273",
154        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
155        quotearg:
156        "\302\253simple\302\273",
157        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
158        quotearg_colon:
159        "\302\253simple\302\273",
160        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
161     */
162     clocale_quoting_style,
163 
164     /* Like clocale_quoting_style except use the custom quotation marks
165        set by set_custom_quoting.  If custom quotation marks are not
166        set, the behavior is undefined.
167 
168        left_quote = right_quote = "'"
169        quotearg_buffer:
170        "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
171        quotearg:
172        "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
173        quotearg_colon:
174        "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a\\:b'"
175 
176        left_quote = "(" and right_quote = ")"
177        quotearg_buffer:
178        "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
179        quotearg:
180        "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
181        quotearg_colon:
182        "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a\\:b)"
183 
184        left_quote = ":" and right_quote = " "
185        quotearg_buffer:
186        ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
187        quotearg:
188        ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
189        quotearg_colon:
190        ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a\\:b "
191 
192        left_quote = "\"'" and right_quote = "'\""
193        Notice that this is treated as a single level of quotes or two
194        levels where the outer quote need not be escaped within the inner
195        quotes.  For two levels where the outer quote must be escaped
196        within the inner quotes, you must use separate quotearg
197        invocations.
198        quotearg_buffer:
199        "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
200        quotearg:
201        "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
202        quotearg_colon:
203        "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a\\:b'\""
204     */
205     custom_quoting_style
206   };
207 
208 /* Flags for use in set_quoting_flags.  */
209 enum quoting_flags
210   {
211     /* Always elide null bytes from styles that do not quote them,
212        even when the length of the result is available to the
213        caller.  */
214     QA_ELIDE_NULL_BYTES = 0x01,
215 
216     /* Omit the surrounding quote characters if no escaped characters
217        are encountered.  Note that if no other character needs
218        escaping, then neither does the escape character.  */
219     QA_ELIDE_OUTER_QUOTES = 0x02,
220 
221     /* In the c_quoting_style and c_maybe_quoting_style, split ANSI
222        trigraph sequences into concatenated strings (for example,
223        "?""?/" rather than "??/", which could be confused with
224        "\\").  */
225     QA_SPLIT_TRIGRAPHS = 0x04
226   };
227 
228 /* For now, --quoting-style=literal is the default, but this may change.  */
229 # ifndef DEFAULT_QUOTING_STYLE
230 #  define DEFAULT_QUOTING_STYLE literal_quoting_style
231 # endif
232 
233 /* Names of quoting styles and their corresponding values.  */
234 extern char const *const quoting_style_args[];
235 extern enum quoting_style const quoting_style_vals[];
236 
237 struct quoting_options;
238 
239 /* The functions listed below set and use a hidden variable
240    that contains the default quoting style options.  */
241 
242 /* Allocate a new set of quoting options, with contents initially identical
243    to O if O is not null, or to the default if O is null.
244    It is the caller's responsibility to free the result.  */
245 struct quoting_options *clone_quoting_options (struct quoting_options *o);
246 
247 /* Get the value of O's quoting style.  If O is null, use the default.  */
248 enum quoting_style get_quoting_style (struct quoting_options *o);
249 
250 /* In O (or in the default if O is null),
251    set the value of the quoting style to S.  */
252 void set_quoting_style (struct quoting_options *o, enum quoting_style s);
253 
254 /* In O (or in the default if O is null),
255    set the value of the quoting options for character C to I.
256    Return the old value.  Currently, the only values defined for I are
257    0 (the default) and 1 (which means to quote the character even if
258    it would not otherwise be quoted).  C must never be a digit or a
259    letter that has special meaning after a backslash (for example, "\t"
260    for tab).  */
261 int set_char_quoting (struct quoting_options *o, char c, int i);
262 
263 /* In O (or in the default if O is null),
264    set the value of the quoting options flag to I, which can be a
265    bitwise combination of enum quoting_flags, or 0 for default
266    behavior.  Return the old value.  */
267 int set_quoting_flags (struct quoting_options *o, int i);
268 
269 /* In O (or in the default if O is null),
270    set the value of the quoting style to custom_quoting_style,
271    set the left quote to LEFT_QUOTE, and set the right quote to
272    RIGHT_QUOTE.  Each of LEFT_QUOTE and RIGHT_QUOTE must be
273    null-terminated and can be the empty string.  Because backslashes are
274    used for escaping, it does not make sense for RIGHT_QUOTE to contain
275    a backslash.  RIGHT_QUOTE must not begin with a digit or a letter
276    that has special meaning after a backslash (for example, "\t" for
277    tab).  */
278 void set_custom_quoting (struct quoting_options *o,
279                          char const *left_quote,
280                          char const *right_quote);
281 
282 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
283    argument ARG (of size ARGSIZE), using O to control quoting.
284    If O is null, use the default.
285    Terminate the output with a null character, and return the written
286    size of the output, not counting the terminating null.
287    If BUFFERSIZE is too small to store the output string, return the
288    value that would have been returned had BUFFERSIZE been large enough.
289    If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
290    On output, BUFFER might contain embedded null bytes if ARGSIZE was
291    not -1, the style of O does not use backslash escapes, and the
292    flags of O do not request elision of null bytes.*/
293 size_t quotearg_buffer (char *buffer, size_t buffersize,
294                         char const *arg, size_t argsize,
295                         struct quoting_options const *o);
296 
297 /* Like quotearg_buffer, except return the result in a newly allocated
298    buffer.  It is the caller's responsibility to free the result.  The
299    result will not contain embedded null bytes.  */
300 char *quotearg_alloc (char const *arg, size_t argsize,
301                       struct quoting_options const *o);
302 
303 /* Like quotearg_alloc, except that the length of the result,
304    excluding the terminating null byte, is stored into SIZE if it is
305    non-NULL.  The result might contain embedded null bytes if ARGSIZE
306    was not -1, SIZE was not NULL, the style of O does not use
307    backslash escapes, and the flags of O do not request elision of
308    null bytes.*/
309 char *quotearg_alloc_mem (char const *arg, size_t argsize,
310                           size_t *size, struct quoting_options const *o);
311 
312 /* Use storage slot N to return a quoted version of the string ARG.
313    Use the default quoting options.
314    The returned value points to static storage that can be
315    reused by the next call to this function with the same value of N.
316    N must be nonnegative.  The output of all functions in the
317    quotearg_n family are guaranteed to not contain embedded null
318    bytes.*/
319 char *quotearg_n (int n, char const *arg);
320 
321 /* Equivalent to quotearg_n (0, ARG).  */
322 char *quotearg (char const *arg);
323 
324 /* Use storage slot N to return a quoted version of the argument ARG
325    of size ARGSIZE.  This is like quotearg_n (N, ARG), except it can
326    quote null bytes.  */
327 char *quotearg_n_mem (int n, char const *arg, size_t argsize);
328 
329 /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE).  */
330 char *quotearg_mem (char const *arg, size_t argsize);
331 
332 /* Use style S and storage slot N to return a quoted version of the string ARG.
333    This is like quotearg_n (N, ARG), except that it uses S with no other
334    options to specify the quoting method.  */
335 char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
336 
337 /* Use style S and storage slot N to return a quoted version of the
338    argument ARG of size ARGSIZE.  This is like quotearg_n_style
339    (N, S, ARG), except it can quote null bytes.  */
340 char *quotearg_n_style_mem (int n, enum quoting_style s,
341                             char const *arg, size_t argsize);
342 
343 /* Equivalent to quotearg_n_style (0, S, ARG).  */
344 char *quotearg_style (enum quoting_style s, char const *arg);
345 
346 /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE).  */
347 char *quotearg_style_mem (enum quoting_style s,
348                           char const *arg, size_t argsize);
349 
350 /* Like quotearg (ARG), except also quote any instances of CH.
351    See set_char_quoting for a description of acceptable CH values.  */
352 char *quotearg_char (char const *arg, char ch);
353 
354 /* Like quotearg_char (ARG, CH), except it can quote null bytes.  */
355 char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
356 
357 /* Equivalent to quotearg_char (ARG, ':').  */
358 char *quotearg_colon (char const *arg);
359 
360 /* Like quotearg_colon (ARG), except it can quote null bytes.  */
361 char *quotearg_colon_mem (char const *arg, size_t argsize);
362 
363 /* Like quotearg_n_style (N, S, ARG) but with S as custom_quoting_style
364    with left quote as LEFT_QUOTE and right quote as RIGHT_QUOTE.  See
365    set_custom_quoting for a description of acceptable LEFT_QUOTE and
366    RIGHT_QUOTE values.  */
367 char *quotearg_n_custom (int n, char const *left_quote,
368                          char const *right_quote, char const *arg);
369 
370 /* Like quotearg_n_custom (N, LEFT_QUOTE, RIGHT_QUOTE, ARG) except it
371    can quote null bytes.  */
372 char *quotearg_n_custom_mem (int n, char const *left_quote,
373                              char const *right_quote,
374                              char const *arg, size_t argsize);
375 
376 /* Equivalent to quotearg_n_custom (0, LEFT_QUOTE, RIGHT_QUOTE, ARG).  */
377 char *quotearg_custom (char const *left_quote, char const *right_quote,
378                        char const *arg);
379 
380 /* Equivalent to quotearg_n_custom_mem (0, LEFT_QUOTE, RIGHT_QUOTE, ARG,
381                                         ARGSIZE).  */
382 char *quotearg_custom_mem (char const *left_quote,
383                            char const *right_quote,
384                            char const *arg, size_t argsize);
385 
386 /* Free any dynamically allocated memory.  */
387 void quotearg_free (void);
388 
389 #endif /* !QUOTEARG_H_ */
390