1 /* shquote - functions to quote and dequote strings */
3 /* Copyright (C) 1999 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)
25 # include <sys/types.h>
35 /* **************************************************************** */
37 /* Functions for quoting strings to be re-read as input */
39 /* **************************************************************** */
41 /* Return a new string which is the single-quoted version of STRING.
42 Used by alias and trap, among others. */
44 sh_single_quote (string)
51 result = (char *)xmalloc (3 + (4 * strlen (string)));
55 for (s = string; s && (c = *s); s++)
61 *r++ = '\\'; /* insert escaped single quote */
63 *r++ = '\''; /* start new quoted string */
73 /* Quote STRING using double quotes. Return a new string. */
75 sh_double_quote (string)
78 register unsigned char c;
82 result = (char *)xmalloc (3 + (2 * strlen (string)));
86 for (s = string; s && (c = *s); s++)
88 /* Backslash-newline disappears within double quotes, so don't add one. */
89 if ((sh_syntaxtab[c] & CBSDQUOTE) && c != '\n')
91 else if (c == CTLESC || c == CTLNUL)
92 *r++ = CTLESC; /* could be '\\'? */
103 /* Turn S into a simple double-quoted string. If FLAGS is non-zero, quote
104 double quote characters in S with backslashes. */
106 sh_mkdoublequoted (s, slen, flags)
113 rlen = (flags == 0) ? slen + 3 : (2 * slen) + 1;
114 ret = r = (char *)xmalloc (rlen);
119 if (flags && *s == '"')
129 /* Remove backslashes that are quoting characters that are special between
130 double quotes. Return a new string. XXX - should this handle CTLESC
133 sh_un_double_quote (string)
136 register int c, pass_next;
137 char *result, *r, *s;
139 r = result = (char *)xmalloc (strlen (string) + 1);
141 for (pass_next = 0, s = string; s && (c = *s); s++)
149 if (c == '\\' && (sh_syntaxtab[(unsigned char) s[1]] & CBSDQUOTE))
161 /* Quote special characters in STRING using backslashes. Return a new
162 string. NOTE: if the string is to be further expanded, we need a
163 way to protect the CTLESC and CTLNUL characters. As I write this,
164 the current callers will never cause the string to be expanded without
165 going through the shell parser, which will protect the internal
166 quoting characters. */
168 sh_backslash_quote (string)
172 char *result, *r, *s;
174 result = (char *)xmalloc (2 * strlen (string) + 1);
176 for (r = result, s = string; s && (c = *s); s++)
180 case ' ': case '\t': case '\n': /* IFS white space */
181 case '\'': case '"': case '\\': /* quoting chars */
182 case '|': case '&': case ';': /* shell metacharacters */
183 case '(': case ')': case '<': case '>':
184 case '!': case '{': case '}': /* reserved words */
185 case '*': case '[': case '?': case ']': /* globbing chars */
187 case '$': case '`': /* expansion chars */
188 case ',': /* brace expansion */
193 case '~': /* tilde expansion */
194 if (s == string || s[-1] == '=' || s[-1] == ':')
199 case CTLESC: case CTLNUL: /* internal quoting characters */
200 *r++ = CTLESC; /* could be '\\'? */
205 case '#': /* comment char */
219 #if defined (PROMPT_STRING_DECODE)
220 /* Quote characters that get special treatment when in double quotes in STRING
221 using backslashes. Return a new string. */
223 sh_backslash_quote_for_double_quotes (string)
227 char *result, *r, *s;
229 result = (char *)xmalloc (2 * strlen (string) + 1);
231 for (r = result, s = string; s && (c = *s); s++)
233 if (sh_syntaxtab[c] & CBSDQUOTE)
235 /* I should probably add flags for these to sh_syntaxtab[] */
236 else if (c == CTLESC || c == CTLNUL)
237 *r++ = CTLESC; /* could be '\\'? */
245 #endif /* PROMPT_STRING_DECODE */
248 sh_contains_shell_metas (string)
253 for (s = string; s && *s; s++)
257 case ' ': case '\t': case '\n': /* IFS white space */
258 case '\'': case '"': case '\\': /* quoting chars */
259 case '|': case '&': case ';': /* shell metacharacters */
260 case '(': case ')': case '<': case '>':
261 case '!': case '{': case '}': /* reserved words */
262 case '*': case '[': case '?': case ']': /* globbing chars */
264 case '$': case '`': /* expansion chars */
266 case '~': /* tilde expansion */
267 if (s == string || s[-1] == '=' || s[-1] == ':')
271 if (s == string) /* comment char */