Author: mturk
Date: Wed Aug 12 10:25:39 2009
New Revision: 803431

URL: http://svn.apache.org/viewvc?rev=803431&view=rev
Log:
Add getopt

Added:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_getopt.h
    commons/sandbox/runtime/trunk/src/main/native/shared/getopt.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/srclib/zlib/zutil.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=803431&r1=803430&r2=803431&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Wed Aug 12 
10:25:39 2009
@@ -82,6 +82,7 @@
        $(SRCDIR)/shared/db.$(OBJ) \
        $(SRCDIR)/shared/error.$(OBJ) \
        $(SRCDIR)/shared/fco.$(OBJ) \
+       $(SRCDIR)/shared/getopt.$(OBJ) \
        $(SRCDIR)/shared/mbstr.$(OBJ) \
        $(SRCDIR)/shared/memory.$(OBJ) \
        $(SRCDIR)/shared/modules.$(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=803431&r1=803430&r2=803431&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Wed Aug 12 
10:25:39 2009
@@ -74,6 +74,7 @@
        $(SRCDIR)/shared/db.$(OBJ) \
        $(SRCDIR)/shared/error.$(OBJ) \
        $(SRCDIR)/shared/fco.$(OBJ) \
+       $(SRCDIR)/shared/getopt.$(OBJ) \
        $(SRCDIR)/shared/mbstr.$(OBJ) \
        $(SRCDIR)/shared/memory.$(OBJ) \
        $(SRCDIR)/shared/modules.$(OBJ) \

Added: commons/sandbox/runtime/trunk/src/main/native/include/acr_getopt.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_getopt.h?rev=803431&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_getopt.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_getopt.h Wed Aug 
12 10:25:39 2009
@@ -0,0 +1,121 @@
+/* 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.
+ */
+
+#ifndef _ACR_GETOPT_H
+#define _ACR_GETOPT_H
+
+#include "acr.h"
+
+/**
+ * @file acr_getopt.h
+ * @brief
+ *
+ * GNU-like getopt_long() and 4.4BSD getsubopt()/optreset extensions
+ *
+ */
+
+ACR_BEGIN_DECLS
+
+typedef enum {
+    ACR_GETOPT_NO_ARGUMENT = 0,
+    ACR_GETOPT_REQUIRED_ARGUMENT,
+    ACR_GETOPT_OPTIONAL_ARGUMENT
+} acr_getopt_argument_e;
+
+#define ACR_GETOPT_ALLARGS    0x01    /* treat non-options as args to option 
"-1" */
+#define ACR_GETOPT_LONG       0x02    /* operate as getopt_long_only */
+#define ACR_GETOPT_LONGONLY   0x04    /* operate as getopt_long_only */
+#define ACR_GETOPT_POSIXLY    0x08    /* Used instead POSIXLY_CORRECT env var 
*/
+
+/** 
+ * An @c apr_getopt_t error callback function.
+ *
+ * @a arg is this @c apr_getopt_t's @c errarg member.
+ */
+typedef void (acr_getopt_err_fn_t)(void *arg, const char *err, ...);
+
+/** @see apr_getopt_t */
+typedef struct acr_getopt_t acr_getopt_t;
+/**
+ * Structure to store command line argument information.
+ */ 
+struct acr_getopt_t {
+    /** function to print error message (NULL == no messages) */
+    acr_getopt_err_fn_t *errfn;
+    /** user defined first arg to pass to error message  */
+    void *errarg;
+    /** index into parent argv vector */
+    int ind;
+    /** character checked for validity */
+    int opt;
+    /** reset getopt */
+    int reset;
+    /** getopt flags */
+    int flags;
+    /** count of arguments */
+    int argc;
+    /** array of pointers to arguments */
+    const char **argv;
+    /** argument associated with option */
+    char const *arg;
+    /** argument associated with suboption */
+    char const *subopt;
+
+    /* Following are private members */
+    /** argument associated with option */
+    char const *place;
+    /** set to nonzero to support interleaving options with regular args */
+    int interleave;
+    /** first non option argument (for permute) */
+    int nonopt_start;
+    /** first option after non options (for permute) */
+    int nonopt_end;
+};
+
+
+typedef struct acr_getopt_option_t acr_getopt_option_t;
+struct acr_getopt_option_t {
+    /** name of long option */
+    const char *name;
+    /**
+     * one of no_argument, required_argument, and optional_argument:
+     * whether option takes an argument
+     */
+    acr_getopt_argument_e has_arg;
+    /** if not NULL, set *flag to val when option found */
+    int *flag;
+    /** if flag not NULL, value to set *flag to; else return value */
+    int val;
+    /** description of long option */
+    const char *description;
+};
+
+ACR_DECLARE(int)
+ACR_Getopt(acr_getopt_t *os, const char *options,
+           const acr_getopt_option_t *long_options, int *idx);
+
+ACR_DECLARE(int)
+ACR_Getsubopt(acr_getopt_t *os, char * const *tokens, char **valuep);
+
+ACR_DECLARE(acr_getopt_t *)
+ACR_GetoptInit(int argc, const char *const *argv, int flags);
+
+ACR_DECLARE(void)
+ACR_GetoptFree(acr_getopt_t *os);
+
+ACR_END_DECLS
+#endif /* !_ACR_GETOPT_H_ */
+

Added: commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c?rev=803431&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c Wed Aug 12 
10:25:39 2009
@@ -0,0 +1,537 @@
+/* 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) 2002 Todd C. Miller <todd.mil...@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Sponsored in part by the Defense Advanced Research Projects
+ * Agency (DARPA) and Air Force Research Laboratory, Air Force
+ * Materiel Command, USAF, under agreement number F39502-99-1-0512.
+ */
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+#include "acr_string.h"
+#include "acr_getopt.h"
+
+#define PRINT_ERROR ((os->errfn) && (*options != ':'))
+#define ACR_GETOPT_PERMUTE    0x10    /* permute non-options to the end of 
argv */
+
+/* return values */
+#define BADCH       '?'
+#define BADARG      ((*options == ':') ? ':' : '?')
+#define INORDER     1
+
+#define EMSG        ""
+
+static int parse_long_options(acr_getopt_t *, const char *,
+                              const acr_getopt_option_t *, int *, int);
+static int gcd(int, int);
+static void permute_args(int, int, int, const char **);
+
+/* Error messages */
+static const char recargchar[]   = "option requires an argument -- %c";
+static const char recargstring[] = "option requires an argument -- %s";
+static const char ambig[]        = "ambiguous option -- %.*s";
+static const char noarg[]        = "option doesn't take an argument -- %.*s";
+static const char illoptchar[]   = "unknown option -- %c";
+static const char illoptstring[] = "unknown option -- %s";
+
+/*
+ * Compute the greatest common divisor of a and b.
+ */
+static int gcd(int a, int b)
+{
+    int c;
+
+    c = a % b;
+    while (c != 0) {
+        a = b;
+        b = c;
+        c = a % b;
+    }
+
+    return b;
+}
+
+/*
+ * Exchange the block from os->nonopt_start to os->nonopt_end with the block
+ * from os->nonopt_end to opt_end (keeping the same order of arguments
+ * in each block).
+ */
+static void
+permute_args(int panonopt_start, int panonopt_end, int opt_end,
+             const char **nargv)
+{
+    int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
+    const char *swap;
+
+    /*
+     * compute lengths of blocks and number and size of cycles
+     */
+    nnonopts = panonopt_end - panonopt_start;
+    nopts    = opt_end - panonopt_end;
+    ncycle   = gcd(nnonopts, nopts);
+    cyclelen = (opt_end - panonopt_start) / ncycle;
+
+    for (i = 0; i < ncycle; i++) {
+        cstart = panonopt_end+i;
+        pos = cstart;
+        for (j = 0; j < cyclelen; j++) {
+            if (pos >= panonopt_end)
+                pos -= nnonopts;
+            else
+                pos += nopts;
+            swap = nargv[pos];
+            nargv[pos]    = nargv[cstart];
+            nargv[cstart] = swap;
+        }
+    }
+}
+
+/*
+ * parse_long_options --
+ *  Parse long options in argc/argv argument vector.
+ * Returns -1 if short_too is set and the option does not match long_options.
+ */
+static int
+parse_long_options(acr_getopt_t *os, const char *options,
+                   const acr_getopt_option_t *long_options,
+                   int *idx, int short_too)
+{
+    const char *current_argv, *has_equal;
+    size_t current_argv_len;
+    int i, match;
+
+    current_argv = os->place;
+    match = -1;
+
+    os->ind++;
+
+    if ((has_equal = strchr(current_argv, '=')) != NULL) {
+        /* argument found (--option=arg) */
+        current_argv_len = has_equal - current_argv;
+        has_equal++;
+    } else
+        current_argv_len = strlen(current_argv);
+
+    for (i = 0; long_options[i].name; i++) {
+        /* find matching long option */
+        if (strncmp(current_argv, long_options[i].name,
+            current_argv_len))
+            continue;
+
+        if (strlen(long_options[i].name) == current_argv_len) {
+            /* exact match */
+            match = i;
+            break;
+        }
+        /*
+         * If this is a known short option, don't allow
+         * a partial match of a single character.
+         */
+        if (short_too && current_argv_len == 1)
+            continue;
+
+        if (match == -1)    /* partial match */
+            match = i;
+        else {
+            /* ambiguous abbreviation */
+            if (PRINT_ERROR)
+                (*os->errfn)(os->errarg, ambig, (int)current_argv_len,
+                     current_argv);
+            os->opt = 0;
+            return BADCH;
+        }
+    }
+    if (match != -1) {      /* option found */
+        if (long_options[match].has_arg == ACR_GETOPT_NO_ARGUMENT
+            && has_equal) {
+            if (PRINT_ERROR)
+                (*os->errfn)(os->errarg, noarg, (int)current_argv_len,
+                     current_argv);
+            /*
+             * XXX: GNU sets os->opt to val regardless of flag
+             */
+            if (long_options[match].flag == NULL)
+                os->opt = long_options[match].val;
+            else
+                os->opt = 0;
+            return BADARG;
+        }
+        if (long_options[match].has_arg == ACR_GETOPT_REQUIRED_ARGUMENT ||
+            long_options[match].has_arg == ACR_GETOPT_OPTIONAL_ARGUMENT) {
+            if (has_equal) {
+                os->arg = has_equal;
+            }
+            else if (long_options[match].has_arg ==
+                     ACR_GETOPT_REQUIRED_ARGUMENT) {
+                /*
+                 * optional argument doesn't use next os->argv
+                 */
+                os->arg = os->argv[os->ind++];
+            }
+        }
+        if ((long_options[match].has_arg == ACR_GETOPT_REQUIRED_ARGUMENT)
+            && (os->arg == NULL)) {
+            /*
+             * Missing argument; leading ':' indicates no error
+             * should be generated.
+             */
+            if (PRINT_ERROR)
+                (*os->errfn)(os->errarg, recargstring,
+                    current_argv);
+            /*
+             * XXX: GNU sets os->opt to val regardless of flag
+             */
+            if (long_options[match].flag == NULL)
+                os->opt = long_options[match].val;
+            else
+                os->opt = 0;
+            --os->ind;
+            return BADARG;
+        }
+    }
+    else {            /* unknown option */
+        if (short_too) {
+            --os->ind;
+            return -1;
+        }
+        if (PRINT_ERROR)
+            (*os->errfn)(os->errarg, illoptstring, current_argv);
+        os->opt = 0;
+        return BADCH;
+    }
+    if (idx)
+        *idx = match;
+    if (long_options[match].flag) {
+        *long_options[match].flag = long_options[match].val;
+        return 0;
+    }
+    else
+        return long_options[match].val;
+}
+
+/*
+ * getopt_internal --
+ *  Parse argc/argv argument vector.  Called by user level routines.
+ */
+ACR_DECLARE(int)
+ACR_Getopt(acr_getopt_t *os, const char *options,
+           const acr_getopt_option_t *long_options, int *idx)
+{
+    char *oli;              /* option letter list index */
+    int optchar, short_too;
+    static int posixly_correct = -1;
+
+    if (options == NULL)
+        return -1;
+
+    /*
+     * Disable GNU extensions if POSIXLY_CORRECT is set or options
+     * string begins with a '+'.
+     */
+    if (posixly_correct == -1)
+        posixly_correct = (os->flags & ACR_GETOPT_POSIXLY) ? 1 : 0;
+    if (posixly_correct || *options == '+')
+        os->flags &= ~ACR_GETOPT_PERMUTE;
+    else if (*options == '-')
+        os->flags |= ACR_GETOPT_ALLARGS;
+    if (*options == '+' || *options == '-')
+        options++;
+
+    /*
+     * XXX Some GNU programs (like cvs) set os->ind to 0 instead of
+     * XXX using os->reset.  Work around this braindamage.
+     */
+    if (os->ind == 0)
+        os->ind = os->reset = 1;
+
+    os->arg = NULL;
+    if (os->reset)
+        os->nonopt_start = os->nonopt_end = -1;
+start:
+    if (os->reset || !*os->place) {      /* update scanning pointer */
+        os->reset = 0;
+        if (os->ind >= os->argc) {          /* end of argument vector */
+            os->place = EMSG;
+            if (os->nonopt_end != -1) {
+                /* do permutation, if we have to */
+                permute_args(os->nonopt_start, os->nonopt_end,
+                    os->ind, os->argv);
+                os->ind -= os->nonopt_end - os->nonopt_start;
+            }
+            else if (os->nonopt_start != -1) {
+                /*
+                 * If we skipped non-options, set os->ind
+                 * to the first of them.
+                 */
+                os->ind = os->nonopt_start;
+            }
+            os->nonopt_start = os->nonopt_end = -1;
+            return -1;
+        }
+        if (*(os->place = os->argv[os->ind]) != '-' ||
+            (os->place[1] == '\0' && strchr(options, '-') == NULL)) {
+            os->place = EMSG;       /* found non-option */
+            if (os->flags & ACR_GETOPT_ALLARGS) {
+                /*
+                 * GNU extension:
+                 * return non-option as argument to option 1
+                 */
+                os->arg = os->argv[os->ind++];
+                return INORDER;
+            }
+            if (!(os->flags & ACR_GETOPT_PERMUTE)) {
+                /*
+                 * If no permutation wanted, stop parsing
+                 * at first non-option.
+                 */
+                return -1;
+            }
+            /* do permutation */
+            if (os->nonopt_start == -1)
+                os->nonopt_start = os->ind;
+            else if (os->nonopt_end != -1) {
+                permute_args(os->nonopt_start, os->nonopt_end,
+                    os->ind, os->argv);
+                os->nonopt_start = os->ind -
+                    (os->nonopt_end - os->nonopt_start);
+                os->nonopt_end = -1;
+            }
+            os->ind++;
+            /* process next argument */
+            goto start;
+        }
+        if (os->nonopt_start != -1 && os->nonopt_end == -1)
+            os->nonopt_end = os->ind;
+
+        /*
+         * If we have "-" do nothing, if "--" we are done.
+         */
+        if (os->place[1] != '\0' && *++os->place == '-' && os->place[1] == 
'\0') {
+            os->ind++;
+            os->place = EMSG;
+            /*
+             * We found an option (--), so if we skipped
+             * non-options, we have to permute.
+             */
+            if (os->nonopt_end != -1) {
+                permute_args(os->nonopt_start, os->nonopt_end,
+                    os->ind, os->argv);
+                os->ind -= os->nonopt_end - os->nonopt_start;
+            }
+            os->nonopt_start = os->nonopt_end = -1;
+            return -1;
+        }
+    }
+
+    /*
+     * Check long options if:
+     *  1) we were passed some
+     *  2) the arg is not just "-"
+     *  3) either the arg starts with -- we are getopt_long_only()
+     */
+    if (long_options != NULL && os->place != os->argv[os->ind] &&
+        (*os->place == '-' || (os->flags & ACR_GETOPT_LONGONLY))) {
+        short_too = 0;
+        if (*os->place == '-')
+            os->place++;        /* --foo long option */
+        else if (*os->place != ':' && strchr(options, *os->place) != NULL)
+            short_too = 1;      /* could be short option too */
+
+        optchar = parse_long_options(os, options, long_options,
+            idx, short_too);
+        if (optchar != -1) {
+            os->place = EMSG;
+            return optchar;
+        }
+    }
+
+    if ((optchar = *os->place++) == ':' ||
+        (optchar == '-' && *os->place != '\0') ||
+        (oli = strchr(options, optchar)) == NULL) {
+        /*
+         * If the user specified "-" and  '-' isn't listed in
+         * options, return -1 (non-option) as per POSIX.
+         * Otherwise, it is an unknown option character (or ':').
+         */
+        if (optchar == '-' && *os->place == '\0')
+            return -1;
+        if (!*os->place)
+            ++os->ind;
+        if (PRINT_ERROR)
+            (*os->errfn)(os->errarg, illoptchar, optchar);
+        os->opt = optchar;
+        return BADCH;
+    }
+    if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
+        /* -W long-option */
+        if (*os->place)         /* no space */
+            /* NOTHING */;
+        else if (++os->ind >= os->argc) {   /* no arg */
+            os->place = EMSG;
+            if (PRINT_ERROR)
+                (*os->errfn)(os->errarg, recargchar, optchar);
+            os->opt = optchar;
+            return BADARG;
+        } else              /* white space */
+            os->place = os->argv[os->ind];
+        optchar = parse_long_options(os, options, long_options, idx, 0);
+        os->place = EMSG;
+        return optchar;
+    }
+    if (*++oli != ':') {            /* doesn't take argument */
+        if (!*os->place)
+            ++os->ind;
+    } else {                /* takes (optional) argument */
+        os->arg = NULL;
+        if (*os->place)         /* no white space */
+            os->arg = os->place;
+        else if (oli[1] != ':') {   /* arg not optional */
+            if (++os->ind >= os->argc) {    /* no arg */
+                os->place = EMSG;
+                if (PRINT_ERROR)
+                    (*os->errfn)(os->errarg, recargchar, optchar);
+                os->opt = optchar;
+                return BADARG;
+            } else
+                os->arg = os->argv[os->ind];
+        }
+        os->place = EMSG;
+        ++os->ind;
+    }
+    /* dump back option letter */
+    return optchar;
+}
+
+/*
+ * The SVID interface to getsubopt provides no way of figuring out which
+ * part of the suboptions list wasn't matched.  This makes error messages
+ * tricky...  The extern variable suboptarg is a pointer to the token
+ * which didn't match.
+ */
+ACR_DECLARE(int)
+ACR_Getsubopt(acr_getopt_t *os, char * const *tokens, char **valuep)
+{
+    int cnt;
+    char *p;
+
+    os->subopt = *valuep = NULL;
+
+    if (!os->arg || !*os->arg)
+        return -1;
+
+    /* skip leading white-space, commas */
+    for (p = (char *)os->arg; *p && (*p == ',' || acr_isspace(*p)); ++p);
+
+    if (!*p) {
+        os->arg = p;
+        return -1;
+    }
+
+    /* save the start of the token, and skip the rest of the token. */
+    for (os->subopt = p;
+         *++p && *p != ',' && *p != '=' && !acr_isspace(*p););
+
+    if (*p) {
+        /*
+         * If there's an equals sign, set the value pointer, and
+         * skip over the value part of the token.  Terminate the
+         * token.
+         */
+        if (*p == '=') {
+            *p = '\0';
+            for (*valuep = ++p;
+                 *p && *p != ',' && !acr_isspace(*p); ++p);
+            if (*p) 
+                *p++ = '\0';
+        }
+        else
+            *p++ = '\0';
+        /* Skip any whitespace or commas after this token. */
+        for (; *p && (*p == ',' || acr_isspace(*p)); ++p);
+    }
+
+    /* set optionp for next round. */
+    os->arg = p;
+
+    for (cnt = 0; *tokens; ++tokens, ++cnt) {
+        if (!strcmp(os->subopt, *tokens))
+            return cnt;
+    }
+    return -1;
+}
+
+ACR_DECLARE(acr_getopt_t *)
+ACR_GetoptInit(int argc, const char *const *argv, int flags)
+{
+    acr_getopt_t *os;
+    void *avbuf;
+
+    if (!(os = calloc(1, sizeof(acr_getopt_t)))) {
+        return NULL;
+    }
+    os->errfn   = (acr_getopt_err_fn_t *)(fprintf);
+    os->errarg  = (void *)(stderr);
+    os->ind     = 1;
+    os->opt     = '?';
+    os->flags   = flags;
+    os->place   = EMSG;
+    os->argc    = argc;
+    /* The argv parameter must be compatible with main()'s argv, since
+       that's the primary purpose of this function.  But people might
+       want to use this function with arrays other than the main argv,
+       and we shouldn't touch the caller's data.  So we copy. */
+    avbuf = malloc((argc + 1) * sizeof(const char *));
+    if (!avbuf) {
+        int saved = ACR_GET_OS_ERROR();
+        free(os);
+        ACR_SET_OS_ERROR(saved);
+        return NULL;
+    }
+    memcpy(avbuf, argv, argc * sizeof(const char *));
+    os->argv       = avbuf;
+    os->argv[argc] = NULL;
+
+    if (os->flags & (ACR_GETOPT_LONG | ACR_GETOPT_LONGONLY))
+        os->flags |= ACR_GETOPT_PERMUTE;
+
+    return os;
+}
+
+ACR_DECLARE(void)
+ACR_GetoptFree(acr_getopt_t *os)
+{
+    if (os) {
+        free(os->argv);
+        free(os);
+    }
+}
+

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

