Hi,

Attached is a patch to fix compilation of the dynamic library glue in
JSVC. This has been tested on Mac OS X 10.4.8, and to ensure I didn't
break regular Unix support I also tested on CentOS 3. I have also
attached a patch that tidies up the "replace" code that replaces
occurrences of one string with another one. This simplifies the code and
avoids a possible buffer overrun.

I also have a few other changes, including some consistency fixes to the
help text and command line options. I will submit patches for them when
I have tested on both Mac OS X and Linux.

Regards,

Chris
Index: src/native/unix/native/dso.h
===================================================================
--- src/native/unix/native/dso.h        (revision 467118)
+++ src/native/unix/native/dso.h        (working copy)
@@ -1,12 +1,12 @@
 /*
    Copyright 2001-2004 The Apache Software Foundation.
- 
+
    Licensed 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.
@@ -14,14 +14,16 @@
    limitations under the License.
 */
 /* @version $Id$ */
-#include "jsvc.h"
 
-/**
- * A library handle represents a unique pointer to its location in memory.
- */
+#ifndef JSVC_DSO_H
+#define JSVC_DSO_H
+
 typedef void *dso_handle;
 
-bool dso_init(void);
-dso_handle dso_link(const char *pth);
-bool dso_unlink(dso_handle lib);
-void *dso_symbol(dso_handle lib, const char *nam);
+int dso_init(void);
+void *dso_link(const char *);
+int dso_unlink(void *);
+void *dso_symbol(void *, const char *);
+const char *dso_error(void);
+
+#endif
Index: src/native/unix/native/dso-dlfcn.c
===================================================================
--- src/native/unix/native/dso-dlfcn.c  (revision 467118)
+++ src/native/unix/native/dso-dlfcn.c  (working copy)
@@ -1,55 +1,67 @@
 /*
    Copyright 2001-2004 The Apache Software Foundation.
- 
+
    Licensed 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.
 */
+
 /* @version $Id$ */
-#include "jsvc.h"
 
 #ifdef DSO_DLFCN
 
 #include <dlfcn.h>
 
+#include "dso.h"
+#include "debug.h"
+
 #ifdef OS_LINUX
-bool ld_library_path_set=false;
-#endif /* ifdef OS_LINUX */
+int ld_library_path_set=false;
+#endif
 
 /* Initialize all DSO stuff */
