Avoid trigraph quoting in default output.
[gnulib.git] / lib / quotearg.h
1 /* quotearg.h - quote arguments for output
2
3    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006, 2008 Free
4    Software Foundation, 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 (ls --quoting-style=escape).
104
105        quotearg_buffer:
106        "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
107        quotearg:
108        "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
109        quotearg_colon:
110        "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a\\:b"
111     */
112     escape_quoting_style,
113
114     /* Like clocale_quoting_style, but quote `like this' instead of
115        "like this" in the default C locale (ls --quoting-style=locale).
116
117        LC_MESSAGES=C
118        quotearg_buffer:
119        "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
120        quotearg:
121        "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
122        quotearg_colon:
123        "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a\\:b'"
124
125        LC_MESSAGES=pt_PT.utf8
126        quotearg_buffer:
127        "\302\253simple\302\273",
128        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
129        quotearg:
130        "\302\253simple\302\273",
131        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
132        quotearg_colon:
133        "\302\253simple\302\273",
134        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
135     */
136     locale_quoting_style,
137
138     /* Like c_quoting_style except use quotation marks appropriate for
139        the locale (ls --quoting-style=clocale).
140
141        LC_MESSAGES=C
142        quotearg_buffer:
143        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
144        quotearg:
145        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
146        quotearg_colon:
147        "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
148
149        LC_MESSAGES=pt_PT.utf8
150        quotearg_buffer:
151        "\302\253simple\302\273",
152        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
153        quotearg:
154        "\302\253simple\302\273",
155        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
156        quotearg_colon:
157        "\302\253simple\302\273",
158        "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
159     */
160     clocale_quoting_style
161   };
162
163 /* Flags for use in set_quoting_flags.  */
164 enum quoting_flags
165   {
166     /* Always elide null bytes from styles that do not quote them,
167        even when the length of the result is available to the
168        caller.  */
169     QA_ELIDE_NULL_BYTES = 0x01,
170
171     /* Omit the surrounding quote characters if no escaped characters
172        are encountered.  */
173     QA_ELIDE_OUTER_QUOTES = 0x02,
174
175     /* In the c_quoting_style and c_maybe_quoting_style, split ANSI
176        trigraph sequences into concatenated strings (for example,
177        "?""?/" rather than "??/", which could be confused with
178        "\\").  */
179     QA_SPLIT_TRIGRAPHS = 0x04
180   };
181
182 /* For now, --quoting-style=literal is the default, but this may change.  */
183 # ifndef DEFAULT_QUOTING_STYLE
184 #  define DEFAULT_QUOTING_STYLE literal_quoting_style
185 # endif
186
187 /* Names of quoting styles and their corresponding values.  */
188 extern char const *const quoting_style_args[];
189 extern enum quoting_style const quoting_style_vals[];
190
191 struct quoting_options;
192
193 /* The functions listed below set and use a hidden variable
194    that contains the default quoting style options.  */
195
196 /* Allocate a new set of quoting options, with contents initially identical
197    to O if O is not null, or to the default if O is null.
198    It is the caller's responsibility to free the result.  */
199 struct quoting_options *clone_quoting_options (struct quoting_options *o);
200
201 /* Get the value of O's quoting style.  If O is null, use the default.  */
202 enum quoting_style get_quoting_style (struct quoting_options *o);
203
204 /* In O (or in the default if O is null),
205    set the value of the quoting style to S.  */
206 void set_quoting_style (struct quoting_options *o, enum quoting_style s);
207
208 /* In O (or in the default if O is null),
209    set the value of the quoting options for character C to I.
210    Return the old value.  Currently, the only values defined for I are
211    0 (the default) and 1 (which means to quote the character even if
212    it would not otherwise be quoted).  */
213 int set_char_quoting (struct quoting_options *o, char c, int i);
214
215 /* In O (or in the default if O is null),
216    set the value of the quoting options flag to I, which can be a
217    bitwise combination of enum quoting_flags, or 0 for default
218    behavior.  Return the old value.  */
219 int set_quoting_flags (struct quoting_options *o, int i);
220
221 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
222    argument ARG (of size ARGSIZE), using O to control quoting.
223    If O is null, use the default.
224    Terminate the output with a null character, and return the written
225    size of the output, not counting the terminating null.
226    If BUFFERSIZE is too small to store the output string, return the
227    value that would have been returned had BUFFERSIZE been large enough.
228    If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
229    On output, BUFFER might contain embedded null bytes if ARGSIZE was
230    not -1, the style of O does not use backslash escapes, and the
231    flags of O do not request elision of null bytes.*/
232 size_t quotearg_buffer (char *buffer, size_t buffersize,
233                         char const *arg, size_t argsize,
234                         struct quoting_options const *o);
235
236 /* Like quotearg_buffer, except return the result in a newly allocated
237    buffer.  It is the caller's responsibility to free the result.  The
238    result will not contain embedded null bytes.  */
239 char *quotearg_alloc (char const *arg, size_t argsize,
240                       struct quoting_options const *o);
241
242 /* Like quotearg_alloc, except that the length of the result,
243    excluding the terminating null byte, is stored into SIZE if it is
244    non-NULL.  The result might contain embedded null bytes if ARGSIZE
245    was not -1, SIZE was not NULL, the style of O does not use
246    backslash escapes, and the flags of O do not request elision of
247    null bytes.*/
248 char *quotearg_alloc_mem (char const *arg, size_t argsize,
249                           size_t *size, struct quoting_options const *o);
250
251 /* Use storage slot N to return a quoted version of the string ARG.
252    Use the default quoting options.
253    The returned value points to static storage that can be
254    reused by the next call to this function with the same value of N.
255    N must be nonnegative.  The output of all functions in the
256    quotearg_n family are guaranteed to not contain embedded null
257    bytes.*/
258 char *quotearg_n (int n, char const *arg);
259
260 /* Equivalent to quotearg_n (0, ARG).  */
261 char *quotearg (char const *arg);
262
263 /* Use storage slot N to return a quoted version of the argument ARG
264    of size ARGSIZE.  This is like quotearg_n (N, ARG), except it can
265    quote null bytes.  */
266 char *quotearg_n_mem (int n, char const *arg, size_t argsize);
267
268 /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE).  */
269 char *quotearg_mem (char const *arg, size_t argsize);
270
271 /* Use style S and storage slot N to return a quoted version of the string ARG.
272    This is like quotearg_n (N, ARG), except that it uses S with no other
273    options to specify the quoting method.  */
274 char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
275
276 /* Use style S and storage slot N to return a quoted version of the
277    argument ARG of size ARGSIZE.  This is like quotearg_n_style
278    (N, S, ARG), except it can quote null bytes.  */
279 char *quotearg_n_style_mem (int n, enum quoting_style s,
280                             char const *arg, size_t argsize);
281
282 /* Equivalent to quotearg_n_style (0, S, ARG).  */
283 char *quotearg_style (enum quoting_style s, char const *arg);
284
285 /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE).  */
286 char *quotearg_style_mem (enum quoting_style s,
287                           char const *arg, size_t argsize);
288
289 /* Like quotearg (ARG), except also quote any instances of CH.  */
290 char *quotearg_char (char const *arg, char ch);
291
292 /* Like quotearg_char (ARG, CH), except it can quote null bytes.  */
293 char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
294
295 /* Equivalent to quotearg_char (ARG, ':').  */
296 char *quotearg_colon (char const *arg);
297
298 /* Like quotearg_colon (ARG), except it can quote null bytes.  */
299 char *quotearg_colon_mem (char const *arg, size_t argsize);
300
301 /* Free any dynamically allocated memory.  */
302 void quotearg_free (void);
303
304 #endif /* !QUOTEARG_H_ */