1 /* strtrans.c - Translate and untranslate strings with ANSI-C escape sequences. */
3 /* Copyright (C) 2000 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>
36 #define ESC '\033' /* ASCII */
38 /* Convert STRING by expanding the escape sequences specified by the
39 ANSI C standard. If SAWC is non-null, recognize `\c' and use that
40 as a string terminator. If we see \c, set *SAWC to 1 before
41 returning. LEN is the length of STRING. If (FLAGS&1) is non-zero,
42 that we're translating a string for `echo -e', and therefore should not
43 treat a single quote as a character that may be escaped with a backslash.
44 If (FLAGS&2) is non-zero, we're expanding for the parser and want to
45 quote CTLESC and CTLNUL with CTLESC. If (flags&4) is non-zero, we want
46 to remove the backslash before any unrecognized escape sequence. */
48 ansicstr (string, len, flags, sawc, rlen)
50 int len, flags, *sawc, *rlen;
55 if (string == 0 || *string == '\0')
56 return ((char *)NULL);
58 ret = (char *)xmalloc (2*len + 1); /* 2*len for possible CTLESC */
59 for (r = ret, s = string; s && *s; )
62 if (c != '\\' || *s == '\0')
68 #if defined (__STDC__)
69 case 'a': c = '\a'; break;
70 case 'v': c = '\v'; break;
72 case 'a': c = '\007'; break;
73 case 'v': c = (int) 0x0B; break;
75 case 'b': c = '\b'; break;
76 case 'e': case 'E': /* ESC -- non-ANSI */
78 case 'f': c = '\f'; break;
79 case 'n': c = '\n'; break;
80 case 'r': c = '\r'; break;
81 case 't': c = '\t'; break;
82 case '1': case '2': case '3':
83 case '4': case '5': case '6':
94 /* If (FLAGS & 1), we're translating a string for echo -e (or
95 the equivalent xpg_echo option), so we obey the SUSv3/
96 POSIX-2001 requirement and accept 0-3 octal digits after
98 temp = 2 + ((flags & 1) && (c == '0'));
99 for (c -= '0'; ISOCTAL (*s) && temp--; s++)
100 c = (c * 8) + OCTVALUE (*s);
103 case 'x': /* Hex digit -- non-ANSI */
104 if ((flags & 2) && *s == '{')
106 flags |= 16; /* internal flag value */
109 /* Consume at least two hex characters */
110 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)*s) && temp--; s++)
111 c = (c * 16) + HEXVALUE (*s);
112 /* DGK says that after a `\x{' ksh93 consumes ISXDIGIT chars
113 until a non-xdigit or `}', so potentially more than two
114 chars are consumed. */
117 for ( ; ISXDIGIT ((unsigned char)*s); s++)
118 c = (c * 16) + HEXVALUE (*s);
123 /* \x followed by non-hex digits is passed through unchanged */
133 case '\'': case '"': case '?':
146 else if ((flags & 1) == 0 && (c = *s))
154 if ((flags & 4) == 0)
158 if ((flags & 2) && (c == CTLESC || c == CTLNUL))
169 /* Take a string STR, possibly containing non-printing characters, and turn it
170 into a $'...' ANSI-C style quoted string. Returns a new string. */
172 ansic_quote (str, flags, rlen)
180 if (str == 0 || *str == 0)
185 r = ret = (char *)xmalloc (rsize);
190 for (s = str, l = 0; *s; s++)
193 l = 1; /* 1 == add backslash; 0 == no backslash */
196 case ESC: c = 'E'; break;
198 case '\a': c = 'a'; break;
199 case '\v': c = 'v'; break;
201 case '\007': c = 'a'; break;
202 case 0x0b: c = 'v'; break;
205 case '\b': c = 'b'; break;
206 case '\f': c = 'f'; break;
207 case '\n': c = 'n'; break;
208 case '\r': c = 'r'; break;
209 case '\t': c = 't'; break;
214 if (ISPRINT (c) == 0)
217 *r++ = TOCHAR ((c >> 6) & 07);
218 *r++ = TOCHAR ((c >> 3) & 07);
219 *r++ = TOCHAR (c & 07);
237 /* return 1 if we need to quote with $'...' because of non-printing chars. */
239 ansic_shouldquote (string)
248 for (s = string; c = *s; s++)
249 if (ISPRINT (c) == 0)
255 /* $'...' ANSI-C expand the portion of STRING between START and END and
256 return the result. The result cannot be longer than the input string. */
258 ansiexpand (string, start, end, lenp)
260 int start, end, *lenp;
265 temp = (char *)xmalloc (end - start + 1);
266 for (tlen = 0, len = start; len < end; )
267 temp[tlen++] = string[len++];
272 t = ansicstr (temp, tlen, 2, (int *)NULL, lenp);