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 2010/01/12 21:53:44 UTC

svn commit: r898504 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_args.h os/win32/registry.c os/win32/wutil.c shared/args.c shared/ini.c

Author: mturk
Date: Tue Jan 12 20:53:43 2010
New Revision: 898504

URL: http://svn.apache.org/viewvc?rev=898504&view=rev
Log:
Add expand callback to argument expand

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_args.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c
    commons/sandbox/runtime/trunk/src/main/native/shared/args.c
    commons/sandbox/runtime/trunk/src/main/native/shared/ini.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_args.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_args.h?rev=898504&r1=898503&r2=898504&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_args.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_args.h Tue Jan 12 20:53:43 2010
@@ -32,24 +32,35 @@
  */
 
 /**
+ * Callback used to expand the ${FOO} in the argument
+ * This callback takes precedence over the ACR_GetEnv.
+ */
+typedef char    * (acr_expand_var_fn_a)(void *opaque, const char *name);
+typedef wchar_t * (acr_expand_var_fn_w)(void *opaque, const wchar_t *name);
+
+/**
  * This function expands $(FOO) or ${FOO} to ACR_GetEnv("FOO")
  * unless the $(FOO) is enclosed inside single quotes.
  * Input string is freed if expansion took place.
- * 
+ *
  * @param str Input string to expand.
  * @return expanded string.
  */
-ACR_DECLARE(char *) ACR_ArgumentExpandA(char *str);
+ACR_DECLARE(char *) ACR_ArgumentExpandA(char *str,
+                                        acr_expand_var_fn_a *expand,
+                                        void *context);
 
 /**
  * This function expands $(FOO) or ${FOO} to ACR_GetEnv("FOO")
  * unless the $(FOO) is enclosed inside single quotes.
  * Input string is freed if expansion took place.
- * 
+ *
  * @param str Input string to expand.
  * @return expanded string.
  */
