exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / quotearg.h
blob202b79f33ef9ed10ecaab2c52840e12816daefe3
1 /* quotearg.h - quote arguments for output
3 Copyright (C) 1998-2002, 2004, 2006, 2008-2024 Free Software Foundation,
4 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 <https://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert <eggert@twinsun.com> */
21 #ifndef QUOTEARG_H_
22 # define QUOTEARG_H_ 1
24 /* This file uses _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_RETURNS_NONNULL. */
25 # if !_GL_CONFIG_H_INCLUDED
26 # error "Please include config.h first."
27 # endif
29 # include <stdlib.h>
31 /* Basic quoting styles. For each style, an example is given on the
32 input strings "simple", "\0 \t\n'\"\033?""?/\\", and "a:b", using
33 quotearg_buffer, quotearg_mem, and quotearg_colon_mem with that
34 style and the default flags and quoted characters. Note that the
35 examples are shown here as valid C strings rather than what
36 displays on a terminal (with "??/" as a trigraph for "\\"). */
37 enum quoting_style
39 /* Output names as-is (ls --quoting-style=literal). Can result in
40 embedded null bytes if QA_ELIDE_NULL_BYTES is not in
41 effect.
43 quotearg_buffer:
44 "simple", "\0 \t\n'\"\033??/\\", "a:b"
45 quotearg:
46 "simple", " \t\n'\"\033??/\\", "a:b"
47 quotearg_colon:
48 "simple", " \t\n'\"\033??/\\", "a:b"
50 literal_quoting_style,
52 /* Quote names for the shell if they contain shell metacharacters
53 or would cause ambiguous output (ls --quoting-style=shell).
54 Can result in embedded null bytes if QA_ELIDE_NULL_BYTES is not
55 in effect.
57 quotearg_buffer:
58 "simple", "'\0 \t\n'\\''\"\033??/\\'", "a:b"
59 quotearg:
60 "simple", "' \t\n'\\''\"\033??/\\'", "a:b"
61 quotearg_colon:
62 "simple", "' \t\n'\\''\"\033??/\\'", "'a:b'"
64 shell_quoting_style,
66 /* Quote names for the shell, even if they would normally not
67 require quoting (ls --quoting-style=shell-always). Can result
68 in embedded null bytes if QA_ELIDE_NULL_BYTES is not in effect.
69 Behaves like shell_quoting_style if QA_ELIDE_OUTER_QUOTES is in
70 effect.
72 quotearg_buffer:
73 "'simple'", "'\0 \t\n'\\''\"\033??/\\'", "'a:b'"
74 quotearg:
75 "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
76 quotearg_colon:
77 "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
79 shell_always_quoting_style,
81 /* Quote names for the shell if they contain shell metacharacters
82 or other problematic characters (ls --quoting-style=shell-escape).
83 Non printable characters are quoted using the $'...' syntax
84 <https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html>,
85 which originated in ksh93 and is widely supported by most shells,
86 and proposed for inclusion in POSIX.
88 quotearg_buffer:
89 "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\\'", "a:b"
90 quotearg:
91 "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\\'", "a:b"
92 quotearg_colon:
93 "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\\'", "'a:b'"
95 shell_escape_quoting_style,
97 /* Quote names for the shell even if they would normally not
98 require quoting (ls --quoting-style=shell-escape).
99 Non printable characters are quoted using the $'...' syntax
100 <https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html>,
101 which originated in ksh93 and is widely supported by most shells,
102 and proposed for inclusion in POSIX. Behaves like
103 shell_escape_quoting_style if QA_ELIDE_OUTER_QUOTES is in effect.
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'"
112 shell_escape_always_quoting_style,
114 /* Quote names as for a C language string (ls --quoting-style=c).
115 Behaves like c_maybe_quoting_style if QA_ELIDE_OUTER_QUOTES is
116 in effect. Split into consecutive strings if
117 QA_SPLIT_TRIGRAPHS.
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\""
126 c_quoting_style,
128 /* Like c_quoting_style except omit the surrounding double-quote
129 characters if no quoted characters are encountered.
131 quotearg_buffer:
132 "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
133 quotearg:
134 "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
135 quotearg_colon:
136 "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
138 c_maybe_quoting_style,
140 /* Like c_quoting_style except always omit the surrounding
141 double-quote characters and ignore QA_SPLIT_TRIGRAPHS
142 (ls --quoting-style=escape).
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"
151 escape_quoting_style,
153 /* Like clocale_quoting_style, but use single quotes in the
154 default C locale or if the program does not use gettext
155 (ls --quoting-style=locale). For UTF-8 locales, quote
156 characters will use Unicode.
158 LC_MESSAGES=C
159 quotearg_buffer:
160 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
161 quotearg:
162 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
163 quotearg_colon:
164 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a\\:b'"
166 LC_MESSAGES=pt_PT.utf8
167 quotearg_buffer:
168 "\302\253simple\302\273",
169 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
170 quotearg:
171 "\302\253simple\302\273",
172 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
173 quotearg_colon:
174 "\302\253simple\302\273",
175 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
177 locale_quoting_style,
179 /* Like c_quoting_style except use quotation marks appropriate for
180 the locale and ignore QA_SPLIT_TRIGRAPHS
181 (ls --quoting-style=clocale).
183 LC_MESSAGES=C
184 quotearg_buffer:
185 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
186 quotearg:
187 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
188 quotearg_colon:
189 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
191 LC_MESSAGES=pt_PT.utf8
192 quotearg_buffer:
193 "\302\253simple\302\273",
194 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
195 quotearg:
196 "\302\253simple\302\273",
197 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
198 quotearg_colon:
199 "\302\253simple\302\273",
200 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
202 clocale_quoting_style,
204 /* Like clocale_quoting_style except use the custom quotation marks
205 set by set_custom_quoting. If custom quotation marks are not
206 set, the behavior is undefined.
208 left_quote = right_quote = "'"
209 quotearg_buffer:
210 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
211 quotearg:
212 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
213 quotearg_colon:
214 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a\\:b'"
216 left_quote = "(" and right_quote = ")"
217 quotearg_buffer:
218 "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
219 quotearg:
220 "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
221 quotearg_colon:
222 "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a\\:b)"
224 left_quote = ":" and right_quote = " "
225 quotearg_buffer:
226 ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
227 quotearg:
228 ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
229 quotearg_colon:
230 ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a\\:b "
232 left_quote = "\"'" and right_quote = "'\""
233 Notice that this is treated as a single level of quotes or two
234 levels where the outer quote need not be escaped within the inner
235 quotes. For two levels where the outer quote must be escaped
236 within the inner quotes, you must use separate quotearg
237 invocations.
238 quotearg_buffer:
239 "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
240 quotearg:
241 "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
242 quotearg_colon:
243 "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a\\:b'\""
245 custom_quoting_style
248 /* Flags for use in set_quoting_flags. */
249 enum quoting_flags
251 /* Always elide null bytes from styles that do not quote them,
252 even when the length of the result is available to the
253 caller. */
254 QA_ELIDE_NULL_BYTES = 0x01,
256 /* Omit the surrounding quote characters if no escaped characters
257 are encountered. Note that if no other character needs
258 escaping, then neither does the escape character.
259 *Attention!* This flag is unsupported in combination with the styles
260 shell_escape_quoting_style and shell_escape_always_quoting_style
261 (because in this situation it cannot handle strings that start
262 with a non-printable character). */
263 QA_ELIDE_OUTER_QUOTES = 0x02,
265 /* In the c_quoting_style and c_maybe_quoting_style, split ANSI
266 trigraph sequences into concatenated strings (for example,
267 "?""?/" rather than "??/", which could be confused with
268 "\\"). */
269 QA_SPLIT_TRIGRAPHS = 0x04
272 /* For now, --quoting-style=literal is the default, but this may change. */
273 # ifndef DEFAULT_QUOTING_STYLE
274 # define DEFAULT_QUOTING_STYLE literal_quoting_style
275 # endif
277 /* Names of quoting styles and their corresponding values. */
278 extern char const *const quoting_style_args[];
279 extern enum quoting_style const quoting_style_vals[];
281 struct quoting_options;
283 /* The functions listed below set and use a hidden variable
284 that contains the default quoting style options. */
286 /* Allocate a new set of quoting options, with contents initially identical
287 to O if O is not null, or to the default if O is null.
288 It is the caller's responsibility to free the result. */
289 struct quoting_options *clone_quoting_options (struct quoting_options *o)
290 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
291 _GL_ATTRIBUTE_RETURNS_NONNULL;
293 /* Get the value of O's quoting style. If O is null, use the default. */
294 enum quoting_style get_quoting_style (struct quoting_options const *o);
296 /* In O (or in the default if O is null),
297 set the value of the quoting style to S. */
298 void set_quoting_style (struct quoting_options *o, enum quoting_style s);
300 /* In O (or in the default if O is null),
301 set the value of the quoting options for character C to I.
302 Return the old value. Currently, the only values defined for I are
303 0 (the default) and 1 (which means to quote the character even if
304 it would not otherwise be quoted). C must never be a digit or a
305 letter that has special meaning after a backslash (for example, "\t"
306 for tab). */
307 int set_char_quoting (struct quoting_options *o, char c, int i);
309 /* In O (or in the default if O is null),
310 set the value of the quoting options flag to I, which can be a
311 bitwise combination of enum quoting_flags, or 0 for default
312 behavior. Return the old value. */
313 int set_quoting_flags (struct quoting_options *o, int i);
315 /* In O (or in the default if O is null),
316 set the value of the quoting style to custom_quoting_style,
317 set the left quote to LEFT_QUOTE, and set the right quote to
318 RIGHT_QUOTE. Each of LEFT_QUOTE and RIGHT_QUOTE must be
319 null-terminated and can be the empty string. Because backslashes are
320 used for escaping, it does not make sense for RIGHT_QUOTE to contain
321 a backslash. RIGHT_QUOTE must not begin with a digit or a letter
322 that has special meaning after a backslash (for example, "\t" for
323 tab). */
324 void set_custom_quoting (struct quoting_options *o,
325 char const *left_quote,
326 char const *right_quote);
328 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
329 argument ARG (of size ARGSIZE), using O to control quoting.
330 If O is null, use the default.
331 Terminate the output with a null character, and return the written
332 size of the output, not counting the terminating null.
333 If BUFFERSIZE is too small to store the output string, return the
334 value that would have been returned had BUFFERSIZE been large enough.
335 If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
336 On output, BUFFER might contain embedded null bytes if ARGSIZE was
337 not -1, the style of O does not use backslash escapes, and the
338 flags of O do not request elision of null bytes.*/
339 size_t quotearg_buffer (char *restrict buffer, size_t buffersize,
340 char const *arg, size_t argsize,
341 struct quoting_options const *o);
343 /* Like quotearg_buffer, except return the result in a newly allocated
344 buffer. It is the caller's responsibility to free the result. The
345 result will not contain embedded null bytes. */
346 char *quotearg_alloc (char const *arg, size_t argsize,
347 struct quoting_options const *o)
348 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
349 _GL_ATTRIBUTE_RETURNS_NONNULL;
351 /* Like quotearg_alloc, except that the length of the result,
352 excluding the terminating null byte, is stored into SIZE if it is
353 non-NULL. The result might contain embedded null bytes if ARGSIZE
354 was not -1, SIZE was not NULL, the style of O does not use
355 backslash escapes, and the flags of O do not request elision of
356 null bytes.*/
357 char *quotearg_alloc_mem (char const *arg, size_t argsize,
358 size_t *size, struct quoting_options const *o)
359 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
360 _GL_ATTRIBUTE_RETURNS_NONNULL;
362 /* Use storage slot N to return a quoted version of the string ARG.
363 Use the default quoting options.
364 The returned value points to static storage that can be
365 reused by the next call to this function with the same value of N.
366 N must be nonnegative. The output of all functions in the
367 quotearg_n family are guaranteed to not contain embedded null
368 bytes.*/
369 char *quotearg_n (int n, char const *arg);
371 /* Equivalent to quotearg_n (0, ARG). */
372 char *quotearg (char const *arg);
374 /* Use storage slot N to return a quoted version of the argument ARG
375 of size ARGSIZE. This is like quotearg_n (N, ARG), except it can
376 quote null bytes. */
377 char *quotearg_n_mem (int n, char const *arg, size_t argsize);
379 /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE). */
380 char *quotearg_mem (char const *arg, size_t argsize);
382 /* Use style S and storage slot N to return a quoted version of the string ARG.
383 This is like quotearg_n (N, ARG), except that it uses S with no other
384 options to specify the quoting method. */
385 char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
387 /* Use style S and storage slot N to return a quoted version of the
388 argument ARG of size ARGSIZE. This is like quotearg_n_style
389 (N, S, ARG), except it can quote null bytes. */
390 char *quotearg_n_style_mem (int n, enum quoting_style s,
391 char const *arg, size_t argsize);
393 /* Equivalent to quotearg_n_style (0, S, ARG). */
394 char *quotearg_style (enum quoting_style s, char const *arg);
396 /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE). */
397 char *quotearg_style_mem (enum quoting_style s,
398 char const *arg, size_t argsize);
400 /* Like quotearg (ARG), except also quote any instances of CH.
401 See set_char_quoting for a description of acceptable CH values. */
402 char *quotearg_char (char const *arg, char ch);
404 /* Like quotearg_char (ARG, CH), except it can quote null bytes. */
405 char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
407 /* Equivalent to quotearg_char (ARG, ':'). */
408 char *quotearg_colon (char const *arg);
410 /* Like quotearg_colon (ARG), except it can quote null bytes. */
411 char *quotearg_colon_mem (char const *arg, size_t argsize);
413 /* Like quotearg_n_style, except with ':' quoting enabled. */
414 char *quotearg_n_style_colon (int n, enum quoting_style s, char const *arg);
416 /* Like quotearg_n_style (N, S, ARG) but with S as custom_quoting_style
417 with left quote as LEFT_QUOTE and right quote as RIGHT_QUOTE. See
418 set_custom_quoting for a description of acceptable LEFT_QUOTE and
419 RIGHT_QUOTE values. */
420 char *quotearg_n_custom (int n, char const *left_quote,
421 char const *right_quote, char const *arg);
423 /* Like quotearg_n_custom (N, LEFT_QUOTE, RIGHT_QUOTE, ARG) except it
424 can quote null bytes. */
425 char *quotearg_n_custom_mem (int n, char const *left_quote,
426 char const *right_quote,
427 char const *arg, size_t argsize);
429 /* Equivalent to quotearg_n_custom (0, LEFT_QUOTE, RIGHT_QUOTE, ARG). */
430 char *quotearg_custom (char const *left_quote, char const *right_quote,
431 char const *arg);
433 /* Equivalent to quotearg_n_custom_mem (0, LEFT_QUOTE, RIGHT_QUOTE, ARG,
434 ARGSIZE). */
435 char *quotearg_custom_mem (char const *left_quote,
436 char const *right_quote,
437 char const *arg, size_t argsize);
439 /* Free any dynamically allocated memory. */
440 void quotearg_free (void);
442 #endif /* !QUOTEARG_H_ */