| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* shquote - functions to quote and dequote strings */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | /* Copyright (C) 1999-2015 Free Software Foundation, Inc.
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This file is part of GNU Bash, the Bourne Again SHell. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    Bash is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |    it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |    the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |    (at your option) any later version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Bash is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |    GNU General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <config.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_UNISTD_H)
 | 
					
						
							|  |  |  | #  ifdef _MINIX
 | 
					
						
							|  |  |  | #    include <sys/types.h>
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | #  include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | #include <stdc.h>
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | #include "syntax.h"
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | #include <xmalloc.h>
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | #include "shmbchar.h"
 | 
					
						
							|  |  |  | #include "shmbutil.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern char *ansic_quote __P((char *, int, int *)); | 
					
						
							|  |  |  | extern int ansic_shouldquote __P((const char *)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | /* Default set of characters that should be backslash-quoted in strings */ | 
					
						
							|  |  |  | static const char bstab[256] = | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 1, 1, 0, 0, 0, 0, 0,	/* TAB, NL */ | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     1, 1, 1, 0, 1, 0, 1, 1,	/* SPACE, !, DQUOTE, DOL, AMP, SQUOTE */ | 
					
						
							|  |  |  |     1, 1, 1, 0, 1, 0, 0, 0,	/* LPAR, RPAR, STAR, COMMA */ | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 1, 1, 0, 1, 1,	/* SEMI, LESSTHAN, GREATERTHAN, QUEST */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 1, 1, 1, 1, 0,	/* LBRACK, BS, RBRACK, CARAT */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     1, 0, 0, 0, 0, 0, 0, 0,	/* BACKQ */ | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 1, 1, 1, 0, 0,	/* LBRACE, BAR, RBRACE */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*	 Functions for quoting strings to be re-read as input	    */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return a new string which is the single-quoted version of STRING.
 | 
					
						
							|  |  |  |    Used by alias and trap, among others. */ | 
					
						
							|  |  |  | char * | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | sh_single_quote (string) | 
					
						
							| 
									
										
										
										
											2011-11-22 19:11:26 -05:00
										 |  |  |      const char *string; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   register int c; | 
					
						
							| 
									
										
										
										
											2011-11-22 19:11:26 -05:00
										 |  |  |   char *result, *r; | 
					
						
							|  |  |  |   const char *s; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   result = (char *)xmalloc (3 + (4 * strlen (string))); | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   r = result; | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (string[0] == '\'' && string[1] == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       *r++ = '\\'; | 
					
						
							|  |  |  |       *r++ = '\''; | 
					
						
							|  |  |  |       *r++ = 0; | 
					
						
							|  |  |  |       return result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   *r++ = '\''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (s = string; s && (c = *s); s++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       *r++ = c; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (c == '\'') | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  *r++ = '\\';	/* insert escaped single quote */ | 
					
						
							|  |  |  | 	  *r++ = '\''; | 
					
						
							|  |  |  | 	  *r++ = '\'';	/* start new quoted string */ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *r++ = '\''; | 
					
						
							|  |  |  |   *r = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Quote STRING using double quotes.  Return a new string. */ | 
					
						
							|  |  |  | char * | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | sh_double_quote (string) | 
					
						
							| 
									
										
										
										
											2011-11-22 19:11:26 -05:00
										 |  |  |      const char *string; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   register unsigned char c; | 
					
						
							| 
									
										
										
										
											2011-11-22 19:11:26 -05:00
										 |  |  |   char *result, *r; | 
					
						
							|  |  |  |   const char *s; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   result = (char *)xmalloc (3 + (2 * strlen (string))); | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   r = result; | 
					
						
							|  |  |  |   *r++ = '"'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (s = string; s && (c = *s); s++) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |       /* Backslash-newline disappears within double quotes, so don't add one. */ | 
					
						
							|  |  |  |       if ((sh_syntaxtab[c] & CBSDQUOTE) && c != '\n') | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 	*r++ = '\\'; | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  |       /* Assume that the string will not be further expanded. */ | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       else if (c == CTLESC || c == CTLNUL) | 
					
						
							|  |  |  | 	*r++ = CTLESC;		/* could be '\\'? */ | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       *r++ = c; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *r++ = '"'; | 
					
						
							|  |  |  |   *r = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | /* Turn S into a simple double-quoted string.  If FLAGS is non-zero, quote
 | 
					
						
							|  |  |  |    double quote characters in S with backslashes. */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | sh_mkdoublequoted (s, slen, flags) | 
					
						
							|  |  |  |      const char *s; | 
					
						
							|  |  |  |      int slen, flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *r, *ret; | 
					
						
							|  |  |  |   int rlen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   rlen = (flags == 0) ? slen + 3 : (2 * slen) + 1; | 
					
						
							|  |  |  |   ret = r = (char *)xmalloc (rlen); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   *r++ = '"'; | 
					
						
							|  |  |  |   while (*s) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (flags && *s == '"') | 
					
						
							|  |  |  | 	*r++ = '\\'; | 
					
						
							|  |  |  |       *r++ = *s++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   *r++ = '"'; | 
					
						
							|  |  |  |   *r = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | /* Remove backslashes that are quoting characters that are special between
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |    double quotes.  Return a new string.  XXX - should this handle CTLESC | 
					
						
							|  |  |  |    and CTLNUL? */ | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | char * | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | sh_un_double_quote (string) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |      char *string; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   register int c, pass_next; | 
					
						
							|  |  |  |   char *result, *r, *s; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   r = result = (char *)xmalloc (strlen (string) + 1); | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (pass_next = 0, s = string; s && (c = *s); s++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (pass_next) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  *r++ = c; | 
					
						
							|  |  |  | 	  pass_next = 0; | 
					
						
							|  |  |  | 	  continue; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       if (c == '\\' && (sh_syntaxtab[(unsigned char) s[1]] & CBSDQUOTE)) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	  pass_next = 1; | 
					
						
							|  |  |  | 	  continue; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       *r++ = c; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *r = '\0'; | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Quote special characters in STRING using backslashes.  Return a new
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |    string.  NOTE:  if the string is to be further expanded, we need a | 
					
						
							|  |  |  |    way to protect the CTLESC and CTLNUL characters.  As I write this, | 
					
						
							|  |  |  |    the current callers will never cause the string to be expanded without | 
					
						
							|  |  |  |    going through the shell parser, which will protect the internal | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  |    quoting characters.  TABLE, if set, points to a map of the ascii code | 
					
						
							|  |  |  |    set with char needing to be backslash-quoted if table[char]==1.  FLAGS, | 
					
						
							|  |  |  |    if 1, causes tildes to be quoted as well. */ | 
					
						
							|  |  |  |     | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | char * | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | sh_backslash_quote (string, table, flags) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |      char *string; | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  |      char *table; | 
					
						
							|  |  |  |      int flags; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |   int c, mb_cur_max; | 
					
						
							|  |  |  |   size_t slen; | 
					
						
							|  |  |  |   char *result, *r, *s, *backslash_table, *send; | 
					
						
							|  |  |  |   DECLARE_MBSTATE; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |   slen = strlen (string); | 
					
						
							|  |  |  |   send = string + slen; | 
					
						
							|  |  |  |   result = (char *)xmalloc (2 * slen + 1); | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  |   backslash_table = table ? table : (char *)bstab; | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |   mb_cur_max = MB_CUR_MAX; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   for (r = result, s = string; s && (c = *s); s++) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | #if defined (HANDLE_MULTIBYTE)
 | 
					
						
							|  |  |  |       /* XXX - isascii, even if is_basic(c) == 0 - works in most cases. */ | 
					
						
							|  |  |  |       if (c >= 0 && c <= 127 && backslash_table[(unsigned char)c] == 1) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  *r++ = '\\'; | 
					
						
							|  |  |  | 	  *r++ = c; | 
					
						
							|  |  |  | 	  continue; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       if (mb_cur_max > 1 && is_basic (c) == 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  COPY_CHAR_P (r, s, send); | 
					
						
							|  |  |  | 	  s--;		/* compensate for auto-increment in loop above */ | 
					
						
							|  |  |  | 	  continue; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |       if (backslash_table[(unsigned char)c] == 1) | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | 	*r++ = '\\'; | 
					
						
							|  |  |  |       else if (c == '#' && s == string)			/* comment char */ | 
					
						
							|  |  |  | 	*r++ = '\\'; | 
					
						
							|  |  |  |       else if ((flags&1) && c == '~' && (s == string || s[-1] == ':' || s[-1] == '=')) | 
					
						
							|  |  |  |         /* Tildes are special at the start of a word or after a `:' or `='
 | 
					
						
							|  |  |  | 	   (technically unquoted, but it doesn't make a difference in practice) */ | 
					
						
							|  |  |  | 	*r++ = '\\'; | 
					
						
							|  |  |  |       *r++ = c; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *r = '\0'; | 
					
						
							|  |  |  |   return (result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | #if defined (PROMPT_STRING_DECODE)
 | 
					
						
							|  |  |  | /* Quote characters that get special treatment when in double quotes in STRING
 | 
					
						
							|  |  |  |    using backslashes.  Return a new string. */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | sh_backslash_quote_for_double_quotes (string) | 
					
						
							|  |  |  |      char *string; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   unsigned char c; | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   char *result, *r, *s; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   result = (char *)xmalloc (2 * strlen (string) + 1); | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (r = result, s = string; s && (c = *s); s++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (sh_syntaxtab[c] & CBSDQUOTE) | 
					
						
							|  |  |  | 	*r++ = '\\'; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       /* I should probably add flags for these to sh_syntaxtab[] */ | 
					
						
							|  |  |  |       else if (c == CTLESC || c == CTLNUL) | 
					
						
							|  |  |  | 	*r++ = CTLESC;		/* could be '\\'? */ | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       *r++ = c; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *r = '\0'; | 
					
						
							|  |  |  |   return (result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif /* PROMPT_STRING_DECODE */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | char * | 
					
						
							|  |  |  | sh_quote_reusable (s, flags) | 
					
						
							|  |  |  |      char *s; | 
					
						
							|  |  |  |      int flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (s == 0) | 
					
						
							|  |  |  |     return s; | 
					
						
							|  |  |  |   else if (*s == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       ret = (char *)xmalloc (3); | 
					
						
							|  |  |  |       ret[0] = ret[1] = '\''; | 
					
						
							|  |  |  |       ret[2] = '\0'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if (ansic_shouldquote (s)) | 
					
						
							|  |  |  |     ret = ansic_quote (s, 0, (int *)0); | 
					
						
							|  |  |  |   else if (flags) | 
					
						
							|  |  |  |     ret = sh_backslash_quote (s, 0, 1); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     ret = sh_single_quote (s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | sh_contains_shell_metas (string) | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |      const char *string; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |   const char *s; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (s = string; s && *s; s++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       switch (*s) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	case ' ': case '\t': case '\n':		/* IFS white space */ | 
					
						
							|  |  |  | 	case '\'': case '"': case '\\':		/* quoting chars */ | 
					
						
							|  |  |  | 	case '|': case '&': case ';':		/* shell metacharacters */ | 
					
						
							|  |  |  | 	case '(': case ')': case '<': case '>': | 
					
						
							|  |  |  | 	case '!': case '{': case '}':		/* reserved words */ | 
					
						
							|  |  |  | 	case '*': case '[': case '?': case ']':	/* globbing chars */ | 
					
						
							|  |  |  | 	case '^': | 
					
						
							|  |  |  | 	case '$': case '`':			/* expansion chars */ | 
					
						
							|  |  |  | 	  return (1); | 
					
						
							|  |  |  | 	case '~':				/* tilde expansion */ | 
					
						
							|  |  |  | 	  if (s == string || s[-1] == '=' || s[-1] == ':') | 
					
						
							|  |  |  | 	    return (1); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	  break; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 	case '#': | 
					
						
							|  |  |  | 	  if (s == string)			/* comment char */ | 
					
						
							|  |  |  | 	    return (1); | 
					
						
							|  |  |  | 	  /* FALLTHROUGH */ | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 	  break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (0); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-04-11 11:05:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | sh_contains_quotes (string) | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |      const char *string; | 
					
						
							| 
									
										
										
										
											2014-04-11 11:05:05 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |   const char *s; | 
					
						
							| 
									
										
										
										
											2014-04-11 11:05:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (s = string; s && *s; s++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (*s == '\'' || *s == '"' || *s == '\\') | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } |