1 /* quotearg.c - quote arguments for output
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Eggert <eggert@twinsun.com> */
20 /* FIXME: Multibyte characters are not supported yet. */
26 #include <sys/types.h>
31 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
34 # define ISASCII(c) isascii (c)
37 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
39 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
44 # define _(text) gettext (text)
56 # define UCHAR_MAX ((unsigned char) -1)
67 #define INT_BITS (sizeof (int) * CHAR_BIT)
69 struct quoting_options
71 /* Basic quoting style. */
72 enum quoting_style style
;
74 /* Quote the chararacters indicated by this bit vector even if the
75 quoting style would not normally require them to be quoted. */
76 int quote_these_too
[((UCHAR_MAX
+ 1) / INT_BITS
77 + ((UCHAR_MAX
+ 1) % INT_BITS
!= 0))];
80 /* Names of quoting styles. */
81 char const *const quoting_style_args
[] =
92 /* Correspondances to quoting style names. */
93 enum quoting_style
const quoting_style_vals
[] =
95 literal_quoting_style
,
97 shell_always_quoting_style
,
103 /* The default quoting options. */
104 static struct quoting_options default_quoting_options
;
106 /* Allocate a new set of quoting options, with contents initially identical
107 to O if O is not null, or to the default if O is null.
108 It is the caller's responsibility to free the result. */
109 struct quoting_options
*
110 clone_quoting_options (struct quoting_options
*o
)
112 struct quoting_options
*p
113 = (struct quoting_options
*) xmalloc (sizeof (struct quoting_options
));
114 *p
= *(o
? o
: &default_quoting_options
);
118 /* Get the value of O's quoting style. If O is null, use the default. */
120 get_quoting_style (struct quoting_options
*o
)
122 return (o
? o
: &default_quoting_options
)->style
;
125 /* In O (or in the default if O is null),
126 set the value of the quoting style to S. */
128 set_quoting_style (struct quoting_options
*o
, enum quoting_style s
)
130 (o
? o
: &default_quoting_options
)->style
= s
;
133 /* In O (or in the default if O is null),
134 set the value of the quoting options for character C to I.
135 Return the old value. Currently, the only values defined for I are
136 0 (the default) and 1 (which means to quote the character even if
137 it would not otherwise be quoted). */
139 set_char_quoting (struct quoting_options
*o
, char c
, int i
)
141 unsigned char uc
= c
;
142 int *p
= (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
;
149 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
150 argument ARG (of size ARGSIZE), using O to control quoting.
151 If O is null, use the default.
152 Terminate the output with a null character, and return the written
153 size of the output, not counting the terminating null.
154 If BUFFERSIZE is too small to store the output string, return the
155 value that would have been returned had BUFFERSIZE been large enough.
156 If ARGSIZE is -1, use the string length of the argument for ARGSIZE. */
158 quotearg_buffer (char *buffer
, size_t buffersize
,
159 char const *arg
, size_t argsize
,
160 struct quoting_options
const *o
)
165 char const *quote_string
;
166 size_t quote_string_len
;
167 struct quoting_options
const *p
= o
? o
: &default_quoting_options
;
168 enum quoting_style quoting_style
= p
->style
;
172 if (len < buffersize) \
178 switch (quoting_style
)
180 case shell_quoting_style
:
181 if (! (argsize
== (size_t) -1 ? arg
[0] == '\0' : argsize
== 0))
191 if (argsize
== (size_t) -1 ? arg
[i
] == '\0' : i
== argsize
)
198 case '\t': case '\n': case ' ':
199 case '!': /* special in csh */
200 case '"': case '$': case '&': case '\'':
201 case '(': case ')': case '*': case ';':
202 case '<': case '>': case '?': case '[': case '\\':
203 case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
208 if (p
->quote_these_too
[c
/ INT_BITS
] & (1 << (c
% INT_BITS
)))
221 case shell_always_quoting_style
:
224 quote_string_len
= 1;
227 case c_quoting_style
:
230 quote_string_len
= 1;
233 case locale_quoting_style
:
234 for (quote_string
= _("`"); *quote_string
; quote_string
++)
235 STORE (*quote_string
);
236 quote_string
= _("'");
237 quote_string_len
= strlen (quote_string
);
242 quote_string_len
= 0;
246 for (i
= 0; ! (argsize
== (size_t) -1 ? arg
[i
] == '\0' : i
== argsize
); i
++)
250 switch (quoting_style
)
252 case literal_quoting_style
:
255 case shell_quoting_style
:
256 case shell_always_quoting_style
:
265 case c_quoting_style
:
266 case escape_quoting_style
:
267 case locale_quoting_style
:
270 case '?': /* Do not generate trigraphs. */
271 case '\\': goto store_escape
;
272 /* Not all C compilers know what \a means. */
273 case 7 : c
= 'a'; goto store_escape
;
274 case '\b': c
= 'b'; goto store_escape
;
275 case '\f': c
= 'f'; goto store_escape
;
276 case '\n': c
= 'n'; goto store_escape
;
277 case '\r': c
= 'r'; goto store_escape
;
278 case '\t': c
= 't'; goto store_escape
;
279 case '\v': c
= 'v'; goto store_escape
;
285 && strncmp (arg
+ i
, quote_string
, quote_string_len
) == 0)
290 STORE ('0' + (c
>> 6));
291 STORE ('0' + ((c
>> 3) & 7));
298 if (! (p
->quote_these_too
[c
/ INT_BITS
] & (1 << (c
% INT_BITS
))))
310 for (; *quote_string
; quote_string
++)
311 STORE (*quote_string
);
314 if (len
< buffersize
)
319 /* Use storage slot N to return a quoted version of the string ARG.
320 OPTIONS specifies the quoting options.
321 The returned value points to static storage that can be
322 reused by the next call to this function with the same value of N.
323 N must be nonnegative. N is deliberately declared with type `int'
324 to allow for future extensions (using negative values). */
326 quotearg_n_options (int n
, char const *arg
,
327 struct quoting_options
const *options
)
329 static unsigned int nslots
;
330 static struct slotvec
339 size_t s
= n1
* sizeof (struct slotvec
);
340 if (! (0 < n1
&& n1
== s
/ sizeof (struct slotvec
)))
342 slotvec
= (struct slotvec
*) xrealloc (slotvec
, s
);
343 memset (slotvec
+ nslots
, 0, (n1
- nslots
) * sizeof (struct slotvec
));
348 size_t size
= slotvec
[n
].size
;
349 char *val
= slotvec
[n
].val
;
350 size_t qsize
= quotearg_buffer (val
, size
, arg
, (size_t) -1, options
);
354 slotvec
[n
].size
= size
= qsize
+ 1;
355 slotvec
[n
].val
= val
= xrealloc (val
, size
);
356 quotearg_buffer (val
, size
, arg
, (size_t) -1, options
);
364 quotearg_n (unsigned int n
, char const *arg
)
366 return quotearg_n_options (n
, arg
, &default_quoting_options
);
370 quotearg (char const *arg
)
372 return quotearg_n (0, arg
);
376 quotearg_n_style (unsigned int n
, enum quoting_style s
, char const *arg
)
378 struct quoting_options o
;
380 memset (o
.quote_these_too
, 0, sizeof o
.quote_these_too
);
381 return quotearg_n_options (n
, arg
, &o
);
385 quotearg_style (enum quoting_style s
, char const *arg
)
387 return quotearg_n_style (0, s
, arg
);
391 quotearg_char (char const *arg
, char ch
)
393 struct quoting_options options
;
394 options
= default_quoting_options
;
395 set_char_quoting (&options
, ch
, 1);
396 return quotearg_n_options (0, arg
, &options
);
400 quotearg_colon (char const *arg
)
402 return quotearg_char (arg
, ':');