]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Add subroutines for passing strings to a program through a shell.
authorBruno Haible <bruno@clisp.org>
Sun, 16 Sep 2001 15:53:03 +0000 (15:53 +0000)
committerBruno Haible <bruno@clisp.org>
Sun, 16 Sep 2001 15:53:03 +0000 (15:53 +0000)
lib/ChangeLog
lib/Makefile.am
lib/sh-quote.c [new file with mode: 0644]
lib/sh-quote.h [new file with mode: 0644]

index b61da9fd7e89ff5efac093baaf67ae29e25f90a7..46917cd587d816f2e7c4ed2f5e71cdaaaff073e7 100644 (file)
@@ -1,3 +1,10 @@
+2001-09-06  Bruno Haible  <haible@clisp.cons.org>
+
+       * sh-quote.h: New file.
+       * sh-quote.c: New file.
+       * Makefile.am (libnlsut_a_SOURCES): Add sh-quote.c.
+       (noinst_HEADERS): Add sh-quote.h.
+
 2001-09-06  Bruno Haible  <haible@clisp.cons.org>
 
        * strpbrk.h: New file.
index d0fea8797fffbe77810dae1f1f42ab9020d57c78..cec62be3b77e5c8fbfd928ee5fccbdcb37b0a879 100644 (file)
@@ -30,15 +30,16 @@ gen-lbrkprop.c 3level.h
 libnlsut_a_SOURCES = basename.c c-ctype.c concatpath.c execute.c findprog.c \
 fstrcmp.c full-write.c gcd.c getopt.c getopt1.c hash.c linebreak.c \
 localcharset.c mbswidth.c obstack.c pipe-bidi.c pipe-in.c pipe-out.c \
-progname.c safe-read.c tmpdir.c wait-process.c xerror.c xgetcwd.c xmalloc.c \
-xstrdup.c
+progname.c safe-read.c sh-quote.c tmpdir.c wait-process.c xerror.c xgetcwd.c \
+xmalloc.c xstrdup.c
 
 libnlsut_a_LIBADD = @ALLOCA@ @LIBOBJS@
 
 noinst_HEADERS = c-ctype.h error.h execute.h findprog.h fstrcmp.h \
 full-write.h gcd.h getline.h getopt.h hash.h lbrkprop.h linebreak.h mbswidth.h \
-mkdtemp.h obstack.h pathmax.h pipe.h progname.h safe-read.h setenv.h strpbrk.h \
-system.h tmpdir.h utf8-ucs4.h utf16-ucs4.h wait-process.h xerror.h
+mkdtemp.h obstack.h pathmax.h pipe.h progname.h safe-read.h setenv.h \
+sh-quote.h strpbrk.h system.h tmpdir.h utf8-ucs4.h utf16-ucs4.h wait-process.h \
+xerror.h
 
 DEFS = -DLIBDIR=\"$(libdir)\" @DEFS@
 INCLUDES = -I. -I$(srcdir) -I.. -I../intl
diff --git a/lib/sh-quote.c b/lib/sh-quote.c
new file mode 100644 (file)
index 0000000..4ecbc52
--- /dev/null
@@ -0,0 +1,154 @@
+/* Shell quoting.
+   Copyright (C) 2001 Free Software Foundation, Inc.
+   Written by Bruno Haible <haible@clisp.cons.org>, 2001.
+
+   This program 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.
+
+   This program 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 this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification.  */
+#include "sh-quote.h"
+
+#include <string.h>
+
+#include "strpbrk.h"
+#include "system.h"
+
+
+#define SHELL_SPECIAL_CHARS "\t\n !\"#$&'()*;<=>?[\\]`{|}~"
+
+/* Returns the number of bytes needed for the quoted string.  */
+size_t
+shell_quote_length (string)
+     const char *string;
+{
+  if (strpbrk (string, SHELL_SPECIAL_CHARS) == NULL)
+    return strlen (string);
+  else
+    {
+      char qchar = '\0';       /* last quote character: none or ' or " */
+      size_t length = 0;
+
+      for (; *string != '\0'; string++)
+       {
+         char c = *string;
+         char q = (c == '\'' ? '"' : '\'');
+
+         if (qchar != q)
+           {
+             if (qchar)
+               length++;
+             qchar = q;
+             length++;
+           }
+         length++;
+       }
+      if (qchar)
+       length++;
+
+      return length;
+    }
+}
+
+/* Copies the quoted string to p and returns the incremented p.  */
+char *
+shell_quote_copy (p, string)
+     char *p;
+     const char *string;
+{
+  if (strpbrk (string, SHELL_SPECIAL_CHARS) == NULL)
+    {
+      memcpy (p, string, strlen (string));
+      return p + strlen (string);
+    }
+  else
+    {
+      char qchar = '\0';       /* last quote character: none or ' or " */
+
+      for (; *string != '\0'; string++)
+       {
+         char c = *string;
+         char q = (c == '\'' ? '"' : '\'');
+
+         if (qchar != q)
+           {
+             if (qchar)
+               *p++ = qchar;
+             qchar = q;
+             *p++ = qchar;
+           }
+         *p++ = c;
+       }
+      if (qchar)
+       *p++ = qchar;
+
+      return p;
+    }
+}
+
+/* Returns the freshly allocated quoted string.  */
+char *
+shell_quote (string)
+     const char *string;
+{
+  size_t length = shell_quote_length (string);
+  char *quoted = (char *) xmalloc (length + 1);
+  char *p = shell_quote_copy (quoted, string);
+  *p = '\0';
+  return quoted;
+}
+
+/* Returns a freshly allocated string containing all argument strings, quoted,
+   separated through spaces.  */
+char *
+shell_quote_argv (argv)
+     char **argv;
+{
+  if (*argv != NULL)
+    {
+      char **argp;
+      size_t length;
+      char *command;
+      char *p;
+
+      length = 0;
+      for (argp = argv; ; )
+       {
+         length += shell_quote_length (*argp) + 1;
+         argp++;
+         if (*argp == NULL)
+           break;
+       }
+
+      command = (char *) xmalloc (length);
+
+      p = command;
+      for (argp = argv; ; )
+       {
+         p = shell_quote_copy (p, *argp);
+         argp++;
+         if (*argp == NULL)
+           break;
+         *p++ = ' ';
+       }
+      *p = '\0';
+
+      return command;
+    }
+  else
+    return xstrdup ("");
+}
diff --git a/lib/sh-quote.h b/lib/sh-quote.h
new file mode 100644 (file)
index 0000000..8c18c98
--- /dev/null
@@ -0,0 +1,36 @@
+/* Shell quoting.
+   Copyright (C) 2001 Free Software Foundation, Inc.
+   Written by Bruno Haible <haible@clisp.cons.org>, 2001.
+
+   This program 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.
+
+   This program 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 this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+/* When passing a command to a shell, we must quote the program name and
+   arguments, since Unix shells interpret characters like " ", "'", "<", ">",
+   "$" etc. in a special way.  */
+
+#include <stddef.h>
+
+/* Returns the number of bytes needed for the quoted string.  */
+extern size_t shell_quote_length PARAMS ((const char *string));
+
+/* Copies the quoted string to p and returns the incremented p.  */
+extern char * shell_quote_copy PARAMS ((char *p, const char *string));
+
+/* Returns the freshly allocated quoted string.  */
+extern char * shell_quote PARAMS ((const char *string));
+
+/* Returns a freshly allocated string containing all argument strings, quoted,
+   separated through spaces.  */
+extern char * shell_quote_argv PARAMS ((char **argv));