]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/strtrans.c
1 /* strtrans.c - Translate and untranslate strings with ANSI-C escape sequences. */
3 /* Copyright (C) 2000-2015 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)
65 temp
= 12; /* ensure enough for eventual u32cesc */
66 ret
= (char *)xmalloc (temp
);
68 ret
= (char *)xmalloc (2*len
+ 1); /* 2*len for possible CTLESC */
70 for (r
= ret
, s
= string
; s
&& *s
; )
73 if (c
!= '\\' || *s
== '\0')
79 #if defined (__STDC__)
80 case 'a': c
= '\a'; break;
81 case 'v': c
= '\v'; break;
83 case 'a': c
= (int) 0x07; break;
84 case 'v': c
= (int) 0x0B; break;
86 case 'b': c
= '\b'; break;
87 case 'e': case 'E': /* ESC -- non-ANSI */
89 case 'f': c
= '\f'; break;
90 case 'n': c
= '\n'; break;
91 case 'r': c
= '\r'; break;
92 case 't': c
= '\t'; break;
93 case '1': case '2': case '3':
94 case '4': case '5': case '6':
105 /* If (FLAGS & 1), we're translating a string for echo -e (or
106 the equivalent xpg_echo option), so we obey the SUSv3/
107 POSIX-2001 requirement and accept 0-3 octal digits after
109 temp
= 2 + ((flags
& 1) && (c
== '0'));
110 for (c
-= '0'; ISOCTAL (*s
) && temp
--; s
++)
111 c
= (c
* 8) + OCTVALUE (*s
);
114 case 'x': /* Hex digit -- non-ANSI */
115 if ((flags
& 2) && *s
== '{')
117 flags
|= 16; /* internal flag value */
120 /* Consume at least two hex characters */
121 for (temp
= 2, c
= 0; ISXDIGIT ((unsigned char)*s
) && temp
--; s
++)
122 c
= (c
* 16) + HEXVALUE (*s
);
123 /* DGK says that after a `\x{' ksh93 consumes ISXDIGIT chars
124 until a non-xdigit or `}', so potentially more than two
125 chars are consumed. */
128 for ( ; ISXDIGIT ((unsigned char)*s
); s
++)
129 c
= (c
* 16) + HEXVALUE (*s
);
134 /* \x followed by non-hex digits is passed through unchanged */
142 #if defined (HANDLE_MULTIBYTE)
145 temp
= (c
== 'u') ? 4 : 8; /* \uNNNN \UNNNNNNNN */
146 for (v
= 0; ISXDIGIT ((unsigned char)*s
) && temp
--; s
++)
147 v
= (v
* 16) + HEXVALUE (*s
);
148 if (temp
== ((c
== 'u') ? 4 : 8))
150 *r
++ = '\\'; /* c remains unchanged */
153 else if (v
<= 0x7f) /* <= 0x7f translates directly */
160 temp
= u32cconv (v
, r
);
167 case '\'': case '"': case '?':
180 else if ((flags
& 1) == 0 && *s
== 0)
181 ; /* pass \c through */
182 else if ((flags
& 1) == 0 && (c
= *s
))
185 if ((flags
& 2) && c
== '\\' && c
== *s
)
186 s
++; /* Posix requires $'\c\\' do backslash escaping */
192 if ((flags
& 4) == 0)
196 if ((flags
& 2) && (c
== CTLESC
|| c
== CTLNUL
))
207 /* Take a string STR, possibly containing non-printing characters, and turn it
208 into a $'...' ANSI-C style quoted string. Returns a new string. */
210 ansic_quote (str
, flags
, rlen
)
219 #if defined (HANDLE_MULTIBYTE)
223 if (str
== 0 || *str
== 0)
228 r
= ret
= (char *)xmalloc (rsize
);
235 for (s
= str
; c
= *s
; s
++)
237 b
= l
= 1; /* 1 == add backslash; 0 == no backslash */
242 case ESC
: c
= 'E'; break;
244 case '\a': c
= 'a'; break;
245 case '\v': c
= 'v'; break;
247 case 0x07: c
= 'a'; break;
248 case 0x0b: c
= 'v'; break;
251 case '\b': c
= 'b'; break;
252 case '\f': c
= 'f'; break;
253 case '\n': c
= 'n'; break;
254 case '\r': c
= 'r'; break;
255 case '\t': c
= 't'; break;
260 #if defined (HANDLE_MULTIBYTE)
262 /* XXX - clen comparison to 0 is dicey */
263 if ((b
== 0 && ((clen
= mbrtowc (&wc
, s
, MB_CUR_MAX
, 0)) < 0 || MB_INVALIDCH (clen
) || iswprint (wc
) == 0)) ||
264 (b
== 1 && ISPRINT (c
) == 0))
266 if (ISPRINT (c
) == 0)
270 *r
++ = TOCHAR ((c
>> 6) & 07);
271 *r
++ = TOCHAR ((c
>> 3) & 07);
272 *r
++ = TOCHAR (c
& 07);
278 if (b
== 0 && clen
== 0)
288 for (b
= 0; b
< (int)clen
; b
++)
289 *r
++ = (unsigned char)s
[b
];
290 s
+= clen
- 1; /* -1 because of the increment above */
301 #if defined (HANDLE_MULTIBYTE)
303 ansic_wshouldquote (string
)
309 wchar_t *wcstr
= NULL
;
313 slen
= mbstowcs (wcstr
, string
, 0);
315 if (slen
== (size_t)-1)
318 wcstr
= (wchar_t *)xmalloc (sizeof (wchar_t) * (slen
+ 1));
319 mbstowcs (wcstr
, string
, slen
+ 1);
321 for (wcs
= wcstr
; wcc
= *wcs
; wcs
++)
322 if (iswprint(wcc
) == 0)
333 /* return 1 if we need to quote with $'...' because of non-printing chars. */
335 ansic_shouldquote (string
)
344 for (s
= string
; c
= *s
; s
++)
346 #if defined (HANDLE_MULTIBYTE)
347 if (is_basic (c
) == 0)
348 return (ansic_wshouldquote (s
));
350 if (ISPRINT (c
) == 0)
357 /* $'...' ANSI-C expand the portion of STRING between START and END and
358 return the result. The result cannot be longer than the input string. */
360 ansiexpand (string
, start
, end
, lenp
)
362 int start
, end
, *lenp
;
367 temp
= (char *)xmalloc (end
- start
+ 1);
368 for (tlen
= 0, len
= start
; len
< end
; )
369 temp
[tlen
++] = string
[len
++];
374 t
= ansicstr (temp
, tlen
, 2, (int *)NULL
, lenp
);