-bool dso_init() {
-    return(true);
+int
+dso_init(void)
+{
+    return 1;
 }
 
 /* Attempt to link a library from a specified filename */
-dso_handle dso_link(const char *path) {
-    log_debug("Attemtping to load library %s",path);
+void *
+dso_link(const char *path)
+{
+    log_debug("Attempting to load library %s", path);
 
     return((void *)dlopen(path,RTLD_GLOBAL|RTLD_NOW));
 }
 
 /* Attempt to unload a library */
-bool dso_unlink(dso_handle libr) {
-    if (dlclose(libr)==0) return(true);
-    else return(false);
+int
+dso_unlink(void *libr)
+{
+    return dlclose(libr) == 0 ? 1 : 0;
 }
 
-/* Get the address for a specifed symbol */
-void *dso_symbol(dso_handle hdl, const char *nam) {
-    return(dlsym(hdl,nam));
+/* Get the address for a symbol */
+void *
+dso_symbol(void *hdl, const char *name)
+{
+    return dlsym(hdl, name);
 }
 
 /* Return the error message from dlopen */
-char *dso_error() {
-    return(dlerror());
+const char *
+dso_error(void)
+{
+    return dlerror();
 }
 
-#endif /* ifdef DSO_DLFCN */
+#endif /* DSO_DLFCN */
Index: src/native/unix/native/dso-dyld.c
===================================================================
--- src/native/unix/native/dso-dyld.c   (revision 467118)
+++ src/native/unix/native/dso-dyld.c   (working copy)
@@ -1,12 +1,12 @@
 /*
    Copyright 2001-2004 The Apache Software Foundation.
- 
+
    Licensed 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.
@@ -14,126 +14,161 @@
    limitations under the License.
 */
 /* @version $Id$ */
-#include "jsvc.h"
 
 #ifdef DSO_DYLD
 
 #include <mach-o/dyld.h>
+#include <stdlib.h>
+#include <string.h>
 
-#ifdef __bool_true_false_are_defined 
-/* We define these differently than stdbool.h, so ignore the defs there */
-#undef bool
-#undef true
-#undef false
-#endif
+#include "dso.h"
+#include "debug.h"
 
+static void nosymbol(const char *);
+static NSModule multiple(NSSymbol, NSModule, NSModule);
+static void linkedit(NSLinkEditErrors, int, const char *, const char *);
 
-/* Print an error message and abort all if a specified symbol wasn't found */
-static void nosymbol(const char *s) {
-    log_error("Cannot find symbol '%s' in library",s);
-    abort();
-}
-
-/* We found two symbols for the same name in two different modules */
-static NSModule multiple(NSSymbol s, NSModule om, NSModule nm) {
-    NSModule ret=nm;
-
-    log_debug("Symbol \"%s\" found in modules \"%s\" and \"%s\" (using %s)",
-        NSNameOfSymbol(s), NSNameOfModule(om), NSNameOfModule(nm),
-        NSNameOfModule(ret));
-
-    return(ret);
-}
-
-/* We got an error while linking a module, and if it's not a warning we have
-   to abort the whole program */
-static void linkedit(NSLinkEditErrors category, int number, const char *file,
-                     const char *message) {
-    log_error("Errors during link edit of file \"%s\" (error=%d): %s", file,
-              number,message);
-    /* Check if this error was only a warning */
-    if (category!=NSLinkEditWarningError) {
-        log_error("Cannot continue");
-        abort();
-    }
-}
-
 /* Initialize all DSO stuff */
-bool dso_init() {
+int
+dso_init(void)
+{
     NSLinkEditErrorHandlers h;
 
-    h.undefined=nosymbol;
-    h.multiple=multiple;
-    h.linkEdit=linkedit;
+    h.undefined = nosymbol;
+    h.multiple = multiple;
+    h.linkEdit = linkedit;
 
     NSInstallLinkEditErrorHandlers(&h);
-    return(true);
+
+    return 1;
 }
 
 /* Attempt to link a library from a specified filename */
-dso_handle dso_link(const char *path) {
+void *
+dso_link(const char *path)
+{
+    log_debug("Attempting to load library %s", path);
+
     /* We need to load the library publically as NSModuleFileImage is not
        yet implemented (at least for non MH_BUNDLE libraries */
-    if (NSAddLibrary(path)!=TRUE) return(NULL);
+    if (NSAddLibrary(path) != TRUE)
+        return NULL;
+
     /* We need to return a non-null value, even if it has no meaning. One day
        this whole crap will be fixed */
-    return((void *)!NULL);
+    return (void *)!NULL;
 }
 
 /* Attempt to unload a library */
-bool dso_unlink(dso_handle libr) {
+int
+dso_unlink(void *libr)
+{
     /* Check the handle */
-    if (libr==NULL) {
+    if (libr == NULL) {
         log_error("Attempting to unload a module without handle");
-        return(false);
+        return 0;
     }
 
     /* We don't have a module, so, we don't really have to do anything */
-    return(true);
+    return 1;
 }
 
-/* Get the address for a specifed symbol */
-void *dso_symbol(dso_handle hdl, const char *nam) {
-    NSSymbol sym=NULL;
-    NSModule mod=NULL;
-    char *und=NULL;
-    void *add=NULL;
-    int x=0;
+/* Get the address for a symbol */
+void *
+dso_symbol(void *hdl, const char *name)
+{
+    NSSymbol symbol;
+    NSModule module;
+    void *address;
+    char *buf;
 
     /* Check parameters */
-    if (hdl==NULL) {
+
+    if (hdl == NULL) {
         log_error("Invalid library handler specified");
-        return(NULL);
+        return NULL;
     }
 
-    if (nam==NULL) {
+    if (name == NULL) {
         log_error("Invalid symbol name specified");
-        return(NULL);
+        return NULL;
     }
 
-    /* Process the correct name (add a _ before the name) */
-    while (nam[x]!='\0') x++;
-    und=(char*)malloc(sizeof(char)*(x+2));
-    while(x>=0) und[x+1]=nam[x--];
-    und[0]='_';
+    /* Add an underscore before the name */
 
+    buf = malloc(strlen(name) + 2);
+
+    if (buf == NULL) {
+        log_error("Unable to allocate buffer");
+        return NULL;
+    }
+
+    buf[0] = '_';
+    strcpy(buf + 1, name);
+
     /* Find the symbol */
-    sym=NSLookupAndBindSymbol(und);
-    free(und);
-    if (sym==NULL) return(NULL);
 
+    symbol = NSLookupAndBindSymbol(buf);
+
+    free(buf);
+
+    if (symbol == NULL) {
+        log_error("Unable to find symbol for \"%s\"", name);
+        return NULL;
+    }
+
     /* Dump some debugging output since this part is shaky */
-    mod=NSModuleForSymbol(sym);
-    add=NSAddressOfSymbol(sym);
+
+    module = NSModuleForSymbol(symbol);
+    address = NSAddressOfSymbol(symbol);
     log_debug("Symbol \"%s\" found in module \"%s\" at address \"0x%08X\"",
-              NSNameOfSymbol(sym),NSNameOfModule(mod),add);
+        NSNameOfSymbol(symbol), NSNameOfModule(module), address);
 
-    /* We want to return the address of the symbol */
-    return(add);
+    /* Return the address of the symbol */
+
+    return address;
 }
+
 /* Return the error message from dlopen: Well we already print it */
-char *dso_error() {
-    return("no additional message");
+const char *
+dso_error(void)
+{
+    return "no additional message";
 }
 
-#endif /* ifdef DSO_DYLD */
+/* Print an error message and abort all if a specified symbol wasn't found */
+static void
+nosymbol(const char *s)
+{
+    log_error("Cannot find symbol \"%s\" in library", s);
+    abort();
+}
+
+/* We found two symbols for the same name in two different modules */
+static NSModule
+multiple(NSSymbol s, NSModule om, NSModule nm)
+{
+    NSModule ret = nm;
+
+    log_debug("Symbol \"%s\" found in modules \"%s\" and \"%s\" (using %s)",
+        NSNameOfSymbol(s), NSNameOfModule(om), NSNameOfModule(nm), 
NSNameOfModule(ret));
+
+    return ret;
+}
+
+/* We got an error while linking a module, and if it's not a warning we have
+   to abort the whole program */
+static void
+linkedit(NSLinkEditErrors category, int number, const char *file, const char 
*message)
+{
+    log_error("Errors during link edit of file \"%s\" (error=%d): %s",
+        file, number,message);
+
+    /* Check if this error was only a warning */
+    if (category != NSLinkEditWarningError) {
+        log_error("Cannot continue");
+        abort();
+    }
+}
+
+#endif /* DSO_DYLD */
Index: src/native/unix/native/replace.c
===================================================================
--- src/native/unix/native/replace.c    (revision 467118)
+++ src/native/unix/native/replace.c    (working copy)
@@ -1,109 +1,85 @@
 /*
    Copyright 2001-2004 The Apache Software Foundation.
- 
+
    Licensed 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.
 */
+
 /* @version $Id$ */
-#include "jsvc.h"
 
-/* Replace all occurrences of a string in another */
-int replace(char *new, int len, char *old, char *mch, char *rpl) {
-    char *tmp;
-    int count;
-    int shift;
-    int nlen;
-    int olen;
-    int mlen;
-    int rlen;
-    int x;
+#include <string.h>
 
-    /* The new buffer is NULL, fail */
-    if (new==NULL) return(-1);
-    /* The length of the buffer is less than zero, fail */
-    if (len<0) return(-2);
-    /* The old buffer is NULL, fail */
-    if (old==NULL) return(-3);
+#include "replace.h"
 
-    /* The string to be matched is NULL or empty, simply copy */
-    if ((mch==NULL)||(strlen(mch)==0)) {
-        olen=strlen(old);
-        if (len<=olen) return(olen+1);
-        strcpy(new,old);
-        return(0);
+/*
+ * Replace all occurrences of oldstr in src with newstr, storing the result in 
dst.
+ */
+int
+replace(char *dst, int dstlen, char *src, const char *oldstr, const char 
*newstr)
+{
+    const char *sptr, *eptr;
+    int oldlen, newlen, occurrences, diff, len;
+
+    /* Validate arguments */
+    if (src == NULL || dst == NULL || dstlen < 0)
+        return -1;
+
+    /* If the string to search for is NULL or empty, simply copy */
+    if ((oldstr == NULL) || (strlen(oldstr) == 0)) {
+        len = strlen(src) + 1;
+        if (dstlen < len)
+            return len;
+        strcpy(dst, src);
+        return 0;
     }
 
-    /* The string to be replaced is NULL, assume it's an empty string */
-    if (rpl==NULL) rpl="";
+    /* If the string to be replace with is NULL, set it to an empty string */
+    if (newstr == NULL)
+        newstr = "";
 
-    /* Evaluate some lengths */
-    olen=strlen(old);
-    mlen=strlen(mch);
-    rlen=strlen(rpl);
+    /* Count the number of occurrences of the string to search for */
+    occurrences = 0;
+    oldlen = strlen(oldstr);
+    for (sptr = src; (sptr = strstr(sptr, oldstr)); sptr += oldlen)
+        occurrences++;
 
-    /* Calculate how many times the mch string appears in old */
-    tmp=old;
-    count=0;
-    while((tmp=strstr(tmp,mch))!=NULL) {
-        count++;
-        tmp+=mlen;
+    /* If there were no occurrences, simply copy */
+    if (occurrences == 0) {
+        len = strlen(src) + 1;
+        if (dstlen < len)
+            return len;
+        strcpy(dst, src);
+        return 0;
     }
 
-    /* We have no matches, simply copy */
-    if (count==0) {
-        olen=strlen(old);
-        if (len<=olen) return(olen+1);
-        strcpy(new,old);
-        return(0);
-    }
+    /* Calculate how big the destination buffer needs to be */
+    newlen = strlen(newstr);
+    diff = newlen - oldlen;
+    len = strlen(src) + (diff * occurrences);
 
-    /* Calculate how big the buffer must be to hold the translation
-       and of how many bytes we need to shift the data */
-    shift=rlen-mlen;
-    nlen=olen+(shift*count);
-    /* printf("Count=%d Shift= %d OLen=%d NLen=%d\n",count,shift,olen,nlen); */
+    /* Check we have enough space in the buffer */
+    if (len >= dstlen)
+        return len + 1;
 
-    /* Check if we have enough size in the buffer */
-    if (nlen>=len) return(nlen+1);
+    /* Perform the copying */
+    len = 0;
 
-    /* Copy over the old buffer in the new one (save memory) */
-    strcpy(new,old);
-
-    /* Start replacing */
-    tmp=new;
-    while((tmp=strstr(tmp,mch))!=NULL) {
-        /* If shift is > 0 we need to move data from right to left */
-        if (shift>0) {
-            for (x=(strlen(tmp)+shift);x>shift;x--) {
-                /*
-                printf("src %c(%d) dst %c(%d)\n",
-                        tmp[x-shift],tmp[x-shift],tmp[x],tmp[x]);
-                 */
-                tmp[x]=tmp[x-shift];
-            }
-        /* If shift is < 0 we need to move data from left to right */
-        } else if (shift<0) {
-            for (x=mlen;x<strlen(tmp)-shift;x++) {
-                /*
-                   printf("src %c(%d) dst %c(%d)\n",
-                          tmp[x],tmp[x],tmp[x+shift],tmp[x+shift]);
-                 */
-                tmp[x+shift]=tmp[x];
-            }
-        }
-        /* If shift is = 0 we don't have to shift data */
-        strncpy(tmp,rpl,rlen);
-        tmp+=rlen;
-        /* printf("\"%s\"\n",tmp); */
+    for (sptr = src; (eptr = strstr(sptr, oldstr)); sptr = eptr + oldlen) {
+        memcpy(dst + len, sptr, eptr - sptr);
+        len += eptr - sptr;
+        memcpy(dst + len, newstr, newlen);
+        len += newlen;
     }
-    return(0);
+    strcpy(dst + len, sptr);
+
+    return 0;
 }
Index: src/native/unix/native/replace.h
===================================================================
--- src/native/unix/native/replace.h    (revision 467118)
+++ src/native/unix/native/replace.h    (working copy)
@@ -1,37 +1,34 @@
 /*
    Copyright 2001-2004 The Apache Software Foundation.
- 
+
    Licensed 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.
 */
+
 /* @version $Id$ */
-#ifndef __JSVC_REPLACE_H__
-#define __JSVC_REPLACE_H__
 
+#ifndef JSVC_REPLACE_H
+#define JSVC_REPLACE_H
+
 /**
- * Replace all occurrences of mch in old with the new string rpl, and
- * stores the result in new, provided that its length (specified in len)
- * is enough.
+ * Replace all occurrences of oldstr in src with newstr, storing the result in 
dst.
  *
- * @param new The buffer where the result of the replace operation will be
- *            stored into.
- * @param len The length of the previous buffer.
- * @param old The string where occurrences of mtch must be searched.
- * @param mch The characters to match in old (and to be replaced)
- * @param rpl The characters that will be replaced in place of mch.
- * @return Zero on success, a value less than 0 if an error was encountered
- *         or a value greater than zero (indicating the required storage size
- *         for new) if the buffer was too short to hold the new string.
+ * \param dst The buffer to store the result in.
+ * \param dstlen The length of the buffer to store the result in.
+ * \param src The string to search for occurences of oldstr in.
+ * \param oldstr The string to search src for.
+ * \param newstr The string to replace oldstr with.
+ * \return 0 on success, -1 on error, or the required length of dst if dstlen 
is too short.
  */
-int replace(char *new, int len, char *old, char *mch, char *rpl);
+int replace(char *dst, int dstlen, char *src, const char *oldstr, const char 
*newstr);
 
-#endif /* ifndef __JSVC_REPLACE_H__ */
+#endif /* !JSVC_REPLACE_H */

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to