-ACR_DECLARE(wchar_t *) ACR_ArgumentExpandW(wchar_t *str);
+ACR_DECLARE(wchar_t *) ACR_ArgumentExpandW(wchar_t *str,
+                                           acr_expand_var_fn_w *expand,
+                                           void *context);
 
 /**
  * This function provides a way to parse a generic argument string

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c?rev=898504&r1=898503&r2=898504&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c Tue Jan 12 20:53:43 2010
@@ -808,7 +808,7 @@
             SAFE_CLOSE_KEY(k.key);
             if (exn) {
                 *args = s_malloc(wchar_t *, 2);
-                (*args)[0] = ACR_ArgumentExpandW(exn);
+                (*args)[0] = ACR_ArgumentExpandW(exn, NULL, NULL);
                 (*args)[1] = NULL;
 
                 return 0;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c?rev=898504&r1=898503&r2=898504&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c Tue Jan 12 20:53:43 2010
@@ -585,3 +585,21 @@
     }
 }
 
+/* This function is wrapper over SearchPathW
+ */
+ACR_DECLARE(wchar_t *) ACR_SearchSystemPathW(const wchar_t *file,
+                                             const wchar_t *extension)
+{
+
+    wchar_t buff[ACR_PPATH_MAX];
+
+    if (!SearchPathW(NULL,
+                     file,
+                     extension,
+                     ACR_PPATH_MAX,
+                     buff,
+                     NULL))
+        return NULL;
+    else
+        return wcsdup(buff);
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/args.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/args.c?rev=898504&r1=898503&r2=898504&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/args.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/args.c Tue Jan 12 20:53:43 2010
@@ -85,7 +85,9 @@
  * single quotes.
  * Input string is freed if expansion took place
  */
-ACR_DECLARE(char *) ACR_ArgumentExpandA(char *str)
+ACR_DECLARE(char *) ACR_ArgumentExpandA(char *str,
+                                        acr_expand_var_fn_a *expand,
+                                        void *context)
 {
     char        *var_pos;
     char        *var_end;
@@ -129,7 +131,10 @@
             *var_end++ = '\0';
             /* var_pos holds the variable name
              */
-            var_rep = ACR_EnvGet(var_pos);
+            if (expand)
+                var_rep = (*expand)(context, var_pos);
+            if (var_rep == NULL)
+                var_rep = ACR_EnvGet(var_pos);
             if (var_rep) {
                 /* Replace ${FOO} with getenv("FOO")
                  */
@@ -189,9 +194,10 @@
  * single quotes.
  * Input string is freed if expansion took place
  */
-ACR_DECLARE(wchar_t *) ACR_ArgumentExpandW(wchar_t *str)
+ACR_DECLARE(wchar_t *) ACR_ArgumentExpandW(wchar_t *str,
+                                           acr_expand_var_fn_w *expand,
+                                           void *context)
 {
-#if defined(WIN32)
     /* This function is noop for non windows platforms
      */
     wchar_t     *var_pos;
@@ -236,7 +242,12 @@
             *var_end++ = L'\0';
             /* var_pos holds the variable name
              */
-            var_rep = acr_EnvGetW(var_pos);
+            if (expand)
+                var_rep = (*expand)(context, var_pos);
+#if defined(WIN32)
+            if (var_rep == NULL)
+                var_rep = acr_EnvGetW(var_pos);
+#endif
             if (var_rep) {
                 /* Replace ${FOO} with getenv("FOO")
                  */
@@ -266,6 +277,7 @@
     x_free(str);
 
     str = acr_wbuf_data(&sbuf);
+#if defined(WIN32)
     /* Check for the possible %FOO% */
     if (wcschr(str, L'%')) {
         wchar_t *buf;
@@ -529,7 +541,7 @@
                                             params[argidx],
                                             NULL);
                 }
-                (*argv)[argnum] = ACR_ArgumentExpandA(argument);
+                (*argv)[argnum] = ACR_ArgumentExpandA(argument, NULL, NULL);
                 continue;
             }
             else if (acr_isdigit(*cp) && *(cp + 1) == '\0') {
@@ -555,7 +567,7 @@
                 }
             }
         }
-        (*argv)[argnum] = ACR_ArgumentExpandA(x_strdup(argument));
+        (*argv)[argnum] = ACR_ArgumentExpandA(x_strdup(argument), NULL, NULL);
     }
 
     x_free(cmds);
@@ -624,7 +636,7 @@
                                             params[argidx],
                                             NULL);
                 }
-                (*argv)[argnum] = ACR_ArgumentExpandW(argument);
+                (*argv)[argnum] = ACR_ArgumentExpandW(argument, NULL, NULL);
                 continue;
             }
             else if (iswdigit(*cp) && *(cp + 1) == L'\0') {
@@ -650,7 +662,7 @@
                 }
             }
         }
-        (*argv)[argnum] = ACR_ArgumentExpandW(x_wcsdup(argument));
+        (*argv)[argnum] = ACR_ArgumentExpandW(x_wcsdup(argument), NULL, NULL);
     }
 
     x_free(cmds);

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/ini.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/ini.c?rev=898504&r1=898503&r2=898504&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/ini.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/ini.c Tue Jan 12 20:53:43 2010
@@ -28,6 +28,7 @@
 #include "acr_env.h"
 #include "acr_sbuf.h"
 #include "acr_tables.h"
+#include "acr_args.h"
 #include "acr_ini.h"
 
 /**
@@ -371,9 +372,11 @@
     /* check for empty strings */
     if (!(i = (int)strlen(s)))
         return s;
+    /* right trim */
     for (i = i - 1; i >= 0 && acr_isspace(s[i]); i--)
         ;
     s[i + 1] = '\0';
+    /* left trim */
     for (i = 0; s[i] != '\0' && acr_isspace(s[i]); i++)
         ;
 
@@ -392,6 +395,7 @@
     /* check for empty strings */
     if (!s || !(i = (int)strlen(s)))
         return s;
