]>
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>
30 #if !defined(slashify_in_quotes)
31 # define slashify_in_quotes "\\`$\"\n"
34 extern char *xmalloc ();
36 /* **************************************************************** */
38 /* Functions for quoting strings to be re-read as input */
40 /* **************************************************************** */
42 /* Return a new string which is the single-quoted version of STRING.
43 Used by alias and trap, among others. */
51 result
= 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. */
81 result
= xmalloc (3 + (2 * strlen (string
)));
85 for (s
= string
; s
&& (c
= *s
); s
++)
107 /* Remove backslashes that are quoting characters that are special between
108 double quotes. Return a new string. */
110 un_double_quote (string
)
113 register int c
, pass_next
;
114 char *result
, *r
, *s
;
116 r
= result
= xmalloc (strlen (string
) + 1);
118 for (pass_next
= 0, s
= string
; s
&& (c
= *s
); s
++)
126 if (c
== '\\' && strchr (slashify_in_quotes
, s
[1]))
138 /* Quote special characters in STRING using backslashes. Return a new
141 backslash_quote (string
)
145 char *result
, *r
, *s
;
147 result
= xmalloc (2 * strlen (string
) + 1);
149 for (r
= result
, s
= string
; s
&& (c
= *s
); s
++)
153 case ' ': case '\t': case '\n': /* IFS white space */
154 case '\'': case '"': case '\\': /* quoting chars */
155 case '|': case '&': case ';': /* shell metacharacters */
156 case '(': case ')': case '<': case '>':
157 case '!': case '{': case '}': /* reserved words */
158 case '*': case '[': case '?': case ']': /* globbing chars */
160 case '$': case '`': /* expansion chars */
165 case '~': /* tilde expansion */
166 if (s
== string
|| s
[-1] == '=' || s
[-1] == ':')
171 case '#': /* comment char */
186 contains_shell_metas (string
)
191 for (s
= string
; s
&& *s
; s
++)
195 case ' ': case '\t': case '\n': /* IFS white space */
196 case '\'': case '"': case '\\': /* quoting chars */
197 case '|': case '&': case ';': /* shell metacharacters */
198 case '(': case ')': case '<': case '>':
199 case '!': case '{': case '}': /* reserved words */
200 case '*': case '[': case '?': case ']': /* globbing chars */
202 case '$': case '`': /* expansion chars */
204 case '~': /* tilde expansion */
205 if (s
== string
|| s
[-1] == '=' || s
[-1] == ':')
208 if (s
== string
) /* comment char */