You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/08 12:11:55 UTC

svn commit: r812428 - in /commons/sandbox/runtime/trunk/src/main/native: ./ include/ include/arch/hpux/ include/arch/linux/ include/arch/solaris/ include/arch/windows/ port/ test/

Author: mturk
Date: Tue Sep  8 10:11:54 2009
New Revision: 812428

URL: http://svn.apache.org/viewvc?rev=812428&view=rev
Log:
Add shquote port

Added:
    commons/sandbox/runtime/trunk/src/main/native/port/shquote.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h
    commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=812428&r1=812427&r2=812428&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Tue Sep  8 10:11:54 2009
@@ -215,6 +215,7 @@
 	$(SRCDIR)/port/fnmatch.$(OBJ) \
 	$(SRCDIR)/port/rijndael.$(OBJ) \
 	$(SRCDIR)/port/pmatch.$(OBJ) \
+	$(SRCDIR)/port/shquote.$(OBJ) \
 	$(SRCDIR)/port/strlcat.$(OBJ) \
 	$(SRCDIR)/port/strlcpy.$(OBJ) \
 	$(SRCDIR)/port/wcslcat.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=812428&r1=812427&r2=812428&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Tue Sep  8 10:11:54 2009
@@ -135,6 +135,7 @@
 	$(SRCDIR)/port/fnmatch.$(OBJ) \
 	$(SRCDIR)/port/rijndael.$(OBJ) \
 	$(SRCDIR)/port/pmatch.$(OBJ) \
+	$(SRCDIR)/port/shquote.$(OBJ) \
 	$(SRCDIR)/port/strlcat.$(OBJ) \
 	$(SRCDIR)/port/strlcpy.$(OBJ) \
 	$(SRCDIR)/port/wcslcat.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h?rev=812428&r1=812427&r2=812428&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h Tue Sep  8 10:11:54 2009
@@ -114,5 +114,10 @@
 int  fnmatch(const char *, const char *, int);
 #endif
 
+#if defined(ACR_WANT_SHQUOTE)
+size_t shquote(const char *, char *, size_t);
+size_t shquotev(int, char * const *, char *, size_t);
+#endif
+
 #endif /* _ACR_PORT_H */
 

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h?rev=812428&r1=812427&r2=812428&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h Tue Sep  8 10:11:54 2009
@@ -44,6 +44,7 @@
  * Include needed functions from portable layer
  */
 #define ACR_WANT_PMATCH                     1
+#define ACR_WANT_SHQUOTE                    1
 #define ACR_WANT_STRSIGNAL                  1
 #define ACR_WANT_STRLCAT                    1
 #define ACR_WANT_STRLCPY                    1

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h?rev=812428&r1=812427&r2=812428&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h Tue Sep  8 10:11:54 2009
@@ -44,6 +44,7 @@
  * Include needed functions from portable layer
  */
 #define ACR_WANT_PMATCH                     1
+#define ACR_WANT_SHQUOTE                    1
 #define ACR_WANT_STRLCAT                    1
 #define ACR_WANT_STRLCPY                    1
 #define ACR_WANT_WCSLCAT                    1

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h?rev=812428&r1=812427&r2=812428&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h Tue Sep  8 10:11:54 2009
@@ -43,6 +43,7 @@
  * Include needed functions from portable layer
  */
 #define ACR_WANT_PMATCH                     1
+#define ACR_WANT_SHQUOTE                    1
 #define ACR_WANT_STRLCAT                    1
 #define ACR_WANT_STRLCPY                    1
 #define ACR_WANT_WCSLCAT                    1

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h?rev=812428&r1=812427&r2=812428&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h Tue Sep  8 10:11:54 2009
@@ -102,6 +102,7 @@
  * Include needed functions from portable layer
  */
 #define ACR_WANT_PMATCH                     1
+#define ACR_WANT_SHQUOTE                    1
 #define ACR_WANT_STRSIGNAL                  1
 #define ACR_WANT_STRLCAT                    1
 #define ACR_WANT_STRLCPY                    1

