Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / lib / sh / shquote.c
index 981e967..a267d38 100644 (file)
@@ -1,20 +1,22 @@
+/* shquote - functions to quote and dequote strings */
+
 /* Copyright (C) 1999 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
-   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 2, or (at your option) any later
-   version.
+   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.
 
-   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; see the file COPYING.  If not, write to the Free Software
-   Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include <config.h>
 
@@ -28,8 +30,7 @@
 #include <stdio.h>
 
 #include "syntax.h"
-
-extern char *xmalloc ();
+#include <xmalloc.h>
 
 /* **************************************************************** */
 /*                                                                 */
@@ -46,7 +47,7 @@ sh_single_quote (string)
   register int c;
   char *result, *r, *s;
 
-  result = xmalloc (3 + (4 * strlen (string)));
+  result = (char *)xmalloc (3 + (4 * strlen (string)));
   r = result;
   *r++ = '\'';
 
@@ -73,17 +74,20 @@ char *
 sh_double_quote (string)
      char *string;
 {
-  register int c;
+  register unsigned char c;
   char *result, *r, *s;
 
-  result = xmalloc (3 + (2 * strlen (string)));
+  result = (char *)xmalloc (3 + (2 * strlen (string)));
   r = result;
   *r++ = '"';
 
   for (s = string; s && (c = *s); s++)
     {
-      if (sh_syntaxtab[c] & CBSDQUOTE)
+      /* Backslash-newline disappears within double quotes, so don't add one. */
+      if ((sh_syntaxtab[c] & CBSDQUOTE) && c != '\n')
        *r++ = '\\';
+      else if (c == CTLESC || c == CTLNUL)
+       *r++ = CTLESC;          /* could be '\\'? */
 
       *r++ = c;
     }
@@ -94,8 +98,35 @@ sh_double_quote (string)
   return (result);
 }
 
+/* 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;
+}
+
 /* Remove backslashes that are quoting characters that are special between
-   double quotes.  Return a new string. */
+   double quotes.  Return a new string.  XXX - should this handle CTLESC
+   and CTLNUL? */
 char *
 sh_un_double_quote (string)
      char *string;
@@ -103,7 +134,7 @@ sh_un_double_quote (string)
   register int c, pass_next;
   char *result, *r, *s;
 
-  r = result = xmalloc (strlen (string) + 1);
+  r = result = (char *)xmalloc (strlen (string) + 1);
 
   for (pass_next = 0, s = string; s && (c = *s); s++)
     {
@@ -113,7 +144,7 @@ sh_un_double_quote (string)
          pass_next = 0;
          continue;
        }
-      if (c == '\\' && (sh_syntaxtab[s[1]] & CBSDQUOTE))
+      if (c == '\\' && (sh_syntaxtab[(unsigned char) s[1]] & CBSDQUOTE))
        {
          pass_next = 1;
          continue;
@@ -126,7 +157,11 @@ sh_un_double_quote (string)
 }
 
 /* Quote special characters in STRING using backslashes.  Return a new
-   string. */
+   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
+   quoting characters. */
 char *
 sh_backslash_quote (string)
      char *string;
@@ -134,7 +169,7 @@ sh_backslash_quote (string)
   int c;
   char *result, *r, *s;
 
-  result = xmalloc (2 * strlen (string) + 1);
+  result = (char *)xmalloc (2 * strlen (string) + 1);
 
   for (r = result, s = string; s && (c = *s); s++)
     {
@@ -148,6 +183,7 @@ sh_backslash_quote (string)
        case '*': case '[': case '?': case ']': /* globbing chars */
        case '^':
        case '$': case '`':                     /* expansion chars */
+       case ',':                               /* brace expansion */
          *r++ = '\\';
          *r++ = c;
          break;
@@ -157,7 +193,13 @@ sh_backslash_quote (string)
            *r++ = '\\';
          *r++ = c;
          break;
+
+       case CTLESC: case CTLNUL:               /* internal quoting characters */
+         *r++ = CTLESC;                        /* could be '\\'? */
+         *r++ = c;
+         break;
 #endif
+
        case '#':                               /* comment char */
          if (s == string)
            *r++ = '\\';
@@ -179,15 +221,18 @@ char *
 sh_backslash_quote_for_double_quotes (string)
      char *string;
 {
-  int c;
+  unsigned char c;
   char *result, *r, *s;
 
-  result = xmalloc (2 * strlen (string) + 1);
+  result = (char *)xmalloc (2 * strlen (string) + 1);
 
   for (r = result, s = string; s && (c = *s); s++)
     {
       if (sh_syntaxtab[c] & CBSDQUOTE)
        *r++ = '\\';
+      /* I should probably add flags for these to sh_syntaxtab[] */
+      else if (c == CTLESC || c == CTLNUL)
+       *r++ = CTLESC;          /* could be '\\'? */
 
       *r++ = c;
     }
@@ -219,6 +264,7 @@ sh_contains_shell_metas (string)
        case '~':                               /* tilde expansion */
          if (s == string || s[-1] == '=' || s[-1] == ':')
            return (1);
+         break;
        case '#':
          if (s == string)                      /* comment char */
            return (1);