]> git.saurik.com Git - bison.git/blob - lib/quotearg.c
(_AT_DATA_CALC_Y): Remove unused strcat declaration.
[bison.git] / lib / quotearg.c
1 /* quotearg.c - quote arguments for output
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
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)
7 any later version.
8
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.
13
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. */
17
18 /* Written by Paul Eggert <eggert@twinsun.com> */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #if HAVE_STDDEF_H
25 # include <stddef.h> /* For the definition of size_t on windows w/MSVC. */
26 #endif
27 #include <sys/types.h>
28 #include "quotearg.h"
29 #include "xalloc.h"
30
31 #include <ctype.h>
32
33 #if ENABLE_NLS
34 # include <libintl.h>
35 # define _(text) gettext (text)
36 #else
37 # define _(text) text
38 #endif
39 #define N_(text) text
40
41 #if HAVE_LIMITS_H
42 # include <limits.h>
43 #endif
44 #ifndef CHAR_BIT
45 # define CHAR_BIT 8
46 #endif
47 #ifndef SIZE_MAX
48 # define SIZE_MAX ((size_t) -1)
49 #endif
50 #ifndef UCHAR_MAX
51 # define UCHAR_MAX ((unsigned char) -1)
52 #endif
53 #ifndef UINT_MAX
54 # define UINT_MAX ((unsigned int) -1)
55 #endif
56
57 #if HAVE_C_BACKSLASH_A
58 # define ALERT_CHAR '\a'
59 #else
60 # define ALERT_CHAR '\7'
61 #endif
62
63 #if HAVE_STDLIB_H
64 # include <stdlib.h>
65 #endif
66
67 #if HAVE_STRING_H
68 # include <string.h>
69 #endif
70
71 #if HAVE_WCHAR_H
72
73 /* BSD/OS 4.1 wchar.h requires FILE and struct tm to be declared. */
74 # include <stdio.h>
75 # include <time.h>
76
77 # include <wchar.h>
78 #endif
79
80 #if !HAVE_MBRTOWC
81 /* Disable multibyte processing entirely. Since MB_CUR_MAX is 1, the
82 other macros are defined only for documentation and to satisfy C
83 syntax. */
84 # undef MB_CUR_MAX
85 # define MB_CUR_MAX 1
86 # define mbrtowc(pwc, s, n, ps) ((*(pwc) = *(s)) != 0)
87 # define iswprint(wc) ISPRINT ((unsigned char) (wc))
88 # undef HAVE_MBSINIT
89 #endif
90
91 #if !defined mbsinit && !HAVE_MBSINIT
92 # define mbsinit(ps) 1
93 #endif
94
95 #ifndef iswprint
96 # if HAVE_WCTYPE_H
97 # include <wctype.h>
98 # endif
99 # if !defined iswprint && !HAVE_ISWPRINT
100 # define iswprint(wc) 1
101 # endif
102 #endif
103
104 #define INT_BITS (sizeof (int) * CHAR_BIT)
105
106 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
107 # define IN_CTYPE_DOMAIN(c) 1
108 #else
109 # define IN_CTYPE_DOMAIN(c) isascii(c)
110 #endif
111
112 /* Undefine to protect against the definition in wctype.h of solaris2.6. */
113 #undef ISPRINT
114 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
115
116 struct quoting_options
117 {
118 /* Basic quoting style. */
119 enum quoting_style style;
120
121 /* Quote the characters indicated by this bit vector even if the
122 quoting style would not normally require them to be quoted. */
123 int quote_these_too[(UCHAR_MAX / INT_BITS) + 1];
124 };
125
126 /* Names of quoting styles. */
127 char const *const quoting_style_args[] =
128 {
129 "literal",
130 "shell",
131 "shell-always",
132 "c",
133 "escape",
134 "locale",
135 "clocale",
136 0
137 };
138
139 /* Correspondences to quoting style names. */
140 enum quoting_style const quoting_style_vals[] =
141 {
142 literal_quoting_style,
143 shell_quoting_style,
144 shell_always_quoting_style,
145 c_quoting_style,
146 escape_quoting_style,
147 locale_quoting_style,
148 clocale_quoting_style
149 };
150
151 /* The default quoting options. */
152 static struct quoting_options default_quoting_options;
153
154 /* Allocate a new set of quoting options, with contents initially identical
155 to O if O is not null, or to the default if O is null.
156 It is the caller's responsibility to free the result. */
157 struct quoting_options *
158 clone_quoting_options (struct quoting_options *o)
159 {
160 struct quoting_options *p
161 = (struct quoting_options *) xmalloc (sizeof (struct quoting_options));
162 *p = *(o ? o : &default_quoting_options);
163 return p;
164 }
165
166 /* Get the value of O's quoting style. If O is null, use the default. */
167 enum quoting_style
168 get_quoting_style (struct quoting_options *o)
169 {
170 return (o ? o : &default_quoting_options)->style;
171 }
172
173 /* In O (or in the default if O is null),
174 set the value of the quoting style to S. */
175 void
176 set_quoting_style (struct quoting_options *o, enum quoting_style s)
177 {
178 (o ? o : &default_quoting_options)->style = s;
179 }
180
181 /* In O (or in the default if O is null),
182 set the value of the quoting options for character C to I.
183 Return the old value. Currently, the only values defined for I are
184 0 (the default) and 1 (which means to quote the character even if
185 it would not otherwise be quoted). */
186 int
187 set_char_quoting (struct quoting_options *o, char c, int i)
188 {
189 unsigned char uc = c;
190 int *p = (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
191 int shift = uc % INT_BITS;
192 int r = (*p >> shift) & 1;
193 *p ^= ((i & 1) ^ r) << shift;
194 return r;
195 }
196
197 /* MSGID approximates a quotation mark. Return its translation if it
198 has one; otherwise, return either it or "\"", depending on S. */
199 static char const *
200 gettext_quote (char const *msgid, enum quoting_style s)
201 {
202 char const *translation = _(msgid);
203 if (translation == msgid && s == clocale_quoting_style)
204 translation = "\"";
205 return translation;
206 }
207
208 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
209 argument ARG (of size ARGSIZE), using QUOTING_STYLE and the
210 non-quoting-style part of O to control quoting.
211 Terminate the output with a null character, and return the written
212 size of the output, not counting the terminating null.
213 If BUFFERSIZE is too small to store the output string, return the
214 value that would have been returned had BUFFERSIZE been large enough.
215 If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
216
217 This function acts like quotearg_buffer (BUFFER, BUFFERSIZE, ARG,
218 ARGSIZE, O), except it uses QUOTING_STYLE instead of the quoting
219 style specified by O, and O may not be null. */
220
221 static size_t
222 quotearg_buffer_restyled (char *buffer, size_t buffersize,
223 char const *arg, size_t argsize,
224 enum quoting_style quoting_style,
225 struct quoting_options const *o)
226 {
227 size_t i;
228 size_t len = 0;
229 char const *quote_string = 0;
230 size_t quote_string_len = 0;
231 int backslash_escapes = 0;
232 int unibyte_locale = MB_CUR_MAX == 1;
233
234 #define STORE(c) \
235 do \
236 { \
237 if (len < buffersize) \
238 buffer[len] = (c); \
239 len++; \
240 } \
241 while (0)
242
243 switch (quoting_style)
244 {
245 case c_quoting_style:
246 STORE ('"');
247 backslash_escapes = 1;
248 quote_string = "\"";
249 quote_string_len = 1;
250 break;
251
252 case escape_quoting_style:
253 backslash_escapes = 1;
254 break;
255
256 case locale_quoting_style:
257 case clocale_quoting_style:
258 {
259 /* Get translations for open and closing quotation marks.
260
261 The message catalog should translate "`" to a left
262 quotation mark suitable for the locale, and similarly for
263 "'". If the catalog has no translation,
264 locale_quoting_style quotes `like this', and
265 clocale_quoting_style quotes "like this".
266
267 For example, an American English Unicode locale should
268 translate "`" to U+201C (LEFT DOUBLE QUOTATION MARK), and
269 should translate "'" to U+201D (RIGHT DOUBLE QUOTATION
270 MARK). A British English Unicode locale should instead
271 translate these to U+2018 (LEFT SINGLE QUOTATION MARK) and
272 U+2019 (RIGHT SINGLE QUOTATION MARK), respectively. */
273
274 char const *left = gettext_quote (N_("`"), quoting_style);
275 char const *right = gettext_quote (N_("'"), quoting_style);
276 for (quote_string = left; *quote_string; quote_string++)
277 STORE (*quote_string);
278 backslash_escapes = 1;
279 quote_string = right;
280 quote_string_len = strlen (quote_string);
281 }
282 break;
283
284 case shell_always_quoting_style:
285 STORE ('\'');
286 quote_string = "'";
287 quote_string_len = 1;
288 break;
289
290 default:
291 break;
292 }
293
294 for (i = 0; ! (argsize == (size_t) -1 ? arg[i] == '\0' : i == argsize); i++)
295 {
296 unsigned char c;
297 unsigned char esc;
298
299 if (backslash_escapes
300 && quote_string_len
301 && i + quote_string_len <= argsize
302 && memcmp (arg + i, quote_string, quote_string_len) == 0)
303 STORE ('\\');
304
305 c = arg[i];
306 switch (c)
307 {
308 case '\0':
309 if (backslash_escapes)
310 {
311 STORE ('\\');
312 STORE ('0');
313 STORE ('0');
314 c = '0';
315 }
316 break;
317
318 case '?':
319 switch (quoting_style)
320 {
321 case shell_quoting_style:
322 goto use_shell_always_quoting_style;
323
324 case c_quoting_style:
325 if (i + 2 < argsize && arg[i + 1] == '?')
326 switch (arg[i + 2])
327 {
328 case '!': case '\'':
329 case '(': case ')': case '-': case '/':
330 case '<': case '=': case '>':
331 /* Escape the second '?' in what would otherwise be
332 a trigraph. */
333 i += 2;
334 c = arg[i + 2];
335 STORE ('?');
336 STORE ('\\');
337 STORE ('?');
338 break;
339 }
340 break;
341
342 default:
343 break;
344 }
345 break;
346
347 case ALERT_CHAR: esc = 'a'; goto c_escape;
348 case '\b': esc = 'b'; goto c_escape;
349 case '\f': esc = 'f'; goto c_escape;
350 case '\n': esc = 'n'; goto c_and_shell_escape;
351 case '\r': esc = 'r'; goto c_and_shell_escape;
352 case '\t': esc = 't'; goto c_and_shell_escape;
353 case '\v': esc = 'v'; goto c_escape;
354 case '\\': esc = c; goto c_and_shell_escape;
355
356 c_and_shell_escape:
357 if (quoting_style == shell_quoting_style)
358 goto use_shell_always_quoting_style;
359 c_escape:
360 if (backslash_escapes)
361 {
362 c = esc;
363 goto store_escape;
364 }
365 break;
366
367 case '#': case '~':
368 if (i != 0)
369 break;
370 /* Fall through. */
371 case ' ':
372 case '!': /* special in bash */
373 case '"': case '$': case '&':
374 case '(': case ')': case '*': case ';':
375 case '<': case '>': case '[':
376 case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
377 case '`': case '|':
378 /* A shell special character. In theory, '$' and '`' could
379 be the first bytes of multibyte characters, which means
380 we should check them with mbrtowc, but in practice this
381 doesn't happen so it's not worth worrying about. */
382 if (quoting_style == shell_quoting_style)
383 goto use_shell_always_quoting_style;
384 break;
385
386 case '\'':
387 switch (quoting_style)
388 {
389 case shell_quoting_style:
390 goto use_shell_always_quoting_style;
391
392 case shell_always_quoting_style:
393 STORE ('\'');
394 STORE ('\\');
395 STORE ('\'');
396 break;
397
398 default:
399 break;
400 }
401 break;
402
403 case '%': case '+': case ',': case '-': case '.': case '/':
404 case '0': case '1': case '2': case '3': case '4': case '5':
405 case '6': case '7': case '8': case '9': case ':': case '=':
406 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
407 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
408 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
409 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
410 case 'Y': case 'Z': case ']': case '_': case 'a': case 'b':
411 case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
412 case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
413 case 'o': case 'p': case 'q': case 'r': case 's': case 't':
414 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
415 case '{': case '}':
416 /* These characters don't cause problems, no matter what the
417 quoting style is. They cannot start multibyte sequences. */
418 break;
419
420 default:
421 /* If we have a multibyte sequence, copy it until we reach
422 its end, find an error, or come back to the initial shift
423 state. For C-like styles, if the sequence has
424 unprintable characters, escape the whole sequence, since
425 we can't easily escape single characters within it. */
426 {
427 /* Length of multibyte sequence found so far. */
428 size_t m;
429
430 int printable;
431
432 if (unibyte_locale)
433 {
434 m = 1;
435 printable = ISPRINT (c);
436 }
437 else
438 {
439 mbstate_t mbstate;
440 memset (&mbstate, 0, sizeof mbstate);
441
442 m = 0;
443 printable = 1;
444 if (argsize == (size_t) -1)
445 argsize = strlen (arg);
446
447 do
448 {
449 wchar_t w;
450 size_t bytes = mbrtowc (&w, &arg[i + m],
451 argsize - (i + m), &mbstate);
452 if (bytes == 0)
453 break;
454 else if (bytes == (size_t) -1)
455 {
456 printable = 0;
457 break;
458 }
459 else if (bytes == (size_t) -2)
460 {
461 printable = 0;
462 while (i + m < argsize && arg[i + m])
463 m++;
464 break;
465 }
466 else
467 {
468 if (! iswprint (w))
469 printable = 0;
470 m += bytes;
471 }
472 }
473 while (! mbsinit (&mbstate));
474 }
475
476 if (1 < m || (backslash_escapes && ! printable))
477 {
478 /* Output a multibyte sequence, or an escaped
479 unprintable unibyte character. */
480 size_t ilim = i + m;
481
482 for (;;)
483 {
484 if (backslash_escapes && ! printable)
485 {
486 STORE ('\\');
487 STORE ('0' + (c >> 6));
488 STORE ('0' + ((c >> 3) & 7));
489 c = '0' + (c & 7);
490 }
491 if (ilim <= i + 1)
492 break;
493 STORE (c);
494 c = arg[++i];
495 }
496
497 goto store_c;
498 }
499 }
500 }
501
502 if (! (backslash_escapes
503 && o->quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS))))
504 goto store_c;
505
506 store_escape:
507 STORE ('\\');
508
509 store_c:
510 STORE (c);
511 }
512
513 if (quote_string)
514 for (; *quote_string; quote_string++)
515 STORE (*quote_string);
516
517 if (len < buffersize)
518 buffer[len] = '\0';
519 return len;
520
521 use_shell_always_quoting_style:
522 return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
523 shell_always_quoting_style, o);
524 }
525
526 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
527 argument ARG (of size ARGSIZE), using O to control quoting.
528 If O is null, use the default.
529 Terminate the output with a null character, and return the written
530 size of the output, not counting the terminating null.
531 If BUFFERSIZE is too small to store the output string, return the
532 value that would have been returned had BUFFERSIZE been large enough.
533 If ARGSIZE is -1, use the string length of the argument for ARGSIZE. */
534 size_t
535 quotearg_buffer (char *buffer, size_t buffersize,
536 char const *arg, size_t argsize,
537 struct quoting_options const *o)
538 {
539 struct quoting_options const *p = o ? o : &default_quoting_options;
540 return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
541 p->style, p);
542 }
543
544 /* Use storage slot N to return a quoted version of argument ARG.
545 ARG is of size ARGSIZE, but if that is -1, ARG is a null-terminated string.
546 OPTIONS specifies the quoting options.
547 The returned value points to static storage that can be
548 reused by the next call to this function with the same value of N.
549 N must be nonnegative. N is deliberately declared with type "int"
550 to allow for future extensions (using negative values). */
551 static char *
552 quotearg_n_options (int n, char const *arg, size_t argsize,
553 struct quoting_options const *options)
554 {
555 /* Preallocate a slot 0 buffer, so that the caller can always quote
556 one small component of a "memory exhausted" message in slot 0. */
557 static char slot0[256];
558 static unsigned int nslots = 1;
559 unsigned int n0 = n;
560 struct slotvec
561 {
562 size_t size;
563 char *val;
564 };
565 static struct slotvec slotvec0 = {sizeof slot0, slot0};
566 static struct slotvec *slotvec = &slotvec0;
567
568 if (n < 0)
569 abort ();
570
571 if (nslots <= n0)
572 {
573 unsigned int n1 = n0 + 1;
574 size_t s = n1 * sizeof *slotvec;
575
576 if (SIZE_MAX / UINT_MAX <= sizeof *slotvec
577 && n1 != s / sizeof *slotvec)
578 xalloc_die ();
579
580 if (slotvec == &slotvec0)
581 {
582 slotvec = (struct slotvec *) xmalloc (sizeof *slotvec);
583 *slotvec = slotvec0;
584 }
585 slotvec = (struct slotvec *) xrealloc (slotvec, s);
586 memset (slotvec + nslots, 0, (n1 - nslots) * sizeof *slotvec);
587 nslots = n1;
588 }
589
590 {
591 size_t size = slotvec[n].size;
592 char *val = slotvec[n].val;
593 size_t qsize = quotearg_buffer (val, size, arg, argsize, options);
594
595 if (size <= qsize)
596 {
597 slotvec[n].size = size = qsize + 1;
598 slotvec[n].val = val = xrealloc (val == slot0 ? 0 : val, size);
599 quotearg_buffer (val, size, arg, argsize, options);
600 }
601
602 return val;
603 }
604 }
605
606 char *
607 quotearg_n (int n, char const *arg)
608 {
609 return quotearg_n_options (n, arg, (size_t) -1, &default_quoting_options);
610 }
611
612 char *
613 quotearg (char const *arg)
614 {
615 return quotearg_n (0, arg);
616 }
617
618 /* Return quoting options for STYLE, with no extra quoting. */
619 static struct quoting_options
620 quoting_options_from_style (enum quoting_style style)
621 {
622 struct quoting_options o;
623 o.style = style;
624 memset (o.quote_these_too, 0, sizeof o.quote_these_too);
625 return o;
626 }
627
628 char *
629 quotearg_n_style (int n, enum quoting_style s, char const *arg)
630 {
631 struct quoting_options const o = quoting_options_from_style (s);
632 return quotearg_n_options (n, arg, (size_t) -1, &o);
633 }
634
635 char *
636 quotearg_n_style_mem (int n, enum quoting_style s,
637 char const *arg, size_t argsize)
638 {
639 struct quoting_options const o = quoting_options_from_style (s);
640 return quotearg_n_options (n, arg, argsize, &o);
641 }
642
643 char *
644 quotearg_style (enum quoting_style s, char const *arg)
645 {
646 return quotearg_n_style (0, s, arg);
647 }
648
649 char *
650 quotearg_char (char const *arg, char ch)
651 {
652 struct quoting_options options;
653 options = default_quoting_options;
654 set_char_quoting (&options, ch, 1);
655 return quotearg_n_options (0, arg, (size_t) -1, &options);
656 }
657
658 char *
659 quotearg_colon (char const *arg)
660 {
661 return quotearg_char (arg, ':');
662 }