1 /* strtrans.c - Translate and untranslate strings with ANSI-C escape sequences. */
3 /* Copyright (C) 2000-2011 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (HAVE_UNISTD_H)
29 #include <chartypes.h>
39 #define ESC '\033' /* ASCII */
41 /* Convert STRING by expanding the escape sequences specified by the
42 ANSI C standard. If SAWC is non-null, recognize `\c' and use that
43 as a string terminator. If we see \c, set *SAWC to 1 before
44 returning. LEN is the length of STRING. If (FLAGS&1) is non-zero,
45 that we're translating a string for `echo -e', and therefore should not
46 treat a single quote as a character that may be escaped with a backslash.
47 If (FLAGS&2) is non-zero, we're expanding for the parser and want to
48 quote CTLESC and CTLNUL with CTLESC. If (flags&4) is non-zero, we want
49 to remove the backslash before any unrecognized escape sequence. */
51 ansicstr (string, len, flags, sawc, rlen)
53 int len, flags, *sawc, *rlen;
59 if (string == 0 || *string == '\0')
60 return ((char *)NULL);
62 #if defined (HANDLE_MULTIBYTE)
63 ret = (char *)xmalloc (4*len + 1);
65 ret = (char *)xmalloc (2*len + 1); /* 2*len for possible CTLESC */
67 for (r = ret, s = string; s && *s; )
70 if (c != '\\' || *s == '\0')
76 #if defined (__STDC__)
77 case 'a': c = '\a'; break;
78 case 'v': c = '\v'; break;
80 case 'a': c = (int) 0x07; break;
81 case 'v': c = (int) 0x0B; break;
83 case 'b': c = '\b'; break;
84 case 'e': case 'E': /* ESC -- non-ANSI */
86 case 'f': c = '\f'; break;
87 case 'n': c = '\n'; break;
88 case 'r': c = '\r'; break;
89 case 't': c = '\t'; break;
90 case '1': case '2': case '3':
91 case '4': case '5': case '6':
102 /* If (FLAGS & 1), we're translating a string for echo -e (or
103 the equivalent xpg_echo option), so we obey the SUSv3/
104 POSIX-2001 requirement and accept 0-3 octal digits after
106 temp = 2 + ((flags & 1) && (c == '0'));
107 for (c -= '0'; ISOCTAL (*s) && temp--; s++)
108 c = (c * 8) + OCTVALUE (*s);
111 case 'x': /* Hex digit -- non-ANSI */
112 if ((flags & 2) && *s == '{')
114 flags |= 16; /* internal flag value */
117 /* Consume at least two hex characters */
118 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)*s) && temp--; s++)
119 c = (c * 16) + HEXVALUE (*s);
120 /* DGK says that after a `\x{' ksh93 consumes ISXDIGIT chars
121 until a non-xdigit or `}', so potentially more than two
122 chars are consumed. */
125 for ( ; ISXDIGIT ((unsigned char)*s); s++)
126 c = (c * 16) + HEXVALUE (*s);
131 /* \x followed by non-hex digits is passed through unchanged */
139 #if defined (HANDLE_MULTIBYTE)
142 temp = (c == 'u') ? 4 : 8; /* \uNNNN \UNNNNNNNN */
143 for (v = 0; ISXDIGIT ((unsigned char)*s) && temp--; s++)
144 v = (v * 16) + HEXVALUE (*s);
145 if (temp == ((c == 'u') ? 4 : 8))
147 *r++ = '\\'; /* c remains unchanged */
150 else if (v <= 0x7f) /* <= 0x7f translates directly */
157 temp = u32cconv (v, r);
164 case '\'': case '"': case '?':
177 else if ((flags & 1) == 0 && *s == 0)
178 ; /* pass \c through */
179 else if ((flags & 1) == 0 && (c = *s))
182 if ((flags & 2) && c == '\\' && c == *s)
183 s++; /* Posix requires $'\c\\' do backslash escaping */
189 if ((flags & 4) == 0)
193 if ((flags & 2) && (c == CTLESC || c == CTLNUL))
204 /* Take a string STR, possibly containing non-printing characters, and turn it
205 into a $'...' ANSI-C style quoted string. Returns a new string. */
207 ansic_quote (str, flags, rlen)
216 #if defined (HANDLE_MULTIBYTE)
220 if (str == 0 || *str == 0)
225 r = ret = (char *)xmalloc (rsize);
232 for (s = str; c = *s; s++)
234 b = l = 1; /* 1 == add backslash; 0 == no backslash */
239 case ESC: c = 'E'; break;
241 case '\a': c = 'a'; break;
242 case '\v': c = 'v'; break;
244 case 0x07: c = 'a'; break;
245 case 0x0b: c = 'v'; break;
248 case '\b': c = 'b'; break;
249 case '\f': c = 'f'; break;
250 case '\n': c = 'n'; break;
251 case '\r': c = 'r'; break;
252 case '\t': c = 't'; break;
257 #if defined (HANDLE_MULTIBYTE)
259 /* XXX - clen comparison to 0 is dicey */
260 if ((b == 0 && ((clen = mbrtowc (&wc, s, MB_CUR_MAX, 0)) < 0 || MB_INVALIDCH (clen) || iswprint (wc) == 0)) ||
261 (b == 1 && ISPRINT (c) == 0))
263 if (ISPRINT (c) == 0)
267 *r++ = TOCHAR ((c >> 6) & 07);
268 *r++ = TOCHAR ((c >> 3) & 07);
269 *r++ = TOCHAR (c & 07);
275 if (b == 0 && clen == 0)
285 for (b = 0; b < (int)clen; b++)
286 *r++ = (unsigned char)s[b];
287 s += clen - 1; /* -1 because of the increment above */
298 #if defined (HANDLE_MULTIBYTE)
300 ansic_wshouldquote (string)
306 wchar_t *wcstr = NULL;
310 slen = mbstowcs (wcstr, string, 0);
314 wcstr = (wchar_t *)xmalloc (sizeof (wchar_t) * (slen + 1));
315 mbstowcs (wcstr, string, slen + 1);
317 for (wcs = wcstr; wcc = *wcs; wcs++)
318 if (iswprint(wcc) == 0)
329 /* return 1 if we need to quote with $'...' because of non-printing chars. */
331 ansic_shouldquote (string)
340 for (s = string; c = *s; s++)
342 #if defined (HANDLE_MULTIBYTE)
343 if (is_basic (c) == 0)
344 return (ansic_wshouldquote (s));
346 if (ISPRINT (c) == 0)
353 /* $'...' ANSI-C expand the portion of STRING between START and END and
354 return the result. The result cannot be longer than the input string. */
356 ansiexpand (string, start, end, lenp)
358 int start, end, *lenp;
363 temp = (char *)xmalloc (end - start + 1);
364 for (tlen = 0, len = start; len < end; )
365 temp[tlen++] = string[len++];
370 t = ansicstr (temp, tlen, 2, (int *)NULL, lenp);