commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r813369 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_port.h port/shquote.c
Date Thu, 10 Sep 2009 11:20:55 GMT
Author: mturk
Date: Thu Sep 10 11:20:54 2009
New Revision: 813369

URL: http://svn.apache.org/viewvc?rev=813369&view=rev
Log:
Add strquote that is mandatory for windows cmdlines

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h
    commons/sandbox/runtime/trunk/src/main/native/port/shquote.c

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=813369&r1=813368&r2=813369&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 Thu Sep 10 11:20:54 2009
@@ -118,6 +118,7 @@
 size_t shquote(const char *, char *, size_t);
 size_t shquotev(int, char * const *, char *, size_t);
 #endif
+size_t strquote(const char *, char *, size_t);
+size_t wcsquote(const wchar_t *, wchar_t *, size_t);
 
 #endif /* _ACR_PORT_H */
-

Modified: 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=813369&r1=813368&r2=813369&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/port/shquote.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/port/shquote.c Thu Sep 10 11:20:54 2009
@@ -78,11 +78,9 @@
 #include "acr_arch.h"
 #include "acr_port.h"
 
-#if defined(ACR_WANT_SHQUOTE)
-
 #define INCH()                              \
     do {                                    \
-        n = ((c = *arg) != '\0') ? 1 : 0;   \
+        n = ((c = *arg) != 0) ? 1 : 0;      \
     } while (0)
 
 #define PUTC(x)                             \
@@ -91,8 +89,8 @@
         if (bufsize != 0) {                 \
             if (bufsize < 1 ||              \
                 (bufsize == 1 &&            \
-                (x) != '\0')) {             \
-                *buf = '\0';                \
+                (x) != 0)) {                \
+                *buf = 0;                   \
                 bufsize = 0;                \
             } else {                        \
                 *(buf++) = (x);             \
@@ -101,6 +99,7 @@
         }                                   \
     } while (0)
 
+#if defined(ACR_WANT_SHQUOTE)
 size_t shquote(const char *arg, char *buf, size_t bufsize)
 {
     char c, lastc;
@@ -191,8 +190,115 @@
 bad:
     return (size_t)-1;
 }
-#else
+#endif /* ACR_WANT_SHQUOTE */
 
-UNUSED_SOURCE_FILE(shquote);
+/* Just like shquote but surounds the arg by double quotes if
+ * the arg contains space sharacters
+ */
+size_t strquote(const char *arg, char *buf, size_t bufsize)
+{
+    char c, lastc;
+    size_t rv;
+    int n;
 
-#endif /* ACR_WANT_SHQUOTE */
+    rv    = 0;
+    lastc = 0;
+    if (!strpbrk(arg, " \t")) {
+        /* Just copy over the arg */
+        return strlcpy(buf, arg, bufsize);
+    }
+    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;
+}
+
+size_t wcsquote(const wchar_t *arg, wchar_t *buf, size_t bufsize)
+{
+    wchar_t c, lastc;
+    size_t rv;
+    int n;
+
+    rv    = 0;
+    lastc = 0;
+    if (!wcspbrk(arg, L" \t")) {
+        /* Just copy over the arg */
+        return wcslcpy(buf, arg, bufsize);
+    }
+    if (*arg != L'"') {
+        PUTC(L'"');
+    }
+    for (;;) {
+        INCH();
+        if (n <= 0)
+            break;
+        arg += n;
+        lastc = c;
+
+        if (c == L'"') {
+            if (rv != 0) {
+                PUTC(L'"');
+            }
+            PUTC(L'\\');
+            PUTC(L'"');
+            for (;;) {
+                INCH();
+                if (n <= 0 || c != L'"')
+                    break;
+                PUTC(L'\\');
+                PUTC(L'"');
+                arg += n;
+            }
+            if (n > 0) {
+                PUTC(L'"');
+            }
+        }
+        else {
+            PUTC(c);
+        }
+    }
+    if (lastc != L'"') {
+        PUTC(L'"');
+    }
+    /* Put NUL terminator, but don't count the NUL. */
+    PUTC(L'\0');
+    rv--;
+
+    return rv;
+}



Mime
View raw message