errno-h: document Haiku errors can’t be -1
[gnulib.git] / lib / quotearg.h
blob27a098944959bfbb47f8eeade6194f0d39abe7d5
1 /* quotearg.h - quote arguments for output
3 Copyright (C) 1998-2002, 2004, 2006, 2008-2025 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 #ifdef __cplusplus
32 extern "C" {
33 #endif
36 /* Basic quoting styles. For each style, an example is given on the
37 input strings "simple", "\0 \t\n'\"\033?""?/\\", and "a:b", using
38 quotearg_buffer, quotearg_mem, and quotearg_colon_mem with that
39 style and the default flags and quoted characters. Note that the
40 examples are shown here as valid C strings rather than what
41 displays on a terminal (with "??/" as a trigraph for "\\"). */
42 enum quoting_style
44 /* Output names as-is (ls --quoting-style=literal). Can result in
45 embedded null bytes if QA_ELIDE_NULL_BYTES is not in
46 effect.
48 quotearg_buffer:
49 "simple", "\0 \t\n'\"\033??/\\", "a:b"
50 quotearg:
51 "simple", " \t\n'\"\033??/\\", "a:b"
52 quotearg_colon:
53 "simple", " \t\n'\"\033??/\\", "a:b"
55 literal_quoting_style,
57 /* Quote names for the shell if they contain shell metacharacters
58 or would cause ambiguous output (ls --quoting-style=shell).
59 Can result in embedded null bytes if QA_ELIDE_NULL_BYTES is not
60 in effect.
62 quotearg_buffer:
63 "simple", "'\0 \t\n'\\''\"\033??/\\'", "a:b"
64 quotearg:
65 "simple", "' \t\n'\\''\"\033??/\\'", "a:b"
66 quotearg_colon:
67 "simple", "' \t\n'\\''\"\033??/\\'", "'a:b'"
69 shell_quoting_style,
71 /* Quote names for the shell, even if they would normally not
72 require quoting (ls --quoting-style=shell-always). Can result
73 in embedded null bytes if QA_ELIDE_NULL_BYTES is not in effect.
74 Behaves like shell_quoting_style if QA_ELIDE_OUTER_QUOTES is in
75 effect.
77 quotearg_buffer:
78 "'simple'", "'\0 \t\n'\\''\"\033??/\\'", "'a:b'"
79 quotearg:
80 "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
81 quotearg_colon:
82 "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
84 shell_always_quoting_style,
86 /* Quote names for the shell if they contain shell metacharacters
87 or other problematic characters (ls --quoting-style=shell-escape).
88 Non printable characters are quoted using the $'...' syntax
89 <https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html>,
90 which originated in ksh93 and is widely supported by most shells,
91 and proposed for inclusion in POSIX.
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'"
100 shell_escape_quoting_style,
102 /* Quote names for the shell even if they would normally not
103 require quoting (ls --quoting-style=shell-escape).
104 Non printable characters are quoted using the $'...' syntax
105 <https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html>,
106 which originated in ksh93 and is widely supported by most shells,
107 and proposed for inclusion in POSIX. Behaves like
108 shell_escape_quoting_style if QA_ELIDE_OUTER_QUOTES is in effect.
110 quotearg_buffer:
111 "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\'", "a:b"
112 quotearg:
113 "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\'", "a:b"
114 quotearg_colon:
115 "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\'", "'a:b'"
117 shell_escape_always_quoting_style,
119 /* Quote names as for a C language string (ls --quoting-style=c).
120 Behaves like c_maybe_quoting_style if QA_ELIDE_OUTER_QUOTES is
121 in effect. Split into consecutive strings if
122 QA_SPLIT_TRIGRAPHS.
124 quotearg_buffer:
125 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
126 quotearg:
127 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
128 quotearg_colon:
129 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
131 c_quoting_style,
133 /* Like c_quoting_style except omit the surrounding double-quote
134 characters if no quoted characters are encountered.
136 quotearg_buffer:
137 "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
138 quotearg:
139 "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
140 quotearg_colon:
141 "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
143 c_maybe_quoting_style,
145 /* Like c_quoting_style except always omit the surrounding
146 double-quote characters and ignore QA_SPLIT_TRIGRAPHS
147 (ls --quoting-style=escape).
149 quotearg_buffer:
150 "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
151 quotearg:
152 "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
153 quotearg_colon:
154 "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a\\:b"
156 escape_quoting_style,
158 /* Like clocale_quoting_style, but use single quotes in the
159 default C locale or if the program does not use gettext
160 (ls --quoting-style=locale). For UTF-8 locales, quote
161 characters will use Unicode.
163 LC_MESSAGES=C
164 quotearg_buffer:
165 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
166 quotearg:
167 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
168 quotearg_colon:
169 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a\\:b'"
171 LC_MESSAGES=pt_PT.utf8
172 quotearg_buffer:
173 "\302\253simple\302\273",
174 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
175 quotearg:
176 "\302\253simple\302\273",
177 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
178 quotearg_colon:
179 "\302\253simple\302\273",
180 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
182 locale_quoting_style,
184 /* Like c_quoting_style except use quotation marks appropriate for
185 the locale and ignore QA_SPLIT_TRIGRAPHS
186 (ls --quoting-style=clocale).
188 LC_MESSAGES=C
189 quotearg_buffer:
190 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
191 quotearg:
192 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
193 quotearg_colon:
194 "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
196 LC_MESSAGES=pt_PT.utf8
197 quotearg_buffer:
198 "\302\253simple\302\273",
199 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
200 quotearg:
201 "\302\253simple\302\273",
202 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
203 quotearg_colon:
204 "\302\253simple\302\273",
205 "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
207 clocale_quoting_style,
209 /* Like clocale_quoting_style except use the custom quotation marks
210 set by set_custom_quoting. If custom quotation marks are not
211 set, the behavior is undefined.
213 left_quote = right_quote = "'"
214 quotearg_buffer:
215 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
216 quotearg:
217 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
218 quotearg_colon:
219 "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a\\:b'"
221 left_quote = "(" and right_quote = ")"
222 quotearg_buffer:
223 "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
224 quotearg:
225 "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
226 quotearg_colon:
227 "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a\\:b)"
229 left_quote = ":" and right_quote = " "
230 quotearg_buffer:
231 ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
232 quotearg:
233 ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
234 quotearg_colon:
235 ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a\\:b "
237 left_quote = "\"'" and right_quote = "'\""
238 Notice that this is treated as a single level of quotes or two
239 levels where the outer quote need not be escaped within the inner
240 quotes. For two levels where the outer quote must be escaped
241 within the inner quotes, you must use separate quotearg
242 invocations.
243 quotearg_buffer:
244 "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
245 quotearg:
246 "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
247 quotearg_colon:
248 "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a\\:b'\""
250 custom_quoting_style
253 /* Flags for use in set_quoting_flags. */
254 enum quoting_flags
256 /* Always elide null bytes from styles that do not quote them,
257 even when the length of the result is available to the
258 caller. */
259 QA_ELIDE_NULL_BYTES = 0x01,
261 /* Omit the surrounding quote characters if no escaped characters
262 are encountered. Note that if no other character needs
263 escaping, then neither does the escape character.
264 *Attention!* This flag is unsupported in combination with the styles
265 shell_escape_quoting_style and shell_escape_always_quoting_style
266 (because in this situation it cannot handle strings that start
267 with a non-printable character). */
268 QA_ELIDE_OUTER_QUOTES = 0x02,
270 /* In the c_quoting_style and c_maybe_quoting_style, split ANSI
271 trigraph sequences into concatenated strings (for example,
272 "?""?/" rather than "??/", which could be confused with
273 "\\"). */
274 QA_SPLIT_TRIGRAPHS = 0x04
277 /* For now, --quoting-style=literal is the default, but this may change. */
278 #ifndef DEFAULT_QUOTING_STYLE
279 # define DEFAULT_QUOTING_STYLE literal_quoting_style
280 #endif
282 /* Names of quoting styles and their corresponding values. */
283 extern char const *const quoting_style_args[];
284 extern enum quoting_style const quoting_style_vals[];
286 struct quoting_options;
288 /* The functions listed below set and use a hidden variable
289 that contains the default quoting style options. */
291 /* Allocate a new set of quoting options, with contents initially identical
292 to O if O is not null, or to the default if O is null.
293 It is the caller's responsibility to free the result. */
294 struct quoting_options *clone_quoting_options (struct quoting_options *o)
295 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
296 _GL_ATTRIBUTE_RETURNS_NONNULL;
298 /* Get the value of O's quoting style. If O is null, use the default. */
299 enum quoting_style get_quoting_style (struct quoting_options const *o);
301 /* In O (or in the default if O is null),
302 set the value of the quoting style to S. */
303 void set_quoting_style (struct quoting_options *o, enum quoting_style s);
305 /* In O (or in the default if O is null),
306 set the value of the quoting options for character C to I.
307 Return the old value. Currently, the only values defined for I are
308 0 (the default) and 1 (which means to quote the character even if
309 it would not otherwise be quoted). C must never be a digit or a
310 letter that has special meaning after a backslash (for example, "\t"
311 for tab). */
312 int set_char_quoting (struct quoting_options *o, char c, int i);
314 /* In O (or in the default if O is null),
315 set the value of the quoting options flag to I, which can be a
316 bitwise combination of enum quoting_flags, or 0 for default
317 behavior. Return the old value. */
318 int set_quoting_flags (struct quoting_options *o, int i);
320 /* In O (or in the default if O is null),
321 set the value of the quoting style to custom_quoting_style,
322 set the left quote to LEFT_QUOTE, and set the right quote to
323 RIGHT_QUOTE. Each of LEFT_QUOTE and RIGHT_QUOTE must be
324 null-terminated and can be the empty string. Because backslashes are
325 used for escaping, it does not make sense for RIGHT_QUOTE to contain
326 a backslash. RIGHT_QUOTE must not begin with a digit or a letter
327 that has special meaning after a backslash (for example, "\t" for
328 tab). */
329 void set_custom_quoting (struct quoting_options *o,
330 char const *left_quote,
331 char const *right_quote);
333 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
334 argument ARG (of size ARGSIZE), using O to control quoting.
335 If O is null, use the default.
336 Terminate the output with a null character, and return the written
337 size of the output, not counting the terminating null.
338 If BUFFERSIZE is too small to store the output string, return the
339 value that would have been returned had BUFFERSIZE been large enough.
340 If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
341 On output, BUFFER might contain embedded null bytes if ARGSIZE was
342 not -1, the style of O does not use backslash escapes, and the
343 flags of O do not request elision of null bytes.*/
344 size_t quotearg_buffer (char *restrict buffer, size_t buffersize,
345 char const *arg, size_t argsize,
346 struct quoting_options const *o);
348 /* Like quotearg_buffer, except return the result in a newly allocated
349 buffer. It is the caller's responsibility to free the result. The
350 result will not contain embedded null bytes. */
351 char *quotearg_alloc (char const *arg, size_t argsize,
352 struct quoting_options const *o)
353 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
354 _GL_ATTRIBUTE_RETURNS_NONNULL;
356 /* Like quotearg_alloc, except that the length of the result,
357 excluding the terminating null byte, is stored into SIZE if it is
358 non-NULL. The result might contain embedded null bytes if ARGSIZE
359 was not -1, SIZE was not NULL, the style of O does not use
360 backslash escapes, and the flags of O do not request elision of
361 null bytes.*/
362 char *quotearg_alloc_mem (char const *arg, size_t argsize,
363 size_t *size, struct quoting_options const *o)
364 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
365 _GL_ATTRIBUTE_RETURNS_NONNULL;
367 /* Use storage slot N to return a quoted version of the string ARG.
368 Use the default quoting options.
369 The returned value points to static storage that can be
370 reused by the next call to this function with the same value of N.
371 N must be nonnegative. The output of all functions in the
372 quotearg_n family are guaranteed to not contain embedded null
373 bytes.*/
374 char *quotearg_n (int n, char const *arg);
376 /* Equivalent to quotearg_n (0, ARG). */
377 char *quotearg (char const *arg);
379 /* Use storage slot N to return a quoted version of the argument ARG
380 of size ARGSIZE. This is like quotearg_n (N, ARG), except it can
381 quote null bytes. */
382 char *quotearg_n_mem (int n, char const *arg, size_t argsize);
384 /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE). */
385 char *quotearg_mem (char const *arg, size_t argsize);
387 /* Use style S and storage slot N to return a quoted version of the string ARG.
388 This is like quotearg_n (N, ARG), except that it uses S with no other
389 options to specify the quoting method. */
390 char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
392 /* Use style S and storage slot N to return a quoted version of the
393 argument ARG of size ARGSIZE. This is like quotearg_n_style
394 (N, S, ARG), except it can quote null bytes. */
395 char *quotearg_n_style_mem (int n, enum quoting_style s,
396 char const *arg, size_t argsize);
398 /* Equivalent to quotearg_n_style (0, S, ARG). */
399 char *quotearg_style (enum quoting_style s, char const *arg);
401 /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE). */
402 char *quotearg_style_mem (enum quoting_style s,
403 char const *arg, size_t argsize);
405 /* Like quotearg (ARG), except also quote any instances of CH.
406 See set_char_quoting for a description of acceptable CH values. */
407 char *quotearg_char (char const *arg, char ch);
409 /* Like quotearg_char (ARG, CH), except it can quote null bytes. */
410 char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
412 /* Equivalent to quotearg_char (ARG, ':'). */
413 char *quotearg_colon (char const *arg);
415 /* Like quotearg_colon (ARG), except it can quote null bytes. */
416 char *quotearg_colon_mem (char const *arg, size_t argsize);
418 /* Like quotearg_n_style, except with ':' quoting enabled. */
419 char *quotearg_n_style_colon (int n, enum quoting_style s, char const *arg);
421 /* Like quotearg_n_style (N, S, ARG) but with S as custom_quoting_style
422 with left quote as LEFT_QUOTE and right quote as RIGHT_QUOTE. See
423 set_custom_quoting for a description of acceptable LEFT_QUOTE and
424 RIGHT_QUOTE values. */
425 char *quotearg_n_custom (int n, char const *left_quote,
426 char const *right_quote, char const *arg);
428 /* Like quotearg_n_custom (N, LEFT_QUOTE, RIGHT_QUOTE, ARG) except it
429 can quote null bytes. */
430 char *quotearg_n_custom_mem (int n, char const *left_quote,
431 char const *right_quote,
432 char const *arg, size_t argsize);
434 /* Equivalent to quotearg_n_custom (0, LEFT_QUOTE, RIGHT_QUOTE, ARG). */
435 char *quotearg_custom (char const *left_quote, char const *right_quote,
436 char const *arg);
438 /* Equivalent to quotearg_n_custom_mem (0, LEFT_QUOTE, RIGHT_QUOTE, ARG,
439 ARGSIZE). */
440 char *quotearg_custom_mem (char const *left_quote,
441 char const *right_quote,
442 char const *arg, size_t argsize);
444 /* Free any dynamically allocated memory. */
445 void quotearg_free (void);
448 #ifdef __cplusplus
450 #endif
452 #endif /* !QUOTEARG_H_ */