Modified: commons/sandbox/runtime/trunk/src/main/native/srclib/zlib/zutil.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/srclib/zlib/zutil.h?rev=803431&r1=803430&r2=803431&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/srclib/zlib/zutil.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/srclib/zlib/zutil.h Wed Aug 
12 10:25:39 2009
@@ -13,7 +13,7 @@
 #define ZUTIL_H
 
 #define ZLIB_INTERNAL
-#include "zlib.h"
+#include "zlib/zlib.h"
 
 #ifdef _STANDALONE
 #include <stand.h>

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=803431&r1=803430&r2=803431&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c Wed Aug 12 
10:25:39 2009
@@ -32,6 +32,7 @@
 #include "acr_xdr.h"
 #include "acr_shm.h"
 #include "acr_crypto.h"
+#include "acr_getopt.h"
 #include "acr_version.h"
 
 #if defined (WIN32)
@@ -47,15 +48,52 @@
 #endif
 
 
+static acr_getopt_option_t toptions[] = {
+    {"help",    ACR_GETOPT_NO_ARGUMENT,         NULL,   'h', NULL },
+    {"test",    ACR_GETOPT_REQUIRED_ARGUMENT,   NULL,   't', NULL },
 
+    {NULL,                                 0,   NULL,     0, NULL }
+};
 
 int main(int argc, const char *const argv[])
 {
+    int rv = 0;
+    acr_getopt_t *os;
+    int ch;
+
     fprintf(stdout,
             "Running Apache Commons Runtime %s (%s version) test suite.\n",
             ACR_VERSION_STRING, ACR_GetLibraryBuilt());
     fflush(stdout);
 
+    os = ACR_GetoptInit(argc, argv, ACR_GETOPT_LONGONLY);
+    if (!os) {
+        return ACR_GET_OS_ERROR();
+    }
+
+    while ((ch = ACR_Getopt(os, "W;ht:", toptions, NULL)) != -1) {
+
+        switch (ch) {
+            case 'h' :
+                fprintf(stdout, "Help   (TODO)\n");
+                goto cleanup;
+            break;
+            case 't' :
+                fprintf(stdout, "Using test %s\n", os->arg);
+            break;
+            default:
+                fprintf(stderr, "\n\nUsage  (TODO)\n");
+                rv = ACR_EINVAL;
+                goto cleanup;
+            break;
+        }
+    }
+    argc -= os->ind;
+    argv += os->ind;
+
+
+cleanup:
+    ACR_GetoptFree(os);
     return 0;
 }
 


Reply via email to