+    /* right trim */
     for (i = i - 1; i >= 0 && acr_isspace(s[i]); i--)
         ;
     s[i + 1] = '\0';
@@ -413,50 +417,22 @@
 {
     const char *scanp;
     int c, sc, pc = *s1;
+    int escaped = 0;
 
     /* Some early sanity check */
     if (!s1 || !*s1)
         return NULL;
     while ((c = *s1++) != 0) {
         for (scanp = s2; (sc = *scanp++) != 0;) {
-            if (sc == c && pc != '\\')
+            if (sc == c && !escaped)
                 return (char *)(s1 - 1);
         }
-        /* Don't update the previous marker if it was '\\' already
-         * In that case we have escaped backslash.
+        /* Flip escaping flag.
          */
-        if (pc == '\\' && c == '\\')
-            pc = 0;
+        if (c == '\\')
+            escaped = !escaped;
         else
-            pc = c;
-    }
-    return NULL;
-}
-
-/**
- * Just like strpbrk but it doesn't break if the char
- * is escaped inside single quotes.
- */
-static char *strpbrk_s(const char *s1, const char *s2)
-{
-    const char *scanp;
-    int inquote = 0;
-    int c, sc, pc = *s1;
-
-    /* Some early sanity check */
-    if (!s1 || !*s1)
-        return NULL;
-    while ((c = *s1++) != 0) {
-        for (scanp = s2; (sc = *scanp++) != '\0';) {
-            if (sc == c && inquote == 0)
-                return (char *)(s1 - 1);
-        }
-        /* Don't update the previous marker if it was \' already
-         * In that case we have escaped single quote.
-         */
-        if (c == '\'' && pc != '\\')
-            inquote = !inquote;
-        pc = c;
+            escaped = 0;
     }
     return NULL;
 }
@@ -495,93 +471,30 @@
     return saved;
 }
 
