21 |
# include <config.h> |
# include <config.h> |
22 |
#endif |
#endif |
23 |
|
|
|
#if HAVE_STDDEF_H |
|
|
# include <stddef.h> /* For the definition of size_t on windows w/MSVC. */ |
|
|
#endif |
|
|
#include <sys/types.h> |
|
24 |
#include "quotearg.h" |
#include "quotearg.h" |
25 |
|
|
26 |
#include "xalloc.h" |
#include "xalloc.h" |
27 |
|
|
28 |
#include <ctype.h> |
#include <ctype.h> |
29 |
|
#include <errno.h> |
30 |
#if ENABLE_NLS |
#include <limits.h> |
31 |
# include <libintl.h> |
#include <stdlib.h> |
32 |
# define _(text) gettext (text) |
#include <string.h> |
33 |
#else |
|
34 |
# define _(text) text |
#include "gettext.h" |
35 |
#endif |
#define _(msgid) gettext (msgid) |
36 |
#define N_(text) text |
#define N_(msgid) msgid |
|
|
|
|
#if HAVE_LIMITS_H |
|
|
# include <limits.h> |
|
|
#endif |
|
|
#ifndef CHAR_BIT |
|
|
# define CHAR_BIT 8 |
|
|
#endif |
|
|
#ifndef SIZE_MAX |
|
|
# define SIZE_MAX ((size_t) -1) |
|
|
#endif |
|
|
#ifndef UCHAR_MAX |
|
|
# define UCHAR_MAX ((unsigned char) -1) |
|
|
#endif |
|
|
#ifndef UINT_MAX |
|
|
# define UINT_MAX ((unsigned int) -1) |
|
|
#endif |
|
|
|
|
|
#if HAVE_C_BACKSLASH_A |
|
|
# define ALERT_CHAR '\a' |
|
|
#else |
|
|
# define ALERT_CHAR '\7' |
|
|
#endif |
|
|
|
|
|
#if HAVE_STDLIB_H |
|
|
# include <stdlib.h> |
|
|
#endif |
|
|
|
|
|
#if HAVE_STRING_H |
|
|
# include <string.h> |
|
|
#endif |
|
37 |
|
|
38 |
#if HAVE_WCHAR_H |
#if HAVE_WCHAR_H |
39 |
|
|
51 |
# undef MB_CUR_MAX |
# undef MB_CUR_MAX |
52 |
# define MB_CUR_MAX 1 |
# define MB_CUR_MAX 1 |
53 |
# define mbrtowc(pwc, s, n, ps) ((*(pwc) = *(s)) != 0) |
# define mbrtowc(pwc, s, n, ps) ((*(pwc) = *(s)) != 0) |
54 |
# define iswprint(wc) ISPRINT ((unsigned char) (wc)) |
# define iswprint(wc) isprint ((unsigned char) (wc)) |
55 |
# undef HAVE_MBSINIT |
# undef HAVE_MBSINIT |
56 |
#endif |
#endif |
57 |
|
|
68 |
# endif |
# endif |
69 |
#endif |
#endif |
70 |
|
|
71 |
#define INT_BITS (sizeof (int) * CHAR_BIT) |
#ifndef SIZE_MAX |
72 |
|
# define SIZE_MAX ((size_t) -1) |
|
#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII)) |
|
|
# define IN_CTYPE_DOMAIN(c) 1 |
|
|
#else |
|
|
# define IN_CTYPE_DOMAIN(c) isascii(c) |
|
73 |
#endif |
#endif |
74 |
|
|
75 |
/* Undefine to protect against the definition in wctype.h of solaris2.6. */ |
#define INT_BITS (sizeof (int) * CHAR_BIT) |
|
#undef ISPRINT |
|
|
#define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c)) |
|
76 |
|
|
77 |
struct quoting_options |
struct quoting_options |
78 |
{ |
{ |
118 |
struct quoting_options * |
struct quoting_options * |
119 |
clone_quoting_options (struct quoting_options *o) |
clone_quoting_options (struct quoting_options *o) |
120 |
{ |
{ |
121 |
struct quoting_options *p |
int e = errno; |
122 |
= (struct quoting_options *) xmalloc (sizeof (struct quoting_options)); |
struct quoting_options *p = xmalloc (sizeof *p); |
123 |
*p = *(o ? o : &default_quoting_options); |
*p = *(o ? o : &default_quoting_options); |
124 |
|
errno = e; |
125 |
return p; |
return p; |
126 |
} |
} |
127 |
|
|
253 |
break; |
break; |
254 |
} |
} |
255 |
|
|
256 |
for (i = 0; ! (argsize == (size_t) -1 ? arg[i] == '\0' : i == argsize); i++) |
for (i = 0; ! (argsize == SIZE_MAX ? arg[i] == '\0' : i == argsize); i++) |
257 |
{ |
{ |
258 |
unsigned char c; |
unsigned char c; |
259 |
unsigned char esc; |
unsigned char esc; |
306 |
} |
} |
307 |
break; |
break; |
308 |
|
|
309 |
case ALERT_CHAR: esc = 'a'; goto c_escape; |
case '\a': esc = 'a'; goto c_escape; |
310 |
case '\b': esc = 'b'; goto c_escape; |
case '\b': esc = 'b'; goto c_escape; |
311 |
case '\f': esc = 'f'; goto c_escape; |
case '\f': esc = 'f'; goto c_escape; |
312 |
case '\n': esc = 'n'; goto c_and_shell_escape; |
case '\n': esc = 'n'; goto c_and_shell_escape; |
394 |
if (unibyte_locale) |
if (unibyte_locale) |
395 |
{ |
{ |
396 |
m = 1; |
m = 1; |
397 |
printable = ISPRINT (c); |
printable = isprint (c); |
398 |
} |
} |
399 |
else |
else |
400 |
{ |
{ |
403 |
|
|
404 |
m = 0; |
m = 0; |
405 |
printable = 1; |
printable = 1; |
406 |
if (argsize == (size_t) -1) |
if (argsize == SIZE_MAX) |
407 |
argsize = strlen (arg); |
argsize = strlen (arg); |
408 |
|
|
409 |
do |
do |
499 |
struct quoting_options const *o) |
struct quoting_options const *o) |
500 |
{ |
{ |
501 |
struct quoting_options const *p = o ? o : &default_quoting_options; |
struct quoting_options const *p = o ? o : &default_quoting_options; |
502 |
return quotearg_buffer_restyled (buffer, buffersize, arg, argsize, |
int e = errno; |
503 |
p->style, p); |
size_t r = quotearg_buffer_restyled (buffer, buffersize, arg, argsize, |
504 |
|
p->style, p); |
505 |
|
errno = e; |
506 |
|
return r; |
507 |
} |
} |
508 |
|
|
509 |
/* Use storage slot N to return a quoted version of argument ARG. |
/* Use storage slot N to return a quoted version of argument ARG. |
517 |
quotearg_n_options (int n, char const *arg, size_t argsize, |
quotearg_n_options (int n, char const *arg, size_t argsize, |
518 |
struct quoting_options const *options) |
struct quoting_options const *options) |
519 |
{ |
{ |
520 |
|
int e = errno; |
521 |
|
|
522 |
/* Preallocate a slot 0 buffer, so that the caller can always quote |
/* Preallocate a slot 0 buffer, so that the caller can always quote |
523 |
one small component of a "memory exhausted" message in slot 0. */ |
one small component of a "memory exhausted" message in slot 0. */ |
524 |
static char slot0[256]; |
static char slot0[256]; |
546 |
|
|
547 |
if (slotvec == &slotvec0) |
if (slotvec == &slotvec0) |
548 |
{ |
{ |
549 |
slotvec = (struct slotvec *) xmalloc (sizeof *slotvec); |
slotvec = xmalloc (sizeof *slotvec); |
550 |
*slotvec = slotvec0; |
*slotvec = slotvec0; |
551 |
} |
} |
552 |
slotvec = (struct slotvec *) xrealloc (slotvec, s); |
slotvec = xrealloc (slotvec, s); |
553 |
memset (slotvec + nslots, 0, (n1 - nslots) * sizeof *slotvec); |
memset (slotvec + nslots, 0, (n1 - nslots) * sizeof *slotvec); |
554 |
nslots = n1; |
nslots = n1; |
555 |
} |
} |
566 |
quotearg_buffer (val, size, arg, argsize, options); |
quotearg_buffer (val, size, arg, argsize, options); |
567 |
} |
} |
568 |
|
|
569 |
|
errno = e; |
570 |
return val; |
return val; |
571 |
} |
} |
572 |
} |
} |
574 |
char * |
char * |
575 |
quotearg_n (int n, char const *arg) |
quotearg_n (int n, char const *arg) |
576 |
{ |
{ |
577 |
return quotearg_n_options (n, arg, (size_t) -1, &default_quoting_options); |
return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options); |
578 |
} |
} |
579 |
|
|
580 |
char * |
char * |
597 |
quotearg_n_style (int n, enum quoting_style s, char const *arg) |
quotearg_n_style (int n, enum quoting_style s, char const *arg) |
598 |
{ |
{ |
599 |
struct quoting_options const o = quoting_options_from_style (s); |
struct quoting_options const o = quoting_options_from_style (s); |
600 |
return quotearg_n_options (n, arg, (size_t) -1, &o); |
return quotearg_n_options (n, arg, SIZE_MAX, &o); |
601 |
} |
} |
602 |
|
|
603 |
char * |
char * |
620 |
struct quoting_options options; |
struct quoting_options options; |
621 |
options = default_quoting_options; |
options = default_quoting_options; |
622 |
set_char_quoting (&options, ch, 1); |
set_char_quoting (&options, ch, 1); |
623 |
return quotearg_n_options (0, arg, (size_t) -1, &options); |
return quotearg_n_options (0, arg, SIZE_MAX, &options); |
624 |
} |
} |
625 |
|
|
626 |
char * |
char * |