1 /* Copyright (C) 1999 Free Software Foundation, Inc.
3 This file is part of GNU Bash, the Bourne Again SHell.
5 Bash is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
10 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License along
16 with Bash; see the file COPYING. If not, write to the Free Software
17 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21 #if defined (HAVE_UNISTD_H)
23 # include <sys/types.h>
33 /* **************************************************************** */
35 /* Functions for quoting strings to be re-read as input */
37 /* **************************************************************** */
39 /* Return a new string which is the single-quoted version of STRING.
40 Used by alias and trap, among others. */
42 sh_single_quote (string)
48 result = (char *)xmalloc (3 + (4 * strlen (string)));
52 for (s = string; s && (c = *s); s++)
58 *r++ = '\\'; /* insert escaped single quote */
60 *r++ = '\''; /* start new quoted string */
70 /* Quote STRING using double quotes. Return a new string. */
72 sh_double_quote (string)
75 register unsigned char c;
78 result = (char *)xmalloc (3 + (2 * strlen (string)));
82 for (s = string; s && (c = *s); s++)
84 /* Backslash-newline disappears within double quotes, so don't add one. */
85 if ((sh_syntaxtab[c] & CBSDQUOTE) && c != '\n')
87 else if (c == CTLESC || c == CTLNUL)
88 *r++ = CTLESC; /* could be '\\'? */
99 /* Turn S into a simple double-quoted string. If FLAGS is non-zero, quote
100 double quote characters in S with backslashes. */
102 sh_mkdoublequoted (s, slen, flags)
109 rlen = (flags == 0) ? slen + 3 : (2 * slen) + 1;
110 ret = r = (char *)xmalloc (rlen);
115 if (flags && *s == '"')
125 /* Remove backslashes that are quoting characters that are special between
126 double quotes. Return a new string. XXX - should this handle CTLESC
129 sh_un_double_quote (string)
132 register int c, pass_next;
133 char *result, *r, *s;
135 r = result = (char *)xmalloc (strlen (string) + 1);
137 for (pass_next = 0, s = string; s && (c = *s); s++)
145 if (c == '\\' && (sh_syntaxtab[(unsigned char) s[1]] & CBSDQUOTE))
157 /* Quote special characters in STRING using backslashes. Return a new
158 string. NOTE: if the string is to be further expanded, we need a
159 way to protect the CTLESC and CTLNUL characters. As I write this,
160 the current callers will never cause the string to be expanded without
161 going through the shell parser, which will protect the internal
162 quoting characters. */
164 sh_backslash_quote (string)
168 char *result, *r, *s;
170 result = (char *)xmalloc (2 * strlen (string) + 1);
172 for (r = result, s = string; s && (c = *s); s++)
176 case ' ': case '\t': case '\n': /* IFS white space */
177 case '\'': case '"': case '\\': /* quoting chars */
178 case '|': case '&': case ';': /* shell metacharacters */
179 case '(': case ')': case '<': case '>':
180 case '!': case '{': case '}': /* reserved words */
181 case '*': case '[': case '?': case ']': /* globbing chars */
183 case '$': case '`': /* expansion chars */
184 case ',': /* brace expansion */
189 case '~': /* tilde expansion */
190 if (s == string || s[-1] == '=' || s[-1] == ':')
195 case CTLESC: case CTLNUL: /* internal quoting characters */
196 *r++ = CTLESC; /* could be '\\'? */
201 case '#': /* comment char */
215 #if defined (PROMPT_STRING_DECODE)
216 /* Quote characters that get special treatment when in double quotes in STRING
217 using backslashes. Return a new string. */
219 sh_backslash_quote_for_double_quotes (string)
223 char *result, *r, *s;
225 result = (char *)xmalloc (2 * strlen (string) + 1);
227 for (r = result, s = string; s && (c = *s); s++)
229 if (sh_syntaxtab[c] & CBSDQUOTE)
231 /* I should probably add flags for these to sh_syntaxtab[] */
232 else if (c == CTLESC || c == CTLNUL)
233 *r++ = CTLESC; /* could be '\\'? */
241 #endif /* PROMPT_STRING_DECODE */
244 sh_contains_shell_metas (string)
249 for (s = string; s && *s; s++)
253 case ' ': case '\t': case '\n': /* IFS white space */
254 case '\'': case '"': case '\\': /* quoting chars */
255 case '|': case '&': case ';': /* shell metacharacters */
256 case '(': case ')': case '<': case '>':
257 case '!': case '{': case '}': /* reserved words */
258 case '*': case '[': case '?': case ']': /* globbing chars */
260 case '$': case '`': /* expansion chars */
262 case '~': /* tilde expansion */
263 if (s == string || s[-1] == '=' || s[-1] == ':')
267 if (s == string) /* comment char */