| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__ |
| 24 |
# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure" |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <config.h> |
| 28 |
|
| 29 |
#include "quotearg.h" |
| 30 |
#include "quote.h" |
| 31 |
|
| 32 |
#include "xalloc.h" |
| 33 |
#include "c-strcaseeq.h" |
| 34 |
#include "localcharset.h" |
| 35 |
|
| 36 |
#include <ctype.h> |
| 37 |
#include <errno.h> |
| 38 |
#include <limits.h> |
| 39 |
#include <stdbool.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
#include <wchar.h> |
| 43 |
#include <wctype.h> |
| 44 |
|
| 45 |
#include "gettext.h" |
| 46 |
#define _(msgid) gettext (msgid) |
| 47 |
#define N_(msgid) msgid |
| 48 |
|
| 49 |
#ifndef SIZE_MAX |
| 50 |
# define SIZE_MAX ((size_t) -1) |
| 51 |
#endif |
| 52 |
|
| 53 |
#define INT_BITS (sizeof (int) * CHAR_BIT) |
| 54 |
|
| 55 |
struct quoting_options |
| 56 |
{ |
| 57 |
|
| 58 |
enum quoting_style style; |
| 59 |
|
| 60 |
|
| 61 |
int flags; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
unsigned int quote_these_too[(UCHAR_MAX / INT_BITS) + 1]; |
| 66 |
|
| 67 |
|
| 68 |
char const *left_quote; |
| 69 |
|
| 70 |
|
| 71 |
char const *right_quote; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
char const *const quoting_style_args[] = |
| 76 |
{ |
| 77 |
"literal", |
| 78 |
"shell", |
| 79 |
"shell-always", |
| 80 |
"c", |
| 81 |
"c-maybe", |
| 82 |
"escape", |
| 83 |
"locale", |
| 84 |
"clocale", |
| 85 |
0 |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
enum quoting_style const quoting_style_vals[] = |
| 90 |
{ |
| 91 |
literal_quoting_style, |
| 92 |
shell_quoting_style, |
| 93 |
shell_always_quoting_style, |
| 94 |
c_quoting_style, |
| 95 |
c_maybe_quoting_style, |
| 96 |
escape_quoting_style, |
| 97 |
locale_quoting_style, |
| 98 |
clocale_quoting_style |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
static struct quoting_options default_quoting_options; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
struct quoting_options * |
| 108 |
clone_quoting_options (struct quoting_options *o) |
| 109 |
{ |
| 110 |
int e = errno; |
| 111 |
struct quoting_options *p = xmemdup (o ? o : &default_quoting_options, |
| 112 |
sizeof *o); |
| 113 |
errno = e; |
| 114 |
return p; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
enum quoting_style |
| 119 |
get_quoting_style (struct quoting_options *o) |
| 120 |
{ |
| 121 |
return (o ? o : &default_quoting_options)->style; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
void |
| 127 |
set_quoting_style (struct quoting_options *o, enum quoting_style s) |
| 128 |
{ |
| 129 |
(o ? o : &default_quoting_options)->style = s; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
int |
| 138 |
set_char_quoting (struct quoting_options *o, char c, int i) |
| 139 |
{ |
| 140 |
unsigned char uc = c; |
| 141 |
unsigned int *p = |
| 142 |
(o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS; |
| 143 |
int shift = uc % INT_BITS; |
| 144 |
int r = (*p >> shift) & 1; |
| 145 |
*p ^= ((i & 1) ^ r) << shift; |
| 146 |
return r; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
int |
| 154 |
set_quoting_flags (struct quoting_options *o, int i) |
| 155 |
{ |
| 156 |
int r; |
| 157 |
if (!o) |
| 158 |
o = &default_quoting_options; |
| 159 |
r = o->flags; |
| 160 |
o->flags = i; |
| 161 |
return r; |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
set_custom_quoting (struct quoting_options *o, |
| 166 |
char const *left_quote, char const *right_quote) |
| 167 |
{ |
| 168 |
if (!o) |
| 169 |
o = &default_quoting_options; |
| 170 |
o->style = custom_quoting_style; |
| 171 |
if (!left_quote || !right_quote) |
| 172 |
abort (); |
| 173 |
o->left_quote = left_quote; |
| 174 |
o->right_quote = right_quote; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
static struct quoting_options |
| 179 |
quoting_options_from_style (enum quoting_style style) |
| 180 |
{ |
| 181 |
struct quoting_options o = { 0, 0, { 0 }, NULL, NULL }; |
| 182 |
if (style == custom_quoting_style) |
| 183 |
abort (); |
| 184 |
o.style = style; |
| 185 |
return o; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
static char const * |
| 193 |
gettext_quote (char const *msgid, enum quoting_style s) |
| 194 |
{ |
| 195 |
char const *translation = _(msgid); |
| 196 |
char const *locale_code; |
| 197 |
|
| 198 |
if (translation != msgid) |
| 199 |
return translation; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
locale_code = locale_charset (); |
| 220 |
if (STRCASEEQ (locale_code, "UTF-8", 'U','T','F','-','8',0,0,0,0)) |
| 221 |
return msgid[0] == '`' ? "\xe2\x80\x98": "\xe2\x80\x99"; |
| 222 |
if (STRCASEEQ (locale_code, "GB18030", 'G','B','1','8','0','3','0',0,0)) |
| 223 |
return msgid[0] == '`' ? "\xa1\ae": "\xa1\xaf"; |
| 224 |
|
| 225 |
return (s == clocale_quoting_style ? "\"" : "'"); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
static size_t |
| 242 |
quotearg_buffer_restyled (char *buffer, size_t buffersize, |
| 243 |
char const *arg, size_t argsize, |
| 244 |
enum quoting_style quoting_style, int flags, |
| 245 |
unsigned int const *quote_these_too, |
| 246 |
char const *left_quote, |
| 247 |
char const *right_quote) |
| 248 |
{ |
| 249 |
size_t i; |
| 250 |
size_t len = 0; |
| 251 |
char const *quote_string = 0; |
| 252 |
size_t quote_string_len = 0; |
| 253 |
bool backslash_escapes = false; |
| 254 |
bool unibyte_locale = MB_CUR_MAX == 1; |
| 255 |
bool elide_outer_quotes = (flags & QA_ELIDE_OUTER_QUOTES) != 0; |
| 256 |
|
| 257 |
#define STORE(c) \ |
| 258 |
do \ |
| 259 |
{ \ |
| 260 |
if (len < buffersize) \ |
| 261 |
buffer[len] = (c); \ |
| 262 |
len++; \ |
| 263 |
} \ |
| 264 |
while (0) |
| 265 |
|
| 266 |
switch (quoting_style) |
| 267 |
{ |
| 268 |
case c_maybe_quoting_style: |
| 269 |
quoting_style = c_quoting_style; |
| 270 |
elide_outer_quotes = true; |
| 271 |
|
| 272 |
case c_quoting_style: |
| 273 |
if (!elide_outer_quotes) |
| 274 |
STORE ('"'); |
| 275 |
backslash_escapes = true; |
| 276 |
quote_string = "\""; |
| 277 |
quote_string_len = 1; |
| 278 |
break; |
| 279 |
|
| 280 |
case escape_quoting_style: |
| 281 |
backslash_escapes = true; |
| 282 |
elide_outer_quotes = false; |
| 283 |
break; |
| 284 |
|
| 285 |
case locale_quoting_style: |
| 286 |
case clocale_quoting_style: |
| 287 |
case custom_quoting_style: |
| 288 |
{ |
| 289 |
if (quoting_style != custom_quoting_style) |
| 290 |
{ |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
left_quote = gettext_quote (N_("`"), quoting_style); |
| 313 |
right_quote = gettext_quote (N_("'"), quoting_style); |
| 314 |
} |
| 315 |
if (!elide_outer_quotes) |
| 316 |
for (quote_string = left_quote; *quote_string; quote_string++) |
| 317 |
STORE (*quote_string); |
| 318 |
backslash_escapes = true; |
| 319 |
quote_string = right_quote; |
| 320 |
quote_string_len = strlen (quote_string); |
| 321 |
} |
| 322 |
break; |
| 323 |
|
| 324 |
case shell_quoting_style: |
| 325 |
quoting_style = shell_always_quoting_style; |
| 326 |
elide_outer_quotes = true; |
| 327 |
|
| 328 |
case shell_always_quoting_style: |
| 329 |
if (!elide_outer_quotes) |
| 330 |
STORE ('\''); |
| 331 |
quote_string = "'"; |
| 332 |
quote_string_len = 1; |
| 333 |
break; |
| 334 |
|
| 335 |
case literal_quoting_style: |
| 336 |
elide_outer_quotes = false; |
| 337 |
break; |
| 338 |
|
| 339 |
default: |
| 340 |
abort (); |
| 341 |
} |
| 342 |
|
| 343 |
for (i = 0; ! (argsize == SIZE_MAX ? arg[i] == '\0' : i == argsize); i++) |
| 344 |
{ |
| 345 |
unsigned char c; |
| 346 |
unsigned char esc; |
| 347 |
bool is_right_quote = false; |
| 348 |
|
| 349 |
if (backslash_escapes |
| 350 |
&& quote_string_len |
| 351 |
&& i + quote_string_len <= argsize |
| 352 |
&& memcmp (arg + i, quote_string, quote_string_len) == 0) |
| 353 |
{ |
| 354 |
if (elide_outer_quotes) |
| 355 |
goto force_outer_quoting_style; |
| 356 |
is_right_quote = true; |
| 357 |
} |
| 358 |
|
| 359 |
c = arg[i]; |
| 360 |
switch (c) |
| 361 |
{ |
| 362 |
case '\0': |
| 363 |
if (backslash_escapes) |
| 364 |
{ |
| 365 |
if (elide_outer_quotes) |
| 366 |
goto force_outer_quoting_style; |
| 367 |
STORE ('\\'); |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if (i + 1 < argsize && '0' <= arg[i + 1] && arg[i + 1] <= '9') |
| 374 |
{ |
| 375 |
STORE ('0'); |
| 376 |
STORE ('0'); |
| 377 |
} |
| 378 |
c = '0'; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
} |
| 384 |
else if (flags & QA_ELIDE_NULL_BYTES) |
| 385 |
continue; |
| 386 |
break; |
| 387 |
|
| 388 |
case '?': |
| 389 |
switch (quoting_style) |
| 390 |
{ |
| 391 |
case shell_always_quoting_style: |
| 392 |
if (elide_outer_quotes) |
| 393 |
goto force_outer_quoting_style; |
| 394 |
break; |
| 395 |
|
| 396 |
case c_quoting_style: |
| 397 |
if ((flags & QA_SPLIT_TRIGRAPHS) |
| 398 |
&& i + 2 < argsize && arg[i + 1] == '?') |
| 399 |
switch (arg[i + 2]) |
| 400 |
{ |
| 401 |
case '!': case '\'': |
| 402 |
case '(': case ')': case '-': case '/': |
| 403 |
case '<': case '=': case '>': |
| 404 |
|
| 405 |
|
| 406 |
if (elide_outer_quotes) |
| 407 |
goto force_outer_quoting_style; |
| 408 |
c = arg[i + 2]; |
| 409 |
i += 2; |
| 410 |
STORE ('?'); |
| 411 |
STORE ('"'); |
| 412 |
STORE ('"'); |
| 413 |
STORE ('?'); |
| 414 |
break; |
| 415 |
|
| 416 |
default: |
| 417 |
break; |
| 418 |
} |
| 419 |
break; |
| 420 |
|
| 421 |
default: |
| 422 |
break; |
| 423 |
} |
| 424 |
break; |
| 425 |
|
| 426 |
case '\a': esc = 'a'; goto c_escape; |
| 427 |
case '\b': esc = 'b'; goto c_escape; |
| 428 |
case '\f': esc = 'f'; goto c_escape; |
| 429 |
case '\n': esc = 'n'; goto c_and_shell_escape; |
| 430 |
case '\r': esc = 'r'; goto c_and_shell_escape; |
| 431 |
case '\t': esc = 't'; goto c_and_shell_escape; |
| 432 |
case '\v': esc = 'v'; goto c_escape; |
| 433 |
case '\\': esc = c; |
| 434 |
|
| 435 |
|
| 436 |
if (backslash_escapes && elide_outer_quotes && quote_string_len) |
| 437 |
goto store_c; |
| 438 |
|
| 439 |
c_and_shell_escape: |
| 440 |
if (quoting_style == shell_always_quoting_style |
| 441 |
&& elide_outer_quotes) |
| 442 |
goto force_outer_quoting_style; |
| 443 |
|
| 444 |
c_escape: |
| 445 |
if (backslash_escapes) |
| 446 |
{ |
| 447 |
c = esc; |
| 448 |
goto store_escape; |
| 449 |
} |
| 450 |
break; |
| 451 |
|
| 452 |
case '{': case '}': |
| 453 |
if (! (argsize == SIZE_MAX ? arg[1] == '\0' : argsize == 1)) |
| 454 |
break; |
| 455 |
|
| 456 |
case '#': case '~': |
| 457 |
if (i != 0) |
| 458 |
break; |
| 459 |
|
| 460 |
case ' ': |
| 461 |
case '!': |
| 462 |
case '"': case '$': case '&': |
| 463 |
case '(': case ')': case '*': case ';': |
| 464 |
case '<': |
| 465 |
case '=': |
| 466 |
case '>': case '[': |
| 467 |
case '^': |
| 468 |
case '`': case '|': |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
if (quoting_style == shell_always_quoting_style |
| 474 |
&& elide_outer_quotes) |
| 475 |
goto force_outer_quoting_style; |
| 476 |
break; |
| 477 |
|
| 478 |
case '\'': |
| 479 |
if (quoting_style == shell_always_quoting_style) |
| 480 |
{ |
| 481 |
if (elide_outer_quotes) |
| 482 |
goto force_outer_quoting_style; |
| 483 |
STORE ('\''); |
| 484 |
STORE ('\\'); |
| 485 |
STORE ('\''); |
| 486 |
} |
| 487 |
break; |
| 488 |
|
| 489 |
case '%': case '+': case ',': case '-': case '.': case '/': |
| 490 |
case '0': case '1': case '2': case '3': case '4': case '5': |
| 491 |
case '6': case '7': case '8': case '9': case ':': |
| 492 |
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': |
| 493 |
case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': |
| 494 |
case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': |
| 495 |
case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': |
| 496 |
case 'Y': case 'Z': case ']': case '_': case 'a': case 'b': |
| 497 |
case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': |
| 498 |
case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': |
| 499 |
case 'o': case 'p': case 'q': case 'r': case 's': case 't': |
| 500 |
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
break; |
| 512 |
|
| 513 |
default: |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
{ |
| 520 |
|
| 521 |
size_t m; |
| 522 |
|
| 523 |
bool printable; |
| 524 |
|
| 525 |
if (unibyte_locale) |
| 526 |
{ |
| 527 |
m = 1; |
| 528 |
printable = isprint (c) != 0; |
| 529 |
} |
| 530 |
else |
| 531 |
{ |
| 532 |
mbstate_t mbstate; |
| 533 |
memset (&mbstate, 0, sizeof mbstate); |
| 534 |
|
| 535 |
m = 0; |
| 536 |
printable = true; |
| 537 |
if (argsize == SIZE_MAX) |
| 538 |
argsize = strlen (arg); |
| 539 |
|
| 540 |
do |
| 541 |
{ |
| 542 |
wchar_t w; |
| 543 |
size_t bytes = mbrtowc (&w, &arg[i + m], |
| 544 |
argsize - (i + m), &mbstate); |
| 545 |
if (bytes == 0) |
| 546 |
break; |
| 547 |
else if (bytes == (size_t) -1) |
| 548 |
{ |
| 549 |
printable = false; |
| 550 |
break; |
| 551 |
} |
| 552 |
else if (bytes == (size_t) -2) |
| 553 |
{ |
| 554 |
printable = false; |
| 555 |
while (i + m < argsize && arg[i + m]) |
| 556 |
m++; |
| 557 |
break; |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
if ('[' == 0x5b && elide_outer_quotes |
| 566 |
&& quoting_style == shell_always_quoting_style) |
| 567 |
{ |
| 568 |
size_t j; |
| 569 |
for (j = 1; j < bytes; j++) |
| 570 |
switch (arg[i + m + j]) |
| 571 |
{ |
| 572 |
case '[': case '\\': case '^': |
| 573 |
case '`': case '|': |
| 574 |
goto force_outer_quoting_style; |
| 575 |
|
| 576 |
default: |
| 577 |
break; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
if (! iswprint (w)) |
| 582 |
printable = false; |
| 583 |
m += bytes; |
| 584 |
} |
| 585 |
} |
| 586 |
while (! mbsinit (&mbstate)); |
| 587 |
} |
| 588 |
|
| 589 |
if (1 < m || (backslash_escapes && ! printable)) |
| 590 |
{ |
| 591 |
|
| 592 |
|
| 593 |
size_t ilim = i + m; |
| 594 |
|
| 595 |
for (;;) |
| 596 |
{ |
| 597 |
if (backslash_escapes && ! printable) |
| 598 |
{ |
| 599 |
if (elide_outer_quotes) |
| 600 |
goto force_outer_quoting_style; |
| 601 |
STORE ('\\'); |
| 602 |
STORE ('0' + (c >> 6)); |
| 603 |
STORE ('0' + ((c >> 3) & 7)); |
| 604 |
c = '0' + (c & 7); |
| 605 |
} |
| 606 |
else if (is_right_quote) |
| 607 |
{ |
| 608 |
STORE ('\\'); |
| 609 |
is_right_quote = false; |
| 610 |
} |
| 611 |
if (ilim <= i + 1) |
| 612 |
break; |
| 613 |
STORE (c); |
| 614 |
c = arg[++i]; |
| 615 |
} |
| 616 |
|
| 617 |
goto store_c; |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
if (! ((backslash_escapes || elide_outer_quotes) |
| 623 |
&& quote_these_too |
| 624 |
&& quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS))) |
| 625 |
&& !is_right_quote) |
| 626 |
goto store_c; |
| 627 |
|
| 628 |
store_escape: |
| 629 |
if (elide_outer_quotes) |
| 630 |
goto force_outer_quoting_style; |
| 631 |
STORE ('\\'); |
| 632 |
|
| 633 |
store_c: |
| 634 |
STORE (c); |
| 635 |
} |
| 636 |
|
| 637 |
if (len == 0 && quoting_style == shell_always_quoting_style |
| 638 |
&& elide_outer_quotes) |
| 639 |
goto force_outer_quoting_style; |
| 640 |
|
| 641 |
if (quote_string && !elide_outer_quotes) |
| 642 |
for (; *quote_string; quote_string++) |
| 643 |
STORE (*quote_string); |
| 644 |
|
| 645 |
if (len < buffersize) |
| 646 |
buffer[len] = '\0'; |
| 647 |
return len; |
| 648 |
|
| 649 |
force_outer_quoting_style: |
| 650 |
|
| 651 |
|
| 652 |
return quotearg_buffer_restyled (buffer, buffersize, arg, argsize, |
| 653 |
quoting_style, |
| 654 |
flags & ~QA_ELIDE_OUTER_QUOTES, NULL, |
| 655 |
left_quote, right_quote); |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
size_t |
| 668 |
quotearg_buffer (char *buffer, size_t buffersize, |
| 669 |
char const *arg, size_t argsize, |
| 670 |
struct quoting_options const *o) |
| 671 |
{ |
| 672 |
struct quoting_options const *p = o ? o : &default_quoting_options; |
| 673 |
int e = errno; |
| 674 |
size_t r = quotearg_buffer_restyled (buffer, buffersize, arg, argsize, |
| 675 |
p->style, p->flags, p->quote_these_too, |
| 676 |
p->left_quote, p->right_quote); |
| 677 |
errno = e; |
| 678 |
return r; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
char * |
| 683 |
quotearg_alloc (char const *arg, size_t argsize, |
| 684 |
struct quoting_options const *o) |
| 685 |
{ |
| 686 |
return quotearg_alloc_mem (arg, argsize, NULL, o); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
char * |
| 696 |
quotearg_alloc_mem (char const *arg, size_t argsize, size_t *size, |
| 697 |
struct quoting_options const *o) |
| 698 |
{ |
| 699 |
struct quoting_options const *p = o ? o : &default_quoting_options; |
| 700 |
int e = errno; |
| 701 |
|
| 702 |
int flags = p->flags | (size ? 0 : QA_ELIDE_NULL_BYTES); |
| 703 |
size_t bufsize = quotearg_buffer_restyled (0, 0, arg, argsize, p->style, |
| 704 |
flags, p->quote_these_too, |
| 705 |
p->left_quote, |
| 706 |
p->right_quote) + 1; |
| 707 |
char *buf = xcharalloc (bufsize); |
| 708 |
quotearg_buffer_restyled (buf, bufsize, arg, argsize, p->style, flags, |
| 709 |
p->quote_these_too, |
| 710 |
p->left_quote, p->right_quote); |
| 711 |
errno = e; |
| 712 |
if (size) |
| 713 |
*size = bufsize - 1; |
| 714 |
return buf; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
struct slotvec |
| 719 |
{ |
| 720 |
size_t size; |
| 721 |
char *val; |
| 722 |
}; |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
static char slot0[256]; |
| 727 |
static unsigned int nslots = 1; |
| 728 |
static struct slotvec slotvec0 = {sizeof slot0, slot0}; |
| 729 |
static struct slotvec *slotvec = &slotvec0; |
| 730 |
|
| 731 |
void |
| 732 |
quotearg_free (void) |
| 733 |
{ |
| 734 |
struct slotvec *sv = slotvec; |
| 735 |
unsigned int i; |
| 736 |
for (i = 1; i < nslots; i++) |
| 737 |
free (sv[i].val); |
| 738 |
if (sv[0].val != slot0) |
| 739 |
{ |
| 740 |
free (sv[0].val); |
| 741 |
slotvec0.size = sizeof slot0; |
| 742 |
slotvec0.val = slot0; |
| 743 |
} |
| 744 |
if (sv != &slotvec0) |
| 745 |
{ |
| 746 |
free (sv); |
| 747 |
slotvec = &slotvec0; |
| 748 |
} |
| 749 |
nslots = 1; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
static char * |
| 761 |
quotearg_n_options (int n, char const *arg, size_t argsize, |
| 762 |
struct quoting_options const *options) |
| 763 |
{ |
| 764 |
int e = errno; |
| 765 |
|
| 766 |
unsigned int n0 = n; |
| 767 |
struct slotvec *sv = slotvec; |
| 768 |
|
| 769 |
if (n < 0) |
| 770 |
abort (); |
| 771 |
|
| 772 |
if (nslots <= n0) |
| 773 |
{ |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
size_t n1 = n0 + 1; |
| 780 |
bool preallocated = (sv == &slotvec0); |
| 781 |
|
| 782 |
if (xalloc_oversized (n1, sizeof *sv)) |
| 783 |
xalloc_die (); |
| 784 |
|
| 785 |
slotvec = sv = xrealloc (preallocated ? NULL : sv, n1 * sizeof *sv); |
| 786 |
if (preallocated) |
| 787 |
*sv = slotvec0; |
| 788 |
memset (sv + nslots, 0, (n1 - nslots) * sizeof *sv); |
| 789 |
nslots = n1; |
| 790 |
} |
| 791 |
|
| 792 |
{ |
| 793 |
size_t size = sv[n].size; |
| 794 |
char *val = sv[n].val; |
| 795 |
|
| 796 |
int flags = options->flags | QA_ELIDE_NULL_BYTES; |
| 797 |
size_t qsize = quotearg_buffer_restyled (val, size, arg, argsize, |
| 798 |
options->style, flags, |
| 799 |
options->quote_these_too, |
| 800 |
options->left_quote, |
| 801 |
options->right_quote); |
| 802 |
|
| 803 |
if (size <= qsize) |
| 804 |
{ |
| 805 |
sv[n].size = size = qsize + 1; |
| 806 |
if (val != slot0) |
| 807 |
free (val); |
| 808 |
sv[n].val = val = xcharalloc (size); |
| 809 |
quotearg_buffer_restyled (val, size, arg, argsize, options->style, |
| 810 |
flags, options->quote_these_too, |
| 811 |
options->left_quote, |
| 812 |
options->right_quote); |
| 813 |
} |
| 814 |
|
| 815 |
errno = e; |
| 816 |
return val; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
char * |
| 821 |
quotearg_n (int n, char const *arg) |
| 822 |
{ |
| 823 |
return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options); |
| 824 |
} |
| 825 |
|
| 826 |
char * |
| 827 |
quotearg_n_mem (int n, char const *arg, size_t argsize) |
| 828 |
{ |
| 829 |
return quotearg_n_options (n, arg, argsize, &default_quoting_options); |
| 830 |
} |
| 831 |
|
| 832 |
char * |
| 833 |
quotearg (char const *arg) |
| 834 |
{ |
| 835 |
return quotearg_n (0, arg); |
| 836 |
} |
| 837 |
|
| 838 |
char * |
| 839 |
quotearg_mem (char const *arg, size_t argsize) |
| 840 |
{ |
| 841 |
return quotearg_n_mem (0, arg, argsize); |
| 842 |
} |
| 843 |
|
| 844 |
char * |
| 845 |
quotearg_n_style (int n, enum quoting_style s, char const *arg) |
| 846 |
{ |
| 847 |
struct quoting_options const o = quoting_options_from_style (s); |
| 848 |
return quotearg_n_options (n, arg, SIZE_MAX, &o); |
| 849 |
} |
| 850 |
|
| 851 |
char * |
| 852 |
quotearg_n_style_mem (int n, enum quoting_style s, |
| 853 |
char const *arg, size_t argsize) |
| 854 |
{ |
| 855 |
struct quoting_options const o = quoting_options_from_style (s); |
| 856 |
return quotearg_n_options (n, arg, argsize, &o); |
| 857 |
} |
| 858 |
|
| 859 |
char * |
| 860 |
quotearg_style (enum quoting_style s, char const *arg) |
| 861 |
{ |
| 862 |
return quotearg_n_style (0, s, arg); |
| 863 |
} |
| 864 |
|
| 865 |
char * |
| 866 |
quotearg_style_mem (enum quoting_style s, char const *arg, size_t argsize) |
| 867 |
{ |
| 868 |
return quotearg_n_style_mem (0, s, arg, argsize); |
| 869 |
} |
| 870 |
|
| 871 |
char * |
| 872 |
quotearg_char_mem (char const *arg, size_t argsize, char ch) |
| 873 |
{ |
| 874 |
struct quoting_options options; |
| 875 |
options = default_quoting_options; |
| 876 |
set_char_quoting (&options, ch, 1); |
| 877 |
return quotearg_n_options (0, arg, argsize, &options); |
| 878 |
} |
| 879 |
|
| 880 |
char * |
| 881 |
quotearg_char (char const *arg, char ch) |
| 882 |
{ |
| 883 |
return quotearg_char_mem (arg, SIZE_MAX, ch); |
| 884 |
} |
| 885 |
|
| 886 |
char * |
| 887 |
quotearg_colon (char const *arg) |
| 888 |
{ |
| 889 |
return quotearg_char (arg, ':'); |
| 890 |
} |
| 891 |
|
| 892 |
char * |
| 893 |
quotearg_colon_mem (char const *arg, size_t argsize) |
| 894 |
{ |
| 895 |
return quotearg_char_mem (arg, argsize, ':'); |
| 896 |
} |
| 897 |
|
| 898 |
char * |
| 899 |
quotearg_n_custom (int n, char const *left_quote, |
| 900 |
char const *right_quote, char const *arg) |
| 901 |
{ |
| 902 |
return quotearg_n_custom_mem (n, left_quote, right_quote, arg, |
| 903 |
SIZE_MAX); |
| 904 |
} |
| 905 |
|
| 906 |
char * |
| 907 |
quotearg_n_custom_mem (int n, char const *left_quote, |
| 908 |
char const *right_quote, |
| 909 |
char const *arg, size_t argsize) |
| 910 |
{ |
| 911 |
struct quoting_options o = default_quoting_options; |
| 912 |
set_custom_quoting (&o, left_quote, right_quote); |
| 913 |
return quotearg_n_options (n, arg, argsize, &o); |
| 914 |
} |
| 915 |
|
| 916 |
char * |
| 917 |
quotearg_custom (char const *left_quote, char const *right_quote, |
| 918 |
char const *arg) |
| 919 |
{ |
| 920 |
return quotearg_n_custom (0, left_quote, right_quote, arg); |
| 921 |
} |
| 922 |
|
| 923 |
char * |
| 924 |
quotearg_custom_mem (char const *left_quote, char const *right_quote, |
| 925 |
char const *arg, size_t argsize) |
| 926 |
{ |
| 927 |
return quotearg_n_custom_mem (0, left_quote, right_quote, arg, |
| 928 |
argsize); |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
struct quoting_options quote_quoting_options = |
| 934 |
{ |
| 935 |
locale_quoting_style, |
| 936 |
0, |
| 937 |
{ 0 }, |
| 938 |
NULL, NULL |
| 939 |
}; |
| 940 |
|
| 941 |
char const * |
| 942 |
quote_n_mem (int n, char const *arg, size_t argsize) |
| 943 |
{ |
| 944 |
return quotearg_n_options (n, arg, argsize, "e_quoting_options); |
| 945 |
} |
| 946 |
|
| 947 |
char const * |
| 948 |
quote_mem (char const *arg, size_t argsize) |
| 949 |
{ |
| 950 |
return quote_n_mem (0, arg, argsize); |
| 951 |
} |
| 952 |
|
| 953 |
char const * |
| 954 |
quote_n (int n, char const *arg) |
| 955 |
{ |
| 956 |
return quote_n_mem (n, arg, SIZE_MAX); |
| 957 |
} |
| 958 |
|
| 959 |
char const * |
| 960 |
quote (char const *arg) |
| 961 |
{ |
| 962 |
return quote_n (0, arg); |
| 963 |
} |