source: vendor/bash/3.1/lib/sh/strtrans.c

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 6.4 KB
Line 
1/* strtrans.c - Translate and untranslate strings with ANSI-C escape
2 sequences. */
3
4/* Copyright (C) 2000
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Bash, the Bourne Again SHell.
8
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
12 version.
13
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
17 for more details.
18
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. */
22
23#include <config.h>
24
25#if defined (HAVE_UNISTD_H)
26# include <unistd.h>
27#endif
28
29#include <bashansi.h>
30#include <stdio.h>
31#include <chartypes.h>
32
33#include "shell.h"
34
35#ifdef ESC
36#undef ESC
37#endif
38#define ESC '\033' /* ASCII */
39
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. If (flags&4) is non-zero, we want
48 to remove the backslash before any unrecognized escape sequence. */
49char *
50ansicstr (string, len, flags, sawc, rlen)
51 char *string;
52 int len, flags, *sawc, *rlen;
53{
54 int c, temp;
55 char *ret, *r, *s;
56
57 if (string == 0 || *string == '\0')
58 return ((char *)NULL);
59
60 ret = (char *)xmalloc (2*len + 1); /* 2*len for possible CTLESC */
61 for (r = ret, s = string; s && *s; )
62 {
63 c = *s++;
64 if (c != '\\' || *s == '\0')
65 *r++ = c;
66 else
67 {
68 switch (c = *s++)
69 {
70#if defined (__STDC__)
71 case 'a': c = '\a'; break;
72 case 'v': c = '\v'; break;
73#else
74 case 'a': c = '\007'; break;
75 case 'v': c = (int) 0x0B; break;
76#endif
77 case 'b': c = '\b'; break;
78 case 'e': case 'E': /* ESC -- non-ANSI */
79 c = ESC; break;
80 case 'f': c = '\f'; break;
81 case 'n': c = '\n'; break;
82 case 'r': c = '\r'; break;
83 case 't': c = '\t'; break;
84 case '0': case '1': case '2': case '3':
85 case '4': case '5': case '6': case '7':
86 /* If (FLAGS & 1), we're translating a string for echo -e (or
87 the equivalent xpg_echo option), so we obey the SUSv3/
88 POSIX-2001 requirement and accept 0-3 octal digits after
89 a leading `0'. */
90 temp = 2 + ((flags & 1) && (c == '0'));
91 for (c -= '0'; ISOCTAL (*s) && temp--; s++)
92 c = (c * 8) + OCTVALUE (*s);
93 c &= 0xFF;
94 break;
95 case 'x': /* Hex digit -- non-ANSI */
96 if ((flags & 2) && *s == '{')
97 {
98 flags |= 16; /* internal flag value */
99 s++;
100 }
101 /* Consume at least two hex characters */
102 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)*s) && temp--; s++)
103 c = (c * 16) + HEXVALUE (*s);
104 /* DGK says that after a `\x{' ksh93 consumes ISXDIGIT chars
105 until a non-xdigit or `}', so potentially more than two
106 chars are consumed. */
107 if (flags & 16)
108 {
109 for ( ; ISXDIGIT ((unsigned char)*s); s++)
110 c = (c * 16) + HEXVALUE (*s);
111 flags &= ~16;
112 if (*s == '}')
113 s++;
114 }
115 /* \x followed by non-hex digits is passed through unchanged */
116 else if (temp == 2)
117 {
118 *r++ = '\\';
119 c = 'x';
120 }
121 c &= 0xFF;
122 break;
123 case '\\':
124 break;
125 case '\'': case '"': case '?':
126 if (flags & 1)
127 *r++ = '\\';
128 break;
129 case 'c':
130 if (sawc)
131 {
132 *sawc = 1;
133 *r = '\0';
134 if (rlen)
135 *rlen = r - ret;
136 return ret;
137 }
138 else if ((flags & 1) == 0 && (c = *s))
139 {
140 s++;
141 c = TOCTRL(c);
142 break;
143 }
144 /*FALLTHROUGH*/
145 default:
146 if ((flags & 4) == 0)
147 *r++ = '\\';
148 break;
149 }
150 if ((flags & 2) && (c == CTLESC || c == CTLNUL))
151 *r++ = CTLESC;
152 *r++ = c;
153 }
154 }
155 *r = '\0';
156 if (rlen)
157 *rlen = r - ret;
158 return ret;
159}
160
161/* Take a string STR, possibly containing non-printing characters, and turn it
162 into a $'...' ANSI-C style quoted string. Returns a new string. */
163char *
164ansic_quote (str, flags, rlen)
165 char *str;
166 int flags, *rlen;
167{
168 char *r, *ret, *s;
169 int l, rsize, t;
170 unsigned char c;
171
172 if (str == 0 || *str == 0)
173 return ((char *)0);
174
175 l = strlen (str);
176 rsize = 4 * l + 4;
177 r = ret = (char *)xmalloc (rsize);
178
179 *r++ = '$';
180 *r++ = '\'';
181
182 for (s = str, l = 0; *s; s++)
183 {
184 c = *s;
185 l = 1; /* 1 == add backslash; 0 == no backslash */
186 switch (c)
187 {
188 case ESC: c = 'E'; break;
189#ifdef __STDC__
190 case '\a': c = 'a'; break;
191 case '\v': c = 'v'; break;
192#else
193 case '\007': c = 'a'; break;
194 case 0x0b: c = 'v'; break;
195#endif
196
197 case '\b': c = 'b'; break;
198 case '\f': c = 'f'; break;
199 case '\n': c = 'n'; break;
200 case '\r': c = 'r'; break;
201 case '\t': c = 't'; break;
202 case '\\':
203 case '\'':
204 break;
205 default:
206 if (ISPRINT (c) == 0)
207 {
208 *r++ = '\\';
209 *r++ = TOCHAR ((c >> 6) & 07);
210 *r++ = TOCHAR ((c >> 3) & 07);
211 *r++ = TOCHAR (c & 07);
212 continue;
213 }
214 l = 0;
215 break;
216 }
217 if (l)
218 *r++ = '\\';
219 *r++ = c;
220 }
221
222 *r++ = '\'';
223 *r = '\0';
224 if (rlen)
225 *rlen = r - ret;
226 return ret;
227}
228
229/* return 1 if we need to quote with $'...' because of non-printing chars. */
230int
231ansic_shouldquote (string)
232 const char *string;
233{
234 const char *s;
235 unsigned char c;
236
237 if (string == 0)
238 return 0;
239
240 for (s = string; c = *s; s++)
241 if (ISPRINT (c) == 0)
242 return 1;
243
244 return 0;
245}
246
247/* $'...' ANSI-C expand the portion of STRING between START and END and
248 return the result. The result cannot be longer than the input string. */
249char *
250ansiexpand (string, start, end, lenp)
251 char *string;
252 int start, end, *lenp;
253{
254 char *temp, *t;
255 int len, tlen;
256
257 temp = (char *)xmalloc (end - start + 1);
258 for (tlen = 0, len = start; len < end; )
259 temp[tlen++] = string[len++];
260 temp[tlen] = '\0';
261
262 if (*temp)
263 {
264 t = ansicstr (temp, tlen, 2, (int *)NULL, lenp);
265 free (temp);
266 return (t);
267 }
268 else
269 {
270 if (lenp)
271 *lenp = 0;
272 return (temp);
273 }
274}
Note: See TracBrowser for help on using the repository browser.