Added: commons/sandbox/runtime/trunk/src/main/native/port/shquote.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/port/shquote.c?rev=812428&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/port/shquote.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/port/shquote.c Tue Sep  8 10:11:54 2009
@@ -0,0 +1,198 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Copyright (c) 2001 Christopher G. Demetriou
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *          This product includes software developed for the
+ *          NetBSD Project.  See http://www.NetBSD.org/ for
+ *          information about NetBSD.
+ * 4. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
+ */
+
+/*
+ * shquote():
+ *
+ * Requotes arguments so that they'll be interpreted properly by the
+ * shell (/bin/sh).
+ *
+ * Wraps single quotes around the string, and replaces single quotes
+ * in the string with the sequence:
+ *  '\''
+ *
+ * Returns the number of characters required to hold the resulting quoted
+ * argument.
+ *
+ * The buffer supplied is filled in and NUL-terminated.  If 'bufsize'
+ * indicates that the buffer is too short to hold the output string, the
+ * first (bufsize - 1) bytes of quoted argument are filled in and the
+ * buffer is NUL-terminated.
+ *
+ * Changes could be made to optimize the length of strings output by this
+ * function:
+ *
+ *  * if there are no metacharacters or whitespace in the input,
+ *    the output could be the input string.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_port.h"
+
+#if defined(ACR_WANT_SHQUOTE)
+
+#define INCH()                              \
+    do {                                    \
+        n = ((c = *arg) != '\0') ? 1 : 0;   \
+    } while (0)
+
+#define PUTC(x)                             \
+    do {                                    \
+        ++rv;                               \
+        if (bufsize != 0) {                 \
+            if (bufsize < 1 ||              \
+                (bufsize == 1 &&            \
+                (x) != '\0')) {             \
+                *buf = '\0';                \
+                bufsize = 0;                \
+            } else {                        \
+                *(buf++) = (x);             \
+                bufsize--;                  \
+            }                               \
+        }                                   \
+    } while (0)
+
+size_t shquote(const char *arg, char *buf, size_t bufsize)
+{
+    char c, lastc;
+    size_t rv;
+    int n;
+
+    rv    = 0;
+    lastc = 0;
+    if (*arg != '\'') {
+        PUTC('\'');
+    }
+    for (;;) {
+        INCH();
+        if (n <= 0)
+            break;
+        arg += n;
+        lastc = c;
+
+        if (c == '\'') {
+            if (rv != 0) {
+                PUTC('\'');
+            }
+            PUTC('\\');
+            PUTC('\'');
+            for (;;) {
+                INCH();
+                if (n <= 0 || c != '\'')
+                    break;
+                PUTC('\\');
+                PUTC('\'');
+                arg += n;
+            }
+            if (n > 0) {
+                PUTC('\'');
+            }
+        }
+        else {
+            PUTC(c);
+        }
+    }
+    if (lastc != '\'') {
+        PUTC('\'');
+    }
+    /* Put NUL terminator, but don't count the NUL. */
+    PUTC('\0');
+    rv--;
+
+    return rv;
+}
+
+/*
+ * shquotev():
+ *
+ * Apply shquote() to a set of strings, separating the results by spaces.
+ */
+size_t shquotev(int argc, char * const * argv, char *buf, size_t bufsize)
+{
+    size_t rv, callrv;
+    int i;
+
+    rv = 0;
+
+    if (argc == 0) {
+        if (bufsize != 0)
+            *buf = '\0';
+        return rv;
+    }
+
+    for (i = 0; i < argc; i++) {
+        callrv = shquote(argv[i], buf, bufsize);
+        if (callrv == (size_t)-1)
+            goto bad;
+        rv += callrv;
+        buf += callrv;
+        bufsize = (bufsize > callrv) ? (bufsize - callrv) : 0;
+
+        if (i < (argc - 1)) {
+            rv++;
+            if (bufsize > 1) {
+                *buf++ = ' ';
+                bufsize--;
+            }
+        }
+    }
+
+    return rv;
+
+bad:
+    return (size_t)-1;
+}
+#else
+
+UNUSED_SOURCE_FILE(shquote);
+
+#endif /* ACR_WANT_SHQUOTE */

Propchange: commons/sandbox/runtime/trunk/src/main/native/port/shquote.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c?rev=812428&r1=812427&r2=812428&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c Tue Sep  8 10:11:54 2009
@@ -37,6 +37,7 @@
 #include "acr_dso.h"
 #include "acr_ring.h"
 #include "acr_ini.h"
+#include "acr_port.h"
 #include "acr_time.h"
 #include "acr_version.h"
 
@@ -408,6 +409,7 @@
 {
     int i;
     size_t mp = 0;
+    char buf[1024];
     const char *end = NULL;
 
     if (argc < 2) {
@@ -418,6 +420,9 @@
     printf("smatch returned  %d\n", i);
     printf("smatch ended on '%s'\n", argv[0] + mp);
 
+    printf("original string is %s.\n", argv[0]);
+    shquote(argv[0], buf, 1024);
+    printf("quoted   string is %s.\n", buf);
     return 0;
 }