]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/shquote.c
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>
32 extern char *xmalloc ();
34 /* **************************************************************** */
36 /* Functions for quoting strings to be re-read as input */
38 /* **************************************************************** */
40 /* Return a new string which is the single-quoted version of STRING.
41 Used by alias and trap, among others. */
43 sh_single_quote (string
)
49 result
= xmalloc (3 + (4 * strlen (string
)));
53 for (s
= string
; s
&& (c
= *s
); s
++)
59 *r
++ = '\\'; /* insert escaped single quote */
61 *r
++ = '\''; /* start new quoted string */
71 /* Quote STRING using double quotes. Return a new string. */
73 sh_double_quote (string
)
79 result
= xmalloc (3 + (2 * strlen (string
)));
83 for (s
= string
; s
&& (c
= *s
); s
++)
85 if (sh_syntaxtab
[c
] & CBSDQUOTE
)
97 /* Remove backslashes that are quoting characters that are special between
98 double quotes. Return a new string. */
100 sh_un_double_quote (string
)
103 register int c
, pass_next
;
104 char *result
, *r
, *s
;
106 r
= result
= xmalloc (strlen (string
) + 1);
108 for (pass_next
= 0, s
= string
; s
&& (c
= *s
); s
++)
116 if (c
== '\\' && (sh_syntaxtab
[s
[1]] & CBSDQUOTE
))
128 /* Quote special characters in STRING using backslashes. Return a new
131 sh_backslash_quote (string
)
135 char *result
, *r
, *s
;
137 result
= xmalloc (2 * strlen (string
) + 1);
139 for (r
= result
, s
= string
; s
&& (c
= *s
); s
++)
143 case ' ': case '\t': case '\n': /* IFS white space */
144 case '\'': case '"': case '\\': /* quoting chars */
145 case '|': case '&': case ';': /* shell metacharacters */
146 case '(': case ')': case '<': case '>':
147 case '!': case '{': case '}': /* reserved words */
148 case '*': case '[': case '?': case ']': /* globbing chars */
150 case '$': case '`': /* expansion chars */
155 case '~': /* tilde expansion */
156 if (s
== string
|| s
[-1] == '=' || s
[-1] == ':')
161 case '#': /* comment char */
175 #if defined (PROMPT_STRING_DECODE)
176 /* Quote characters that get special treatment when in double quotes in STRING
177 using backslashes. Return a new string. */
179 sh_backslash_quote_for_double_quotes (string
)
183 char *result
, *r
, *s
;
185 result
= xmalloc (2 * strlen (string
) + 1);
187 for (r
= result
, s
= string
; s
&& (c
= *s
); s
++)
189 if (sh_syntaxtab
[c
] & CBSDQUOTE
)
198 #endif /* PROMPT_STRING_DECODE */
201 sh_contains_shell_metas (string
)
206 for (s
= string
; s
&& *s
; s
++)
210 case ' ': case '\t': case '\n': /* IFS white space */
211 case '\'': case '"': case '\\': /* quoting chars */
212 case '|': case '&': case ';': /* shell metacharacters */
213 case '(': case ')': case '<': case '>':
214 case '!': case '{': case '}': /* reserved words */
215 case '*': case '[': case '?': case ']': /* globbing chars */
217 case '$': case '`': /* expansion chars */
219 case '~': /* tilde expansion */
220 if (s
== string
|| s
[-1] == '=' || s
[-1] == ':')
223 if (s
== string
) /* comment char */