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 /* Default set of characters that should be backslash-quoted in strings */
36 static const char bstab[256] =
38 0, 0, 0, 0, 0, 0, 0, 0,
39 0, 1, 1, 0, 0, 0, 0, 0, /* TAB, NL */
40 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0,
43 1, 1, 1, 0, 1, 0, 1, 1, /* SPACE, !, DQUOTE, DOL, AMP, SQUOTE */
44 1, 1, 1, 0, 1, 0, 0, 0, /* LPAR, RPAR, STAR, COMMA */
45 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 1, 1, 0, 1, 1, /* SEMI, LESSTHAN, GREATERTHAN, QUEST */
48 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 1, 1, 1, 1, 0, /* LBRACK, BS, RBRACK, CARAT */
53 1, 0, 0, 0, 0, 0, 0, 0, /* BACKQ */
54 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 1, 1, 1, 0, 0, /* LBRACE, BAR, RBRACE */
58 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0,
79 /* **************************************************************** */
81 /* Functions for quoting strings to be re-read as input */
83 /* **************************************************************** */
85 /* Return a new string which is the single-quoted version of STRING.
86 Used by alias and trap, among others. */
88 sh_single_quote (string)
95 result = (char *)xmalloc (3 + (4 * strlen (string)));
98 if (string[0] == '\'' && string[1] == 0)
108 for (s = string; s && (c = *s); s++)
114 *r++ = '\\'; /* insert escaped single quote */
116 *r++ = '\''; /* start new quoted string */
126 /* Quote STRING using double quotes. Return a new string. */
128 sh_double_quote (string)
131 register unsigned char c;
135 result = (char *)xmalloc (3 + (2 * strlen (string)));
139 for (s = string; s && (c = *s); s++)
141 /* Backslash-newline disappears within double quotes, so don't add one. */
142 if ((sh_syntaxtab[c] & CBSDQUOTE) && c != '\n')
144 else if (c == CTLESC || c == CTLNUL)
145 *r++ = CTLESC; /* could be '\\'? */
156 /* Turn S into a simple double-quoted string. If FLAGS is non-zero, quote
157 double quote characters in S with backslashes. */
159 sh_mkdoublequoted (s, slen, flags)
166 rlen = (flags == 0) ? slen + 3 : (2 * slen) + 1;
167 ret = r = (char *)xmalloc (rlen);
172 if (flags && *s == '"')
182 /* Remove backslashes that are quoting characters that are special between
183 double quotes. Return a new string. XXX - should this handle CTLESC
186 sh_un_double_quote (string)
189 register int c, pass_next;
190 char *result, *r, *s;
192 r = result = (char *)xmalloc (strlen (string) + 1);
194 for (pass_next = 0, s = string; s && (c = *s); s++)
202 if (c == '\\' && (sh_syntaxtab[(unsigned char) s[1]] & CBSDQUOTE))
214 /* Quote special characters in STRING using backslashes. Return a new
215 string. NOTE: if the string is to be further expanded, we need a
216 way to protect the CTLESC and CTLNUL characters. As I write this,
217 the current callers will never cause the string to be expanded without
218 going through the shell parser, which will protect the internal
219 quoting characters. TABLE, if set, points to a map of the ascii code
220 set with char needing to be backslash-quoted if table[char]==1. FLAGS,
221 if 1, causes tildes to be quoted as well. */
224 sh_backslash_quote (string, table, flags)
230 char *result, *r, *s, *backslash_table;
232 result = (char *)xmalloc (2 * strlen (string) + 1);
234 backslash_table = table ? table : (char *)bstab;
235 for (r = result, s = string; s && (c = *s); s++)
237 if (backslash_table[c] == 1)
239 else if (c == '#' && s == string) /* comment char */
241 else if ((flags&1) && c == '~' && (s == string || s[-1] == ':' || s[-1] == '='))
242 /* Tildes are special at the start of a word or after a `:' or `='
243 (technically unquoted, but it doesn't make a difference in practice) */
252 #if defined (PROMPT_STRING_DECODE)
253 /* Quote characters that get special treatment when in double quotes in STRING
254 using backslashes. Return a new string. */
256 sh_backslash_quote_for_double_quotes (string)
260 char *result, *r, *s;
262 result = (char *)xmalloc (2 * strlen (string) + 1);
264 for (r = result, s = string; s && (c = *s); s++)
266 if (sh_syntaxtab[c] & CBSDQUOTE)
268 /* I should probably add flags for these to sh_syntaxtab[] */
269 else if (c == CTLESC || c == CTLNUL)
270 *r++ = CTLESC; /* could be '\\'? */
278 #endif /* PROMPT_STRING_DECODE */
281 sh_contains_shell_metas (string)
286 for (s = string; s && *s; s++)
290 case ' ': case '\t': case '\n': /* IFS white space */
291 case '\'': case '"': case '\\': /* quoting chars */
292 case '|': case '&': case ';': /* shell metacharacters */
293 case '(': case ')': case '<': case '>':
294 case '!': case '{': case '}': /* reserved words */
295 case '*': case '[': case '?': case ']': /* globbing chars */
297 case '$': case '`': /* expansion chars */
299 case '~': /* tilde expansion */
300 if (s == string || s[-1] == '=' || s[-1] == ':')
304 if (s == string) /* comment char */