-static char *ini_expand(char *str, acr_ini_t *ini)
+/* Expand callback
+ * Uses the attributes of the root node as keys for
+ * ${FOO} or $(FOO) replacement. It takes precedence
+ * over the environment variables with the same name.
+ */
+static char *ini_expand_cb(void *i, const char *name)
 {
-    char        *var_pos;
-    char        *var_end;
-    char        *var_ptr;
-    char        *var_rep;
-    acr_sbuf_t   sbuf;
-
-    var_ptr = str;
-
-    if (!var_ptr || !*var_ptr)
-        return str; /* Guard against zero input */
-    var_pos = strpbrk_s(var_ptr, "$");
-    if (!var_pos) {
-        /* Nothing to replace.
+    acr_ini_t *ini = (acr_ini_t *)i;
+    char *var_rep  = NULL;
+    acr_ini_attr_t *ap;
+    acr_ini_node_t *np = &ini->root;
+
+    ACR_RING_FOREACH(ap, &np->attr_ring, acr_ini_attr_t, link) {
+        /* Match the Variable with Attribute key.
          */
-        return str;
-    }
-    acr_sbuf_new(&sbuf, NULL, strlen(str), ACR_SBUF_AUTOEXTEND);
-    /* Loop for each unescaped $ */
-    while (var_pos) {
-        int wch = 0;
-        var_end = NULL;
-        var_rep = NULL;
-        if (*(var_pos + 1) == '(') {
-            var_end = strpbrk(var_pos + 1, " })");
-            wch = ')';
-        }
-        else if (*(var_pos + 1) == '{') {
-            var_end = strpbrk(var_pos + 1, " })");
-            wch = '}';
-        }
-        if (var_end && *var_end == wch) {
-            *var_pos++ = '\0';
-            *var_pos++ = '\0';
-            *var_end++ = '\0';
-            /* Add the string before $ */
-            acr_sbuf_cat(&sbuf, var_ptr);
-            /* var_pos holds the variable name */
-            if (ini) {
-                acr_ini_attr_t *ap;
-                acr_ini_node_t *np = &ini->root;
-                ACR_RING_FOREACH(ap, &np->attr_ring, acr_ini_attr_t, link) {
-                    /* Match the Variable with Attribute key.
-                     */
-                    if (*ap->key && strcmp(ap->key, var_pos) == 0) {
-                        if (!ap->val)
-                            var_rep = "";
-                        else
-                            var_rep = ap->val;
-                        break;
-                    }
-                }
-            }
-            if (var_rep == NULL)
-                var_rep = ACR_EnvGet(var_pos);
-            if (var_rep) {
-                acr_sbuf_cat(&sbuf, var_rep);
-                x_free(var_rep);
-            }
-            else {
-                acr_sbuf_putc(&sbuf, '$');
-                if (wch == '}')
-                    acr_sbuf_putc(&sbuf, '{');
-                else
-                    acr_sbuf_putc(&sbuf, '(');
-                acr_sbuf_cat(&sbuf, var_pos);
-                acr_sbuf_putc(&sbuf, wch);
-            }
-            var_ptr = var_end;
-            var_pos = strpbrk_s(var_ptr, "$");
-        }
-        else {
-            *var_pos++ = '\0';
-            acr_sbuf_cat(&sbuf, var_ptr);
-            acr_sbuf_putc(&sbuf, '$');
-            var_ptr = var_pos;
-            var_pos = strpbrk_s(var_ptr, "$");
+        if (*ap->key && strcmp(ap->key, name) == 0) {
+            if (!ap->val)
+                var_rep = strdup("");
+            else
+                var_rep = strdup(ap->val);
+            break;
         }
     }
-    /* Add what's left from the original string */
-    acr_sbuf_cat(&sbuf, var_ptr);
-    acr_sbuf_finish(&sbuf);
-    x_free(str);
-
-    str = acr_sbuf_data(&sbuf);
-    return str;
+    return var_rep;
 }
 
 /*
@@ -634,7 +547,7 @@
             ACR_IniAttrAddVal(_E, attr, line);
             if (!nextline) {
                 attr->val = strunesc(attr->val);
-                attr->val = ini_expand(attr->val, NULL);
+                attr->val = ACR_ArgumentExpandA(attr->val, NULL, NULL);
             }
             continue;
         }
@@ -684,7 +597,7 @@
             attr = ACR_IniNodeAttrAdd(_E, cur, line, val);
             if (attr->val && !nextline) {
                 attr->val = strunesc(attr->val);
-                attr->val = ini_expand(attr->val, NULL);
+                attr->val = ACR_ArgumentExpandA(attr->val, NULL, NULL);
             }
         }
     }
@@ -740,7 +653,7 @@
             ACR_IniAttrAddVal(_E, attr, line);
             if (!nextline) {
                 attr->val = strunesc(attr->val);
-                attr->val = ini_expand(attr->val, NULL);
+                attr->val = ACR_ArgumentExpandA(attr->val, NULL, NULL);
             }
             continue;
         }
@@ -774,7 +687,7 @@
         }
         if (attr->val && !nextline) {
             attr->val = strunesc(attr->val);
-            attr->val = ini_expand(attr->val, NULL);
+            attr->val = ACR_ArgumentExpandA(attr->val, NULL, NULL);
         }
     }
     fclose(fp);
@@ -823,7 +736,7 @@
             ACR_IniAttrAddVal(_E, attr, line);
             if (!nextline) {
                 attr->val = strunesc(attr->val);
-                attr->val = ini_expand(attr->val, ini);
+                attr->val = ACR_ArgumentExpandA(attr->val, ini_expand_cb, ini);
             }
             continue;
         }
@@ -892,7 +805,7 @@
             attr = ACR_IniNodeAttrAdd(_E, cur, key, val);
             if (attr->val && !nextline) {
                 attr->val = strunesc(attr->val);
-                attr->val = ini_expand(attr->val, ini);
+                attr->val = ACR_ArgumentExpandA(attr->val, ini_expand_cb, ini);
             }
         }
     }