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