1 /* strtrans.c - Translate and untranslate strings with ANSI-C escape
5 Free Software Foundation, Inc.
7 This file is part of GNU Bash, the Bourne Again SHell.
9 Bash is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License along
20 with Bash; see the file COPYING. If not, write to the Free Software
21 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
25 #if defined (HAVE_UNISTD_H)
31 #include <chartypes.h>
38 #define ESC '\033' /* ASCII */
40 /* Convert STRING by expanding the escape sequences specified by the
41 ANSI C standard. If SAWC is non-null, recognize `\c' and use that
42 as a string terminator. If we see \c, set *SAWC to 1 before
43 returning. LEN is the length of STRING. If (FLAGS&1) is non-zero,
44 that we're translating a string for `echo -e', and therefore should not
45 treat a single quote as a character that may be escaped with a backslash.
46 If (FLAGS&2) is non-zero, we're expanding for the parser and want to
47 quote CTLESC and CTLNUL with CTLESC */
49 ansicstr (string, len, flags, sawc, rlen)
51 int len, flags, *sawc, *rlen;
56 if (string == 0 || *string == '\0')
57 return ((char *)NULL);
59 ret = (char *)xmalloc (2*len + 1); /* 2*len for possible CTLESC */
60 for (r = ret, s = string; s && *s; )
63 if (c != '\\' || *s == '\0')
69 #if defined (__STDC__)
70 case 'a': c = '\a'; break;
71 case 'v': c = '\v'; break;
73 case 'a': c = '\007'; break;
74 case 'v': c = (int) 0x0B; break;
76 case 'b': c = '\b'; break;
77 case 'e': case 'E': /* ESC -- non-ANSI */
79 case 'f': c = '\f'; break;
80 case 'n': c = '\n'; break;
81 case 'r': c = '\r'; break;
82 case 't': c = '\t'; break;
83 case '0': case '1': case '2': case '3':
84 case '4': case '5': case '6': case '7':
85 /* If (FLAGS & 1), we're translating a string for echo -e (or
86 the equivalent xpg_echo option), so we obey the SUSv3/
87 POSIX-2001 requirement and accept 0-3 octal digits after
89 temp = 2 + ((flags & 1) && (c == '0'));
90 for (c -= '0'; ISOCTAL (*s) && temp--; s++)
91 c = (c * 8) + OCTVALUE (*s);
94 case 'x': /* Hex digit -- non-ANSI */
95 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)*s) && temp--; s++)
96 c = (c * 16) + HEXVALUE (*s);
97 /* \x followed by non-hex digits is passed through unchanged */
120 else if ((flags & 1) == 0 && (c = *s))
127 default: *r++ = '\\'; break;
129 if ((flags & 2) && (c == CTLESC || c == CTLNUL))
140 /* Take a string STR, possibly containing non-printing characters, and turn it
141 into a $'...' ANSI-C style quoted string. Returns a new string. */
143 ansic_quote (str, flags, rlen)
151 if (str == 0 || *str == 0)
156 r = ret = (char *)xmalloc (rsize);
161 for (s = str, l = 0; *s; s++)
164 l = 1; /* 1 == add backslash; 0 == no backslash */
167 case ESC: c = 'E'; break;
169 case '\a': c = 'a'; break;
170 case '\v': c = 'v'; break;
172 case '\007': c = 'a'; break;
173 case 0x0b: c = 'v'; break;
176 case '\b': c = 'b'; break;
177 case '\f': c = 'f'; break;
178 case '\n': c = 'n'; break;
179 case '\r': c = 'r'; break;
180 case '\t': c = 't'; break;
185 if (ISPRINT (c) == 0)
188 *r++ = TOCHAR ((c >> 6) & 07);
189 *r++ = TOCHAR ((c >> 3) & 07);
190 *r++ = TOCHAR (c & 07);
208 /* return 1 if we need to quote with $'...' because of non-printing chars. */
210 ansic_shouldquote (string)
219 for (s = string; c = *s; s++)
220 if (ISPRINT (c) == 0)
226 /* $'...' ANSI-C expand the portion of STRING between START and END and
227 return the result. The result cannot be longer than the input string. */
229 ansiexpand (string, start, end, lenp)
231 int start, end, *lenp;
236 temp = (char *)xmalloc (end - start + 1);
237 for (tlen = 0, len = start; len < end; )
238 temp[tlen++] = string[len++];
243 t = ansicstr (temp, tlen, 2, (int *)NULL, lenp);