Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/lib/sh/strtrans.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* strtrans.c - Translate and untranslate strings with ANSI-C escape sequences. */ | - | ||||||||||||||||||||||||||||||||||||
2 | - | |||||||||||||||||||||||||||||||||||||
3 | /* Copyright (C) 2000-2015 Free Software Foundation, Inc. | - | ||||||||||||||||||||||||||||||||||||
4 | - | |||||||||||||||||||||||||||||||||||||
5 | This file is part of GNU Bash, the Bourne Again SHell. | - | ||||||||||||||||||||||||||||||||||||
6 | - | |||||||||||||||||||||||||||||||||||||
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. | - | ||||||||||||||||||||||||||||||||||||
11 | - | |||||||||||||||||||||||||||||||||||||
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. | - | ||||||||||||||||||||||||||||||||||||
16 | - | |||||||||||||||||||||||||||||||||||||
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/>. | - | ||||||||||||||||||||||||||||||||||||
19 | */ | - | ||||||||||||||||||||||||||||||||||||
20 | - | |||||||||||||||||||||||||||||||||||||
21 | #include <config.h> | - | ||||||||||||||||||||||||||||||||||||
22 | - | |||||||||||||||||||||||||||||||||||||
23 | #if defined (HAVE_UNISTD_H) | - | ||||||||||||||||||||||||||||||||||||
24 | # include <unistd.h> | - | ||||||||||||||||||||||||||||||||||||
25 | #endif | - | ||||||||||||||||||||||||||||||||||||
26 | - | |||||||||||||||||||||||||||||||||||||
27 | #include <bashansi.h> | - | ||||||||||||||||||||||||||||||||||||
28 | #include <stdio.h> | - | ||||||||||||||||||||||||||||||||||||
29 | #include <chartypes.h> | - | ||||||||||||||||||||||||||||||||||||
30 | - | |||||||||||||||||||||||||||||||||||||
31 | #include "shell.h" | - | ||||||||||||||||||||||||||||||||||||
32 | - | |||||||||||||||||||||||||||||||||||||
33 | #include "shmbchar.h" | - | ||||||||||||||||||||||||||||||||||||
34 | #include "shmbutil.h" | - | ||||||||||||||||||||||||||||||||||||
35 | - | |||||||||||||||||||||||||||||||||||||
36 | #ifdef ESC | - | ||||||||||||||||||||||||||||||||||||
37 | #undef ESC | - | ||||||||||||||||||||||||||||||||||||
38 | #endif | - | ||||||||||||||||||||||||||||||||||||
39 | #define ESC '\033' /* ASCII */ | - | ||||||||||||||||||||||||||||||||||||
40 | - | |||||||||||||||||||||||||||||||||||||
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. */ | - | ||||||||||||||||||||||||||||||||||||
50 | char * | - | ||||||||||||||||||||||||||||||||||||
51 | ansicstr (string, len, flags, sawc, rlen) | - | ||||||||||||||||||||||||||||||||||||
52 | char *string; | - | ||||||||||||||||||||||||||||||||||||
53 | int len, flags, *sawc, *rlen; | - | ||||||||||||||||||||||||||||||||||||
54 | { | - | ||||||||||||||||||||||||||||||||||||
55 | int c, temp; | - | ||||||||||||||||||||||||||||||||||||
56 | char *ret, *r, *s; | - | ||||||||||||||||||||||||||||||||||||
57 | unsigned long v; | - | ||||||||||||||||||||||||||||||||||||
58 | - | |||||||||||||||||||||||||||||||||||||
59 | if (string == 0 || *string == '\0')
| 0-2682 | ||||||||||||||||||||||||||||||||||||
60 | return ((char *)NULL); never executed: return ((char *) ((void *)0) ); | 0 | ||||||||||||||||||||||||||||||||||||
61 | - | |||||||||||||||||||||||||||||||||||||
62 | #if defined (HANDLE_MULTIBYTE) | - | ||||||||||||||||||||||||||||||||||||
63 | temp = 4*len + 1; | - | ||||||||||||||||||||||||||||||||||||
64 | if (temp < 12)
| 176-2506 | ||||||||||||||||||||||||||||||||||||
65 | temp = 12; /* ensure enough for eventual u32cesc */ executed 176 times by 1 test: temp = 12; Executed by:
| 176 | ||||||||||||||||||||||||||||||||||||
66 | ret = (char *)xmalloc (temp); | - | ||||||||||||||||||||||||||||||||||||
67 | #else | - | ||||||||||||||||||||||||||||||||||||
68 | ret = (char *)xmalloc (2*len + 1); /* 2*len for possible CTLESC */ | - | ||||||||||||||||||||||||||||||||||||
69 | #endif | - | ||||||||||||||||||||||||||||||||||||
70 | for (r = ret, s = string; s && *s; )
| 0-9248 | ||||||||||||||||||||||||||||||||||||
71 | { | - | ||||||||||||||||||||||||||||||||||||
72 | c = *s++; | - | ||||||||||||||||||||||||||||||||||||
73 | if (c != '\\' || *s == '\0')
| 0-4907 | ||||||||||||||||||||||||||||||||||||
74 | *r++ = c; executed 1679 times by 1 test: *r++ = c; Executed by:
| 1679 | ||||||||||||||||||||||||||||||||||||
75 | else | - | ||||||||||||||||||||||||||||||||||||
76 | { | - | ||||||||||||||||||||||||||||||||||||
77 | switch (c = *s++) | - | ||||||||||||||||||||||||||||||||||||
78 | { | - | ||||||||||||||||||||||||||||||||||||
79 | #if defined (__STDC__) | - | ||||||||||||||||||||||||||||||||||||
80 | case 'a': c = '\a'; break; executed 28 times by 1 test: break; Executed by:
executed 28 times by 1 test: case 'a': Executed by:
| 28 | ||||||||||||||||||||||||||||||||||||
81 | case 'v': c = '\v'; break; executed 16 times by 1 test: break; Executed by:
executed 16 times by 1 test: case 'v': Executed by:
| 16 | ||||||||||||||||||||||||||||||||||||
82 | #else | - | ||||||||||||||||||||||||||||||||||||
83 | case 'a': c = (int) 0x07; break; | - | ||||||||||||||||||||||||||||||||||||
84 | case 'v': c = (int) 0x0B; break; | - | ||||||||||||||||||||||||||||||||||||
85 | #endif | - | ||||||||||||||||||||||||||||||||||||
86 | case 'b': c = '\b'; break; executed 18 times by 1 test: break; Executed by:
executed 18 times by 1 test: case 'b': Executed by:
| 18 | ||||||||||||||||||||||||||||||||||||
87 | case 'e': case 'E': /* ESC -- non-ANSI */ executed 10 times by 1 test: case 'e': Executed by:
executed 1 time by 1 test: case 'E': Executed by:
| 1-10 | ||||||||||||||||||||||||||||||||||||
88 | c = ESC; break; executed 11 times by 1 test: break; Executed by:
| 11 | ||||||||||||||||||||||||||||||||||||
89 | case 'f': c = '\f'; break; executed 16 times by 1 test: break; Executed by:
executed 16 times by 1 test: case 'f': Executed by:
| 16 | ||||||||||||||||||||||||||||||||||||
90 | case 'n': c = '\n'; break; executed 218 times by 1 test: break; Executed by:
executed 218 times by 1 test: case 'n': Executed by:
| 218 | ||||||||||||||||||||||||||||||||||||
91 | case 'r': c = '\r'; break; executed 11 times by 1 test: break; Executed by:
executed 11 times by 1 test: case 'r': Executed by:
| 11 | ||||||||||||||||||||||||||||||||||||
92 | case 't': c = '\t'; break; executed 60 times by 1 test: break; Executed by:
executed 60 times by 1 test: case 't': Executed by:
| 60 | ||||||||||||||||||||||||||||||||||||
93 | case '1': case '2': case '3': executed 293 times by 1 test: case '1': Executed by:
executed 2093 times by 1 test: case '2': Executed by:
executed 1284 times by 1 test: case '3': Executed by:
| 293-2093 | ||||||||||||||||||||||||||||||||||||
94 | case '4': case '5': case '6': never executed: case '4': never executed: case '5': never executed: case '6': | 0 | ||||||||||||||||||||||||||||||||||||
95 | case '7': never executed: case '7': | 0 | ||||||||||||||||||||||||||||||||||||
96 | #if 1 | - | ||||||||||||||||||||||||||||||||||||
97 | if (flags & 1)
| 0-3670 | ||||||||||||||||||||||||||||||||||||
98 | { | - | ||||||||||||||||||||||||||||||||||||
99 | *r++ = '\\'; | - | ||||||||||||||||||||||||||||||||||||
100 | break; never executed: break; | 0 | ||||||||||||||||||||||||||||||||||||
101 | } | - | ||||||||||||||||||||||||||||||||||||
102 | /*FALLTHROUGH*/ | - | ||||||||||||||||||||||||||||||||||||
103 | #endif | - | ||||||||||||||||||||||||||||||||||||
104 | case '0': code before this statement executed 3670 times by 1 test: case '0': Executed by:
executed 332 times by 1 test: case '0': Executed by:
| 332-3670 | ||||||||||||||||||||||||||||||||||||
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 | - | ||||||||||||||||||||||||||||||||||||
108 | a leading `0'. */ | - | ||||||||||||||||||||||||||||||||||||
109 | temp = 2 + ((flags & 1) && (c == '0'));
| 0-3995 | ||||||||||||||||||||||||||||||||||||
110 | for (c -= '0'; ISOCTAL (*s) && temp--; s++)
| 4-10112 | ||||||||||||||||||||||||||||||||||||
111 | c = (c * 8) + OCTVALUE (*s); executed 7966 times by 1 test: c = (c * 8) + ((*s) - '0'); Executed by:
| 7966 | ||||||||||||||||||||||||||||||||||||
112 | c &= 0xFF; | - | ||||||||||||||||||||||||||||||||||||
113 | break; executed 4002 times by 1 test: break; Executed by:
| 4002 | ||||||||||||||||||||||||||||||||||||
114 | case 'x': /* Hex digit -- non-ANSI */ executed 269 times by 1 test: case 'x': Executed by:
| 269 | ||||||||||||||||||||||||||||||||||||
115 | if ((flags & 2) && *s == '{')
| 14-255 | ||||||||||||||||||||||||||||||||||||
116 | { | - | ||||||||||||||||||||||||||||||||||||
117 | flags |= 16; /* internal flag value */ | - | ||||||||||||||||||||||||||||||||||||
118 | s++; | - | ||||||||||||||||||||||||||||||||||||
119 | } executed 16 times by 1 test: end of block Executed by:
| 16 | ||||||||||||||||||||||||||||||||||||
120 | /* Consume at least two hex characters */ | - | ||||||||||||||||||||||||||||||||||||
121 | for (temp = 2, c = 0; ISXDIGIT ((unsigned char)*s) && temp--; s++)
| 22-529 | ||||||||||||||||||||||||||||||||||||
122 | c = (c * 16) + HEXVALUE (*s); executed 507 times by 1 test: c = (c * 16) + (((*s) >= 'a' && (*s) <= 'f') ? (*s)-'a'+10 : (*s) >= 'A' && (*s) <= 'F' ? (*s)-'A'+10 : (*s)-'0'); Executed by:
| 0-507 | ||||||||||||||||||||||||||||||||||||
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. */ | - | ||||||||||||||||||||||||||||||||||||
126 | if (flags & 16)
| 16-253 | ||||||||||||||||||||||||||||||||||||
127 | { | - | ||||||||||||||||||||||||||||||||||||
128 | for ( ; ISXDIGIT ((unsigned char)*s); s++)
| 10-16 | ||||||||||||||||||||||||||||||||||||
129 | c = (c * 16) + HEXVALUE (*s); executed 10 times by 1 test: c = (c * 16) + (((*s) >= 'a' && (*s) <= 'f') ? (*s)-'a'+10 : (*s) >= 'A' && (*s) <= 'F' ? (*s)-'A'+10 : (*s)-'0'); Executed by:
| 0-10 | ||||||||||||||||||||||||||||||||||||
130 | flags &= ~16; | - | ||||||||||||||||||||||||||||||||||||
131 | if (*s == '}')
| 6-10 | ||||||||||||||||||||||||||||||||||||
132 | s++; executed 10 times by 1 test: s++; Executed by:
| 10 | ||||||||||||||||||||||||||||||||||||
133 | } executed 16 times by 1 test: end of block Executed by:
| 16 | ||||||||||||||||||||||||||||||||||||
134 | /* \x followed by non-hex digits is passed through unchanged */ | - | ||||||||||||||||||||||||||||||||||||
135 | else if (temp == 2)
| 10-243 | ||||||||||||||||||||||||||||||||||||
136 | { | - | ||||||||||||||||||||||||||||||||||||
137 | *r++ = '\\'; | - | ||||||||||||||||||||||||||||||||||||
138 | c = 'x'; | - | ||||||||||||||||||||||||||||||||||||
139 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||||||||||||||||||||||||||
140 | c &= 0xFF; | - | ||||||||||||||||||||||||||||||||||||
141 | break; executed 269 times by 1 test: break; Executed by:
| 269 | ||||||||||||||||||||||||||||||||||||
142 | #if defined (HANDLE_MULTIBYTE) | - | ||||||||||||||||||||||||||||||||||||
143 | case 'u': executed 81 times by 1 test: case 'u': Executed by:
| 81 | ||||||||||||||||||||||||||||||||||||
144 | case 'U': executed 26 times by 1 test: case 'U': Executed by:
| 26 | ||||||||||||||||||||||||||||||||||||
145 | temp = (c == 'u') ? 4 : 8; /* \uNNNN \UNNNNNNNN */
| 26-81 | ||||||||||||||||||||||||||||||||||||
146 | for (v = 0; ISXDIGIT ((unsigned char)*s) && temp--; s++)
| 0-423 | ||||||||||||||||||||||||||||||||||||
147 | v = (v * 16) + HEXVALUE (*s); executed 423 times by 1 test: v = (v * 16) + (((*s) >= 'a' && (*s) <= 'f') ? (*s)-'a'+10 : (*s) >= 'A' && (*s) <= 'F' ? (*s)-'A'+10 : (*s)-'0'); Executed by:
| 0-423 | ||||||||||||||||||||||||||||||||||||
148 | if (temp == ((c == 'u') ? 4 : 8))
| 0-107 | ||||||||||||||||||||||||||||||||||||
149 | { | - | ||||||||||||||||||||||||||||||||||||
150 | *r++ = '\\'; /* c remains unchanged */ | - | ||||||||||||||||||||||||||||||||||||
151 | break; never executed: break; | 0 | ||||||||||||||||||||||||||||||||||||
152 | } | - | ||||||||||||||||||||||||||||||||||||
153 | else if (v <= 0x7f) /* <= 0x7f translates directly */
| 1-106 | ||||||||||||||||||||||||||||||||||||
154 | { | - | ||||||||||||||||||||||||||||||||||||
155 | c = v; | - | ||||||||||||||||||||||||||||||||||||
156 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||||||||||||||||||||||||||
157 | } | - | ||||||||||||||||||||||||||||||||||||
158 | else | - | ||||||||||||||||||||||||||||||||||||
159 | { | - | ||||||||||||||||||||||||||||||||||||
160 | temp = u32cconv (v, r); | - | ||||||||||||||||||||||||||||||||||||
161 | r += temp; | - | ||||||||||||||||||||||||||||||||||||
162 | continue; executed 106 times by 1 test: continue; Executed by:
| 106 | ||||||||||||||||||||||||||||||||||||
163 | } | - | ||||||||||||||||||||||||||||||||||||
164 | #endif | - | ||||||||||||||||||||||||||||||||||||
165 | case '\\': executed 37 times by 1 test: case '\\': Executed by:
| 37 | ||||||||||||||||||||||||||||||||||||
166 | break; executed 37 times by 1 test: break; Executed by:
| 37 | ||||||||||||||||||||||||||||||||||||
167 | case '\'': case '"': case '?': executed 45 times by 1 test: case '\'': Executed by:
executed 5 times by 1 test: case '"': Executed by:
never executed: case '?': | 0-45 | ||||||||||||||||||||||||||||||||||||
168 | if (flags & 1)
| 20-30 | ||||||||||||||||||||||||||||||||||||
169 | *r++ = '\\'; executed 20 times by 1 test: *r++ = '\\'; Executed by:
| 20 | ||||||||||||||||||||||||||||||||||||
170 | break; executed 50 times by 1 test: break; Executed by:
| 50 | ||||||||||||||||||||||||||||||||||||
171 | case 'c': executed 48 times by 1 test: case 'c': Executed by:
| 48 | ||||||||||||||||||||||||||||||||||||
172 | if (sawc)
| 20-28 | ||||||||||||||||||||||||||||||||||||
173 | { | - | ||||||||||||||||||||||||||||||||||||
174 | *sawc = 1; | - | ||||||||||||||||||||||||||||||||||||
175 | *r = '\0'; | - | ||||||||||||||||||||||||||||||||||||
176 | if (rlen)
| 0-20 | ||||||||||||||||||||||||||||||||||||
177 | *rlen = r - ret; executed 20 times by 1 test: *rlen = r - ret; Executed by:
| 20 | ||||||||||||||||||||||||||||||||||||
178 | return ret; executed 20 times by 1 test: return ret; Executed by:
| 20 | ||||||||||||||||||||||||||||||||||||
179 | } | - | ||||||||||||||||||||||||||||||||||||
180 | else if ((flags & 1) == 0 && *s == 0)
| 0-28 | ||||||||||||||||||||||||||||||||||||
181 | ; /* pass \c through */ never executed: ; | 0 | ||||||||||||||||||||||||||||||||||||
182 | else if ((flags & 1) == 0 && (c = *s))
| 0-28 | ||||||||||||||||||||||||||||||||||||
183 | { | - | ||||||||||||||||||||||||||||||||||||
184 | s++; | - | ||||||||||||||||||||||||||||||||||||
185 | if ((flags & 2) && c == '\\' && c == *s)
| 0-28 | ||||||||||||||||||||||||||||||||||||
186 | s++; /* Posix requires $'\c\\' do backslash escaping */ executed 3 times by 1 test: s++; Executed by:
| 3 | ||||||||||||||||||||||||||||||||||||
187 | c = TOCTRL(c); never executed: end of block never executed: __res = toupper ( c ); never executed: __res = (*__ctype_toupper_loc ())[(int) ( c )];
| 0-15 | ||||||||||||||||||||||||||||||||||||
188 | break; executed 28 times by 1 test: break; Executed by:
| 28 | ||||||||||||||||||||||||||||||||||||
189 | } | - | ||||||||||||||||||||||||||||||||||||
190 | /*FALLTHROUGH*/ | - | ||||||||||||||||||||||||||||||||||||
191 | default: code before this statement never executed: default: executed 16 times by 1 test: default: Executed by:
| 0-16 | ||||||||||||||||||||||||||||||||||||
192 | if ((flags & 4) == 0)
| 0-16 | ||||||||||||||||||||||||||||||||||||
193 | *r++ = '\\'; executed 16 times by 1 test: *r++ = '\\'; Executed by:
| 16 | ||||||||||||||||||||||||||||||||||||
194 | break; executed 16 times by 1 test: break; Executed by:
| 16 | ||||||||||||||||||||||||||||||||||||
195 | } | - | ||||||||||||||||||||||||||||||||||||
196 | if ((flags & 2) && (c == CTLESC || c == CTLNUL))
| 61-4720 | ||||||||||||||||||||||||||||||||||||
197 | *r++ = CTLESC; executed 501 times by 1 test: *r++ = '\001'; Executed by:
| 501 | ||||||||||||||||||||||||||||||||||||
198 | *r++ = c; | - | ||||||||||||||||||||||||||||||||||||
199 | } executed 4781 times by 1 test: end of block Executed by:
| 4781 | ||||||||||||||||||||||||||||||||||||
200 | } | - | ||||||||||||||||||||||||||||||||||||
201 | *r = '\0'; | - | ||||||||||||||||||||||||||||||||||||
202 | if (rlen)
| 11-2651 | ||||||||||||||||||||||||||||||||||||
203 | *rlen = r - ret; executed 2651 times by 1 test: *rlen = r - ret; Executed by:
| 2651 | ||||||||||||||||||||||||||||||||||||
204 | return ret; executed 2662 times by 1 test: return ret; Executed by:
| 2662 | ||||||||||||||||||||||||||||||||||||
205 | } | - | ||||||||||||||||||||||||||||||||||||
206 | - | |||||||||||||||||||||||||||||||||||||
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. */ | - | ||||||||||||||||||||||||||||||||||||
209 | char * | - | ||||||||||||||||||||||||||||||||||||
210 | ansic_quote (str, flags, rlen) | - | ||||||||||||||||||||||||||||||||||||
211 | char *str; | - | ||||||||||||||||||||||||||||||||||||
212 | int flags, *rlen; | - | ||||||||||||||||||||||||||||||||||||
213 | { | - | ||||||||||||||||||||||||||||||||||||
214 | char *r, *ret, *s; | - | ||||||||||||||||||||||||||||||||||||
215 | int l, rsize; | - | ||||||||||||||||||||||||||||||||||||
216 | unsigned char c; | - | ||||||||||||||||||||||||||||||||||||
217 | size_t clen; | - | ||||||||||||||||||||||||||||||||||||
218 | int b; | - | ||||||||||||||||||||||||||||||||||||
219 | #if defined (HANDLE_MULTIBYTE) | - | ||||||||||||||||||||||||||||||||||||
220 | wchar_t wc; | - | ||||||||||||||||||||||||||||||||||||
221 | #endif | - | ||||||||||||||||||||||||||||||||||||
222 | - | |||||||||||||||||||||||||||||||||||||
223 | if (str == 0 || *str == 0)
| 0-198 | ||||||||||||||||||||||||||||||||||||
224 | return ((char *)0); never executed: return ((char *)0); | 0 | ||||||||||||||||||||||||||||||||||||
225 | - | |||||||||||||||||||||||||||||||||||||
226 | l = strlen (str); | - | ||||||||||||||||||||||||||||||||||||
227 | rsize = 4 * l + 4; | - | ||||||||||||||||||||||||||||||||||||
228 | r = ret = (char *)xmalloc (rsize); | - | ||||||||||||||||||||||||||||||||||||
229 | - | |||||||||||||||||||||||||||||||||||||
230 | *r++ = '$'; | - | ||||||||||||||||||||||||||||||||||||
231 | *r++ = '\''; | - | ||||||||||||||||||||||||||||||||||||
232 | - | |||||||||||||||||||||||||||||||||||||
233 | for (s = str; c = *s; s++)
| 198-337 | ||||||||||||||||||||||||||||||||||||
234 | { | - | ||||||||||||||||||||||||||||||||||||
235 | b = l = 1; /* 1 == add backslash; 0 == no backslash */ | - | ||||||||||||||||||||||||||||||||||||
236 | clen = 1; | - | ||||||||||||||||||||||||||||||||||||
237 | - | |||||||||||||||||||||||||||||||||||||
238 | switch (c) | - | ||||||||||||||||||||||||||||||||||||
239 | { | - | ||||||||||||||||||||||||||||||||||||
240 | case ESC: c = 'E'; break; never executed: break; never executed: case '\033': | 0 | ||||||||||||||||||||||||||||||||||||
241 | #ifdef __STDC__ | - | ||||||||||||||||||||||||||||||||||||
242 | case '\a': c = 'a'; break; executed 6 times by 1 test: break; Executed by:
executed 6 times by 1 test: case '\a': Executed by:
| 6 | ||||||||||||||||||||||||||||||||||||
243 | case '\v': c = 'v'; break; never executed: break; never executed: case '\v': | 0 | ||||||||||||||||||||||||||||||||||||
244 | #else | - | ||||||||||||||||||||||||||||||||||||
245 | case 0x07: c = 'a'; break; | - | ||||||||||||||||||||||||||||||||||||
246 | case 0x0b: c = 'v'; break; | - | ||||||||||||||||||||||||||||||||||||
247 | #endif | - | ||||||||||||||||||||||||||||||||||||
248 | - | |||||||||||||||||||||||||||||||||||||
249 | case '\b': c = 'b'; break; never executed: break; never executed: case '\b': | 0 | ||||||||||||||||||||||||||||||||||||
250 | case '\f': c = 'f'; break; never executed: break; never executed: case '\f': | 0 | ||||||||||||||||||||||||||||||||||||
251 | case '\n': c = 'n'; break; executed 12 times by 1 test: break; Executed by:
executed 12 times by 1 test: case '\n': Executed by:
| 12 | ||||||||||||||||||||||||||||||||||||
252 | case '\r': c = 'r'; break; never executed: break; never executed: case '\r': | 0 | ||||||||||||||||||||||||||||||||||||
253 | case '\t': c = 't'; break; executed 11 times by 1 test: break; Executed by:
executed 11 times by 1 test: case '\t': Executed by:
| 11 | ||||||||||||||||||||||||||||||||||||
254 | case '\\': executed 6 times by 1 test: case '\\': Executed by:
| 6 | ||||||||||||||||||||||||||||||||||||
255 | case '\'': never executed: case '\'': | 0 | ||||||||||||||||||||||||||||||||||||
256 | break; executed 6 times by 1 test: break; Executed by:
| 6 | ||||||||||||||||||||||||||||||||||||
257 | default: executed 302 times by 1 test: default: Executed by:
| 302 | ||||||||||||||||||||||||||||||||||||
258 | #if defined (HANDLE_MULTIBYTE) | - | ||||||||||||||||||||||||||||||||||||
259 | b = is_basic (c); | - | ||||||||||||||||||||||||||||||||||||
260 | /* XXX - clen comparison to 0 is dicey */ | - | ||||||||||||||||||||||||||||||||||||
261 | if ((b == 0 && ((clen = mbrtowc (&wc, s, MB_CUR_MAX, 0)) < 0 || MB_INVALIDCH (clen) || iswprint (wc) == 0)) ||
| 0-246 | ||||||||||||||||||||||||||||||||||||
262 | (b == 1 && ISPRINT (c) == 0))
| 0-56 | ||||||||||||||||||||||||||||||||||||
263 | #else | - | ||||||||||||||||||||||||||||||||||||
264 | if (ISPRINT (c) == 0) | - | ||||||||||||||||||||||||||||||||||||
265 | #endif | - | ||||||||||||||||||||||||||||||||||||
266 | { | - | ||||||||||||||||||||||||||||||||||||
267 | *r++ = '\\'; | - | ||||||||||||||||||||||||||||||||||||
268 | *r++ = TOCHAR ((c >> 6) & 07); | - | ||||||||||||||||||||||||||||||||||||
269 | *r++ = TOCHAR ((c >> 3) & 07); | - | ||||||||||||||||||||||||||||||||||||
270 | *r++ = TOCHAR (c & 07); | - | ||||||||||||||||||||||||||||||||||||
271 | continue; executed 242 times by 1 test: continue; Executed by:
| 242 | ||||||||||||||||||||||||||||||||||||
272 | } | - | ||||||||||||||||||||||||||||||||||||
273 | l = 0; | - | ||||||||||||||||||||||||||||||||||||
274 | break; executed 60 times by 1 test: break; Executed by:
| 60 | ||||||||||||||||||||||||||||||||||||
275 | } | - | ||||||||||||||||||||||||||||||||||||
276 | if (b == 0 && clen == 0)
| 0-91 | ||||||||||||||||||||||||||||||||||||
277 | break; never executed: break; | 0 | ||||||||||||||||||||||||||||||||||||
278 | - | |||||||||||||||||||||||||||||||||||||
279 | if (l)
| 35-60 | ||||||||||||||||||||||||||||||||||||
280 | *r++ = '\\'; executed 35 times by 1 test: *r++ = '\\'; Executed by:
| 35 | ||||||||||||||||||||||||||||||||||||
281 | - | |||||||||||||||||||||||||||||||||||||
282 | if (clen == 1)
| 0-95 | ||||||||||||||||||||||||||||||||||||
283 | *r++ = c; executed 95 times by 1 test: *r++ = c; Executed by:
| 95 | ||||||||||||||||||||||||||||||||||||
284 | else | - | ||||||||||||||||||||||||||||||||||||
285 | { | - | ||||||||||||||||||||||||||||||||||||
286 | for (b = 0; b < (int)clen; b++)
| 0 | ||||||||||||||||||||||||||||||||||||
287 | *r++ = (unsigned char)s[b]; never executed: *r++ = (unsigned char)s[b]; | 0 | ||||||||||||||||||||||||||||||||||||
288 | s += clen - 1; /* -1 because of the increment above */ | - | ||||||||||||||||||||||||||||||||||||
289 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||||||||
290 | } | - | ||||||||||||||||||||||||||||||||||||
291 | - | |||||||||||||||||||||||||||||||||||||
292 | *r++ = '\''; | - | ||||||||||||||||||||||||||||||||||||
293 | *r = '\0'; | - | ||||||||||||||||||||||||||||||||||||
294 | if (rlen)
| 0-198 | ||||||||||||||||||||||||||||||||||||
295 | *rlen = r - ret; never executed: *rlen = r - ret; | 0 | ||||||||||||||||||||||||||||||||||||
296 | return ret; executed 198 times by 1 test: return ret; Executed by:
| 198 | ||||||||||||||||||||||||||||||||||||
297 | } | - | ||||||||||||||||||||||||||||||||||||
298 | - | |||||||||||||||||||||||||||||||||||||
299 | #if defined (HANDLE_MULTIBYTE) | - | ||||||||||||||||||||||||||||||||||||
300 | int | - | ||||||||||||||||||||||||||||||||||||
301 | ansic_wshouldquote (string) | - | ||||||||||||||||||||||||||||||||||||
302 | const char *string; | - | ||||||||||||||||||||||||||||||||||||
303 | { | - | ||||||||||||||||||||||||||||||||||||
304 | const wchar_t *wcs; | - | ||||||||||||||||||||||||||||||||||||
305 | wchar_t wcc; | - | ||||||||||||||||||||||||||||||||||||
306 | wchar_t *wcstr = NULL; | - | ||||||||||||||||||||||||||||||||||||
307 | size_t slen; | - | ||||||||||||||||||||||||||||||||||||
308 | - | |||||||||||||||||||||||||||||||||||||
309 | slen = mbstowcs (wcstr, string, 0); | - | ||||||||||||||||||||||||||||||||||||
310 | - | |||||||||||||||||||||||||||||||||||||
311 | if (slen == (size_t)-1)
| 131-168 | ||||||||||||||||||||||||||||||||||||
312 | return 1; executed 131 times by 1 test: return 1; Executed by:
| 131 | ||||||||||||||||||||||||||||||||||||
313 | - | |||||||||||||||||||||||||||||||||||||
314 | wcstr = (wchar_t *)xmalloc (sizeof (wchar_t) * (slen + 1)); | - | ||||||||||||||||||||||||||||||||||||
315 | mbstowcs (wcstr, string, slen + 1); | - | ||||||||||||||||||||||||||||||||||||
316 | - | |||||||||||||||||||||||||||||||||||||
317 | for (wcs = wcstr; wcc = *wcs; wcs++)
| 112-294 | ||||||||||||||||||||||||||||||||||||
318 | if (iswprint(wcc) == 0)
| 56-238 | ||||||||||||||||||||||||||||||||||||
319 | { | - | ||||||||||||||||||||||||||||||||||||
320 | free (wcstr); | - | ||||||||||||||||||||||||||||||||||||
321 | return 1; executed 56 times by 1 test: return 1; Executed by:
| 56 | ||||||||||||||||||||||||||||||||||||
322 | } | - | ||||||||||||||||||||||||||||||||||||
323 | - | |||||||||||||||||||||||||||||||||||||
324 | free (wcstr); | - | ||||||||||||||||||||||||||||||||||||
325 | return 0; executed 112 times by 1 test: return 0; Executed by:
| 112 | ||||||||||||||||||||||||||||||||||||
326 | } | - | ||||||||||||||||||||||||||||||||||||
327 | #endif | - | ||||||||||||||||||||||||||||||||||||
328 | - | |||||||||||||||||||||||||||||||||||||
329 | /* return 1 if we need to quote with $'...' because of non-printing chars. */ | - | ||||||||||||||||||||||||||||||||||||
330 | int | - | ||||||||||||||||||||||||||||||||||||
331 | ansic_shouldquote (string) | - | ||||||||||||||||||||||||||||||||||||
332 | const char *string; | - | ||||||||||||||||||||||||||||||||||||
333 | { | - | ||||||||||||||||||||||||||||||||||||
334 | const char *s; | - | ||||||||||||||||||||||||||||||||||||
335 | unsigned char c; | - | ||||||||||||||||||||||||||||||||||||
336 | - | |||||||||||||||||||||||||||||||||||||
337 | if (string == 0)
| 0-2088 | ||||||||||||||||||||||||||||||||||||
338 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||||||||
339 | - | |||||||||||||||||||||||||||||||||||||
340 | for (s = string; c = *s; s++)
| 1778-11447 | ||||||||||||||||||||||||||||||||||||
341 | { | - | ||||||||||||||||||||||||||||||||||||
342 | #if defined (HANDLE_MULTIBYTE) | - | ||||||||||||||||||||||||||||||||||||
343 | if (is_basic (c) == 0)
| 299-11148 | ||||||||||||||||||||||||||||||||||||
344 | return (ansic_wshouldquote (s)); executed 299 times by 1 test: return (ansic_wshouldquote (s)); Executed by:
| 299 | ||||||||||||||||||||||||||||||||||||
345 | #endif | - | ||||||||||||||||||||||||||||||||||||
346 | if (ISPRINT (c) == 0)
| 11-11137 | ||||||||||||||||||||||||||||||||||||
347 | return 1; executed 11 times by 1 test: return 1; Executed by:
| 11 | ||||||||||||||||||||||||||||||||||||
348 | } executed 11137 times by 1 test: end of block Executed by:
| 11137 | ||||||||||||||||||||||||||||||||||||
349 | - | |||||||||||||||||||||||||||||||||||||
350 | return 0; executed 1778 times by 1 test: return 0; Executed by:
| 1778 | ||||||||||||||||||||||||||||||||||||
351 | } | - | ||||||||||||||||||||||||||||||||||||
352 | - | |||||||||||||||||||||||||||||||||||||
353 | /* $'...' ANSI-C expand the portion of STRING between START and END and | - | ||||||||||||||||||||||||||||||||||||
354 | return the result. The result cannot be longer than the input string. */ | - | ||||||||||||||||||||||||||||||||||||
355 | char * | - | ||||||||||||||||||||||||||||||||||||
356 | ansiexpand (string, start, end, lenp) | - | ||||||||||||||||||||||||||||||||||||
357 | char *string; | - | ||||||||||||||||||||||||||||||||||||
358 | int start, end, *lenp; | - | ||||||||||||||||||||||||||||||||||||
359 | { | - | ||||||||||||||||||||||||||||||||||||
360 | char *temp, *t; | - | ||||||||||||||||||||||||||||||||||||
361 | int len, tlen; | - | ||||||||||||||||||||||||||||||||||||
362 | - | |||||||||||||||||||||||||||||||||||||
363 | temp = (char *)xmalloc (end - start + 1); | - | ||||||||||||||||||||||||||||||||||||
364 | for (tlen = 0, len = start; len < end; )
| 2608-20079 | ||||||||||||||||||||||||||||||||||||
365 | temp[tlen++] = string[len++]; executed 20079 times by 1 test: temp[tlen++] = string[len++]; Executed by:
| 20079 | ||||||||||||||||||||||||||||||||||||
366 | temp[tlen] = '\0'; | - | ||||||||||||||||||||||||||||||||||||
367 | - | |||||||||||||||||||||||||||||||||||||
368 | if (*temp)
| 5-2603 | ||||||||||||||||||||||||||||||||||||
369 | { | - | ||||||||||||||||||||||||||||||||||||
370 | t = ansicstr (temp, tlen, 2, (int *)NULL, lenp); | - | ||||||||||||||||||||||||||||||||||||
371 | free (temp); | - | ||||||||||||||||||||||||||||||||||||
372 | return (t); executed 2603 times by 1 test: return (t); Executed by:
| 2603 | ||||||||||||||||||||||||||||||||||||
373 | } | - | ||||||||||||||||||||||||||||||||||||
374 | else | - | ||||||||||||||||||||||||||||||||||||
375 | { | - | ||||||||||||||||||||||||||||||||||||
376 | if (lenp)
| 0-5 | ||||||||||||||||||||||||||||||||||||
377 | *lenp = 0; executed 5 times by 1 test: *lenp = 0; Executed by:
| 5 | ||||||||||||||||||||||||||||||||||||
378 | return (temp); executed 5 times by 1 test: return (temp); Executed by:
| 5 | ||||||||||||||||||||||||||||||||||||
379 | } | - | ||||||||||||||||||||||||||||||||||||
380 | } | - | ||||||||||||||||||||||||||||||||||||
Source code | Switch to Preprocessed file |