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)
50 result = (char *)xmalloc (3 + (4 * strlen (string)));
54 for (s = string; s && (c = *s); s++)
60 *r++ = '\\'; /* insert escaped single quote */
62 *r++ = '\''; /* start new quoted string */
72 /* Quote STRING using double quotes. Return a new string. */
74 sh_double_quote (string)
77 register unsigned char c;
80 result = (char *)xmalloc (3 + (2 * strlen (string)));
84 for (s = string; s && (c = *s); s++)
86 /* Backslash-newline disappears within double quotes, so don't add one. */
87 if ((sh_syntaxtab[c] & CBSDQUOTE) && c != '\n')
89 else if (c == CTLESC || c == CTLNUL)
90 *r++ = CTLESC; /* could be '\\'? */
101 /* Turn S into a simple double-quoted string. If FLAGS is non-zero, quote
102 double quote characters in S with backslashes. */
104 sh_mkdoublequoted (s, slen, flags)
111 rlen = (flags == 0) ? slen + 3 : (2 * slen) + 1;
112 ret = r = (char *)xmalloc (rlen);
117 if (flags && *s == '"')
127 /* Remove backslashes that are quoting characters that are special between
128 double quotes. Return a new string. XXX - should this handle CTLESC
131 sh_un_double_quote (string)
134 register int c, pass_next;
135 char *result, *r, *s;
137 r = result = (char *)xmalloc (strlen (string) + 1);
139 for (pass_next = 0, s = string; s && (c = *s); s++)
147 if (c == '\\' && (sh_syntaxtab[(unsigned char) s[1]] & CBSDQUOTE))
159 /* Quote special characters in STRING using backslashes. Return a new
160 string. NOTE: if the string is to be further expanded, we need a
161 way to protect the CTLESC and CTLNUL characters. As I write this,
162 the current callers will never cause the string to be expanded without
163 going through the shell parser, which will protect the internal
164 quoting characters. */
166 sh_backslash_quote (string)
170 char *result, *r, *s;
172 result = (char *)xmalloc (2 * strlen (string) + 1);
174 for (r = result, s = string; s && (c = *s); s++)
178 case ' ': case '\t': case '\n': /* IFS white space */
179 case '\'': case '"': case '\\': /* quoting chars */
180 case '|': case '&': case ';': /* shell metacharacters */
181 case '(': case ')': case '<': case '>':
182 case '!': case '{': case '}': /* reserved words */
183 case '*': case '[': case '?': case ']': /* globbing chars */
185 case '$': case '`': /* expansion chars */
186 case ',': /* brace expansion */
191 case '~': /* tilde expansion */
192 if (s == string || s[-1] == '=' || s[-1] == ':')
197 case CTLESC: case CTLNUL: /* internal quoting characters */
198 *r++ = CTLESC; /* could be '\\'? */
203 case '#': /* comment char */
217 #if defined (PROMPT_STRING_DECODE)
218 /* Quote characters that get special treatment when in double quotes in STRING
219 using backslashes. Return a new string. */
221 sh_backslash_quote_for_double_quotes (string)
225 char *result, *r, *s;
227 result = (char *)xmalloc (2 * strlen (string) + 1);
229 for (r = result, s = string; s && (c = *s); s++)
231 if (sh_syntaxtab[c] & CBSDQUOTE)
233 /* I should probably add flags for these to sh_syntaxtab[] */
234 else if (c == CTLESC || c == CTLNUL)
235 *r++ = CTLESC; /* could be '\\'? */
243 #endif /* PROMPT_STRING_DECODE */
246 sh_contains_shell_metas (string)
251 for (s = string; s && *s; s++)
255 case ' ': case '\t': case '\n': /* IFS white space */
256 case '\'': case '"': case '\\': /* quoting chars */
257 case '|': case '&': case ';': /* shell metacharacters */
258 case '(': case ')': case '<': case '>':
259 case '!': case '{': case '}': /* reserved words */
260 case '*': case '[': case '?': case ']': /* globbing chars */
262 case '$': case '`': /* expansion chars */
264 case '~': /* tilde expansion */
265 if (s == string || s[-1] == '=' || s[-1] == ':')
269 if (s == string) /* comment char */