]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/strtrans.c
7005c4656faae4d9e9a65b4f71eacbce377c078d
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)
38 #define ESC '\033' /* ASCII */
41 #define ISOCTAL(c) ((c) >= '0' && (c) <= '7')
45 #define OCTVALUE(c) ((c) - '0')
49 # define isxdigit(c) (isdigit((c)) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
53 ((c) >= 'a' && (c) <= 'f' ? (c)-'a'+10 : (c) >= 'A' && (c) <= 'F' ? (c)-'A'+10 : (c)-'0')
55 /* Convert STRING by expanding the escape sequences specified by the
56 ANSI C standard. If SAWC is non-null, recognize `\c' and use that
57 as a string terminator. If we see \c, set *SAWC to 1 before
58 returning. LEN is the length of STRING. FOR_ECHO is a flag that
59 means, if non-zero, that we're translating a string for `echo -e',
60 and therefore should not treat a single quote as a character that
61 may be escaped with a backslash. */
63 ansicstr (string
, len
, for_echo
, sawc
, rlen
)
65 int len
, for_echo
, *sawc
, *rlen
;
70 if (string
== 0 || *string
== '\0')
71 return ((char *)NULL
);
73 ret
= xmalloc (len
+ 1);
74 for (r
= ret
, s
= string
; s
&& *s
; )
77 if (c
!= '\\' || *s
== '\0')
83 #if defined (__STDC__)
84 case 'a': c
= '\a'; break;
85 case 'v': c
= '\v'; break;
87 case 'a': c
= '\007'; break;
88 case 'v': c
= (int) 0x0B; break;
90 case 'b': c
= '\b'; break;
91 case 'e': case 'E': /* ESC -- non-ANSI */
93 case 'f': c
= '\f'; break;
94 case 'n': c
= '\n'; break;
95 case 'r': c
= '\r'; break;
96 case 't': c
= '\t'; break;
97 case '0': case '1': case '2': case '3':
98 case '4': case '5': case '6': case '7':
99 for (temp
= 2, c
-= '0'; ISOCTAL (*s
) && temp
--; s
++)
100 c
= (c
* 8) + OCTVALUE (*s
);
102 case 'x': /* Hex digit -- non-ANSI */
103 for (temp
= 3, c
= 0; isxdigit (*s
) && temp
--; s
++)
104 c
= (c
* 16) + HEXVALUE (*s
);
105 /* \x followed by non-hex digits is passed through unchanged */
127 default: *r
++ = '\\'; break;
138 /* Take a string STR, possibly containing non-printing characters, and turn it
139 into a $'...' ANSI-C style quoted string. Returns a new string. */
141 ansic_quote (str
, flags
, rlen
)
145 char *r
, *ret
, *s
, obuf
[8];
148 if (str
== 0 || *str
== 0)
153 r
= ret
= xmalloc (rsize
);
158 for (s
= str
, l
= 0; *s
; s
++)
160 c
= *(unsigned char *)s
;
161 l
= 1; /* 1 == add backslash; 0 == no backslash */
164 case ESC
: c
= 'E'; break;
166 case '\a': c
= 'a'; break;
167 case '\v': c
= 'v'; break;
169 case '\007': c
= 'a'; break;
170 case 0x0b: c
= 'v'; break;
173 case '\b': c
= 'b'; break;
174 case '\f': c
= 'f'; break;
175 case '\n': c
= 'n'; break;
176 case '\r': c
= 'r'; break;
177 case '\t': c
= 't'; break;
182 if (isprint (c
) == 0)
184 sprintf (obuf
, "\\%.3o", c
);
186 RESIZE_MALLOCED_BUFFER (ret
, t
, 5, rsize
, 16);
187 r
= ret
+ t
; /* in case reallocated */
188 for (t
= 0; t
< 4; t
++)