Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / lib / sh / shquote.c
1 /* Copyright (C) 1999 Free Software Foundation, Inc.
2
3    This file is part of GNU Bash, the Bourne Again SHell.
4
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
8    version.
9
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
13    for more details.
14    
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. */
18
19 #include <config.h>
20
21 #if defined (HAVE_UNISTD_H)
22 #  ifdef _MINIX
23 #    include <sys/types.h>
24 #  endif
25 #  include <unistd.h>
26 #endif
27
28 #include <stdio.h>
29
30 #include "syntax.h"
31 #include <xmalloc.h>
32
33 /* **************************************************************** */
34 /*                                                                  */
35 /*       Functions for quoting strings to be re-read as input       */
36 /*                                                                  */
37 /* **************************************************************** */
38
39 /* Return a new string which is the single-quoted version of STRING.
40    Used by alias and trap, among others. */
41 char *
42 sh_single_quote (string)
43      char *string;
44 {
45   register int c;
46   char *result, *r, *s;
47
48   result = (char *)xmalloc (3 + (4 * strlen (string)));
49   r = result;
50   *r++ = '\'';
51
52   for (s = string; s && (c = *s); s++)
53     {
54       *r++ = c;
55
56       if (c == '\'')
57         {
58           *r++ = '\\';  /* insert escaped single quote */
59           *r++ = '\'';
60           *r++ = '\'';  /* start new quoted string */
61         }
62     }
63
64   *r++ = '\'';
65   *r = '\0';
66
67   return (result);
68 }
69
70 /* Quote STRING using double quotes.  Return a new string. */
71 char *
72 sh_double_quote (string)
73      char *string;
74 {
75   register unsigned char c;
76   char *result, *r, *s;
77
78   result = (char *)xmalloc (3 + (2 * strlen (string)));
79   r = result;
80   *r++ = '"';
81
82   for (s = string; s && (c = *s); s++)
83     {
84       if (sh_syntaxtab[c] & CBSDQUOTE)
85         *r++ = '\\';
86
87       *r++ = c;
88     }
89
90   *r++ = '"';
91   *r = '\0';
92
93   return (result);
94 }
95
96 /* Remove backslashes that are quoting characters that are special between
97    double quotes.  Return a new string. */
98 char *
99 sh_un_double_quote (string)
100      char *string;
101 {
102   register int c, pass_next;
103   char *result, *r, *s;
104
105   r = result = (char *)xmalloc (strlen (string) + 1);
106
107   for (pass_next = 0, s = string; s && (c = *s); s++)
108     {
109       if (pass_next)
110         {
111           *r++ = c;
112           pass_next = 0;
113           continue;
114         }
115       if (c == '\\' && (sh_syntaxtab[(unsigned char) s[1]] & CBSDQUOTE))
116         {
117           pass_next = 1;
118           continue;
119         }
120       *r++ = c;
121     }
122
123   *r = '\0';
124   return result;
125 }
126
127 /* Quote special characters in STRING using backslashes.  Return a new
128    string. */
129 char *
130 sh_backslash_quote (string)
131      char *string;
132 {
133   int c;
134   char *result, *r, *s;
135
136   result = (char *)xmalloc (2 * strlen (string) + 1);
137
138   for (r = result, s = string; s && (c = *s); s++)
139     {
140       switch (c)
141         {
142         case ' ': case '\t': case '\n':         /* IFS white space */
143         case '\'': case '"': case '\\':         /* quoting chars */
144         case '|': case '&': case ';':           /* shell metacharacters */
145         case '(': case ')': case '<': case '>':
146         case '!': case '{': case '}':           /* reserved words */
147         case '*': case '[': case '?': case ']': /* globbing chars */
148         case '^':
149         case '$': case '`':                     /* expansion chars */
150           *r++ = '\\';
151           *r++ = c;
152           break;
153 #if 0
154         case '~':                               /* tilde expansion */
155           if (s == string || s[-1] == '=' || s[-1] == ':')
156             *r++ = '\\';
157           *r++ = c;
158           break;
159 #endif
160         case '#':                               /* comment char */
161           if (s == string)
162             *r++ = '\\';
163           /* FALLTHROUGH */
164         default:
165           *r++ = c;
166           break;
167         }
168     }
169
170   *r = '\0';
171   return (result);
172 }
173
174 #if defined (PROMPT_STRING_DECODE)
175 /* Quote characters that get special treatment when in double quotes in STRING
176    using backslashes.  Return a new string. */
177 char *
178 sh_backslash_quote_for_double_quotes (string)
179      char *string;
180 {
181   unsigned char c;
182   char *result, *r, *s;
183
184   result = (char *)xmalloc (2 * strlen (string) + 1);
185
186   for (r = result, s = string; s && (c = *s); s++)
187     {
188       if (sh_syntaxtab[c] & CBSDQUOTE)
189         *r++ = '\\';
190
191       *r++ = c;
192     }
193
194   *r = '\0';
195   return (result);
196 }
197 #endif /* PROMPT_STRING_DECODE */
198
199 int
200 sh_contains_shell_metas (string)
201      char *string;
202 {
203   char *s;
204
205   for (s = string; s && *s; s++)
206     {
207       switch (*s)
208         {
209         case ' ': case '\t': case '\n':         /* IFS white space */
210         case '\'': case '"': case '\\':         /* quoting chars */
211         case '|': case '&': case ';':           /* shell metacharacters */
212         case '(': case ')': case '<': case '>':
213         case '!': case '{': case '}':           /* reserved words */
214         case '*': case '[': case '?': case ']': /* globbing chars */
215         case '^':
216         case '$': case '`':                     /* expansion chars */
217           return (1);
218         case '~':                               /* tilde expansion */
219           if (s == string || s[-1] == '=' || s[-1] == ':')
220             return (1);
221           break;
222         case '#':
223           if (s == string)                      /* comment char */
224             return (1);
225           /* FALLTHROUGH */
226         default:
227           break;
228         }
229     }
230
231   return (0);
232 }